index.cjs 7.2 KB

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