index.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. var GOOD_LEAF_SIZE = 200;
  2. // :: class<T> A rope sequence is a persistent sequence data structure
  3. // that supports appending, prepending, and slicing without doing a
  4. // full copy. It is represented as a mostly-balanced tree.
  5. var RopeSequence = function RopeSequence () {};
  6. RopeSequence.prototype.append = function append (other) {
  7. if (!other.length) { return this }
  8. other = RopeSequence.from(other);
  9. return (!this.length && other) ||
  10. (other.length < GOOD_LEAF_SIZE && this.leafAppend(other)) ||
  11. (this.length < GOOD_LEAF_SIZE && other.leafPrepend(this)) ||
  12. this.appendInner(other)
  13. };
  14. // :: (union<[T], RopeSequence<T>>) → RopeSequence<T>
  15. // Prepend an array or other rope to this one, returning a new rope.
  16. RopeSequence.prototype.prepend = function prepend (other) {
  17. if (!other.length) { return this }
  18. return RopeSequence.from(other).append(this)
  19. };
  20. RopeSequence.prototype.appendInner = function appendInner (other) {
  21. return new Append(this, other)
  22. };
  23. // :: (?number, ?number) → RopeSequence<T>
  24. // Create a rope repesenting a sub-sequence of this rope.
  25. RopeSequence.prototype.slice = function slice (from, to) {
  26. if ( from === void 0 ) from = 0;
  27. if ( to === void 0 ) to = this.length;
  28. if (from >= to) { return RopeSequence.empty }
  29. return this.sliceInner(Math.max(0, from), Math.min(this.length, to))
  30. };
  31. // :: (number) → T
  32. // Retrieve the element at the given position from this rope.
  33. RopeSequence.prototype.get = function get (i) {
  34. if (i < 0 || i >= this.length) { return undefined }
  35. return this.getInner(i)
  36. };
  37. // :: ((element: T, index: number) → ?bool, ?number, ?number)
  38. // Call the given function for each element between the given
  39. // indices. This tends to be more efficient than looping over the
  40. // indices and calling `get`, because it doesn't have to descend the
  41. // tree for every element.
  42. RopeSequence.prototype.forEach = function forEach (f, from, to) {
  43. if ( from === void 0 ) from = 0;
  44. if ( to === void 0 ) to = this.length;
  45. if (from <= to)
  46. { this.forEachInner(f, from, to, 0); }
  47. else
  48. { this.forEachInvertedInner(f, from, to, 0); }
  49. };
  50. // :: ((element: T, index: number) → U, ?number, ?number) → [U]
  51. // Map the given functions over the elements of the rope, producing
  52. // a flat array.
  53. RopeSequence.prototype.map = function map (f, from, to) {
  54. if ( from === void 0 ) from = 0;
  55. if ( to === void 0 ) to = this.length;
  56. var result = [];
  57. this.forEach(function (elt, i) { return result.push(f(elt, i)); }, from, to);
  58. return result
  59. };
  60. // :: (?union<[T], RopeSequence<T>>) → RopeSequence<T>
  61. // Create a rope representing the given array, or return the rope
  62. // itself if a rope was given.
  63. RopeSequence.from = function from (values) {
  64. if (values instanceof RopeSequence) { return values }
  65. return values && values.length ? new Leaf(values) : RopeSequence.empty
  66. };
  67. var Leaf = /*@__PURE__*/(function (RopeSequence) {
  68. function Leaf(values) {
  69. RopeSequence.call(this);
  70. this.values = values;
  71. }
  72. if ( RopeSequence ) Leaf.__proto__ = RopeSequence;
  73. Leaf.prototype = Object.create( RopeSequence && RopeSequence.prototype );
  74. Leaf.prototype.constructor = Leaf;
  75. var prototypeAccessors = { length: { configurable: true },depth: { configurable: true } };
  76. Leaf.prototype.flatten = function flatten () {
  77. return this.values
  78. };
  79. Leaf.prototype.sliceInner = function sliceInner (from, to) {
  80. if (from == 0 && to == this.length) { return this }
  81. return new Leaf(this.values.slice(from, to))
  82. };
  83. Leaf.prototype.getInner = function getInner (i) {
  84. return this.values[i]
  85. };
  86. Leaf.prototype.forEachInner = function forEachInner (f, from, to, start) {
  87. for (var i = from; i < to; i++)
  88. { if (f(this.values[i], start + i) === false) { return false } }
  89. };
  90. Leaf.prototype.forEachInvertedInner = function forEachInvertedInner (f, from, to, start) {
  91. for (var i = from - 1; i >= to; i--)
  92. { if (f(this.values[i], start + i) === false) { return false } }
  93. };
  94. Leaf.prototype.leafAppend = function leafAppend (other) {
  95. if (this.length + other.length <= GOOD_LEAF_SIZE)
  96. { return new Leaf(this.values.concat(other.flatten())) }
  97. };
  98. Leaf.prototype.leafPrepend = function leafPrepend (other) {
  99. if (this.length + other.length <= GOOD_LEAF_SIZE)
  100. { return new Leaf(other.flatten().concat(this.values)) }
  101. };
  102. prototypeAccessors.length.get = function () { return this.values.length };
  103. prototypeAccessors.depth.get = function () { return 0 };
  104. Object.defineProperties( Leaf.prototype, prototypeAccessors );
  105. return Leaf;
  106. }(RopeSequence));
  107. // :: RopeSequence
  108. // The empty rope sequence.
  109. RopeSequence.empty = new Leaf([]);
  110. var Append = /*@__PURE__*/(function (RopeSequence) {
  111. function Append(left, right) {
  112. RopeSequence.call(this);
  113. this.left = left;
  114. this.right = right;
  115. this.length = left.length + right.length;
  116. this.depth = Math.max(left.depth, right.depth) + 1;
  117. }
  118. if ( RopeSequence ) Append.__proto__ = RopeSequence;
  119. Append.prototype = Object.create( RopeSequence && RopeSequence.prototype );
  120. Append.prototype.constructor = Append;
  121. Append.prototype.flatten = function flatten () {
  122. return this.left.flatten().concat(this.right.flatten())
  123. };
  124. Append.prototype.getInner = function getInner (i) {
  125. return i < this.left.length ? this.left.get(i) : this.right.get(i - this.left.length)
  126. };
  127. Append.prototype.forEachInner = function forEachInner (f, from, to, start) {
  128. var leftLen = this.left.length;
  129. if (from < leftLen &&
  130. this.left.forEachInner(f, from, Math.min(to, leftLen), start) === false)
  131. { return false }
  132. if (to > leftLen &&
  133. this.right.forEachInner(f, Math.max(from - leftLen, 0), Math.min(this.length, to) - leftLen, start + leftLen) === false)
  134. { return false }
  135. };
  136. Append.prototype.forEachInvertedInner = function forEachInvertedInner (f, from, to, start) {
  137. var leftLen = this.left.length;
  138. if (from > leftLen &&
  139. this.right.forEachInvertedInner(f, from - leftLen, Math.max(to, leftLen) - leftLen, start + leftLen) === false)
  140. { return false }
  141. if (to < leftLen &&
  142. this.left.forEachInvertedInner(f, Math.min(from, leftLen), to, start) === false)
  143. { return false }
  144. };
  145. Append.prototype.sliceInner = function sliceInner (from, to) {
  146. if (from == 0 && to == this.length) { return this }
  147. var leftLen = this.left.length;
  148. if (to <= leftLen) { return this.left.slice(from, to) }
  149. if (from >= leftLen) { return this.right.slice(from - leftLen, to - leftLen) }
  150. return this.left.slice(from, leftLen).append(this.right.slice(0, to - leftLen))
  151. };
  152. Append.prototype.leafAppend = function leafAppend (other) {
  153. var inner = this.right.leafAppend(other);
  154. if (inner) { return new Append(this.left, inner) }
  155. };
  156. Append.prototype.leafPrepend = function leafPrepend (other) {
  157. var inner = this.left.leafPrepend(other);
  158. if (inner) { return new Append(inner, this.right) }
  159. };
  160. Append.prototype.appendInner = function appendInner (other) {
  161. if (this.left.depth >= Math.max(this.right.depth, other.depth) + 1)
  162. { return new Append(this.left, new Append(this.right, other)) }
  163. return new Append(this, other)
  164. };
  165. return Append;
  166. }(RopeSequence));
  167. export default RopeSequence;