@procedures/collections 0.47.1 → 0.48.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (238) hide show
  1. package/CHANGELOG.md +32 -8
  2. package/README.md +3 -1
  3. package/dist/.tsbuildinfo +1 -1
  4. package/dist/index.d.ts +1 -59
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +3 -95
  7. package/dist/index.js.map +1 -1
  8. package/dist/storage/index.d.ts +2 -2
  9. package/dist/storage/index.d.ts.map +1 -1
  10. package/dist/storage/index.js +1 -1
  11. package/dist/storage/index.js.map +1 -1
  12. package/dist/storage/memory.d.ts +1 -1
  13. package/dist/storage/memory.d.ts.map +1 -1
  14. package/dist/storage/memory.js +1 -1
  15. package/dist/storage/memory.js.map +1 -1
  16. package/dist/testing/index.d.ts +1 -1
  17. package/dist/testing/index.d.ts.map +1 -1
  18. package/dist/testing/index.js +1 -1
  19. package/dist/testing/index.js.map +1 -1
  20. package/dist/testing/storage-contract.js +1 -1
  21. package/dist/testing/storage-contract.js.map +1 -1
  22. package/dist/undo/index.d.ts +1 -1
  23. package/dist/undo/index.js +1 -1
  24. package/dist/undo/undo-log.d.ts +1 -1
  25. package/dist/undo/undo-log.js +1 -1
  26. package/package.json +5 -5
  27. package/src/index.ts +338 -214
  28. package/src/storage/index.ts +2 -2
  29. package/src/storage/memory.ts +1 -1
  30. package/src/testing/index.ts +1 -1
  31. package/src/testing/storage-contract.ts +1 -1
  32. package/src/undo/index.ts +1 -1
  33. package/src/undo/undo-log.ts +1 -1
  34. package/dist/async/async-queue.d.ts +0 -170
  35. package/dist/async/async-queue.d.ts.map +0 -1
  36. package/dist/async/async-queue.js +0 -404
  37. package/dist/async/async-queue.js.map +0 -1
  38. package/dist/async/channels.d.ts +0 -211
  39. package/dist/async/channels.d.ts.map +0 -1
  40. package/dist/async/channels.js +0 -393
  41. package/dist/async/channels.js.map +0 -1
  42. package/dist/async/settled.d.ts +0 -23
  43. package/dist/async/settled.d.ts.map +0 -1
  44. package/dist/async/settled.js +0 -32
  45. package/dist/async/settled.js.map +0 -1
  46. package/dist/behaviors/bounded.d.ts +0 -79
  47. package/dist/behaviors/bounded.d.ts.map +0 -1
  48. package/dist/behaviors/bounded.js +0 -393
  49. package/dist/behaviors/bounded.js.map +0 -1
  50. package/dist/behaviors/composite.d.ts +0 -17
  51. package/dist/behaviors/composite.d.ts.map +0 -1
  52. package/dist/behaviors/composite.js +0 -93
  53. package/dist/behaviors/composite.js.map +0 -1
  54. package/dist/behaviors/evented.d.ts +0 -154
  55. package/dist/behaviors/evented.d.ts.map +0 -1
  56. package/dist/behaviors/evented.js +0 -299
  57. package/dist/behaviors/evented.js.map +0 -1
  58. package/dist/behaviors/lru.d.ts +0 -84
  59. package/dist/behaviors/lru.d.ts.map +0 -1
  60. package/dist/behaviors/lru.js +0 -376
  61. package/dist/behaviors/lru.js.map +0 -1
  62. package/dist/behaviors/readonly.d.ts +0 -73
  63. package/dist/behaviors/readonly.d.ts.map +0 -1
  64. package/dist/behaviors/readonly.js +0 -189
  65. package/dist/behaviors/readonly.js.map +0 -1
  66. package/dist/behaviors/safe.d.ts +0 -163
  67. package/dist/behaviors/safe.d.ts.map +0 -1
  68. package/dist/behaviors/safe.js +0 -176
  69. package/dist/behaviors/safe.js.map +0 -1
  70. package/dist/behaviors/synchronized.d.ts +0 -88
  71. package/dist/behaviors/synchronized.d.ts.map +0 -1
  72. package/dist/behaviors/synchronized.js +0 -285
  73. package/dist/behaviors/synchronized.js.map +0 -1
  74. package/dist/behaviors/ttl.d.ts +0 -118
  75. package/dist/behaviors/ttl.d.ts.map +0 -1
  76. package/dist/behaviors/ttl.js +0 -354
  77. package/dist/behaviors/ttl.js.map +0 -1
  78. package/dist/contracts/model.d.ts +0 -29
  79. package/dist/contracts/model.d.ts.map +0 -1
  80. package/dist/contracts/model.js +0 -26
  81. package/dist/contracts/model.js.map +0 -1
  82. package/dist/core/effects.d.ts +0 -176
  83. package/dist/core/effects.d.ts.map +0 -1
  84. package/dist/core/effects.js +0 -180
  85. package/dist/core/effects.js.map +0 -1
  86. package/dist/core/events.d.ts +0 -192
  87. package/dist/core/events.d.ts.map +0 -1
  88. package/dist/core/events.js +0 -291
  89. package/dist/core/events.js.map +0 -1
  90. package/dist/core/middleware.d.ts +0 -231
  91. package/dist/core/middleware.d.ts.map +0 -1
  92. package/dist/core/middleware.js +0 -161
  93. package/dist/core/middleware.js.map +0 -1
  94. package/dist/core/policies.d.ts +0 -220
  95. package/dist/core/policies.d.ts.map +0 -1
  96. package/dist/core/policies.js +0 -48
  97. package/dist/core/policies.js.map +0 -1
  98. package/dist/core/traits.d.ts +0 -142
  99. package/dist/core/traits.d.ts.map +0 -1
  100. package/dist/core/traits.js +0 -8
  101. package/dist/core/traits.js.map +0 -1
  102. package/dist/fx/collectors.d.ts +0 -193
  103. package/dist/fx/collectors.d.ts.map +0 -1
  104. package/dist/fx/collectors.js +0 -375
  105. package/dist/fx/collectors.js.map +0 -1
  106. package/dist/fx/iter.d.ts +0 -166
  107. package/dist/fx/iter.d.ts.map +0 -1
  108. package/dist/fx/iter.js +0 -421
  109. package/dist/fx/iter.js.map +0 -1
  110. package/dist/impl/array-deque.d.ts +0 -112
  111. package/dist/impl/array-deque.d.ts.map +0 -1
  112. package/dist/impl/array-deque.js +0 -380
  113. package/dist/impl/array-deque.js.map +0 -1
  114. package/dist/impl/array-list.d.ts +0 -101
  115. package/dist/impl/array-list.d.ts.map +0 -1
  116. package/dist/impl/array-list.js +0 -387
  117. package/dist/impl/array-list.js.map +0 -1
  118. package/dist/impl/hash-map.d.ts +0 -149
  119. package/dist/impl/hash-map.d.ts.map +0 -1
  120. package/dist/impl/hash-map.js +0 -473
  121. package/dist/impl/hash-map.js.map +0 -1
  122. package/dist/impl/hash-set.d.ts +0 -125
  123. package/dist/impl/hash-set.d.ts.map +0 -1
  124. package/dist/impl/hash-set.js +0 -264
  125. package/dist/impl/hash-set.js.map +0 -1
  126. package/dist/impl/linked-hash-map.d.ts +0 -141
  127. package/dist/impl/linked-hash-map.d.ts.map +0 -1
  128. package/dist/impl/linked-hash-map.js +0 -442
  129. package/dist/impl/linked-hash-map.js.map +0 -1
  130. package/dist/impl/linked-list.d.ts +0 -109
  131. package/dist/impl/linked-list.d.ts.map +0 -1
  132. package/dist/impl/linked-list.js +0 -494
  133. package/dist/impl/linked-list.js.map +0 -1
  134. package/dist/impl/priority-queue.d.ts +0 -139
  135. package/dist/impl/priority-queue.d.ts.map +0 -1
  136. package/dist/impl/priority-queue.js +0 -343
  137. package/dist/impl/priority-queue.js.map +0 -1
  138. package/dist/impl/summary-tree.d.ts +0 -146
  139. package/dist/impl/summary-tree.d.ts.map +0 -1
  140. package/dist/impl/summary-tree.js +0 -524
  141. package/dist/impl/summary-tree.js.map +0 -1
  142. package/dist/impl/tree-map.d.ts +0 -153
  143. package/dist/impl/tree-map.d.ts.map +0 -1
  144. package/dist/impl/tree-map.js +0 -788
  145. package/dist/impl/tree-map.js.map +0 -1
  146. package/dist/impl/tree-set.d.ts +0 -112
  147. package/dist/impl/tree-set.d.ts.map +0 -1
  148. package/dist/impl/tree-set.js +0 -374
  149. package/dist/impl/tree-set.js.map +0 -1
  150. package/dist/interfaces/collection.d.ts +0 -119
  151. package/dist/interfaces/collection.d.ts.map +0 -1
  152. package/dist/interfaces/collection.js +0 -8
  153. package/dist/interfaces/collection.js.map +0 -1
  154. package/dist/interfaces/list.d.ts +0 -247
  155. package/dist/interfaces/list.d.ts.map +0 -1
  156. package/dist/interfaces/list.js +0 -8
  157. package/dist/interfaces/list.js.map +0 -1
  158. package/dist/interfaces/map.d.ts +0 -344
  159. package/dist/interfaces/map.d.ts.map +0 -1
  160. package/dist/interfaces/map.js +0 -8
  161. package/dist/interfaces/map.js.map +0 -1
  162. package/dist/interfaces/queue.d.ts +0 -304
  163. package/dist/interfaces/queue.d.ts.map +0 -1
  164. package/dist/interfaces/queue.js +0 -8
  165. package/dist/interfaces/queue.js.map +0 -1
  166. package/dist/interfaces/set.d.ts +0 -137
  167. package/dist/interfaces/set.d.ts.map +0 -1
  168. package/dist/interfaces/set.js +0 -8
  169. package/dist/interfaces/set.js.map +0 -1
  170. package/dist/order/index.d.ts +0 -7
  171. package/dist/order/index.d.ts.map +0 -1
  172. package/dist/order/index.js +0 -7
  173. package/dist/order/index.js.map +0 -1
  174. package/dist/order/rank.d.ts +0 -100
  175. package/dist/order/rank.d.ts.map +0 -1
  176. package/dist/order/rank.js +0 -286
  177. package/dist/order/rank.js.map +0 -1
  178. package/dist/storage/listeners.d.ts +0 -39
  179. package/dist/storage/listeners.d.ts.map +0 -1
  180. package/dist/storage/listeners.js +0 -65
  181. package/dist/storage/listeners.js.map +0 -1
  182. package/dist/testing/check.d.ts +0 -25
  183. package/dist/testing/check.d.ts.map +0 -1
  184. package/dist/testing/check.js +0 -85
  185. package/dist/testing/check.js.map +0 -1
  186. package/dist/utils/defaults.d.ts +0 -135
  187. package/dist/utils/defaults.d.ts.map +0 -1
  188. package/dist/utils/defaults.js +0 -322
  189. package/dist/utils/defaults.js.map +0 -1
  190. package/dist/utils/factories.d.ts +0 -144
  191. package/dist/utils/factories.d.ts.map +0 -1
  192. package/dist/utils/factories.js +0 -293
  193. package/dist/utils/factories.js.map +0 -1
  194. package/dist/utils/helpers.d.ts +0 -137
  195. package/dist/utils/helpers.d.ts.map +0 -1
  196. package/dist/utils/helpers.js +0 -221
  197. package/dist/utils/helpers.js.map +0 -1
  198. package/src/async/async-queue.ts +0 -481
  199. package/src/async/channels.ts +0 -450
  200. package/src/async/settled.ts +0 -41
  201. package/src/behaviors/bounded.ts +0 -485
  202. package/src/behaviors/composite.ts +0 -99
  203. package/src/behaviors/evented.ts +0 -464
  204. package/src/behaviors/lru.ts +0 -447
  205. package/src/behaviors/readonly.ts +0 -236
  206. package/src/behaviors/safe.ts +0 -372
  207. package/src/behaviors/synchronized.ts +0 -316
  208. package/src/behaviors/ttl.ts +0 -462
  209. package/src/contracts/model.ts +0 -44
  210. package/src/core/effects.ts +0 -323
  211. package/src/core/events.ts +0 -356
  212. package/src/core/middleware.ts +0 -415
  213. package/src/core/policies.ts +0 -316
  214. package/src/core/traits.ts +0 -164
  215. package/src/fx/collectors.ts +0 -500
  216. package/src/fx/iter.ts +0 -536
  217. package/src/impl/array-deque.ts +0 -463
  218. package/src/impl/array-list.ts +0 -490
  219. package/src/impl/hash-map.ts +0 -623
  220. package/src/impl/hash-set.ts +0 -342
  221. package/src/impl/linked-hash-map.ts +0 -572
  222. package/src/impl/linked-list.ts +0 -606
  223. package/src/impl/priority-queue.ts +0 -448
  224. package/src/impl/summary-tree.ts +0 -586
  225. package/src/impl/tree-map.ts +0 -912
  226. package/src/impl/tree-set.ts +0 -469
  227. package/src/interfaces/collection.ts +0 -141
  228. package/src/interfaces/list.ts +0 -281
  229. package/src/interfaces/map.ts +0 -405
  230. package/src/interfaces/queue.ts +0 -353
  231. package/src/interfaces/set.ts +0 -158
  232. package/src/order/index.ts +0 -6
  233. package/src/order/rank.ts +0 -271
  234. package/src/storage/listeners.ts +0 -72
  235. package/src/testing/check.ts +0 -81
  236. package/src/utils/defaults.ts +0 -345
  237. package/src/utils/factories.ts +0 -357
  238. package/src/utils/helpers.ts +0 -248
@@ -1,586 +0,0 @@
1
- /**
2
- * SummaryTree: a persistent sequence of leaves with a summary of each part.
3
- *
4
- * The tree is a balanced binary tree (AVL) with one leaf in each node. Each node keeps the summary of its subtree.
5
- * A summary is a value of a monoid: the caller gives the monoid as data, with `empty` and `combine`. The order of the
6
- * leaves is kept, so `combine` must be associative, but it can be non-commutative.
7
- *
8
- * Each operation returns a new tree and keeps the old tree. The two trees share the nodes that did not change.
9
- * Concatenation, a split at an index or at a summary search, an edit and a lookup are O(log n).
10
- *
11
- * The algorithms are the "join" algorithms of Blelloch, Ferizovic and Sun, "Just Join for Parallel Ordered Sets"
12
- * (SPAA 2016).
13
- *
14
- * @example
15
- * // The number of leaves and the total length of the strings
16
- * const spec = {
17
- * monoid: { empty: { count: 0, length: 0 }, combine: (a, b) => ({ count: a.count + b.count, length: a.length + b.length }) },
18
- * measure: (leaf: string) => ({ count: 1, length: leaf.length }),
19
- * };
20
- * const tree = summaryTree(spec, ["ab", "cde", "f"]);
21
- * tree.summary; // { count: 3, length: 6 }
22
- * const [left, right] = tree.split((prefix) => prefix.length > 2); // left: ["ab"], right: ["cde", "f"]
23
- */
24
-
25
- /** A monoid as data: the identity value `empty` and an associative operation `combine`. */
26
- export interface SummaryMonoid<Summary> {
27
- /** The summary of a sequence with no leaf. `combine(empty, s)` and `combine(s, empty)` are equal to `s`. */
28
- readonly empty: Summary;
29
- /** This function gives the summary of a sequence from the summaries of its two parts, left first. */
30
- readonly combine: (left: Summary, right: Summary) => Summary;
31
- }
32
-
33
- /** The data that a tree needs: the monoid of the summaries and the summary of one leaf. */
34
- export interface SummarySpec<Leaf, Summary> {
35
- readonly monoid: SummaryMonoid<Summary>;
36
- /** This function gives the summary of one leaf. */
37
- readonly measure: (leaf: Leaf) => Summary;
38
- /**
39
- * This optional function joins two neighbor leaves at a seam into one leaf, or gives `undefined` to keep both. A seam
40
- * is the place where `concat`, `append`, `prepend`, `splice`, `insert` or `remove` puts two leaves next to each other.
41
- * The tree calls it once at each seam, and not between the leaves that one call inserts. `set` does not call it.
42
- */
43
- readonly merge?: (left: Leaf, right: Leaf) => Leaf | undefined;
44
- }
45
-
46
- /** The result of a summary search: the first leaf whose prefix summary satisfies the predicate. */
47
- export interface SummarySearch<Leaf, Summary> {
48
- /** The index of the leaf. */
49
- readonly index: number;
50
- readonly leaf: Leaf;
51
- /** The summary of the leaves before the leaf. */
52
- readonly before: Summary;
53
- /** The summary of the leaves before the leaf, and of the leaf. */
54
- readonly through: Summary;
55
- }
56
-
57
- /** The result of `locate`: the leaf that holds a position of an additive key, and the position in that leaf. */
58
- export interface SummaryLocation<Leaf> {
59
- /** The index of the leaf. */
60
- readonly index: number;
61
- readonly leaf: Leaf;
62
- /** The position in the leaf: the target minus the key of the leaves before the leaf. */
63
- readonly offset: number;
64
- }
65
-
66
- /** One node of the tree. The tree never changes a node after it makes it. */
67
- interface Node<Leaf, Summary> {
68
- readonly left: Node<Leaf, Summary> | null;
69
- readonly leaf: Leaf;
70
- /** The summary of the leaf of this node only. */
71
- readonly own: Summary;
72
- /** The summary of the leaves of the left subtree and the leaf of this node. A copy with the same left subtree and leaf keeps it. */
73
- readonly inner: Summary;
74
- readonly right: Node<Leaf, Summary> | null;
75
- readonly height: number;
76
- readonly size: number;
77
- /** The summary of all the leaves of this subtree. */
78
- readonly summary: Summary;
79
- }
80
-
81
- type Tree<Leaf, Summary> = Node<Leaf, Summary> | null;
82
-
83
- function heightOf<L, S>(tree: Tree<L, S>): number {
84
- return tree === null ? 0 : tree.height;
85
- }
86
-
87
- function sizeOf<L, S>(tree: Tree<L, S>): number {
88
- return tree === null ? 0 : tree.size;
89
- }
90
-
91
- /** The functions that make and join nodes for one monoid. */
92
- class Builder<L, S> {
93
- constructor(readonly spec: SummarySpec<L, S>) {}
94
-
95
- summaryOf(tree: Tree<L, S>): S {
96
- return tree === null ? this.spec.monoid.empty : tree.summary;
97
- }
98
-
99
- /** This function makes a node. Give `inner` only when it is the summary of `left` and `own`, to save one `combine`. */
100
- node(left: Tree<L, S>, leaf: L, own: S, right: Tree<L, S>, inner?: S): Node<L, S> {
101
- if (inner === undefined) inner = left === null ? own : this.spec.monoid.combine(left.summary, own);
102
- return {
103
- left,
104
- leaf,
105
- own,
106
- inner,
107
- right,
108
- height: 1 + Math.max(heightOf(left), heightOf(right)),
109
- size: sizeOf(left) + 1 + sizeOf(right),
110
- summary: right === null ? inner : this.spec.monoid.combine(inner, right.summary),
111
- };
112
- }
113
-
114
- /** This function copies a node with a new right subtree. It keeps the summary `inner`, so it calls `combine` once at most. */
115
- withRight(n: Node<L, S>, right: Tree<L, S>): Node<L, S> {
116
- return this.node(n.left, n.leaf, n.own, right, n.inner);
117
- }
118
-
119
- rotateLeft(n: Node<L, S>): Node<L, S> {
120
- const r = n.right!;
121
- return this.node(this.withRight(n, r.left), r.leaf, r.own, r.right);
122
- }
123
-
124
- rotateRight(n: Node<L, S>): Node<L, S> {
125
- const l = n.left!;
126
- return this.withRight(l, this.node(l.right, n.leaf, n.own, n.right));
127
- }
128
-
129
- /** This function joins a tree that is higher by more than 1 with a leaf and a lower tree on its right. */
130
- joinRight(left: Node<L, S>, leaf: L, own: S, right: Tree<L, S>): Node<L, S> {
131
- const c = left.right;
132
- if (heightOf(c) <= heightOf(right) + 1) {
133
- const t = this.node(c, leaf, own, right);
134
- if (t.height <= heightOf(left.left) + 1) return this.withRight(left, t);
135
- return this.rotateLeft(this.withRight(left, this.rotateRight(t)));
136
- }
137
- const t = this.joinRight(c!, leaf, own, right);
138
- const joined = this.withRight(left, t);
139
- return t.height <= heightOf(left.left) + 1 ? joined : this.rotateLeft(joined);
140
- }
141
-
142
- /** This function joins a lower tree on the left with a leaf and a tree that is higher by more than 1. */
143
- joinLeft(left: Tree<L, S>, leaf: L, own: S, right: Node<L, S>, inner?: S): Node<L, S> {
144
- const c = right.left;
145
- if (heightOf(c) <= heightOf(left) + 1) {
146
- const t = this.node(left, leaf, own, c, inner);
147
- if (t.height <= heightOf(right.right) + 1) return this.node(t, right.leaf, right.own, right.right);
148
- return this.rotateRight(this.node(this.rotateLeft(t), right.leaf, right.own, right.right));
149
- }
150
- const t = this.joinLeft(left, leaf, own, c!, inner);
151
- const joined = this.node(t, right.leaf, right.own, right.right);
152
- return t.height <= heightOf(right.right) + 1 ? joined : this.rotateRight(joined);
153
- }
154
-
155
- /**
156
- * This function gives the tree of the leaves of `left`, then `leaf`, then the leaves of `right`. Give `inner` only
157
- * when it is the summary of `left` and `own`.
158
- */
159
- join(left: Tree<L, S>, leaf: L, own: S, right: Tree<L, S>, inner?: S): Node<L, S> {
160
- if (heightOf(left) > heightOf(right) + 1) return this.joinRight(left!, leaf, own, right);
161
- if (heightOf(right) > heightOf(left) + 1) return this.joinLeft(left, leaf, own, right!, inner);
162
- return this.node(left, leaf, own, right, inner);
163
- }
164
-
165
- /** This function removes the last leaf of a tree, and gives the rest and the node of that leaf. */
166
- splitLast(tree: Node<L, S>): [Tree<L, S>, Node<L, S>] {
167
- if (tree.right === null) return [tree.left, tree];
168
- const [rest, last] = this.splitLast(tree.right);
169
- return [this.join(tree.left, tree.leaf, tree.own, rest, tree.inner), last];
170
- }
171
-
172
- concat(left: Tree<L, S>, right: Tree<L, S>): Tree<L, S> {
173
- if (left === null) return right;
174
- if (right === null) return left;
175
- const [rest, last] = this.splitLast(left);
176
- return this.join(rest, last.leaf, last.own, right);
177
- }
178
-
179
- /** This function splits a tree into its first `index` leaves and the other leaves. */
180
- splitAt(tree: Tree<L, S>, index: number): [Tree<L, S>, Tree<L, S>] {
181
- if (tree === null) return [null, null];
182
- const before = sizeOf(tree.left);
183
- if (index <= before) {
184
- const [a, b] = this.splitAt(tree.left, index);
185
- return [a, this.join(b, tree.leaf, tree.own, tree.right)];
186
- }
187
- const [a, b] = this.splitAt(tree.right, index - before - 1);
188
- return [this.join(tree.left, tree.leaf, tree.own, a, tree.inner), b];
189
- }
190
-
191
- /** This function splits a tree before the first leaf whose prefix summary, after `acc`, satisfies the predicate. */
192
- splitBy(tree: Tree<L, S>, predicate: (prefix: S) => boolean, acc: S): [Tree<L, S>, Tree<L, S>] {
193
- if (tree === null) return [null, null];
194
- const { combine } = this.spec.monoid;
195
- const throughLeft = tree.left === null ? acc : combine(acc, tree.left.summary);
196
- if (tree.left !== null && predicate(throughLeft)) {
197
- const [a, b] = this.splitBy(tree.left, predicate, acc);
198
- return [a, this.join(b, tree.leaf, tree.own, tree.right)];
199
- }
200
- const throughLeaf = combine(throughLeft, tree.own);
201
- if (predicate(throughLeaf)) return [tree.left, this.join(null, tree.leaf, tree.own, tree.right)];
202
- const [a, b] = this.splitBy(tree.right, predicate, throughLeaf);
203
- return [this.join(tree.left, tree.leaf, tree.own, a, tree.inner), b];
204
- }
205
-
206
- /** This function replaces the leaf at an index in the tree. It copies only the path to the leaf. */
207
- replaceAt(tree: Node<L, S>, index: number, leaf: L, own: S): Node<L, S> {
208
- const before = sizeOf(tree.left);
209
- if (index === before) return this.node(tree.left, leaf, own, tree.right);
210
- if (index < before) return this.node(this.replaceAt(tree.left!, index, leaf, own), tree.leaf, tree.own, tree.right);
211
- return this.withRight(tree, this.replaceAt(tree.right!, index - before - 1, leaf, own));
212
- }
213
-
214
- /** This function puts one leaf before the leaf at an index. It copies only the path, and `join` keeps the balance. */
215
- insertAt(tree: Tree<L, S>, index: number, leaf: L, own: S): Node<L, S> {
216
- if (tree === null) return this.node(null, leaf, own, null);
217
- const before = sizeOf(tree.left);
218
- if (index <= before) return this.join(this.insertAt(tree.left, index, leaf, own), tree.leaf, tree.own, tree.right);
219
- return this.join(tree.left, tree.leaf, tree.own, this.insertAt(tree.right, index - before - 1, leaf, own), tree.inner);
220
- }
221
-
222
- /**
223
- * This function replaces the leaf at an index with one or more leaves. It descends once: at the node of the leaf, it
224
- * puts the first new leaf in the node and the other new leaves at the start of its right subtree.
225
- */
226
- replaceSpan(tree: Node<L, S>, index: number, leaves: readonly L[]): Node<L, S> {
227
- const before = sizeOf(tree.left);
228
- if (index < before) return this.join(this.replaceSpan(tree.left!, index, leaves), tree.leaf, tree.own, tree.right);
229
- if (index > before) return this.join(tree.left, tree.leaf, tree.own, this.replaceSpan(tree.right!, index - before - 1, leaves), tree.inner);
230
- let right = tree.right;
231
- for (let i = 1; i < leaves.length; i++) right = this.insertAt(right, i - 1, leaves[i]!, this.spec.measure(leaves[i]!));
232
- return this.join(tree.left, leaves[0]!, this.spec.measure(leaves[0]!), right);
233
- }
234
-
235
- /** This function removes the leaf at an index. It copies only the path, and `join` keeps the balance. */
236
- removeAt(tree: Node<L, S>, index: number): Tree<L, S> {
237
- const before = sizeOf(tree.left);
238
- if (index === before) return this.concat(tree.left, tree.right);
239
- if (index < before) return this.join(this.removeAt(tree.left!, index), tree.leaf, tree.own, tree.right);
240
- return this.join(tree.left, tree.leaf, tree.own, this.removeAt(tree.right!, index - before - 1), tree.inner);
241
- }
242
-
243
- /** This function gives the leaf at an index of the tree. The index must be in the tree. */
244
- leafAt(tree: Node<L, S>, index: number): L {
245
- let node = tree;
246
- for (;;) {
247
- const before = sizeOf(node.left);
248
- if (index === before) return node.leaf;
249
- if (index < before) node = node.left!;
250
- else {
251
- index -= before + 1;
252
- node = node.right!;
253
- }
254
- }
255
- }
256
-
257
- /** This function joins the leaves at `index` and `index + 1` when `spec.merge` gives one leaf for them. */
258
- seam(tree: Tree<L, S>, index: number): Tree<L, S> {
259
- const { merge } = this.spec;
260
- if (merge === undefined || tree === null || index < 0 || index + 1 >= tree.size) return tree;
261
- const merged = merge(this.leafAt(tree, index), this.leafAt(tree, index + 1));
262
- if (merged === undefined) return tree;
263
- return this.removeAt(this.replaceAt(tree, index, merged, this.spec.measure(merged)), index + 1);
264
- }
265
-
266
- /** This function makes a balanced tree of the leaves `items[lo]` to `items[hi - 1]`. */
267
- build(items: readonly L[], lo: number, hi: number): Tree<L, S> {
268
- if (lo >= hi) return null;
269
- const mid = (lo + hi) >>> 1;
270
- const leaf = items[mid]!;
271
- return this.node(this.build(items, lo, mid), leaf, this.spec.measure(leaf), this.build(items, mid + 1, hi));
272
- }
273
- }
274
-
275
- /** The largest number of removed and inserted leaves for which `splice` copies paths instead of splitting the tree. */
276
- const SMALL_EDIT = 8;
277
-
278
- /** The builders of each spec. Each spec has one builder, so that two trees of the same spec can be joined. */
279
- const builders = new WeakMap<object, Builder<unknown, unknown>>();
280
-
281
- function builderOf<L, S>(spec: SummarySpec<L, S>): Builder<L, S> {
282
- let builder = builders.get(spec) as Builder<L, S> | undefined;
283
- if (builder === undefined) {
284
- builder = new Builder(spec);
285
- builders.set(spec, builder as Builder<unknown, unknown>);
286
- }
287
- return builder;
288
- }
289
-
290
- function* walk<L, S>(tree: Tree<L, S>): Generator<L, void, undefined> {
291
- // An explicit stack: the depth of the tree is O(log n), so the stack stays small
292
- const stack: Node<L, S>[] = [];
293
- let node = tree;
294
- while (node !== null || stack.length > 0) {
295
- while (node !== null) {
296
- stack.push(node);
297
- node = node.left;
298
- }
299
- const top = stack.pop()!;
300
- yield top.leaf;
301
- node = top.right;
302
- }
303
- }
304
-
305
- /** This function gives the index of a leaf, with a negative index counted from the end, or -1 when it is not in the tree. */
306
- function indexOf(index: number, size: number): number {
307
- if (!Number.isInteger(index)) return -1;
308
- const at = index < 0 ? size + index : index;
309
- return at >= 0 && at < size ? at : -1;
310
- }
311
-
312
- function clamp(value: number, size: number): number {
313
- if (Number.isNaN(value)) return 0;
314
- const whole = Math.trunc(value);
315
- return whole < 0 ? Math.max(0, size + whole) : Math.min(whole, size);
316
- }
317
-
318
- /**
319
- * A persistent sequence of leaves with a summary of each part. Make one with `summaryTree`. Each method that changes
320
- * the sequence returns a new tree.
321
- */
322
- export class SummaryTree<Leaf, Summary> implements Iterable<Leaf> {
323
- private constructor(
324
- private readonly builder: Builder<Leaf, Summary>,
325
- private readonly root: Tree<Leaf, Summary>,
326
- ) {}
327
-
328
- /** This function makes a tree of a spec from leaves, as `summaryTree` does. */
329
- static of<Leaf, Summary>(spec: SummarySpec<Leaf, Summary>, leaves: Iterable<Leaf> = []): SummaryTree<Leaf, Summary> {
330
- const builder = builderOf(spec);
331
- const items = [...leaves];
332
- return new SummaryTree(builder, builder.build(items, 0, items.length));
333
- }
334
-
335
- private wrap(root: Tree<Leaf, Summary>): SummaryTree<Leaf, Summary> {
336
- return root === this.root ? this : new SummaryTree(this.builder, root);
337
- }
338
-
339
- private treeOf(leaves: Iterable<Leaf> | SummaryTree<Leaf, Summary>): Tree<Leaf, Summary> {
340
- if (leaves instanceof SummaryTree) {
341
- if (leaves.builder !== this.builder) throw new TypeError("SummaryTree: the two trees have different specs");
342
- return leaves.root;
343
- }
344
- const items = [...leaves];
345
- return this.builder.build(items, 0, items.length);
346
- }
347
-
348
- /** The spec of the tree: its monoid and its measure. */
349
- get spec(): SummarySpec<Leaf, Summary> {
350
- return this.builder.spec;
351
- }
352
-
353
- /** The number of leaves. */
354
- get size(): number {
355
- return sizeOf(this.root);
356
- }
357
-
358
- get isEmpty(): boolean {
359
- return this.root === null;
360
- }
361
-
362
- /** The summary of all the leaves, in their order. An empty tree has the summary `monoid.empty`. */
363
- get summary(): Summary {
364
- return this.builder.summaryOf(this.root);
365
- }
366
-
367
- /** The height of the tree: 0 for an empty tree. The height is at most 1.45 log2(size + 2). */
368
- get height(): number {
369
- return heightOf(this.root);
370
- }
371
-
372
- /**
373
- * This function gives the leaf at an index, or `undefined` when the index is not in the tree. A negative index counts
374
- * from the end, as `Array.prototype.at` does.
375
- */
376
- at(index: number): Leaf | undefined {
377
- index = indexOf(index, this.size);
378
- return index === -1 ? undefined : this.builder.leafAt(this.root!, index);
379
- }
380
-
381
- /** This function gives the summary of the leaves from `start` to `end`, with `end` not included. */
382
- summaryOf(start = 0, end: number = this.size): Summary {
383
- return this.slice(start, end).summary;
384
- }
385
-
386
- /** This function gives the tree of the leaves of this tree, then the leaves of `other`. The two trees must have the same spec. */
387
- concat(other: SummaryTree<Leaf, Summary> | Iterable<Leaf>): SummaryTree<Leaf, Summary> {
388
- const joined = this.builder.concat(this.root, this.treeOf(other));
389
- return this.wrap(this.builder.seam(joined, this.size - 1));
390
- }
391
-
392
- /** This function splits the tree into its first `index` leaves and the other leaves. */
393
- splitAt(index: number): [SummaryTree<Leaf, Summary>, SummaryTree<Leaf, Summary>] {
394
- const [left, right] = this.builder.splitAt(this.root, clamp(index, this.size));
395
- return [this.wrap(left), this.wrap(right)];
396
- }
397
-
398
- /**
399
- * This function splits the tree before the first leaf whose prefix summary satisfies the predicate. The prefix of a
400
- * leaf includes the leaf. The predicate must be monotone: after it is true for a prefix, it is true for each longer
401
- * prefix. When no prefix satisfies it, the right tree is empty.
402
- */
403
- split(predicate: (prefix: Summary) => boolean): [SummaryTree<Leaf, Summary>, SummaryTree<Leaf, Summary>] {
404
- const [left, right] = this.builder.splitBy(this.root, predicate, this.builder.spec.monoid.empty);
405
- return [this.wrap(left), this.wrap(right)];
406
- }
407
-
408
- /**
409
- * This function finds the first leaf whose prefix summary satisfies a monotone predicate, as `split` does. It changes
410
- * nothing. It gives `undefined` when no prefix satisfies the predicate.
411
- */
412
- search(predicate: (prefix: Summary) => boolean): SummarySearch<Leaf, Summary> | undefined {
413
- const { combine } = this.builder.spec.monoid;
414
- let node = this.root;
415
- let acc = this.builder.spec.monoid.empty;
416
- let index = 0;
417
- while (node !== null) {
418
- const throughLeft = node.left === null ? acc : combine(acc, node.left.summary);
419
- if (node.left !== null && predicate(throughLeft)) {
420
- node = node.left;
421
- continue;
422
- }
423
- const through = combine(throughLeft, node.own);
424
- if (predicate(through)) return { index: index + sizeOf(node.left), leaf: node.leaf, before: throughLeft, through };
425
- index += sizeOf(node.left) + 1;
426
- acc = through;
427
- node = node.right;
428
- }
429
- return undefined;
430
- }
431
-
432
- /**
433
- * This function finds the leaf that holds a position of an additive key, and it calls no `combine`. The key must be
434
- * additive and not negative: `key(combine(a, b))` is `key(a) + key(b)`. The function descends by the key of each left
435
- * subtree and of each leaf, and subtracts it from the target. It gives the first leaf whose prefix key is more than
436
- * the target, or `undefined` when the target is not in `[0, key(summary))`.
437
- *
438
- * @example
439
- * // The leaf and the offset in it of the code unit at position 1000 of a rope
440
- * const at = rope.locate((s) => s.length, 1000);
441
- */
442
- locate(key: (summary: Summary) => number, target: number): SummaryLocation<Leaf> | undefined {
443
- if (!(target >= 0)) return undefined;
444
- let node = this.root;
445
- let index = 0;
446
- let rest = target;
447
- while (node !== null) {
448
- if (node.left !== null) {
449
- const left = key(node.left.summary);
450
- if (rest < left) {
451
- node = node.left;
452
- continue;
453
- }
454
- rest -= left;
455
- index += node.left.size;
456
- }
457
- const own = key(node.own);
458
- if (rest < own) return { index, leaf: node.leaf, offset: rest };
459
- rest -= own;
460
- index += 1;
461
- node = node.right;
462
- }
463
- return undefined;
464
- }
465
-
466
- /** This function gives the tree of the leaves from `start` to `end`, with `end` not included. A negative index counts from the end. */
467
- slice(start = 0, end: number = this.size): SummaryTree<Leaf, Summary> {
468
- const from = clamp(start, this.size);
469
- const to = clamp(end, this.size);
470
- if (to <= from) return this.wrap(null);
471
- const [, rest] = this.builder.splitAt(this.root, from);
472
- const [middle] = this.builder.splitAt(rest, to - from);
473
- return this.wrap(middle);
474
- }
475
-
476
- /**
477
- * This function removes `deleteCount` leaves at `start` and puts the leaves of `inserted` there, as
478
- * `Array.prototype.splice` does. It returns the new tree, not the removed leaves.
479
- */
480
- splice(start: number, deleteCount: number = this.size, inserted: SummaryTree<Leaf, Summary> | Iterable<Leaf> = []): SummaryTree<Leaf, Summary> {
481
- const from = clamp(start, this.size);
482
- const count = Math.max(0, Math.min(Number.isNaN(deleteCount) ? 0 : Math.trunc(deleteCount), this.size - from));
483
- const builder = this.builder;
484
- const leaves = inserted instanceof SummaryTree ? undefined : [...inserted];
485
- const added = leaves === undefined ? (inserted as SummaryTree<Leaf, Summary>).size : leaves.length;
486
- if (count === 0 && added === 0) return this;
487
- let root = this.root;
488
- if (leaves !== undefined && count + added <= SMALL_EDIT) {
489
- // A small edit copies only the paths to its leaves: 1 or 2 combines for each level of each path
490
- const replaced = Math.min(count, added);
491
- if (count > 0 && added > count) {
492
- // The last removed leaf takes all the extra leaves in one descent
493
- for (let i = 0; i < count - 1; i++) root = builder.replaceAt(root!, from + i, leaves[i]!, builder.spec.measure(leaves[i]!));
494
- root = builder.replaceSpan(root!, from + count - 1, leaves.slice(count - 1));
495
- } else for (let i = 0; i < added; i++) {
496
- const leaf = leaves[i]!;
497
- const own = builder.spec.measure(leaf);
498
- root = i < replaced ? builder.replaceAt(root!, from + i, leaf, own) : builder.insertAt(root, from + i, leaf, own);
499
- }
500
- for (let i = replaced; i < count; i++) root = builder.removeAt(root!, from + added);
501
- } else {
502
- const middle = leaves === undefined ? this.treeOf(inserted) : builder.build(leaves, 0, leaves.length);
503
- const [left, rest] = builder.splitAt(root, from);
504
- const [, right] = builder.splitAt(rest, count);
505
- root = builder.concat(builder.concat(left, middle), right);
506
- }
507
- // The right seam first, so that a merge there does not move the index of the left seam
508
- if (added > 0) root = builder.seam(root, from + added - 1);
509
- return this.wrap(builder.seam(root, from - 1));
510
- }
511
-
512
- /** This function puts the leaves of `inserted` before the leaf at `index`. */
513
- insert(index: number, inserted: SummaryTree<Leaf, Summary> | Iterable<Leaf>): SummaryTree<Leaf, Summary> {
514
- return this.splice(index, 0, inserted);
515
- }
516
-
517
- /** This function removes the leaves from `start` to `end`, with `end` not included. */
518
- remove(start: number, end: number = this.size): SummaryTree<Leaf, Summary> {
519
- const from = clamp(start, this.size);
520
- return this.splice(from, Math.max(0, clamp(end, this.size) - from));
521
- }
522
-
523
- /**
524
- * This function replaces the leaf at an index. A negative index counts from the end, as `Array.prototype.with` does.
525
- * An index that is not in the tree changes nothing. It copies only the path to the leaf, and it does not call
526
- * `spec.merge`.
527
- */
528
- set(index: number, leaf: Leaf): SummaryTree<Leaf, Summary> {
529
- const at = indexOf(index, this.size);
530
- return at === -1 ? this : this.wrap(this.builder.replaceAt(this.root!, at, leaf, this.builder.spec.measure(leaf)));
531
- }
532
-
533
- /** This function adds leaves after the last leaf. */
534
- append(...leaves: Leaf[]): SummaryTree<Leaf, Summary> {
535
- return this.concat(leaves);
536
- }
537
-
538
- /** This function adds leaves before the first leaf. */
539
- prepend(...leaves: Leaf[]): SummaryTree<Leaf, Summary> {
540
- const joined = this.builder.concat(this.treeOf(leaves), this.root);
541
- return this.wrap(this.builder.seam(joined, leaves.length - 1));
542
- }
543
-
544
- /** This function gives the leaves in their order. */
545
- [Symbol.iterator](): Iterator<Leaf> {
546
- return walk(this.root);
547
- }
548
-
549
- /** This function gives the leaves in an array. */
550
- toArray(): Leaf[] {
551
- return [...walk(this.root)];
552
- }
553
- }
554
-
555
- /**
556
- * This function makes a persistent tree of leaves with a summary of each part. Use the same `spec` object for each
557
- * tree that `concat` or `splice` joins.
558
- */
559
- export function summaryTree<Leaf, Summary>(spec: SummarySpec<Leaf, Summary>, leaves: Iterable<Leaf> = []): SummaryTree<Leaf, Summary> {
560
- return SummaryTree.of(spec, leaves);
561
- }
562
-
563
- /**
564
- * @internal This function examines the invariants of a tree: the AVL balance, the heights, the sizes and the
565
- * summaries. It gives a list of the faults, empty when there is no fault. The tests use it.
566
- */
567
- export function summaryTreeFaults<Leaf, Summary>(tree: SummaryTree<Leaf, Summary>, equal: (a: Summary, b: Summary) => boolean): string[] {
568
- const builder = tree["builder"];
569
- const faults: string[] = [];
570
- const visit = (node: Tree<Leaf, Summary>, path: string): void => {
571
- if (node === null) return;
572
- visit(node.left, `${path}L`);
573
- visit(node.right, `${path}R`);
574
- const hl = heightOf(node.left);
575
- const hr = heightOf(node.right);
576
- if (Math.abs(hl - hr) > 1) faults.push(`${path}: balance ${hl - hr}`);
577
- if (node.height !== 1 + Math.max(hl, hr)) faults.push(`${path}: height ${node.height}`);
578
- if (node.size !== sizeOf(node.left) + 1 + sizeOf(node.right)) faults.push(`${path}: size ${node.size}`);
579
- const { combine } = builder.spec.monoid;
580
- const inner = combine(builder.summaryOf(node.left), builder.spec.measure(node.leaf));
581
- if (!equal(node.inner, inner)) faults.push(`${path}: inner`);
582
- if (!equal(node.summary, combine(inner, builder.summaryOf(node.right)))) faults.push(`${path}: summary`);
583
- };
584
- visit(tree["root"], "^");
585
- return faults;
586
- }