csLeaf.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.csLeaf = csLeaf;
  6. /**
  7. * This function determines if j is a leaf of the ith row subtree.
  8. * Consider A(i,j), node j in ith row subtree and return lca(jprev,j)
  9. *
  10. * @param {Number} i The ith row subtree
  11. * @param {Number} j The node to test
  12. * @param {Array} w The workspace array
  13. * @param {Number} first The index offset within the workspace for the first array
  14. * @param {Number} maxfirst The index offset within the workspace for the maxfirst array
  15. * @param {Number} prevleaf The index offset within the workspace for the prevleaf array
  16. * @param {Number} ancestor The index offset within the workspace for the ancestor array
  17. *
  18. * @return {Object}
  19. *
  20. * Reference: http://faculty.cse.tamu.edu/davis/publications.html
  21. */
  22. function csLeaf(i, j, w, first, maxfirst, prevleaf, ancestor) {
  23. var s, sparent;
  24. // our result
  25. var jleaf = 0;
  26. var q;
  27. // check j is a leaf
  28. if (i <= j || w[first + j] <= w[maxfirst + i]) {
  29. return -1;
  30. }
  31. // update max first[j] seen so far
  32. w[maxfirst + i] = w[first + j];
  33. // jprev = previous leaf of ith subtree
  34. var jprev = w[prevleaf + i];
  35. w[prevleaf + i] = j;
  36. // check j is first or subsequent leaf
  37. if (jprev === -1) {
  38. // 1st leaf, q = root of ith subtree
  39. jleaf = 1;
  40. q = i;
  41. } else {
  42. // update jleaf
  43. jleaf = 2;
  44. // q = least common ancester (jprev,j)
  45. for (q = jprev; q !== w[ancestor + q]; q = w[ancestor + q]) {
  46. ;
  47. }
  48. for (s = jprev; s !== q; s = sparent) {
  49. // path compression
  50. sparent = w[ancestor + s];
  51. w[ancestor + s] = q;
  52. }
  53. }
  54. return {
  55. jleaf: jleaf,
  56. q: q
  57. };
  58. }