heap-typed 1.51.8 → 1.52.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.
- package/dist/data-structures/base/index.d.ts +2 -1
- package/dist/data-structures/base/index.js +2 -1
- package/dist/data-structures/base/iterable-element-base.d.ts +171 -0
- package/dist/data-structures/base/iterable-element-base.js +225 -0
- package/dist/data-structures/base/{iterable-base.d.ts → iterable-entry-base.d.ts} +4 -147
- package/dist/data-structures/base/{iterable-base.js → iterable-entry-base.js} +12 -189
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +106 -68
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +119 -87
- package/dist/data-structures/binary-tree/avl-tree.d.ts +82 -62
- package/dist/data-structures/binary-tree/avl-tree.js +78 -59
- package/dist/data-structures/binary-tree/binary-tree.d.ts +318 -226
- package/dist/data-structures/binary-tree/binary-tree.js +475 -363
- package/dist/data-structures/binary-tree/bst.d.ts +192 -202
- package/dist/data-structures/binary-tree/bst.js +207 -249
- package/dist/data-structures/binary-tree/rb-tree.d.ts +73 -74
- package/dist/data-structures/binary-tree/rb-tree.js +107 -98
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +92 -75
- package/dist/data-structures/binary-tree/tree-multi-map.js +105 -93
- package/dist/data-structures/graph/abstract-graph.d.ts +10 -15
- package/dist/data-structures/graph/abstract-graph.js +10 -15
- package/dist/data-structures/hash/hash-map.d.ts +33 -40
- package/dist/data-structures/hash/hash-map.js +40 -55
- package/dist/data-structures/heap/heap.d.ts +43 -114
- package/dist/data-structures/heap/heap.js +59 -127
- package/dist/data-structures/heap/max-heap.d.ts +50 -4
- package/dist/data-structures/heap/max-heap.js +76 -10
- package/dist/data-structures/heap/min-heap.d.ts +51 -5
- package/dist/data-structures/heap/min-heap.js +68 -11
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +22 -28
- package/dist/data-structures/linked-list/doubly-linked-list.js +26 -28
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +22 -25
- package/dist/data-structures/linked-list/singly-linked-list.js +29 -26
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +50 -4
- package/dist/data-structures/priority-queue/max-priority-queue.js +79 -10
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +51 -5
- package/dist/data-structures/priority-queue/min-priority-queue.js +71 -11
- package/dist/data-structures/priority-queue/priority-queue.d.ts +50 -4
- package/dist/data-structures/priority-queue/priority-queue.js +70 -1
- package/dist/data-structures/queue/deque.d.ts +21 -20
- package/dist/data-structures/queue/deque.js +29 -23
- package/dist/data-structures/queue/queue.d.ts +8 -28
- package/dist/data-structures/queue/queue.js +15 -31
- package/dist/data-structures/stack/stack.d.ts +17 -22
- package/dist/data-structures/stack/stack.js +25 -24
- package/dist/data-structures/trie/trie.d.ts +19 -14
- package/dist/data-structures/trie/trie.js +27 -16
- package/dist/interfaces/binary-tree.d.ts +7 -7
- package/dist/types/common.d.ts +1 -2
- package/dist/types/data-structures/base/base.d.ts +5 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -4
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +3 -4
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +5 -5
- package/dist/types/data-structures/binary-tree/bst.d.ts +4 -5
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -4
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -4
- package/dist/types/data-structures/heap/heap.d.ts +3 -2
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +2 -1
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/types/data-structures/queue/deque.d.ts +3 -2
- package/dist/types/data-structures/queue/queue.d.ts +2 -1
- package/dist/types/data-structures/stack/stack.d.ts +2 -1
- package/dist/types/data-structures/trie/trie.d.ts +3 -2
- package/dist/utils/utils.js +3 -5
- package/package.json +2 -2
- package/src/data-structures/base/index.ts +2 -1
- package/src/data-structures/base/iterable-element-base.ts +250 -0
- package/src/data-structures/base/{iterable-base.ts → iterable-entry-base.ts} +22 -213
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +144 -95
- package/src/data-structures/binary-tree/avl-tree.ts +96 -69
- package/src/data-structures/binary-tree/binary-tree.ts +535 -403
- package/src/data-structures/binary-tree/bst.ts +247 -277
- package/src/data-structures/binary-tree/rb-tree.ts +123 -103
- package/src/data-structures/binary-tree/tree-multi-map.ts +127 -102
- package/src/data-structures/graph/abstract-graph.ts +10 -10
- package/src/data-structures/hash/hash-map.ts +46 -53
- package/src/data-structures/heap/heap.ts +71 -152
- package/src/data-structures/heap/max-heap.ts +88 -13
- package/src/data-structures/heap/min-heap.ts +78 -15
- package/src/data-structures/linked-list/doubly-linked-list.ts +32 -32
- package/src/data-structures/linked-list/singly-linked-list.ts +37 -29
- package/src/data-structures/priority-queue/max-priority-queue.ts +94 -13
- package/src/data-structures/priority-queue/min-priority-queue.ts +84 -15
- package/src/data-structures/priority-queue/priority-queue.ts +81 -4
- package/src/data-structures/queue/deque.ts +37 -26
- package/src/data-structures/queue/queue.ts +23 -36
- package/src/data-structures/stack/stack.ts +31 -26
- package/src/data-structures/trie/trie.ts +35 -20
- package/src/interfaces/binary-tree.ts +9 -9
- package/src/types/common.ts +1 -2
- package/src/types/data-structures/base/base.ts +14 -6
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +3 -4
- package/src/types/data-structures/binary-tree/avl-tree.ts +3 -4
- package/src/types/data-structures/binary-tree/binary-tree.ts +6 -6
- package/src/types/data-structures/binary-tree/bst.ts +4 -5
- package/src/types/data-structures/binary-tree/rb-tree.ts +3 -4
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +3 -4
- package/src/types/data-structures/heap/heap.ts +4 -1
- package/src/types/data-structures/linked-list/doubly-linked-list.ts +3 -1
- package/src/types/data-structures/linked-list/singly-linked-list.ts +3 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/types/data-structures/queue/deque.ts +3 -1
- package/src/types/data-structures/queue/queue.ts +3 -1
- package/src/types/data-structures/stack/stack.ts +3 -1
- package/src/types/data-structures/trie/trie.ts +3 -1
- package/src/utils/utils.ts +3 -3
|
@@ -16,20 +16,21 @@ import { IterableEntryBase } from '../base';
|
|
|
16
16
|
import { isWeakKey, rangeCheck } from '../../utils';
|
|
17
17
|
|
|
18
18
|
/**
|
|
19
|
-
* 1. Key-Value Pair Storage: HashMap stores key-value pairs. Each key
|
|
19
|
+
* 1. Key-Value Pair Storage: HashMap stores key-value pairs. Each key map to a value.
|
|
20
20
|
* 2. Fast Lookup: It's used when you need to quickly find, insert, or delete entries based on a key.
|
|
21
|
-
* 3. Unique Keys: Keys are unique.
|
|
21
|
+
* 3. Unique Keys: Keys are unique.
|
|
22
|
+
* If you try to insert another entry with the same key, the new one will replace the old entry.
|
|
22
23
|
* 4. Unordered Collection: HashMap does not guarantee the order of entries, and the order may change over time.
|
|
23
24
|
*/
|
|
24
25
|
export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K, V> {
|
|
25
26
|
/**
|
|
26
27
|
* The constructor function initializes a HashMap object with an optional initial collection and
|
|
27
28
|
* options.
|
|
28
|
-
* @param
|
|
29
|
+
* @param entryOrRawElements - The `entryOrRawElements` parameter is an iterable collection of elements of a type
|
|
29
30
|
* `T`. It is an optional parameter and its default value is an empty array `[]`.
|
|
30
31
|
* @param [options] - The `options` parameter is an optional object that can contain two properties:
|
|
31
32
|
*/
|
|
32
|
-
constructor(
|
|
33
|
+
constructor(entryOrRawElements: Iterable<R | [K, V]> = [], options?: HashMapOptions<K, V, R>) {
|
|
33
34
|
super();
|
|
34
35
|
if (options) {
|
|
35
36
|
const { hashFn, toEntryFn } = options;
|
|
@@ -40,8 +41,8 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
40
41
|
this._toEntryFn = toEntryFn;
|
|
41
42
|
}
|
|
42
43
|
}
|
|
43
|
-
if (
|
|
44
|
-
this.setMany(
|
|
44
|
+
if (entryOrRawElements) {
|
|
45
|
+
this.setMany(entryOrRawElements);
|
|
45
46
|
}
|
|
46
47
|
}
|
|
47
48
|
|
|
@@ -67,16 +68,7 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
67
68
|
return this._objMap;
|
|
68
69
|
}
|
|
69
70
|
|
|
70
|
-
protected _toEntryFn
|
|
71
|
-
if (this.isEntry(rawElement)) {
|
|
72
|
-
// TODO, For performance optimization, it may be necessary to only inspect the first element traversed.
|
|
73
|
-
return rawElement;
|
|
74
|
-
} else {
|
|
75
|
-
throw new Error(
|
|
76
|
-
"If the provided rawCollection does not adhere to the [key, value] type format, the toEntryFn in the constructor's options parameter needs to specified."
|
|
77
|
-
);
|
|
78
|
-
}
|
|
79
|
-
};
|
|
71
|
+
protected _toEntryFn?: (rawElement: R) => [K, V];
|
|
80
72
|
|
|
81
73
|
/**
|
|
82
74
|
* The function returns the value of the _toEntryFn property.
|
|
@@ -164,23 +156,24 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
164
156
|
/**
|
|
165
157
|
* The function `setMany` takes an iterable collection of objects, maps each object to a key-value
|
|
166
158
|
* pair using a mapping function, and sets each key-value pair in the current object.
|
|
167
|
-
* @param
|
|
159
|
+
* @param entryOrRawElements - The `entryOrRawElements` parameter is an iterable collection of elements of a type
|
|
168
160
|
* `T`.
|
|
169
161
|
* @returns The `setMany` function is returning an array of booleans.
|
|
170
162
|
*/
|
|
171
|
-
setMany(
|
|
163
|
+
setMany(entryOrRawElements: Iterable<R | [K, V]>): boolean[] {
|
|
172
164
|
const results: boolean[] = [];
|
|
173
|
-
for (const rawEle of
|
|
174
|
-
let key, value;
|
|
165
|
+
for (const rawEle of entryOrRawElements) {
|
|
166
|
+
let key: K | undefined, value: V | undefined;
|
|
175
167
|
if (this.isEntry(rawEle)) {
|
|
176
168
|
key = rawEle[0];
|
|
177
169
|
value = rawEle[1];
|
|
178
|
-
} else {
|
|
170
|
+
} else if (this.toEntryFn) {
|
|
179
171
|
const item = this.toEntryFn(rawEle);
|
|
180
172
|
key = item[0];
|
|
181
173
|
value = item[1];
|
|
182
174
|
}
|
|
183
|
-
|
|
175
|
+
|
|
176
|
+
if (key !== undefined && value !== undefined) results.push(this.set(key, value));
|
|
184
177
|
}
|
|
185
178
|
return results;
|
|
186
179
|
}
|
|
@@ -277,8 +270,8 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
277
270
|
* @returns The `map` method is returning a new `HashMap` object with the transformed values based on
|
|
278
271
|
* the provided callback function.
|
|
279
272
|
*/
|
|
280
|
-
map<
|
|
281
|
-
const resultMap = new HashMap<K,
|
|
273
|
+
map<VM>(callbackfn: EntryCallback<K, V, VM>, thisArg?: any): HashMap<K, VM> {
|
|
274
|
+
const resultMap = new HashMap<K, VM>();
|
|
282
275
|
let index = 0;
|
|
283
276
|
for (const [key, value] of this) {
|
|
284
277
|
resultMap.set(key, callbackfn.call(thisArg, value, key, index++, this));
|
|
@@ -383,14 +376,14 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
|
|
|
383
376
|
|
|
384
377
|
/**
|
|
385
378
|
* The constructor initializes a LinkedHashMap object with an optional raw collection and options.
|
|
386
|
-
* @param
|
|
379
|
+
* @param entryOrRawElements - The `entryOrRawElements` parameter is an iterable collection of elements. It is
|
|
387
380
|
* used to initialize the HashMapLinked instance with key-value pairs. Each element in the
|
|
388
|
-
* `
|
|
381
|
+
* `entryOrRawElements` is converted to a key-value pair using the `toEntryFn` function (if provided) and
|
|
389
382
|
* then added to the HashMap
|
|
390
383
|
* @param [options] - The `options` parameter is an optional object that can contain the following
|
|
391
384
|
* properties:
|
|
392
385
|
*/
|
|
393
|
-
constructor(
|
|
386
|
+
constructor(entryOrRawElements: Iterable<R> = [], options?: LinkedHashMapOptions<K, V, R>) {
|
|
394
387
|
super();
|
|
395
388
|
this._sentinel = <HashMapLinkedNode<K, V>>{};
|
|
396
389
|
this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
|
|
@@ -405,8 +398,8 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
|
|
|
405
398
|
}
|
|
406
399
|
}
|
|
407
400
|
|
|
408
|
-
if (
|
|
409
|
-
for (const el of
|
|
401
|
+
if (entryOrRawElements) {
|
|
402
|
+
for (const el of entryOrRawElements) {
|
|
410
403
|
const [key, value] = this.toEntryFn(el);
|
|
411
404
|
this.set(key, value);
|
|
412
405
|
}
|
|
@@ -460,7 +453,7 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
|
|
|
460
453
|
/**
|
|
461
454
|
* The function returns the head node of a HashMapLinkedNode.
|
|
462
455
|
* @returns The method `getHead()` is returning a `HashMapLinkedNode` object with key type `K` and
|
|
463
|
-
* value type `V | undefined`.
|
|
456
|
+
* a value type `V | undefined`.
|
|
464
457
|
*/
|
|
465
458
|
get head(): HashMapLinkedNode<K, V | undefined> {
|
|
466
459
|
return this._head;
|
|
@@ -482,7 +475,7 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
|
|
|
482
475
|
return rawElement;
|
|
483
476
|
} else {
|
|
484
477
|
throw new Error(
|
|
485
|
-
"If the provided
|
|
478
|
+
"If the provided entryOrRawElements does not adhere to the [key, value] type format, the toEntryFn in the constructor's options parameter needs to specified."
|
|
486
479
|
);
|
|
487
480
|
}
|
|
488
481
|
};
|
|
@@ -590,7 +583,7 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
|
|
|
590
583
|
node = this.objMap.get(hash);
|
|
591
584
|
|
|
592
585
|
if (!node && isNewKey) {
|
|
593
|
-
// Create new node
|
|
586
|
+
// Create a new node
|
|
594
587
|
node = { key: <K>hash, value, prev: this.tail, next: this._sentinel };
|
|
595
588
|
this.objMap.set(hash, node);
|
|
596
589
|
} else if (node) {
|
|
@@ -630,13 +623,13 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
|
|
|
630
623
|
* The function `setMany` takes an iterable collection, converts each element into a key-value pair
|
|
631
624
|
* using a provided function, and sets each key-value pair in the current object, returning an array
|
|
632
625
|
* of booleans indicating the success of each set operation.
|
|
633
|
-
* @param
|
|
626
|
+
* @param entryOrRawElements - The entryOrRawElements parameter is an iterable collection of elements of type
|
|
634
627
|
* R.
|
|
635
628
|
* @returns The `setMany` function returns an array of booleans.
|
|
636
629
|
*/
|
|
637
|
-
setMany(
|
|
630
|
+
setMany(entryOrRawElements: Iterable<R>): boolean[] {
|
|
638
631
|
const results: boolean[] = [];
|
|
639
|
-
for (const rawEle of
|
|
632
|
+
for (const rawEle of entryOrRawElements) {
|
|
640
633
|
const [key, value] = this.toEntryFn(rawEle);
|
|
641
634
|
results.push(this.set(key, value));
|
|
642
635
|
}
|
|
@@ -692,9 +685,9 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
|
|
|
692
685
|
/**
|
|
693
686
|
* Time Complexity: O(n)
|
|
694
687
|
* Space Complexity: O(1)
|
|
695
|
-
|
|
688
|
+
*/
|
|
696
689
|
|
|
697
|
-
|
|
690
|
+
/**
|
|
698
691
|
* Time Complexity: O(n)
|
|
699
692
|
* Space Complexity: O(1)
|
|
700
693
|
*
|
|
@@ -717,9 +710,9 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
|
|
|
717
710
|
/**
|
|
718
711
|
* Time Complexity: O(1)
|
|
719
712
|
* Space Complexity: O(1)
|
|
720
|
-
|
|
713
|
+
*/
|
|
721
714
|
|
|
722
|
-
|
|
715
|
+
/**
|
|
723
716
|
* Time Complexity: O(1)
|
|
724
717
|
* Space Complexity: O(1)
|
|
725
718
|
*
|
|
@@ -764,9 +757,9 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
|
|
|
764
757
|
/**
|
|
765
758
|
* Time Complexity: O(n)
|
|
766
759
|
* Space Complexity: O(1)
|
|
767
|
-
|
|
760
|
+
*/
|
|
768
761
|
|
|
769
|
-
|
|
762
|
+
/**
|
|
770
763
|
* Time Complexity: O(n)
|
|
771
764
|
* Space Complexity: O(1)
|
|
772
765
|
*
|
|
@@ -787,9 +780,9 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
|
|
|
787
780
|
/**
|
|
788
781
|
* Time Complexity: O(1)
|
|
789
782
|
* Space Complexity: O(1)
|
|
790
|
-
|
|
783
|
+
*/
|
|
791
784
|
|
|
792
|
-
|
|
785
|
+
/**
|
|
793
786
|
* Time Complexity: O(1)
|
|
794
787
|
* Space Complexity: O(1)
|
|
795
788
|
*
|
|
@@ -814,9 +807,9 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
|
|
|
814
807
|
/**
|
|
815
808
|
* Time Complexity: O(1)
|
|
816
809
|
* Space Complexity: O(1)
|
|
817
|
-
|
|
810
|
+
*/
|
|
818
811
|
|
|
819
|
-
|
|
812
|
+
/**
|
|
820
813
|
* Time Complexity: O(1)
|
|
821
814
|
* Space Complexity: O(1)
|
|
822
815
|
*
|
|
@@ -854,9 +847,9 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
|
|
|
854
847
|
/**
|
|
855
848
|
* Time Complexity: O(n)
|
|
856
849
|
* Space Complexity: O(n)
|
|
857
|
-
|
|
850
|
+
*/
|
|
858
851
|
|
|
859
|
-
|
|
852
|
+
/**
|
|
860
853
|
* Time Complexity: O(n)
|
|
861
854
|
* Space Complexity: O(n)
|
|
862
855
|
*
|
|
@@ -886,9 +879,9 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
|
|
|
886
879
|
/**
|
|
887
880
|
* Time Complexity: O(n)
|
|
888
881
|
* Space Complexity: O(n)
|
|
889
|
-
|
|
882
|
+
*/
|
|
890
883
|
|
|
891
|
-
|
|
884
|
+
/**
|
|
892
885
|
* Time Complexity: O(n)
|
|
893
886
|
* Space Complexity: O(n)
|
|
894
887
|
*
|
|
@@ -905,8 +898,8 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
|
|
|
905
898
|
* @returns a new `LinkedHashMap` object with the values mapped according to the provided callback
|
|
906
899
|
* function.
|
|
907
900
|
*/
|
|
908
|
-
map<
|
|
909
|
-
const mappedMap = new LinkedHashMap<K,
|
|
901
|
+
map<VM>(callback: EntryCallback<K, V, VM>, thisArg?: any): LinkedHashMap<K, VM> {
|
|
902
|
+
const mappedMap = new LinkedHashMap<K, VM>();
|
|
910
903
|
let index = 0;
|
|
911
904
|
for (const [key, value] of this) {
|
|
912
905
|
const newValue = callback.call(thisArg, value, key, index, this);
|
|
@@ -940,9 +933,9 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
|
|
|
940
933
|
* Time Complexity: O(n)
|
|
941
934
|
* Space Complexity: O(1)
|
|
942
935
|
* where n is the number of entries in the LinkedHashMap.
|
|
943
|
-
|
|
936
|
+
*/
|
|
944
937
|
|
|
945
|
-
|
|
938
|
+
/**
|
|
946
939
|
* Time Complexity: O(n)
|
|
947
940
|
* Space Complexity: O(1)
|
|
948
941
|
* where n is the number of entries in the LinkedHashMap.
|