range-tree.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. class RangeTree {
  4. constructor(start, end, delta, children) {
  5. this.start = start;
  6. this.end = end;
  7. this.delta = delta;
  8. this.children = children;
  9. }
  10. /**
  11. * @precodition `ranges` are well-formed and pre-order sorted
  12. */
  13. static fromSortedRanges(ranges) {
  14. let root;
  15. // Stack of parent trees and parent counts.
  16. const stack = [];
  17. for (const range of ranges) {
  18. const node = new RangeTree(range.startOffset, range.endOffset, range.count, []);
  19. if (root === undefined) {
  20. root = node;
  21. stack.push([node, range.count]);
  22. continue;
  23. }
  24. let parent;
  25. let parentCount;
  26. while (true) {
  27. [parent, parentCount] = stack[stack.length - 1];
  28. // assert: `top !== undefined` (the ranges are sorted)
  29. if (range.startOffset < parent.end) {
  30. break;
  31. }
  32. else {
  33. stack.pop();
  34. }
  35. }
  36. node.delta -= parentCount;
  37. parent.children.push(node);
  38. stack.push([node, range.count]);
  39. }
  40. return root;
  41. }
  42. normalize() {
  43. const children = [];
  44. let curEnd;
  45. let head;
  46. const tail = [];
  47. for (const child of this.children) {
  48. if (head === undefined) {
  49. head = child;
  50. }
  51. else if (child.delta === head.delta && child.start === curEnd) {
  52. tail.push(child);
  53. }
  54. else {
  55. endChain();
  56. head = child;
  57. }
  58. curEnd = child.end;
  59. }
  60. if (head !== undefined) {
  61. endChain();
  62. }
  63. if (children.length === 1) {
  64. const child = children[0];
  65. if (child.start === this.start && child.end === this.end) {
  66. this.delta += child.delta;
  67. this.children = child.children;
  68. // `.lazyCount` is zero for both (both are after normalization)
  69. return;
  70. }
  71. }
  72. this.children = children;
  73. function endChain() {
  74. if (tail.length !== 0) {
  75. head.end = tail[tail.length - 1].end;
  76. for (const tailTree of tail) {
  77. for (const subChild of tailTree.children) {
  78. subChild.delta += tailTree.delta - head.delta;
  79. head.children.push(subChild);
  80. }
  81. }
  82. tail.length = 0;
  83. }
  84. head.normalize();
  85. children.push(head);
  86. }
  87. }
  88. /**
  89. * @precondition `tree.start < value && value < tree.end`
  90. * @return RangeTree Right part
  91. */
  92. split(value) {
  93. let leftChildLen = this.children.length;
  94. let mid;
  95. // TODO(perf): Binary search (check overhead)
  96. for (let i = 0; i < this.children.length; i++) {
  97. const child = this.children[i];
  98. if (child.start < value && value < child.end) {
  99. mid = child.split(value);
  100. leftChildLen = i + 1;
  101. break;
  102. }
  103. else if (child.start >= value) {
  104. leftChildLen = i;
  105. break;
  106. }
  107. }
  108. const rightLen = this.children.length - leftChildLen;
  109. const rightChildren = this.children.splice(leftChildLen, rightLen);
  110. if (mid !== undefined) {
  111. rightChildren.unshift(mid);
  112. }
  113. const result = new RangeTree(value, this.end, this.delta, rightChildren);
  114. this.end = value;
  115. return result;
  116. }
  117. /**
  118. * Get the range coverages corresponding to the tree.
  119. *
  120. * The ranges are pre-order sorted.
  121. */
  122. toRanges() {
  123. const ranges = [];
  124. // Stack of parent trees and counts.
  125. const stack = [[this, 0]];
  126. while (stack.length > 0) {
  127. const [cur, parentCount] = stack.pop();
  128. const count = parentCount + cur.delta;
  129. ranges.push({ startOffset: cur.start, endOffset: cur.end, count });
  130. for (let i = cur.children.length - 1; i >= 0; i--) {
  131. stack.push([cur.children[i], count]);
  132. }
  133. }
  134. return ranges;
  135. }
  136. }
  137. exports.RangeTree = RangeTree;
  138. //# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIl9zcmMvcmFuZ2UtdHJlZS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUVBLE1BQWEsU0FBUztJQU1wQixZQUNFLEtBQWEsRUFDYixHQUFXLEVBQ1gsS0FBYSxFQUNiLFFBQXFCO1FBRXJCLElBQUksQ0FBQyxLQUFLLEdBQUcsS0FBSyxDQUFD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