serializable-bptree 1.0.2 → 1.0.3
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.
- package/dist/cjs/index.js +35 -29
- package/dist/esm/index.js +35 -29
- package/dist/typings/BPTree.d.ts +14 -6
- package/package.json +1 -1
package/dist/cjs/index.js
CHANGED
|
@@ -189,7 +189,8 @@ var BPTree = class {
|
|
|
189
189
|
leftestNode() {
|
|
190
190
|
let node = this.root;
|
|
191
191
|
while (!node.leaf) {
|
|
192
|
-
|
|
192
|
+
const keys = node.keys;
|
|
193
|
+
node = this.getNode(keys[0]);
|
|
193
194
|
}
|
|
194
195
|
return node;
|
|
195
196
|
}
|
|
@@ -198,14 +199,15 @@ var BPTree = class {
|
|
|
198
199
|
while (!node.leaf) {
|
|
199
200
|
for (let i = 0, len = node.values.length; i < len; i++) {
|
|
200
201
|
const nValue = node.values[i];
|
|
202
|
+
const k = node.keys;
|
|
201
203
|
if (this.comparator.isSame(value, nValue)) {
|
|
202
|
-
node =
|
|
204
|
+
node = this.getNode(k[i + 1]);
|
|
203
205
|
break;
|
|
204
206
|
} else if (this.comparator.isLower(value, nValue)) {
|
|
205
|
-
node =
|
|
207
|
+
node = this.getNode(k[i]);
|
|
206
208
|
break;
|
|
207
209
|
} else if (i + 1 === node.values.length) {
|
|
208
|
-
node =
|
|
210
|
+
node = this.getNode(k[i + 1]);
|
|
209
211
|
break;
|
|
210
212
|
}
|
|
211
213
|
}
|
|
@@ -257,7 +259,7 @@ var BPTree = class {
|
|
|
257
259
|
}
|
|
258
260
|
_insertInParent(node, value, pointer) {
|
|
259
261
|
if (this.root === node) {
|
|
260
|
-
const root = this._createNode([node, pointer], [value]);
|
|
262
|
+
const root = this._createNode([node.id, pointer.id], [value]);
|
|
261
263
|
this.root = root;
|
|
262
264
|
node.parent = root.id;
|
|
263
265
|
pointer.parent = root.id;
|
|
@@ -270,9 +272,9 @@ var BPTree = class {
|
|
|
270
272
|
const parentNode = this.getNode(node.parent);
|
|
271
273
|
for (let i = 0, len = parentNode.keys.length; i < len; i++) {
|
|
272
274
|
const nKeys = parentNode.keys[i];
|
|
273
|
-
if (nKeys === node) {
|
|
275
|
+
if (nKeys === node.id) {
|
|
274
276
|
parentNode.values.splice(i, 0, value);
|
|
275
|
-
parentNode.keys.splice(i + 1, 0, pointer);
|
|
277
|
+
parentNode.keys.splice(i + 1, 0, pointer.id);
|
|
276
278
|
this._setUpdates(parentNode);
|
|
277
279
|
if (parentNode.keys.length > this.order) {
|
|
278
280
|
const parentPointer = this._createNode([], []);
|
|
@@ -288,12 +290,14 @@ var BPTree = class {
|
|
|
288
290
|
}
|
|
289
291
|
parentNode.keys = parentNode.keys.slice(0, mid + 1);
|
|
290
292
|
for (const k of parentNode.keys) {
|
|
291
|
-
const
|
|
292
|
-
|
|
293
|
+
const node2 = this.getNode(k);
|
|
294
|
+
node2.parent = parentNode.id;
|
|
295
|
+
this._setUpdates(node2);
|
|
293
296
|
}
|
|
294
297
|
for (const k of parentPointer.keys) {
|
|
295
|
-
const
|
|
296
|
-
|
|
298
|
+
const node2 = this.getNode(k);
|
|
299
|
+
node2.parent = parentPointer.id;
|
|
300
|
+
this._setUpdates(node2);
|
|
297
301
|
}
|
|
298
302
|
this._insertInParent(parentNode, midValue, parentPointer);
|
|
299
303
|
this._setCreates(parentPointer);
|
|
@@ -542,7 +546,7 @@ var BPTree = class {
|
|
|
542
546
|
}
|
|
543
547
|
if (this.root === node && node.keys.length === 1) {
|
|
544
548
|
const keys = node.keys;
|
|
545
|
-
this.root = keys[0];
|
|
549
|
+
this.root = this.getNode(keys[0]);
|
|
546
550
|
this.root.parent = 0;
|
|
547
551
|
this._setHeadUpdate(this._headState);
|
|
548
552
|
this._setUpdates(this.root);
|
|
@@ -558,13 +562,13 @@ var BPTree = class {
|
|
|
558
562
|
let postK = null;
|
|
559
563
|
for (let i = 0, len = parentNode.keys.length; i < len; i++) {
|
|
560
564
|
const nKey = parentNode.keys[i];
|
|
561
|
-
if (nKey === node) {
|
|
565
|
+
if (nKey === node.id) {
|
|
562
566
|
if (i > 0) {
|
|
563
|
-
prevNode = parentNode.keys[i - 1];
|
|
567
|
+
prevNode = this.getNode(parentNode.keys[i - 1]);
|
|
564
568
|
prevK = parentNode.values[i - 1];
|
|
565
569
|
}
|
|
566
570
|
if (i < parentNode.keys.length - 1) {
|
|
567
|
-
nextNode = parentNode.keys[i + 1];
|
|
571
|
+
nextNode = this.getNode(parentNode.keys[i + 1]);
|
|
568
572
|
postK = parentNode.values[i];
|
|
569
573
|
}
|
|
570
574
|
}
|
|
@@ -604,10 +608,12 @@ var BPTree = class {
|
|
|
604
608
|
if (!pointer.leaf) {
|
|
605
609
|
const keys = pointer.keys;
|
|
606
610
|
for (const key2 of keys) {
|
|
607
|
-
|
|
611
|
+
const node2 = this.getNode(key2);
|
|
612
|
+
node2.parent = pointer.id;
|
|
613
|
+
this._setUpdates(node2);
|
|
608
614
|
}
|
|
609
615
|
}
|
|
610
|
-
this._deleteEntry(this.getNode(node.parent), node, guess);
|
|
616
|
+
this._deleteEntry(this.getNode(node.parent), node.id, guess);
|
|
611
617
|
this._setUpdates(pointer);
|
|
612
618
|
} else {
|
|
613
619
|
if (isPredecessor) {
|
|
@@ -680,24 +686,24 @@ var BPTree = class {
|
|
|
680
686
|
this._setUpdates(pointer);
|
|
681
687
|
}
|
|
682
688
|
if (!pointer.leaf) {
|
|
683
|
-
const
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
this._setUpdates(
|
|
689
|
+
for (const key2 of pointer.keys) {
|
|
690
|
+
const n = this.getNode(key2);
|
|
691
|
+
n.parent = pointer.id;
|
|
692
|
+
this._setUpdates(n);
|
|
687
693
|
}
|
|
688
694
|
}
|
|
689
695
|
if (!node.leaf) {
|
|
690
|
-
const
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
this._setUpdates(
|
|
696
|
+
for (const key2 of node.keys) {
|
|
697
|
+
const n = this.getNode(key2);
|
|
698
|
+
n.parent = node.id;
|
|
699
|
+
this._setUpdates(n);
|
|
694
700
|
}
|
|
695
701
|
}
|
|
696
702
|
if (!parentNode.leaf) {
|
|
697
|
-
const
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
this._setUpdates(
|
|
703
|
+
for (const key2 of parentNode.keys) {
|
|
704
|
+
const n = this.getNode(key2);
|
|
705
|
+
n.parent = parentNode.id;
|
|
706
|
+
this._setUpdates(n);
|
|
701
707
|
}
|
|
702
708
|
}
|
|
703
709
|
}
|
package/dist/esm/index.js
CHANGED
|
@@ -158,7 +158,8 @@ var BPTree = class {
|
|
|
158
158
|
leftestNode() {
|
|
159
159
|
let node = this.root;
|
|
160
160
|
while (!node.leaf) {
|
|
161
|
-
|
|
161
|
+
const keys = node.keys;
|
|
162
|
+
node = this.getNode(keys[0]);
|
|
162
163
|
}
|
|
163
164
|
return node;
|
|
164
165
|
}
|
|
@@ -167,14 +168,15 @@ var BPTree = class {
|
|
|
167
168
|
while (!node.leaf) {
|
|
168
169
|
for (let i = 0, len = node.values.length; i < len; i++) {
|
|
169
170
|
const nValue = node.values[i];
|
|
171
|
+
const k = node.keys;
|
|
170
172
|
if (this.comparator.isSame(value, nValue)) {
|
|
171
|
-
node =
|
|
173
|
+
node = this.getNode(k[i + 1]);
|
|
172
174
|
break;
|
|
173
175
|
} else if (this.comparator.isLower(value, nValue)) {
|
|
174
|
-
node =
|
|
176
|
+
node = this.getNode(k[i]);
|
|
175
177
|
break;
|
|
176
178
|
} else if (i + 1 === node.values.length) {
|
|
177
|
-
node =
|
|
179
|
+
node = this.getNode(k[i + 1]);
|
|
178
180
|
break;
|
|
179
181
|
}
|
|
180
182
|
}
|
|
@@ -226,7 +228,7 @@ var BPTree = class {
|
|
|
226
228
|
}
|
|
227
229
|
_insertInParent(node, value, pointer) {
|
|
228
230
|
if (this.root === node) {
|
|
229
|
-
const root = this._createNode([node, pointer], [value]);
|
|
231
|
+
const root = this._createNode([node.id, pointer.id], [value]);
|
|
230
232
|
this.root = root;
|
|
231
233
|
node.parent = root.id;
|
|
232
234
|
pointer.parent = root.id;
|
|
@@ -239,9 +241,9 @@ var BPTree = class {
|
|
|
239
241
|
const parentNode = this.getNode(node.parent);
|
|
240
242
|
for (let i = 0, len = parentNode.keys.length; i < len; i++) {
|
|
241
243
|
const nKeys = parentNode.keys[i];
|
|
242
|
-
if (nKeys === node) {
|
|
244
|
+
if (nKeys === node.id) {
|
|
243
245
|
parentNode.values.splice(i, 0, value);
|
|
244
|
-
parentNode.keys.splice(i + 1, 0, pointer);
|
|
246
|
+
parentNode.keys.splice(i + 1, 0, pointer.id);
|
|
245
247
|
this._setUpdates(parentNode);
|
|
246
248
|
if (parentNode.keys.length > this.order) {
|
|
247
249
|
const parentPointer = this._createNode([], []);
|
|
@@ -257,12 +259,14 @@ var BPTree = class {
|
|
|
257
259
|
}
|
|
258
260
|
parentNode.keys = parentNode.keys.slice(0, mid + 1);
|
|
259
261
|
for (const k of parentNode.keys) {
|
|
260
|
-
const
|
|
261
|
-
|
|
262
|
+
const node2 = this.getNode(k);
|
|
263
|
+
node2.parent = parentNode.id;
|
|
264
|
+
this._setUpdates(node2);
|
|
262
265
|
}
|
|
263
266
|
for (const k of parentPointer.keys) {
|
|
264
|
-
const
|
|
265
|
-
|
|
267
|
+
const node2 = this.getNode(k);
|
|
268
|
+
node2.parent = parentPointer.id;
|
|
269
|
+
this._setUpdates(node2);
|
|
266
270
|
}
|
|
267
271
|
this._insertInParent(parentNode, midValue, parentPointer);
|
|
268
272
|
this._setCreates(parentPointer);
|
|
@@ -511,7 +515,7 @@ var BPTree = class {
|
|
|
511
515
|
}
|
|
512
516
|
if (this.root === node && node.keys.length === 1) {
|
|
513
517
|
const keys = node.keys;
|
|
514
|
-
this.root = keys[0];
|
|
518
|
+
this.root = this.getNode(keys[0]);
|
|
515
519
|
this.root.parent = 0;
|
|
516
520
|
this._setHeadUpdate(this._headState);
|
|
517
521
|
this._setUpdates(this.root);
|
|
@@ -527,13 +531,13 @@ var BPTree = class {
|
|
|
527
531
|
let postK = null;
|
|
528
532
|
for (let i = 0, len = parentNode.keys.length; i < len; i++) {
|
|
529
533
|
const nKey = parentNode.keys[i];
|
|
530
|
-
if (nKey === node) {
|
|
534
|
+
if (nKey === node.id) {
|
|
531
535
|
if (i > 0) {
|
|
532
|
-
prevNode = parentNode.keys[i - 1];
|
|
536
|
+
prevNode = this.getNode(parentNode.keys[i - 1]);
|
|
533
537
|
prevK = parentNode.values[i - 1];
|
|
534
538
|
}
|
|
535
539
|
if (i < parentNode.keys.length - 1) {
|
|
536
|
-
nextNode = parentNode.keys[i + 1];
|
|
540
|
+
nextNode = this.getNode(parentNode.keys[i + 1]);
|
|
537
541
|
postK = parentNode.values[i];
|
|
538
542
|
}
|
|
539
543
|
}
|
|
@@ -573,10 +577,12 @@ var BPTree = class {
|
|
|
573
577
|
if (!pointer.leaf) {
|
|
574
578
|
const keys = pointer.keys;
|
|
575
579
|
for (const key2 of keys) {
|
|
576
|
-
|
|
580
|
+
const node2 = this.getNode(key2);
|
|
581
|
+
node2.parent = pointer.id;
|
|
582
|
+
this._setUpdates(node2);
|
|
577
583
|
}
|
|
578
584
|
}
|
|
579
|
-
this._deleteEntry(this.getNode(node.parent), node, guess);
|
|
585
|
+
this._deleteEntry(this.getNode(node.parent), node.id, guess);
|
|
580
586
|
this._setUpdates(pointer);
|
|
581
587
|
} else {
|
|
582
588
|
if (isPredecessor) {
|
|
@@ -649,24 +655,24 @@ var BPTree = class {
|
|
|
649
655
|
this._setUpdates(pointer);
|
|
650
656
|
}
|
|
651
657
|
if (!pointer.leaf) {
|
|
652
|
-
const
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
this._setUpdates(
|
|
658
|
+
for (const key2 of pointer.keys) {
|
|
659
|
+
const n = this.getNode(key2);
|
|
660
|
+
n.parent = pointer.id;
|
|
661
|
+
this._setUpdates(n);
|
|
656
662
|
}
|
|
657
663
|
}
|
|
658
664
|
if (!node.leaf) {
|
|
659
|
-
const
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
this._setUpdates(
|
|
665
|
+
for (const key2 of node.keys) {
|
|
666
|
+
const n = this.getNode(key2);
|
|
667
|
+
n.parent = node.id;
|
|
668
|
+
this._setUpdates(n);
|
|
663
669
|
}
|
|
664
670
|
}
|
|
665
671
|
if (!parentNode.leaf) {
|
|
666
|
-
const
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
this._setUpdates(
|
|
672
|
+
for (const key2 of parentNode.keys) {
|
|
673
|
+
const n = this.getNode(key2);
|
|
674
|
+
n.parent = parentNode.id;
|
|
675
|
+
this._setUpdates(n);
|
|
670
676
|
}
|
|
671
677
|
}
|
|
672
678
|
}
|
package/dist/typings/BPTree.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { BinarySearch } from './utils/BinarySearch';
|
|
2
2
|
import { ValueComparator } from './ValueComparator';
|
|
3
3
|
import { SerializeStrategy } from './SerializeStrategy';
|
|
4
|
-
type BPTreeNodeKey<K, V> = K[] | BPTreeNode<K, V>;
|
|
5
4
|
type BPTreeCondition<V> = {
|
|
6
5
|
gt?: V;
|
|
7
6
|
lt?: V;
|
|
@@ -14,21 +13,30 @@ type BPTreePair<K, V> = {
|
|
|
14
13
|
key: K;
|
|
15
14
|
value: V;
|
|
16
15
|
};
|
|
16
|
+
export type BPTreeUnknownNode<K, V> = BPTreeInternalNode<K, V> | BPTreeLeafNode<K, V>;
|
|
17
17
|
export interface BPTreeNode<K, V> {
|
|
18
18
|
id: number;
|
|
19
|
-
keys:
|
|
19
|
+
keys: number[] | K[][];
|
|
20
20
|
values: V[];
|
|
21
21
|
leaf: boolean;
|
|
22
22
|
parent: number;
|
|
23
23
|
next: number;
|
|
24
24
|
}
|
|
25
|
+
export interface BPTreeInternalNode<K, V> extends BPTreeNode<K, V> {
|
|
26
|
+
leaf: false;
|
|
27
|
+
keys: number[];
|
|
28
|
+
}
|
|
29
|
+
export interface BPTreeLeafNode<K, V> extends BPTreeNode<K, V> {
|
|
30
|
+
leaf: true;
|
|
31
|
+
keys: K[][];
|
|
32
|
+
}
|
|
25
33
|
export declare class BPTree<K, V> {
|
|
26
34
|
protected readonly strategy: SerializeStrategy<K, V>;
|
|
27
35
|
protected readonly comparator: ValueComparator<V>;
|
|
28
36
|
protected readonly search: BinarySearch<V>;
|
|
29
37
|
protected readonly order: number;
|
|
30
|
-
protected readonly nodes: Map<number,
|
|
31
|
-
protected root:
|
|
38
|
+
protected readonly nodes: Map<number, BPTreeUnknownNode<K, V>>;
|
|
39
|
+
protected root: BPTreeUnknownNode<K, V>;
|
|
32
40
|
private readonly _creates;
|
|
33
41
|
private readonly _updates;
|
|
34
42
|
private _updatedHead;
|
|
@@ -46,8 +54,8 @@ export declare class BPTree<K, V> {
|
|
|
46
54
|
private _emitHeadUpdates;
|
|
47
55
|
private _emitCreates;
|
|
48
56
|
private _emitUpdates;
|
|
49
|
-
protected getNode(id: number):
|
|
50
|
-
protected leftestNode():
|
|
57
|
+
protected getNode(id: number): BPTreeUnknownNode<K, V>;
|
|
58
|
+
protected leftestNode(): BPTreeLeafNode<K, V>;
|
|
51
59
|
private _insertableNode;
|
|
52
60
|
/**
|
|
53
61
|
* It returns whether there is a value in the tree.
|