csLeaf.js 1.6 KB

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