serializable-bptree 1.0.1 → 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/README.md +1 -1
- package/dist/cjs/index.js +37 -30
- package/dist/esm/index.js +37 -30
- package/dist/typings/BPTree.d.ts +14 -6
- package/package.json +1 -1
package/README.md
CHANGED
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);
|
|
@@ -494,7 +498,8 @@ var BPTree = class {
|
|
|
494
498
|
*/
|
|
495
499
|
delete(key, value) {
|
|
496
500
|
const node = this._insertableNode(value);
|
|
497
|
-
|
|
501
|
+
let i = node.values.length;
|
|
502
|
+
while (i--) {
|
|
498
503
|
const nValue = node.values[i];
|
|
499
504
|
if (this.comparator.isSame(value, nValue)) {
|
|
500
505
|
const keys = node.keys[i];
|
|
@@ -541,7 +546,7 @@ var BPTree = class {
|
|
|
541
546
|
}
|
|
542
547
|
if (this.root === node && node.keys.length === 1) {
|
|
543
548
|
const keys = node.keys;
|
|
544
|
-
this.root = keys[0];
|
|
549
|
+
this.root = this.getNode(keys[0]);
|
|
545
550
|
this.root.parent = 0;
|
|
546
551
|
this._setHeadUpdate(this._headState);
|
|
547
552
|
this._setUpdates(this.root);
|
|
@@ -557,13 +562,13 @@ var BPTree = class {
|
|
|
557
562
|
let postK = null;
|
|
558
563
|
for (let i = 0, len = parentNode.keys.length; i < len; i++) {
|
|
559
564
|
const nKey = parentNode.keys[i];
|
|
560
|
-
if (nKey === node) {
|
|
565
|
+
if (nKey === node.id) {
|
|
561
566
|
if (i > 0) {
|
|
562
|
-
prevNode = parentNode.keys[i - 1];
|
|
567
|
+
prevNode = this.getNode(parentNode.keys[i - 1]);
|
|
563
568
|
prevK = parentNode.values[i - 1];
|
|
564
569
|
}
|
|
565
570
|
if (i < parentNode.keys.length - 1) {
|
|
566
|
-
nextNode = parentNode.keys[i + 1];
|
|
571
|
+
nextNode = this.getNode(parentNode.keys[i + 1]);
|
|
567
572
|
postK = parentNode.values[i];
|
|
568
573
|
}
|
|
569
574
|
}
|
|
@@ -603,10 +608,12 @@ var BPTree = class {
|
|
|
603
608
|
if (!pointer.leaf) {
|
|
604
609
|
const keys = pointer.keys;
|
|
605
610
|
for (const key2 of keys) {
|
|
606
|
-
|
|
611
|
+
const node2 = this.getNode(key2);
|
|
612
|
+
node2.parent = pointer.id;
|
|
613
|
+
this._setUpdates(node2);
|
|
607
614
|
}
|
|
608
615
|
}
|
|
609
|
-
this._deleteEntry(this.getNode(node.parent), node, guess);
|
|
616
|
+
this._deleteEntry(this.getNode(node.parent), node.id, guess);
|
|
610
617
|
this._setUpdates(pointer);
|
|
611
618
|
} else {
|
|
612
619
|
if (isPredecessor) {
|
|
@@ -679,24 +686,24 @@ var BPTree = class {
|
|
|
679
686
|
this._setUpdates(pointer);
|
|
680
687
|
}
|
|
681
688
|
if (!pointer.leaf) {
|
|
682
|
-
const
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
this._setUpdates(
|
|
689
|
+
for (const key2 of pointer.keys) {
|
|
690
|
+
const n = this.getNode(key2);
|
|
691
|
+
n.parent = pointer.id;
|
|
692
|
+
this._setUpdates(n);
|
|
686
693
|
}
|
|
687
694
|
}
|
|
688
695
|
if (!node.leaf) {
|
|
689
|
-
const
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
this._setUpdates(
|
|
696
|
+
for (const key2 of node.keys) {
|
|
697
|
+
const n = this.getNode(key2);
|
|
698
|
+
n.parent = node.id;
|
|
699
|
+
this._setUpdates(n);
|
|
693
700
|
}
|
|
694
701
|
}
|
|
695
702
|
if (!parentNode.leaf) {
|
|
696
|
-
const
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
this._setUpdates(
|
|
703
|
+
for (const key2 of parentNode.keys) {
|
|
704
|
+
const n = this.getNode(key2);
|
|
705
|
+
n.parent = parentNode.id;
|
|
706
|
+
this._setUpdates(n);
|
|
700
707
|
}
|
|
701
708
|
}
|
|
702
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);
|
|
@@ -463,7 +467,8 @@ var BPTree = class {
|
|
|
463
467
|
*/
|
|
464
468
|
delete(key, value) {
|
|
465
469
|
const node = this._insertableNode(value);
|
|
466
|
-
|
|
470
|
+
let i = node.values.length;
|
|
471
|
+
while (i--) {
|
|
467
472
|
const nValue = node.values[i];
|
|
468
473
|
if (this.comparator.isSame(value, nValue)) {
|
|
469
474
|
const keys = node.keys[i];
|
|
@@ -510,7 +515,7 @@ var BPTree = class {
|
|
|
510
515
|
}
|
|
511
516
|
if (this.root === node && node.keys.length === 1) {
|
|
512
517
|
const keys = node.keys;
|
|
513
|
-
this.root = keys[0];
|
|
518
|
+
this.root = this.getNode(keys[0]);
|
|
514
519
|
this.root.parent = 0;
|
|
515
520
|
this._setHeadUpdate(this._headState);
|
|
516
521
|
this._setUpdates(this.root);
|
|
@@ -526,13 +531,13 @@ var BPTree = class {
|
|
|
526
531
|
let postK = null;
|
|
527
532
|
for (let i = 0, len = parentNode.keys.length; i < len; i++) {
|
|
528
533
|
const nKey = parentNode.keys[i];
|
|
529
|
-
if (nKey === node) {
|
|
534
|
+
if (nKey === node.id) {
|
|
530
535
|
if (i > 0) {
|
|
531
|
-
prevNode = parentNode.keys[i - 1];
|
|
536
|
+
prevNode = this.getNode(parentNode.keys[i - 1]);
|
|
532
537
|
prevK = parentNode.values[i - 1];
|
|
533
538
|
}
|
|
534
539
|
if (i < parentNode.keys.length - 1) {
|
|
535
|
-
nextNode = parentNode.keys[i + 1];
|
|
540
|
+
nextNode = this.getNode(parentNode.keys[i + 1]);
|
|
536
541
|
postK = parentNode.values[i];
|
|
537
542
|
}
|
|
538
543
|
}
|
|
@@ -572,10 +577,12 @@ var BPTree = class {
|
|
|
572
577
|
if (!pointer.leaf) {
|
|
573
578
|
const keys = pointer.keys;
|
|
574
579
|
for (const key2 of keys) {
|
|
575
|
-
|
|
580
|
+
const node2 = this.getNode(key2);
|
|
581
|
+
node2.parent = pointer.id;
|
|
582
|
+
this._setUpdates(node2);
|
|
576
583
|
}
|
|
577
584
|
}
|
|
578
|
-
this._deleteEntry(this.getNode(node.parent), node, guess);
|
|
585
|
+
this._deleteEntry(this.getNode(node.parent), node.id, guess);
|
|
579
586
|
this._setUpdates(pointer);
|
|
580
587
|
} else {
|
|
581
588
|
if (isPredecessor) {
|
|
@@ -648,24 +655,24 @@ var BPTree = class {
|
|
|
648
655
|
this._setUpdates(pointer);
|
|
649
656
|
}
|
|
650
657
|
if (!pointer.leaf) {
|
|
651
|
-
const
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
this._setUpdates(
|
|
658
|
+
for (const key2 of pointer.keys) {
|
|
659
|
+
const n = this.getNode(key2);
|
|
660
|
+
n.parent = pointer.id;
|
|
661
|
+
this._setUpdates(n);
|
|
655
662
|
}
|
|
656
663
|
}
|
|
657
664
|
if (!node.leaf) {
|
|
658
|
-
const
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
this._setUpdates(
|
|
665
|
+
for (const key2 of node.keys) {
|
|
666
|
+
const n = this.getNode(key2);
|
|
667
|
+
n.parent = node.id;
|
|
668
|
+
this._setUpdates(n);
|
|
662
669
|
}
|
|
663
670
|
}
|
|
664
671
|
if (!parentNode.leaf) {
|
|
665
|
-
const
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
this._setUpdates(
|
|
672
|
+
for (const key2 of parentNode.keys) {
|
|
673
|
+
const n = this.getNode(key2);
|
|
674
|
+
n.parent = parentNode.id;
|
|
675
|
+
this._setUpdates(n);
|
|
669
676
|
}
|
|
670
677
|
}
|
|
671
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.
|