@fluidframework/merge-tree 3.1.0 → 3.2.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 (46) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/dist/index.d.ts +1 -2
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +2 -7
  5. package/dist/index.js.map +1 -1
  6. package/dist/referencePositions.d.ts +0 -10
  7. package/dist/referencePositions.d.ts.map +1 -1
  8. package/dist/referencePositions.js +0 -16
  9. package/dist/referencePositions.js.map +1 -1
  10. package/dist/test/index.d.ts +1 -1
  11. package/dist/test/index.d.ts.map +1 -1
  12. package/dist/test/index.js +2 -5
  13. package/dist/test/index.js.map +1 -1
  14. package/lib/index.d.ts +1 -2
  15. package/lib/index.d.ts.map +1 -1
  16. package/lib/index.js +1 -2
  17. package/lib/index.js.map +1 -1
  18. package/lib/referencePositions.d.ts +0 -10
  19. package/lib/referencePositions.d.ts.map +1 -1
  20. package/lib/referencePositions.js +0 -14
  21. package/lib/referencePositions.js.map +1 -1
  22. package/lib/test/index.d.ts +1 -1
  23. package/lib/test/index.d.ts.map +1 -1
  24. package/lib/test/index.js +1 -1
  25. package/lib/test/index.js.map +1 -1
  26. package/package.json +16 -16
  27. package/src/index.ts +0 -17
  28. package/src/referencePositions.ts +0 -16
  29. package/dist/collections/index.d.ts +0 -6
  30. package/dist/collections/index.d.ts.map +0 -1
  31. package/dist/collections/index.js +0 -11
  32. package/dist/collections/index.js.map +0 -1
  33. package/dist/collections/rbTree.d.ts +0 -160
  34. package/dist/collections/rbTree.d.ts.map +0 -1
  35. package/dist/collections/rbTree.js +0 -496
  36. package/dist/collections/rbTree.js.map +0 -1
  37. package/lib/collections/index.d.ts +0 -6
  38. package/lib/collections/index.d.ts.map +0 -1
  39. package/lib/collections/index.js +0 -6
  40. package/lib/collections/index.js.map +0 -1
  41. package/lib/collections/rbTree.d.ts +0 -160
  42. package/lib/collections/rbTree.d.ts.map +0 -1
  43. package/lib/collections/rbTree.js +0 -492
  44. package/lib/collections/rbTree.js.map +0 -1
  45. package/src/collections/index.ts +0 -20
  46. package/src/collections/rbTree.ts +0 -721
@@ -1,160 +0,0 @@
1
- /*!
2
- * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
- * Licensed under the MIT License.
4
- */
5
- /**
6
- * @internal
7
- */
8
- export declare const RBColor: {
9
- readonly RED: 0;
10
- readonly BLACK: 1;
11
- };
12
- /**
13
- * @internal
14
- */
15
- export type RBColor = (typeof RBColor)[keyof typeof RBColor];
16
- /**
17
- * @internal
18
- */
19
- export interface RBNode<TKey, TData> {
20
- key: TKey;
21
- data: TData;
22
- left: RBNode<TKey, TData> | undefined;
23
- right: RBNode<TKey, TData> | undefined;
24
- color: RBColor;
25
- size: number;
26
- }
27
- /**
28
- * @internal
29
- */
30
- export interface IRBAugmentation<TKey, TData> {
31
- update(node: RBNode<TKey, TData>): void;
32
- }
33
- /**
34
- * @internal
35
- */
36
- export interface IRBMatcher<TKey, TData> {
37
- continueSubtree(node: RBNode<TKey, TData> | undefined, key: TKey): boolean;
38
- matchNode(node: RBNode<TKey, TData> | undefined, key: TKey): boolean;
39
- }
40
- /**
41
- * @internal
42
- */
43
- export interface RBNodeActions<TKey, TData> {
44
- infix?(node: RBNode<TKey, TData>): boolean;
45
- pre?(node: RBNode<TKey, TData>): boolean;
46
- post?(node: RBNode<TKey, TData>): boolean;
47
- showStructure?: boolean;
48
- }
49
- /**
50
- * @internal
51
- */
52
- export interface KeyComparer<TKey> {
53
- (a: TKey, b: TKey): number;
54
- }
55
- /**
56
- * @internal
57
- */
58
- export interface Property<TKey, TData> {
59
- key: TKey;
60
- data: TData;
61
- }
62
- /**
63
- * @internal
64
- */
65
- export interface PropertyAction<TKey, TData> {
66
- <TAccum>(p: Property<TKey, TData>, accum?: TAccum): boolean;
67
- }
68
- /**
69
- * @internal
70
- */
71
- export interface QProperty<TKey, TData> {
72
- key?: TKey;
73
- data?: TData;
74
- }
75
- /**
76
- * @internal
77
- */
78
- export type ConflictAction<TKey, TData> = (key: TKey, currentKey: TKey, data: TData, currentData: TData) => QProperty<TKey, TData>;
79
- /**
80
- * @internal
81
- */
82
- export interface SortedDictionary<TKey, TData> extends Dictionary<TKey, TData> {
83
- max(): Property<TKey, TData> | undefined;
84
- min(): Property<TKey, TData> | undefined;
85
- mapRange<TAccum>(action: PropertyAction<TKey, TData>, accum?: TAccum, start?: TKey, end?: TKey): void;
86
- }
87
- /**
88
- * @internal
89
- */
90
- export interface Dictionary<TKey, TData> {
91
- get(key: TKey): Property<TKey, TData> | undefined;
92
- put(key: TKey, data: TData, conflict?: ConflictAction<TKey, TData>): void;
93
- remove(key: TKey): void;
94
- map<TAccum>(action: PropertyAction<TKey, TData>, accum?: TAccum): void;
95
- }
96
- /**
97
- * @internal
98
- */
99
- export declare class RedBlackTree<TKey, TData> implements SortedDictionary<TKey, TData> {
100
- private readonly compareKeys;
101
- private readonly aug?;
102
- private root;
103
- constructor(compareKeys: KeyComparer<TKey>, aug?: IRBAugmentation<TKey, TData> | undefined);
104
- private makeNode;
105
- private isRed;
106
- private nodeSize;
107
- size(): number;
108
- isEmpty(): boolean;
109
- get(key: TKey): RBNode<TKey, TData> | undefined;
110
- private nodeGet;
111
- private contains;
112
- gather(key: TKey, matcher: IRBMatcher<TKey, TData>): RBNode<TKey, TData>[];
113
- private nodeGather;
114
- walkExactMatchesForward(compareFn: (node: RBNode<TKey, TData>) => number, actionFn: (node: RBNode<TKey, TData>) => void, continueLeftFn: (number: number) => boolean, continueRightFn: (number: number) => boolean): void;
115
- private nodeWalkExactMatchesForward;
116
- walkExactMatchesBackward(compareFn: (node: RBNode<TKey, TData>) => number, actionFn: (node: RBNode<TKey, TData>) => void, continueLeftFn: (number: number) => boolean, continueRightFn: (number: number) => boolean): void;
117
- private nodeWalkExactMatchesBackward;
118
- put(key: TKey, data: TData, conflict?: ConflictAction<TKey, TData>): void;
119
- private nodePut;
120
- private updateLocal;
121
- private nodeRemoveMin;
122
- remove(key: TKey): void;
123
- removeExisting(key: TKey): void;
124
- private nodeRemove;
125
- /**
126
- * Finds the largest node that is less than or equal to a given key.
127
- */
128
- floor(key: TKey): RBNode<TKey, TData> | undefined;
129
- private nodeFloor;
130
- /**
131
- * Finds the smallest node that is greater than or equal to a given key.
132
- */
133
- ceil(key: TKey): RBNode<TKey, TData> | undefined;
134
- private nodeCeil;
135
- min(): RBNode<TKey, TData> | undefined;
136
- private nodeMin;
137
- max(): RBNode<TKey, TData> | undefined;
138
- private nodeMax;
139
- private rotateRight;
140
- private rotateLeft;
141
- private oppositeColor;
142
- private flipColors;
143
- private moveRedLeft;
144
- private moveRedRight;
145
- private balance;
146
- mapRange<TAccum>(action: PropertyAction<TKey, TData>, accum?: TAccum, start?: TKey, end?: TKey): void;
147
- map<TAccum>(action: PropertyAction<TKey, TData>, accum?: TAccum): void;
148
- keys(): TKey[];
149
- /**
150
- * Depth-first traversal with custom action; if action returns
151
- * false, traversal is halted.
152
- * @param action - action to apply to each node
153
- */
154
- walk(actions: RBNodeActions<TKey, TData>): void;
155
- walkBackward(actions: RBNodeActions<TKey, TData>): void;
156
- private nodeWalk;
157
- private nodeWalkBackward;
158
- private nodeMap;
159
- }
160
- //# sourceMappingURL=rbTree.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"rbTree.d.ts","sourceRoot":"","sources":["../../src/collections/rbTree.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;GAEG;AACH,eAAO,MAAM,OAAO;;;CAGV,CAAC;AAEX;;GAEG;AACH,MAAM,MAAM,OAAO,GAAG,CAAC,OAAO,OAAO,CAAC,CAAC,MAAM,OAAO,OAAO,CAAC,CAAC;AAE7D;;GAEG;AACH,MAAM,WAAW,MAAM,CAAC,IAAI,EAAE,KAAK;IAClC,GAAG,EAAE,IAAI,CAAC;IACV,IAAI,EAAE,KAAK,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,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O,CAAC,WAAW;IAgBnB,OAAO,CAAC,UAAU;IAgBlB,OAAO,CAAC,aAAa;IAIrB,OAAO,CAAC,UAAU;IAQlB,OAAO,CAAC,WAAW;IAanB,OAAO,CAAC,YAAY;IAWpB,OAAO,CAAC,OAAO;IAmBR,QAAQ,CAAC,MAAM,EACrB,MAAM,EAAE,cAAc,CAAC,IAAI,EAAE,KAAK,CAAC,EACnC,KAAK,CAAC,EAAE,MAAM,EACd,KAAK,CAAC,EAAE,IAAI,EACZ,GAAG,CAAC,EAAE,IAAI,GACR,IAAI;IAIA,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,cAAc,CAAC,IAAI,EAAE,KAAK,CAAC,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI;IAKtE,IAAI,IAAI,IAAI,EAAE;IAarB;;;;OAIG;IACI,IAAI,CAAC,OAAO,EAAE,aAAa,CAAC,IAAI,EAAE,KAAK,CAAC,GAAG,IAAI;IAI/C,YAAY,CAAC,OAAO,EAAE,aAAa,CAAC,IAAI,EAAE,KAAK,CAAC,GAAG,IAAI;IAI9D,OAAO,CAAC,QAAQ;IAyBhB,OAAO,CAAC,gBAAgB;IAyBxB,OAAO,CAAC,OAAO;CAmCf"}
@@ -1,492 +0,0 @@
1
- /*!
2
- * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
- * Licensed under the MIT License.
4
- */
5
- /**
6
- * @internal
7
- */
8
- export const RBColor = {
9
- RED: 0,
10
- BLACK: 1,
11
- };
12
- /**
13
- * @internal
14
- */
15
- export class RedBlackTree {
16
- compareKeys;
17
- aug;
18
- root;
19
- constructor(compareKeys, aug) {
20
- this.compareKeys = compareKeys;
21
- this.aug = aug;
22
- }
23
- makeNode(key, data, color, size) {
24
- return { key, data, color, size };
25
- }
26
- isRed(node) {
27
- return !!node && node.color === RBColor.RED;
28
- }
29
- nodeSize(node) {
30
- return node ? node.size : 0;
31
- }
32
- size() {
33
- return this.nodeSize(this.root);
34
- }
35
- isEmpty() {
36
- return !this.root;
37
- }
38
- get(key) {
39
- if (key !== undefined) {
40
- return this.nodeGet(this.root, key);
41
- }
42
- }
43
- nodeGet(node, key) {
44
- let _node = node;
45
- while (_node) {
46
- const cmp = this.compareKeys(key, _node.key);
47
- if (cmp < 0) {
48
- _node = _node.left;
49
- }
50
- else if (cmp > 0) {
51
- _node = _node.right;
52
- }
53
- else {
54
- return _node;
55
- }
56
- }
57
- }
58
- contains(key) {
59
- return this.get(key);
60
- }
61
- gather(key, matcher) {
62
- const results = [];
63
- if (key !== undefined) {
64
- this.nodeGather(this.root, results, key, matcher);
65
- }
66
- return results;
67
- }
68
- nodeGather(node, results, key, matcher) {
69
- if (node) {
70
- if (matcher.continueSubtree(node.left, key)) {
71
- this.nodeGather(node.left, results, key, matcher);
72
- }
73
- if (matcher.matchNode(node, key)) {
74
- results.push(node);
75
- }
76
- if (matcher.continueSubtree(node.right, key)) {
77
- this.nodeGather(node.right, results, key, matcher);
78
- }
79
- }
80
- }
81
- walkExactMatchesForward(compareFn, actionFn, continueLeftFn, continueRightFn) {
82
- this.nodeWalkExactMatchesForward(this.root, compareFn, actionFn, continueLeftFn, continueRightFn);
83
- }
84
- nodeWalkExactMatchesForward(node, compareFn, actionFn, continueLeftFn, continueRightFn) {
85
- if (!node) {
86
- return;
87
- }
88
- const result = compareFn(node);
89
- if (continueLeftFn(result)) {
90
- this.nodeWalkExactMatchesForward(node.left, compareFn, actionFn, continueLeftFn, continueRightFn);
91
- }
92
- if (result === 0) {
93
- actionFn(node);
94
- }
95
- if (continueRightFn(result)) {
96
- this.nodeWalkExactMatchesForward(node.right, compareFn, actionFn, continueLeftFn, continueRightFn);
97
- }
98
- }
99
- walkExactMatchesBackward(compareFn, actionFn, continueLeftFn, continueRightFn) {
100
- this.nodeWalkExactMatchesBackward(this.root, compareFn, actionFn, continueLeftFn, continueRightFn);
101
- }
102
- nodeWalkExactMatchesBackward(node, compareFn, actionFn, continueLeftFn, continueRightFn) {
103
- if (!node) {
104
- return;
105
- }
106
- const result = compareFn(node);
107
- if (continueRightFn(result)) {
108
- this.nodeWalkExactMatchesBackward(node.right, compareFn, actionFn, continueLeftFn, continueRightFn);
109
- }
110
- if (result === 0) {
111
- actionFn(node);
112
- }
113
- if (continueLeftFn(result)) {
114
- this.nodeWalkExactMatchesBackward(node.left, compareFn, actionFn, continueLeftFn, continueRightFn);
115
- }
116
- }
117
- put(key, data, conflict) {
118
- if (key !== undefined) {
119
- if (data === undefined) {
120
- this.remove(key);
121
- }
122
- else {
123
- this.root = this.nodePut(this.root, key, data, conflict);
124
- this.root.color = RBColor.BLACK;
125
- }
126
- }
127
- }
128
- nodePut(node, key, data, conflict) {
129
- let _node = node;
130
- if (_node) {
131
- const cmp = this.compareKeys(key, _node.key);
132
- if (cmp < 0) {
133
- _node.left = this.nodePut(_node.left, key, data, conflict);
134
- }
135
- else if (cmp > 0) {
136
- _node.right = this.nodePut(_node.right, key, data, conflict);
137
- }
138
- else {
139
- if (conflict) {
140
- const kd = conflict(key, _node.key, data, _node.data);
141
- if (kd.key) {
142
- _node.key = kd.key;
143
- }
144
- _node.data = kd.data ?? data;
145
- }
146
- else {
147
- _node.data = data;
148
- }
149
- }
150
- if (this.isRed(_node.right) && !this.isRed(_node.left)) {
151
- _node = this.rotateLeft(_node);
152
- }
153
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
154
- if (this.isRed(_node.left) && this.isRed(_node.left.left)) {
155
- _node = this.rotateRight(_node);
156
- }
157
- if (this.isRed(_node.left) && this.isRed(_node.right)) {
158
- this.flipColors(_node);
159
- }
160
- _node.size = this.nodeSize(_node.left) + this.nodeSize(_node.right) + 1;
161
- if (this.aug) {
162
- this.updateLocal(_node);
163
- }
164
- return _node;
165
- }
166
- else {
167
- return this.makeNode(key, data, RBColor.RED, 1);
168
- }
169
- }
170
- updateLocal(node) {
171
- if (this.aug) {
172
- if (this.isRed(node.left)) {
173
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
174
- this.aug.update(node.left);
175
- }
176
- if (this.isRed(node.right)) {
177
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
178
- this.aug.update(node.right);
179
- }
180
- this.aug.update(node);
181
- }
182
- }
183
- nodeRemoveMin(node) {
184
- let _node = node;
185
- if (_node.left) {
186
- if (!this.isRed(_node.left) && !this.isRed(_node.left.left)) {
187
- _node = this.moveRedLeft(_node);
188
- }
189
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
190
- _node.left = this.nodeRemoveMin(_node.left);
191
- return this.balance(_node);
192
- }
193
- }
194
- remove(key) {
195
- if (key !== undefined) {
196
- if (!this.contains(key)) {
197
- return;
198
- }
199
- this.removeExisting(key);
200
- }
201
- // TODO: error on undefined key
202
- }
203
- removeExisting(key) {
204
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
205
- if (!this.isRed(this.root.left) && !this.isRed(this.root.right)) {
206
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
207
- this.root.color = RBColor.RED;
208
- }
209
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
210
- this.root = this.nodeRemove(this.root, key);
211
- }
212
- nodeRemove(node, key) {
213
- let _node = node;
214
- if (this.compareKeys(key, _node.key) < 0) {
215
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
216
- if (!this.isRed(_node.left) && !this.isRed(_node.left.left)) {
217
- _node = this.moveRedLeft(_node);
218
- }
219
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
220
- _node.left = this.nodeRemove(_node.left, key);
221
- }
222
- else {
223
- if (this.isRed(_node.left)) {
224
- _node = this.rotateRight(_node);
225
- }
226
- if (this.compareKeys(key, _node.key) === 0 && !_node.right) {
227
- return undefined;
228
- }
229
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
230
- if (!this.isRed(_node.right) && !this.isRed(_node.right.left)) {
231
- _node = this.moveRedRight(_node);
232
- }
233
- if (this.compareKeys(key, _node.key) === 0) {
234
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
235
- const subtreeMin = this.nodeMin(_node.right);
236
- _node.key = subtreeMin.key;
237
- _node.data = subtreeMin.data;
238
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
239
- _node.right = this.nodeRemoveMin(_node.right);
240
- }
241
- else {
242
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
243
- _node.right = this.nodeRemove(_node.right, key);
244
- }
245
- }
246
- return this.balance(_node);
247
- }
248
- /**
249
- * Finds the largest node that is less than or equal to a given key.
250
- */
251
- floor(key) {
252
- if (!this.isEmpty()) {
253
- return this.nodeFloor(this.root, key);
254
- }
255
- }
256
- nodeFloor(node, key) {
257
- if (node) {
258
- const cmp = this.compareKeys(key, node.key);
259
- if (cmp === 0) {
260
- return node;
261
- }
262
- else if (cmp < 0) {
263
- return this.nodeFloor(node.left, key);
264
- }
265
- else {
266
- const rightFloor = this.nodeFloor(node.right, key);
267
- return rightFloor ?? node;
268
- }
269
- }
270
- }
271
- /**
272
- * Finds the smallest node that is greater than or equal to a given key.
273
- */
274
- ceil(key) {
275
- if (!this.isEmpty()) {
276
- return this.nodeCeil(this.root, key);
277
- }
278
- }
279
- nodeCeil(node, key) {
280
- if (node) {
281
- const cmp = this.compareKeys(key, node.key);
282
- if (cmp === 0) {
283
- return node;
284
- }
285
- else if (cmp > 0) {
286
- return this.nodeCeil(node.right, key);
287
- }
288
- else {
289
- const leftCeil = this.nodeCeil(node.left, key);
290
- return leftCeil ?? node;
291
- }
292
- }
293
- }
294
- min() {
295
- if (this.root) {
296
- return this.nodeMin(this.root);
297
- }
298
- }
299
- nodeMin(node) {
300
- return node.left ? this.nodeMin(node.left) : node;
301
- }
302
- max() {
303
- if (this.root) {
304
- return this.nodeMax(this.root);
305
- }
306
- }
307
- nodeMax(node) {
308
- return node.right ? this.nodeMax(node.right) : node;
309
- }
310
- rotateRight(node) {
311
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
312
- const leftChild = node.left;
313
- node.left = leftChild.right;
314
- leftChild.right = node;
315
- leftChild.color = leftChild.right.color;
316
- leftChild.right.color = RBColor.RED;
317
- leftChild.size = node.size;
318
- node.size = this.nodeSize(node.left) + this.nodeSize(node.right) + 1;
319
- if (this.aug) {
320
- this.updateLocal(node);
321
- this.updateLocal(leftChild);
322
- }
323
- return leftChild;
324
- }
325
- rotateLeft(node) {
326
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
327
- const rightChild = node.right;
328
- node.right = rightChild.left;
329
- rightChild.left = node;
330
- rightChild.color = rightChild.left.color;
331
- rightChild.left.color = RBColor.RED;
332
- rightChild.size = node.size;
333
- node.size = this.nodeSize(node.left) + this.nodeSize(node.right) + 1;
334
- if (this.aug) {
335
- this.updateLocal(node);
336
- this.updateLocal(rightChild);
337
- }
338
- return rightChild;
339
- }
340
- oppositeColor(c) {
341
- return c === RBColor.BLACK ? RBColor.RED : RBColor.BLACK;
342
- }
343
- flipColors(node) {
344
- node.color = this.oppositeColor(node.color);
345
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
346
- node.left.color = this.oppositeColor(node.left.color);
347
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
348
- node.right.color = this.oppositeColor(node.right.color);
349
- }
350
- moveRedLeft(node) {
351
- let _node = node;
352
- this.flipColors(_node);
353
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
354
- if (this.isRed(_node.right.left)) {
355
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
356
- _node.right = this.rotateRight(_node.right);
357
- _node = this.rotateLeft(_node);
358
- this.flipColors(_node);
359
- }
360
- return _node;
361
- }
362
- moveRedRight(node) {
363
- let _node = node;
364
- this.flipColors(_node);
365
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
366
- if (this.isRed(_node.left.left)) {
367
- _node = this.rotateRight(_node);
368
- this.flipColors(_node);
369
- }
370
- return _node;
371
- }
372
- balance(input) {
373
- let node = input;
374
- if (this.isRed(node.right)) {
375
- node = this.rotateLeft(node);
376
- }
377
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
378
- if (this.isRed(node.left) && this.isRed(node.left.left)) {
379
- node = this.rotateRight(node);
380
- }
381
- if (this.isRed(node.left) && this.isRed(node.right)) {
382
- this.flipColors(node);
383
- }
384
- node.size = this.nodeSize(node.left) + this.nodeSize(node.right) + 1;
385
- if (this.aug) {
386
- this.aug.update(node);
387
- }
388
- return node;
389
- }
390
- mapRange(action, accum, start, end) {
391
- this.nodeMap(this.root, action, accum, start, end);
392
- }
393
- map(action, accum) {
394
- // TODO: optimize to avoid comparisons
395
- this.nodeMap(this.root, action, accum);
396
- }
397
- keys() {
398
- const keyList = [];
399
- const actions = {
400
- showStructure: true,
401
- infix: (node) => {
402
- keyList.push(node.key);
403
- return true;
404
- },
405
- };
406
- this.walk(actions);
407
- return keyList;
408
- }
409
- /**
410
- * Depth-first traversal with custom action; if action returns
411
- * false, traversal is halted.
412
- * @param action - action to apply to each node
413
- */
414
- walk(actions) {
415
- this.nodeWalk(this.root, actions);
416
- }
417
- walkBackward(actions) {
418
- this.nodeWalkBackward(this.root, actions);
419
- }
420
- nodeWalk(node, actions) {
421
- let go = true;
422
- if (node) {
423
- if (actions.pre && (!!actions.showStructure || node.color === RBColor.BLACK)) {
424
- go = actions.pre(node);
425
- }
426
- if (node.left) {
427
- go = this.nodeWalk(node.left, actions);
428
- }
429
- if (go && actions.infix && (!!actions.showStructure || node.color === RBColor.BLACK)) {
430
- go = actions.infix(node);
431
- }
432
- if (go) {
433
- go = this.nodeWalk(node.right, actions);
434
- }
435
- if (go && actions.post && (!!actions.showStructure || node.color === RBColor.BLACK)) {
436
- go = actions.post(node);
437
- }
438
- }
439
- return go;
440
- }
441
- nodeWalkBackward(node, actions) {
442
- let go = true;
443
- if (node) {
444
- if (actions.pre && (!!actions.showStructure || node.color === RBColor.BLACK)) {
445
- go = actions.pre(node);
446
- }
447
- if (node.right) {
448
- go = this.nodeWalkBackward(node.right, actions);
449
- }
450
- if (go && actions.infix && (!!actions.showStructure || node.color === RBColor.BLACK)) {
451
- go = actions.infix(node);
452
- }
453
- if (go) {
454
- go = this.nodeWalkBackward(node.left, actions);
455
- }
456
- if (go && actions.post && (!!actions.showStructure || node.color === RBColor.BLACK)) {
457
- go = actions.post(node);
458
- }
459
- }
460
- return go;
461
- }
462
- nodeMap(node, action, accum, start, end) {
463
- let _start = start;
464
- let _end = end;
465
- if (!node) {
466
- return true;
467
- }
468
- // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing -- using ??= could change behavior if value is falsy
469
- if (_start === undefined) {
470
- _start = this.nodeMin(node).key;
471
- }
472
- // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing -- using ??= could change behavior if value is falsy
473
- if (_end === undefined) {
474
- _end = this.nodeMax(node).key;
475
- }
476
- const cmpStart = this.compareKeys(_start, node.key);
477
- const cmpEnd = this.compareKeys(_end, node.key);
478
- let go = true;
479
- if (cmpStart < 0) {
480
- go = this.nodeMap(node.left, action, accum, _start, _end);
481
- }
482
- if (go && cmpStart <= 0 && cmpEnd >= 0) {
483
- // REVIEW: test for black node here
484
- go = action(node, accum);
485
- }
486
- if (go && cmpEnd > 0) {
487
- go = this.nodeMap(node.right, action, accum, _start, _end);
488
- }
489
- return go;
490
- }
491
- }
492
- //# sourceMappingURL=rbTree.js.map