data-structure-typed 1.44.0 → 1.45.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/CHANGELOG.md +1 -1
- package/README.md +15 -15
- package/benchmark/report.html +30 -30
- package/benchmark/report.json +144 -198
- package/dist/cjs/data-structures/hash/hash-map.d.ts +230 -37
- package/dist/cjs/data-structures/hash/hash-map.js +430 -118
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/types/data-structures/graph/directed-graph.d.ts +0 -5
- package/dist/cjs/types/data-structures/graph/directed-graph.js +0 -7
- package/dist/cjs/types/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/types/data-structures/hash/hash-map.d.ts +15 -1
- package/dist/cjs/types/data-structures/hash/index.d.ts +6 -0
- package/dist/cjs/types/data-structures/hash/index.js +20 -0
- package/dist/cjs/types/data-structures/hash/index.js.map +1 -1
- package/dist/cjs/utils/utils.d.ts +3 -0
- package/dist/cjs/utils/utils.js +15 -1
- package/dist/cjs/utils/utils.js.map +1 -1
- package/dist/mjs/data-structures/hash/hash-map.d.ts +230 -37
- package/dist/mjs/data-structures/hash/hash-map.js +435 -123
- package/dist/mjs/types/data-structures/graph/directed-graph.d.ts +0 -5
- package/dist/mjs/types/data-structures/graph/directed-graph.js +1 -6
- package/dist/mjs/types/data-structures/hash/hash-map.d.ts +15 -1
- package/dist/mjs/types/data-structures/hash/index.d.ts +6 -0
- package/dist/mjs/types/data-structures/hash/index.js +6 -1
- package/dist/mjs/utils/utils.d.ts +3 -0
- package/dist/mjs/utils/utils.js +11 -0
- package/dist/umd/data-structure-typed.js +532 -217
- package/dist/umd/data-structure-typed.min.js +1 -1
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +9 -7
- package/src/data-structures/hash/hash-map.ts +432 -125
- package/src/types/data-structures/graph/directed-graph.ts +0 -6
- package/src/types/data-structures/hash/hash-map.ts +17 -1
- package/src/types/data-structures/hash/index.ts +7 -0
- package/src/utils/utils.ts +13 -0
- package/test/integration/all-in-one.ts +110 -0
- package/test/performance/reportor.ts +2 -2
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/bst.test.ts +6 -6
- package/test/unit/data-structures/binary-tree/overall.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +14 -14
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +2 -2
- package/test/unit/data-structures/graph/abstract-graph.test.ts +2 -1
- package/test/unit/data-structures/graph/undirected-graph.test.ts +1 -1
- package/test/unit/data-structures/hash/hash-map.test.ts +151 -20
- package/test/unit/data-structures/heap/heap.test.ts +8 -8
- package/test/unit/data-structures/heap/max-heap.test.ts +4 -4
- package/test/unit/data-structures/heap/min-heap.test.ts +4 -4
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +1 -1
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +2 -2
- package/test/unit/data-structures/linked-list/skip-list.test.ts +4 -4
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +2 -2
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +8 -2
- package/test/unit/data-structures/queue/deque.test.ts +25 -25
- package/test/unit/data-structures/trie/trie.test.ts +9 -9
- package/test/utils/array.ts +1 -1
- package/test/utils/number.ts +1 -1
|
@@ -113,9 +113,11 @@ var dataStructureTyped = (() => {
|
|
|
113
113
|
FibonacciHeap: () => FibonacciHeap,
|
|
114
114
|
FibonacciHeapNode: () => FibonacciHeapNode,
|
|
115
115
|
HashMap: () => HashMap,
|
|
116
|
+
HashMapIterator: () => HashMapIterator,
|
|
116
117
|
HashTable: () => HashTable,
|
|
117
118
|
HashTableNode: () => HashTableNode,
|
|
118
119
|
Heap: () => Heap,
|
|
120
|
+
IterateDirection: () => IterateDirection,
|
|
119
121
|
IterationType: () => IterationType,
|
|
120
122
|
LinkedListQueue: () => LinkedListQueue,
|
|
121
123
|
MapEdge: () => MapEdge,
|
|
@@ -142,7 +144,6 @@ var dataStructureTyped = (() => {
|
|
|
142
144
|
SkipListNode: () => SkipListNode,
|
|
143
145
|
Stack: () => Stack,
|
|
144
146
|
THUNK_SYMBOL: () => THUNK_SYMBOL,
|
|
145
|
-
TopologicalProperty: () => TopologicalProperty,
|
|
146
147
|
TreeMap: () => TreeMap,
|
|
147
148
|
TreeMultimap: () => TreeMultimap,
|
|
148
149
|
TreeMultimapNode: () => TreeMultimapNode,
|
|
@@ -156,7 +157,10 @@ var dataStructureTyped = (() => {
|
|
|
156
157
|
Vector2D: () => Vector2D,
|
|
157
158
|
arrayRemove: () => arrayRemove,
|
|
158
159
|
getMSB: () => getMSB,
|
|
160
|
+
isObjOrFunc: () => isObjOrFunc,
|
|
159
161
|
isThunk: () => isThunk,
|
|
162
|
+
rangeCheck: () => rangeCheck,
|
|
163
|
+
throwRangeError: () => throwRangeError,
|
|
160
164
|
toThunk: () => toThunk,
|
|
161
165
|
trampoline: () => trampoline,
|
|
162
166
|
trampolineAsync: () => trampolineAsync,
|
|
@@ -489,148 +493,561 @@ var dataStructureTyped = (() => {
|
|
|
489
493
|
var TreeSet = class {
|
|
490
494
|
};
|
|
491
495
|
|
|
496
|
+
// src/utils/utils.ts
|
|
497
|
+
var uuidV4 = function() {
|
|
498
|
+
return "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx".replace(/[x]/g, function(c) {
|
|
499
|
+
const r = Math.random() * 16 | 0, v = c == "x" ? r : r & 3 | 8;
|
|
500
|
+
return v.toString(16);
|
|
501
|
+
});
|
|
502
|
+
};
|
|
503
|
+
var arrayRemove = function(array, predicate) {
|
|
504
|
+
let i = -1, len = array ? array.length : 0;
|
|
505
|
+
const result = [];
|
|
506
|
+
while (++i < len) {
|
|
507
|
+
const value = array[i];
|
|
508
|
+
if (predicate(value, i, array)) {
|
|
509
|
+
result.push(value);
|
|
510
|
+
Array.prototype.splice.call(array, i--, 1);
|
|
511
|
+
len--;
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
return result;
|
|
515
|
+
};
|
|
516
|
+
var THUNK_SYMBOL = Symbol("thunk");
|
|
517
|
+
var isThunk = (fnOrValue) => {
|
|
518
|
+
return typeof fnOrValue === "function" && fnOrValue.__THUNK__ === THUNK_SYMBOL;
|
|
519
|
+
};
|
|
520
|
+
var toThunk = (fn) => {
|
|
521
|
+
const thunk = () => fn();
|
|
522
|
+
thunk.__THUNK__ = THUNK_SYMBOL;
|
|
523
|
+
return thunk;
|
|
524
|
+
};
|
|
525
|
+
var trampoline = (fn) => {
|
|
526
|
+
const cont = (...args) => toThunk(() => fn(...args));
|
|
527
|
+
return Object.assign(
|
|
528
|
+
(...args) => {
|
|
529
|
+
let result = fn(...args);
|
|
530
|
+
while (isThunk(result) && typeof result === "function") {
|
|
531
|
+
result = result();
|
|
532
|
+
}
|
|
533
|
+
return result;
|
|
534
|
+
},
|
|
535
|
+
{ cont }
|
|
536
|
+
);
|
|
537
|
+
};
|
|
538
|
+
var trampolineAsync = (fn) => {
|
|
539
|
+
const cont = (...args) => toThunk(() => fn(...args));
|
|
540
|
+
return Object.assign(
|
|
541
|
+
(...args) => __async(void 0, null, function* () {
|
|
542
|
+
let result = yield fn(...args);
|
|
543
|
+
while (isThunk(result) && typeof result === "function") {
|
|
544
|
+
result = yield result();
|
|
545
|
+
}
|
|
546
|
+
return result;
|
|
547
|
+
}),
|
|
548
|
+
{ cont }
|
|
549
|
+
);
|
|
550
|
+
};
|
|
551
|
+
var getMSB = (value) => {
|
|
552
|
+
if (value <= 0) {
|
|
553
|
+
return 0;
|
|
554
|
+
}
|
|
555
|
+
return 1 << 31 - Math.clz32(value);
|
|
556
|
+
};
|
|
557
|
+
var rangeCheck = (index, min, max, message = "Index out of bounds.") => {
|
|
558
|
+
if (index < min || index > max)
|
|
559
|
+
throw new RangeError(message);
|
|
560
|
+
};
|
|
561
|
+
var throwRangeError = (message = "The value is off-limits.") => {
|
|
562
|
+
throw new RangeError(message);
|
|
563
|
+
};
|
|
564
|
+
var isObjOrFunc = (input) => {
|
|
565
|
+
const inputType = typeof input;
|
|
566
|
+
return inputType === "object" && input !== null || inputType === "function";
|
|
567
|
+
};
|
|
568
|
+
|
|
569
|
+
// src/types/data-structures/binary-tree/binary-tree.ts
|
|
570
|
+
var IterationType = /* @__PURE__ */ ((IterationType3) => {
|
|
571
|
+
IterationType3["ITERATIVE"] = "ITERATIVE";
|
|
572
|
+
IterationType3["RECURSIVE"] = "RECURSIVE";
|
|
573
|
+
return IterationType3;
|
|
574
|
+
})(IterationType || {});
|
|
575
|
+
var FamilyPosition = /* @__PURE__ */ ((FamilyPosition2) => {
|
|
576
|
+
FamilyPosition2["ROOT"] = "ROOT";
|
|
577
|
+
FamilyPosition2["LEFT"] = "LEFT";
|
|
578
|
+
FamilyPosition2["RIGHT"] = "RIGHT";
|
|
579
|
+
FamilyPosition2["ROOT_LEFT"] = "ROOT_LEFT";
|
|
580
|
+
FamilyPosition2["ROOT_RIGHT"] = "ROOT_RIGHT";
|
|
581
|
+
FamilyPosition2["ISOLATED"] = "ISOLATED";
|
|
582
|
+
FamilyPosition2["MAL_NODE"] = "MAL_NODE";
|
|
583
|
+
return FamilyPosition2;
|
|
584
|
+
})(FamilyPosition || {});
|
|
585
|
+
|
|
586
|
+
// src/types/data-structures/binary-tree/rb-tree.ts
|
|
587
|
+
var RBTNColor = /* @__PURE__ */ ((RBTNColor2) => {
|
|
588
|
+
RBTNColor2[RBTNColor2["RED"] = 1] = "RED";
|
|
589
|
+
RBTNColor2[RBTNColor2["BLACK"] = 0] = "BLACK";
|
|
590
|
+
return RBTNColor2;
|
|
591
|
+
})(RBTNColor || {});
|
|
592
|
+
|
|
593
|
+
// src/types/data-structures/hash/hash-map.ts
|
|
594
|
+
var IterateDirection = /* @__PURE__ */ ((IterateDirection2) => {
|
|
595
|
+
IterateDirection2[IterateDirection2["DEFAULT"] = 0] = "DEFAULT";
|
|
596
|
+
IterateDirection2[IterateDirection2["REVERSE"] = 1] = "REVERSE";
|
|
597
|
+
return IterateDirection2;
|
|
598
|
+
})(IterateDirection || {});
|
|
599
|
+
|
|
600
|
+
// src/types/helpers.ts
|
|
601
|
+
var CP = /* @__PURE__ */ ((CP2) => {
|
|
602
|
+
CP2["lt"] = "lt";
|
|
603
|
+
CP2["eq"] = "eq";
|
|
604
|
+
CP2["gt"] = "gt";
|
|
605
|
+
return CP2;
|
|
606
|
+
})(CP || {});
|
|
607
|
+
|
|
492
608
|
// src/data-structures/hash/hash-map.ts
|
|
493
|
-
var
|
|
609
|
+
var HashMapIterator = class {
|
|
610
|
+
/**
|
|
611
|
+
* This is a constructor function for a linked list iterator in a HashMap data structure.
|
|
612
|
+
* @param node - The `node` parameter is a reference to a `HashMapLinkedNode` object. This object
|
|
613
|
+
* represents a node in a linked list used in a hash map data structure. It contains a key-value pair
|
|
614
|
+
* and references to the previous and next nodes in the linked list.
|
|
615
|
+
* @param sentinel - The `sentinel` parameter is a reference to a special node in a linked list. It
|
|
616
|
+
* is used to mark the beginning or end of the list and is typically used in data structures like
|
|
617
|
+
* hash maps or linked lists to simplify operations and boundary checks.
|
|
618
|
+
* @param hashMap - A HashMap object that stores key-value pairs.
|
|
619
|
+
* @param {IterateDirection} iterateDirection - The `iterateDirection` parameter is an optional
|
|
620
|
+
* parameter that specifies the direction in which the iterator should iterate over the elements of
|
|
621
|
+
* the HashMap. It can take one of the following values:
|
|
622
|
+
* @returns The constructor does not return anything. It is used to initialize the properties and
|
|
623
|
+
* methods of the object being created.
|
|
624
|
+
*/
|
|
625
|
+
constructor(node, sentinel, hashMap, iterateDirection = 0 /* DEFAULT */) {
|
|
626
|
+
__publicField(this, "hashMap");
|
|
627
|
+
__publicField(this, "_node");
|
|
628
|
+
__publicField(this, "iterateDirection");
|
|
629
|
+
__publicField(this, "_sentinel");
|
|
630
|
+
this._node = node;
|
|
631
|
+
this._sentinel = sentinel;
|
|
632
|
+
this.iterateDirection = iterateDirection;
|
|
633
|
+
if (this.iterateDirection === 0 /* DEFAULT */) {
|
|
634
|
+
this.prev = function() {
|
|
635
|
+
if (this._node.prev === this._sentinel) {
|
|
636
|
+
throwRangeError();
|
|
637
|
+
}
|
|
638
|
+
this._node = this._node.prev;
|
|
639
|
+
return this;
|
|
640
|
+
};
|
|
641
|
+
this.next = function() {
|
|
642
|
+
if (this._node === this._sentinel) {
|
|
643
|
+
throwRangeError();
|
|
644
|
+
}
|
|
645
|
+
this._node = this._node.next;
|
|
646
|
+
return this;
|
|
647
|
+
};
|
|
648
|
+
} else {
|
|
649
|
+
this.prev = function() {
|
|
650
|
+
if (this._node.next === this._sentinel) {
|
|
651
|
+
throwRangeError();
|
|
652
|
+
}
|
|
653
|
+
this._node = this._node.next;
|
|
654
|
+
return this;
|
|
655
|
+
};
|
|
656
|
+
this.next = function() {
|
|
657
|
+
if (this._node === this._sentinel) {
|
|
658
|
+
throwRangeError();
|
|
659
|
+
}
|
|
660
|
+
this._node = this._node.prev;
|
|
661
|
+
return this;
|
|
662
|
+
};
|
|
663
|
+
}
|
|
664
|
+
this.hashMap = hashMap;
|
|
665
|
+
}
|
|
494
666
|
/**
|
|
495
|
-
* The
|
|
496
|
-
*
|
|
497
|
-
* @
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
let hash = 0;
|
|
521
|
-
for (let i = 0; i < strKey.length; i++) {
|
|
522
|
-
hash += strKey.charCodeAt(i);
|
|
523
|
-
}
|
|
524
|
-
return hash % this.table.length;
|
|
667
|
+
* The above function returns a Proxy object that allows access to the key and value of a node in a
|
|
668
|
+
* data structure.
|
|
669
|
+
* @returns The code is returning a Proxy object.
|
|
670
|
+
*/
|
|
671
|
+
get current() {
|
|
672
|
+
if (this._node === this._sentinel) {
|
|
673
|
+
throwRangeError();
|
|
674
|
+
}
|
|
675
|
+
return new Proxy([], {
|
|
676
|
+
get: (target, prop) => {
|
|
677
|
+
if (prop === "0")
|
|
678
|
+
return this._node.key;
|
|
679
|
+
else if (prop === "1")
|
|
680
|
+
return this._node.value;
|
|
681
|
+
target[0] = this._node.key;
|
|
682
|
+
target[1] = this._node.value;
|
|
683
|
+
return target[prop];
|
|
684
|
+
},
|
|
685
|
+
set: (_, prop, newValue) => {
|
|
686
|
+
if (prop !== "1") {
|
|
687
|
+
throw new TypeError(`prop should be string '1'`);
|
|
688
|
+
}
|
|
689
|
+
this._node.value = newValue;
|
|
690
|
+
return true;
|
|
691
|
+
}
|
|
525
692
|
});
|
|
526
693
|
}
|
|
527
|
-
|
|
528
|
-
|
|
694
|
+
/**
|
|
695
|
+
* The function checks if a node is accessible.
|
|
696
|
+
* @returns a boolean value indicating whether the `_node` is not equal to the `_sentinel`.
|
|
697
|
+
*/
|
|
698
|
+
isAccessible() {
|
|
699
|
+
return this._node !== this._sentinel;
|
|
529
700
|
}
|
|
530
|
-
|
|
531
|
-
return this
|
|
701
|
+
prev() {
|
|
702
|
+
return this;
|
|
532
703
|
}
|
|
533
|
-
|
|
534
|
-
return this
|
|
704
|
+
next() {
|
|
705
|
+
return this;
|
|
706
|
+
}
|
|
707
|
+
};
|
|
708
|
+
var HashMap = class {
|
|
709
|
+
/**
|
|
710
|
+
* The constructor initializes a HashMap object with an optional initial set of key-value pairs.
|
|
711
|
+
* @param hashMap - The `hashMap` parameter is an optional parameter of type `HashMapOptions<[K,
|
|
712
|
+
* V]>`. It is an array of key-value pairs, where each pair is represented as an array `[K, V]`. The
|
|
713
|
+
* `K` represents the type of the key and `V` represents the
|
|
714
|
+
*/
|
|
715
|
+
constructor(hashMap = []) {
|
|
716
|
+
__publicField(this, "_nodes", []);
|
|
717
|
+
__publicField(this, "_orgMap", {});
|
|
718
|
+
__publicField(this, "_head");
|
|
719
|
+
__publicField(this, "_tail");
|
|
720
|
+
__publicField(this, "_sentinel");
|
|
721
|
+
__publicField(this, "OBJ_KEY_INDEX", Symbol("OBJ_KEY_INDEX"));
|
|
722
|
+
__publicField(this, "_size", 0);
|
|
723
|
+
Object.setPrototypeOf(this._orgMap, null);
|
|
724
|
+
this._sentinel = {};
|
|
725
|
+
this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
|
|
726
|
+
hashMap.forEach((el) => {
|
|
727
|
+
this.set(el[0], el[1]);
|
|
728
|
+
});
|
|
535
729
|
}
|
|
536
730
|
get size() {
|
|
537
731
|
return this._size;
|
|
538
732
|
}
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
733
|
+
/**
|
|
734
|
+
* Time Complexity: O(1)
|
|
735
|
+
* Space Complexity: O(1)
|
|
736
|
+
*
|
|
737
|
+
* The `set` function adds a new key-value pair to a data structure, either using an object key or a
|
|
738
|
+
* string key.
|
|
739
|
+
* @param {K} key - The `key` parameter is the key to be set in the data structure. It can be of any
|
|
740
|
+
* type, but typically it is a string or symbol.
|
|
741
|
+
* @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the
|
|
742
|
+
* value associated with the key being set in the data structure.
|
|
743
|
+
* @param {boolean} isObjectKey - A boolean flag indicating whether the key is an object key or not.
|
|
744
|
+
* @returns the size of the data structure after the key-value pair has been set.
|
|
745
|
+
*/
|
|
746
|
+
set(key, value, isObjectKey = isObjOrFunc(key)) {
|
|
747
|
+
let newTail;
|
|
748
|
+
if (isObjectKey) {
|
|
749
|
+
const index = key[this.OBJ_KEY_INDEX];
|
|
750
|
+
if (index !== void 0) {
|
|
751
|
+
this._nodes[index].value = value;
|
|
752
|
+
return this._size;
|
|
753
|
+
}
|
|
754
|
+
Object.defineProperty(key, this.OBJ_KEY_INDEX, {
|
|
755
|
+
value: this._nodes.length,
|
|
756
|
+
configurable: true
|
|
757
|
+
});
|
|
758
|
+
newTail = {
|
|
759
|
+
key,
|
|
760
|
+
value,
|
|
761
|
+
prev: this._tail,
|
|
762
|
+
next: this._sentinel
|
|
763
|
+
};
|
|
764
|
+
this._nodes.push(newTail);
|
|
765
|
+
} else {
|
|
766
|
+
const node = this._orgMap[key];
|
|
767
|
+
if (node) {
|
|
768
|
+
node.value = value;
|
|
769
|
+
return this._size;
|
|
770
|
+
}
|
|
771
|
+
this._orgMap[key] = newTail = {
|
|
772
|
+
key,
|
|
773
|
+
value,
|
|
774
|
+
prev: this._tail,
|
|
775
|
+
next: this._sentinel
|
|
776
|
+
};
|
|
549
777
|
}
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
this.
|
|
778
|
+
if (this._size === 0) {
|
|
779
|
+
this._head = newTail;
|
|
780
|
+
this._sentinel.next = newTail;
|
|
781
|
+
} else {
|
|
782
|
+
this._tail.next = newTail;
|
|
553
783
|
}
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
784
|
+
this._tail = newTail;
|
|
785
|
+
this._sentinel.prev = newTail;
|
|
786
|
+
return ++this._size;
|
|
787
|
+
}
|
|
788
|
+
/**
|
|
789
|
+
* Time Complexity: O(1)
|
|
790
|
+
* Space Complexity: O(1)
|
|
791
|
+
*
|
|
792
|
+
* The function `get` retrieves the value associated with a given key from a map, either by using the
|
|
793
|
+
* key directly or by using an index stored in the key object.
|
|
794
|
+
* @param {K} key - The `key` parameter is the key used to retrieve a value from the map. It can be
|
|
795
|
+
* of any type, but typically it is a string or symbol.
|
|
796
|
+
* @param {boolean} isObjectKey - The `isObjectKey` parameter is a boolean flag that indicates
|
|
797
|
+
* whether the `key` parameter is an object key or not. If `isObjectKey` is `true`, it means that
|
|
798
|
+
* `key` is an object key. If `isObjectKey` is `false`, it means that `key`
|
|
799
|
+
* @returns The value associated with the given key is being returned. If the key is an object key,
|
|
800
|
+
* the value is retrieved from the `_nodes` array using the index stored in the `OBJ_KEY_INDEX`
|
|
801
|
+
* property of the key. If the key is a string key, the value is retrieved from the `_orgMap` object
|
|
802
|
+
* using the key itself. If the key is not found, `undefined` is
|
|
803
|
+
*/
|
|
804
|
+
get(key, isObjectKey = isObjOrFunc(key)) {
|
|
805
|
+
if (isObjectKey) {
|
|
806
|
+
const index = key[this.OBJ_KEY_INDEX];
|
|
807
|
+
return index !== void 0 ? this._nodes[index].value : void 0;
|
|
559
808
|
}
|
|
560
|
-
this.
|
|
561
|
-
|
|
809
|
+
const node = this._orgMap[key];
|
|
810
|
+
return node ? node.value : void 0;
|
|
562
811
|
}
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
812
|
+
/**
|
|
813
|
+
* Time Complexity: O(n), where n is the index.
|
|
814
|
+
* Space Complexity: O(1)
|
|
815
|
+
*
|
|
816
|
+
* The function `getAt` retrieves the key-value pair at a specified index in a linked list.
|
|
817
|
+
* @param {number} index - The index parameter is a number that represents the position of the
|
|
818
|
+
* element we want to retrieve from the data structure.
|
|
819
|
+
* @returns The method `getAt(index: number)` is returning an array containing the key-value pair at
|
|
820
|
+
* the specified index in the data structure. The key-value pair is represented as a tuple `[K, V]`,
|
|
821
|
+
* where `K` is the key and `V` is the value.
|
|
822
|
+
*/
|
|
823
|
+
getAt(index) {
|
|
824
|
+
rangeCheck(index, 0, this._size - 1);
|
|
825
|
+
let node = this._head;
|
|
826
|
+
while (index--) {
|
|
827
|
+
node = node.next;
|
|
567
828
|
}
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
829
|
+
return [node.key, node.value];
|
|
830
|
+
}
|
|
831
|
+
/**
|
|
832
|
+
* Time Complexity: O(1)
|
|
833
|
+
* Space Complexity: O(1)
|
|
834
|
+
*
|
|
835
|
+
* The function `getIterator` returns a new instance of `HashMapIterator` based on the provided key
|
|
836
|
+
* and whether it is an object key or not.
|
|
837
|
+
* @param {K} key - The `key` parameter is the key used to retrieve the iterator from the HashMap. It
|
|
838
|
+
* can be of any type, depending on how the HashMap is implemented.
|
|
839
|
+
* @param {boolean} [isObjectKey] - The `isObjectKey` parameter is an optional boolean parameter that
|
|
840
|
+
* indicates whether the `key` parameter is an object key. If `isObjectKey` is `true`, it means that
|
|
841
|
+
* the `key` parameter is an object and needs to be handled differently. If `isObjectKey` is `false`
|
|
842
|
+
* @returns a new instance of the `HashMapIterator` class.
|
|
843
|
+
*/
|
|
844
|
+
getIterator(key, isObjectKey) {
|
|
845
|
+
let node;
|
|
846
|
+
if (isObjectKey) {
|
|
847
|
+
const index = key[this.OBJ_KEY_INDEX];
|
|
848
|
+
if (index === void 0) {
|
|
849
|
+
node = this._sentinel;
|
|
850
|
+
} else {
|
|
851
|
+
node = this._nodes[index];
|
|
571
852
|
}
|
|
853
|
+
} else {
|
|
854
|
+
node = this._orgMap[key] || this._sentinel;
|
|
572
855
|
}
|
|
573
|
-
return
|
|
856
|
+
return new HashMapIterator(node, this._sentinel, this);
|
|
574
857
|
}
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
858
|
+
/**
|
|
859
|
+
* Time Complexity: O(1)
|
|
860
|
+
* Space Complexity: O(1)
|
|
861
|
+
*
|
|
862
|
+
* The `delete` function removes a key-value pair from a map-like data structure.
|
|
863
|
+
* @param {K} key - The `key` parameter is the key that you want to delete from the data structure.
|
|
864
|
+
* It can be of any type, but typically it is a string or an object.
|
|
865
|
+
* @param {boolean} isObjectKey - The `isObjectKey` parameter is a boolean flag that indicates
|
|
866
|
+
* whether the `key` parameter is an object key or not. If `isObjectKey` is `true`, it means that the
|
|
867
|
+
* `key` parameter is an object key. If `isObjectKey` is `false`, it means that the
|
|
868
|
+
* @returns a boolean value. It returns `true` if the deletion was successful, and `false` if the key
|
|
869
|
+
* was not found.
|
|
870
|
+
*/
|
|
871
|
+
delete(key, isObjectKey = isObjOrFunc(key)) {
|
|
872
|
+
let node;
|
|
873
|
+
if (isObjectKey) {
|
|
874
|
+
const index = key[this.OBJ_KEY_INDEX];
|
|
875
|
+
if (index === void 0)
|
|
876
|
+
return false;
|
|
877
|
+
delete key[this.OBJ_KEY_INDEX];
|
|
878
|
+
node = this._nodes[index];
|
|
879
|
+
delete this._nodes[index];
|
|
880
|
+
} else {
|
|
881
|
+
node = this._orgMap[key];
|
|
882
|
+
if (node === void 0)
|
|
883
|
+
return false;
|
|
884
|
+
delete this._orgMap[key];
|
|
590
885
|
}
|
|
886
|
+
this._deleteNode(node);
|
|
887
|
+
return true;
|
|
591
888
|
}
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
889
|
+
/**
|
|
890
|
+
* Time Complexity: O(n), where n is the index.
|
|
891
|
+
* Space Complexity: O(1)
|
|
892
|
+
*
|
|
893
|
+
* The `deleteAt` function deletes a node at a specified index in a linked list.
|
|
894
|
+
* @param {number} index - The index parameter represents the position at which the node should be
|
|
895
|
+
* deleted in the linked list.
|
|
896
|
+
* @returns The size of the list after deleting the element at the specified index.
|
|
897
|
+
*/
|
|
898
|
+
deleteAt(index) {
|
|
899
|
+
rangeCheck(index, 0, this._size - 1);
|
|
900
|
+
let node = this._head;
|
|
901
|
+
while (index--) {
|
|
902
|
+
node = node.next;
|
|
599
903
|
}
|
|
904
|
+
this._deleteNode(node);
|
|
905
|
+
return this._size;
|
|
600
906
|
}
|
|
601
|
-
|
|
602
|
-
|
|
907
|
+
/**
|
|
908
|
+
* Time Complexity: O(1)
|
|
909
|
+
* Space Complexity: O(1)
|
|
910
|
+
*
|
|
911
|
+
* The function checks if a data structure is empty by comparing its size to zero.
|
|
912
|
+
* @returns The method is returning a boolean value indicating whether the size of the object is 0 or
|
|
913
|
+
* not.
|
|
914
|
+
*/
|
|
915
|
+
isEmpty() {
|
|
916
|
+
return this._size === 0;
|
|
603
917
|
}
|
|
918
|
+
/**
|
|
919
|
+
* Time Complexity: O(1)
|
|
920
|
+
* Space Complexity: O(1)
|
|
921
|
+
*
|
|
922
|
+
* The `clear` function clears all the elements in a data structure and resets its properties.
|
|
923
|
+
*/
|
|
604
924
|
clear() {
|
|
925
|
+
this._nodes = [];
|
|
926
|
+
this._orgMap = {};
|
|
927
|
+
Object.setPrototypeOf(this._orgMap, null);
|
|
605
928
|
this._size = 0;
|
|
606
|
-
this.
|
|
929
|
+
this._head = this._tail = this._sentinel.prev = this._sentinel.next = this._sentinel;
|
|
607
930
|
}
|
|
608
|
-
|
|
609
|
-
|
|
931
|
+
/**
|
|
932
|
+
* Time Complexity: O(1)
|
|
933
|
+
* Space Complexity: O(1)
|
|
934
|
+
*
|
|
935
|
+
* The function returns a new iterator object for a HashMap.
|
|
936
|
+
* @returns A new instance of the HashMapIterator class is being returned.
|
|
937
|
+
*/
|
|
938
|
+
get begin() {
|
|
939
|
+
return new HashMapIterator(this._head, this._sentinel, this);
|
|
610
940
|
}
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
* The
|
|
616
|
-
*
|
|
617
|
-
* @
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
941
|
+
/**
|
|
942
|
+
* Time Complexity: O(1)
|
|
943
|
+
* Space Complexity: O(1)
|
|
944
|
+
*
|
|
945
|
+
* The function returns a new HashMapIterator object with the _sentinel value as both the start and
|
|
946
|
+
* end values.
|
|
947
|
+
* @returns A new instance of the HashMapIterator class is being returned.
|
|
948
|
+
*/
|
|
949
|
+
get end() {
|
|
950
|
+
return new HashMapIterator(this._sentinel, this._sentinel, this);
|
|
951
|
+
}
|
|
952
|
+
/**
|
|
953
|
+
* Time Complexity: O(1)
|
|
954
|
+
* Space Complexity: O(1)
|
|
955
|
+
*
|
|
956
|
+
* The reverseBegin function returns a new HashMapIterator object that iterates over the elements of
|
|
957
|
+
* a HashMap in reverse order.
|
|
958
|
+
* @returns A new instance of the HashMapIterator class is being returned.
|
|
959
|
+
*/
|
|
960
|
+
get reverseBegin() {
|
|
961
|
+
return new HashMapIterator(this._tail, this._sentinel, this, 1 /* REVERSE */);
|
|
962
|
+
}
|
|
963
|
+
/**
|
|
964
|
+
* Time Complexity: O(1)
|
|
965
|
+
* Space Complexity: O(1)
|
|
966
|
+
*
|
|
967
|
+
* The reverseEnd function returns a new HashMapIterator object that iterates over the elements of a
|
|
968
|
+
* HashMap in reverse order.
|
|
969
|
+
* @returns A new instance of the HashMapIterator class is being returned.
|
|
970
|
+
*/
|
|
971
|
+
get reverseEnd() {
|
|
972
|
+
return new HashMapIterator(this._sentinel, this._sentinel, this, 1 /* REVERSE */);
|
|
973
|
+
}
|
|
974
|
+
/**
|
|
975
|
+
* Time Complexity: O(1)
|
|
976
|
+
* Space Complexity: O(1)
|
|
977
|
+
*
|
|
978
|
+
* The function returns the key-value pair at the front of a data structure.
|
|
979
|
+
* @returns The front element of the data structure, represented as a tuple with a key (K) and a
|
|
980
|
+
* value (V).
|
|
981
|
+
*/
|
|
982
|
+
get front() {
|
|
983
|
+
if (this._size === 0)
|
|
984
|
+
return;
|
|
985
|
+
return [this._head.key, this._head.value];
|
|
986
|
+
}
|
|
987
|
+
/**
|
|
988
|
+
* Time Complexity: O(1)
|
|
989
|
+
* Space Complexity: O(1)
|
|
990
|
+
*
|
|
991
|
+
* The function returns the key-value pair at the end of a data structure.
|
|
992
|
+
* @returns The method is returning an array containing the key-value pair of the tail element in the
|
|
993
|
+
* data structure.
|
|
994
|
+
*/
|
|
995
|
+
get back() {
|
|
996
|
+
if (this._size === 0)
|
|
997
|
+
return;
|
|
998
|
+
return [this._tail.key, this._tail.value];
|
|
999
|
+
}
|
|
1000
|
+
/**
|
|
1001
|
+
* Time Complexity: O(n), where n is the number of elements in the HashMap.
|
|
1002
|
+
* Space Complexity: O(1)
|
|
1003
|
+
*
|
|
1004
|
+
* The `forEach` function iterates over each element in a HashMap and executes a callback function on
|
|
1005
|
+
* each element.
|
|
1006
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
1007
|
+
* HashMap. It takes three arguments:
|
|
1008
|
+
*/
|
|
1009
|
+
forEach(callback) {
|
|
1010
|
+
let index = 0;
|
|
1011
|
+
let node = this._head;
|
|
1012
|
+
while (node !== this._sentinel) {
|
|
1013
|
+
callback([node.key, node.value], index++, this);
|
|
1014
|
+
node = node.next;
|
|
632
1015
|
}
|
|
633
|
-
|
|
1016
|
+
}
|
|
1017
|
+
/**
|
|
1018
|
+
* Time Complexity: O(n), where n is the number of elements in the HashMap.
|
|
1019
|
+
* Space Complexity: O(1)
|
|
1020
|
+
*
|
|
1021
|
+
* The above function is an iterator that yields key-value pairs from a linked list.
|
|
1022
|
+
*/
|
|
1023
|
+
*[Symbol.iterator]() {
|
|
1024
|
+
let node = this._head;
|
|
1025
|
+
while (node !== this._sentinel) {
|
|
1026
|
+
yield [node.key, node.value];
|
|
1027
|
+
node = node.next;
|
|
1028
|
+
}
|
|
1029
|
+
}
|
|
1030
|
+
/**
|
|
1031
|
+
* Time Complexity: O(1)
|
|
1032
|
+
* Space Complexity: O(1)
|
|
1033
|
+
*
|
|
1034
|
+
* The `_deleteNode` function removes a node from a doubly linked list and updates the head and tail
|
|
1035
|
+
* pointers if necessary.
|
|
1036
|
+
* @param node - The `node` parameter is an instance of the `HashMapLinkedNode` class, which
|
|
1037
|
+
* represents a node in a linked list. It contains a key-value pair and references to the previous
|
|
1038
|
+
* and next nodes in the list.
|
|
1039
|
+
*/
|
|
1040
|
+
_deleteNode(node) {
|
|
1041
|
+
const { prev, next } = node;
|
|
1042
|
+
prev.next = next;
|
|
1043
|
+
next.prev = prev;
|
|
1044
|
+
if (node === this._head) {
|
|
1045
|
+
this._head = next;
|
|
1046
|
+
}
|
|
1047
|
+
if (node === this._tail) {
|
|
1048
|
+
this._tail = prev;
|
|
1049
|
+
}
|
|
1050
|
+
this._size -= 1;
|
|
634
1051
|
}
|
|
635
1052
|
};
|
|
636
1053
|
|
|
@@ -3093,68 +3510,6 @@ var dataStructureTyped = (() => {
|
|
|
3093
3510
|
}
|
|
3094
3511
|
};
|
|
3095
3512
|
|
|
3096
|
-
// src/utils/utils.ts
|
|
3097
|
-
var uuidV4 = function() {
|
|
3098
|
-
return "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx".replace(/[x]/g, function(c) {
|
|
3099
|
-
const r = Math.random() * 16 | 0, v = c == "x" ? r : r & 3 | 8;
|
|
3100
|
-
return v.toString(16);
|
|
3101
|
-
});
|
|
3102
|
-
};
|
|
3103
|
-
var arrayRemove = function(array, predicate) {
|
|
3104
|
-
let i = -1, len = array ? array.length : 0;
|
|
3105
|
-
const result = [];
|
|
3106
|
-
while (++i < len) {
|
|
3107
|
-
const value = array[i];
|
|
3108
|
-
if (predicate(value, i, array)) {
|
|
3109
|
-
result.push(value);
|
|
3110
|
-
Array.prototype.splice.call(array, i--, 1);
|
|
3111
|
-
len--;
|
|
3112
|
-
}
|
|
3113
|
-
}
|
|
3114
|
-
return result;
|
|
3115
|
-
};
|
|
3116
|
-
var THUNK_SYMBOL = Symbol("thunk");
|
|
3117
|
-
var isThunk = (fnOrValue) => {
|
|
3118
|
-
return typeof fnOrValue === "function" && fnOrValue.__THUNK__ === THUNK_SYMBOL;
|
|
3119
|
-
};
|
|
3120
|
-
var toThunk = (fn) => {
|
|
3121
|
-
const thunk = () => fn();
|
|
3122
|
-
thunk.__THUNK__ = THUNK_SYMBOL;
|
|
3123
|
-
return thunk;
|
|
3124
|
-
};
|
|
3125
|
-
var trampoline = (fn) => {
|
|
3126
|
-
const cont = (...args) => toThunk(() => fn(...args));
|
|
3127
|
-
return Object.assign(
|
|
3128
|
-
(...args) => {
|
|
3129
|
-
let result = fn(...args);
|
|
3130
|
-
while (isThunk(result) && typeof result === "function") {
|
|
3131
|
-
result = result();
|
|
3132
|
-
}
|
|
3133
|
-
return result;
|
|
3134
|
-
},
|
|
3135
|
-
{ cont }
|
|
3136
|
-
);
|
|
3137
|
-
};
|
|
3138
|
-
var trampolineAsync = (fn) => {
|
|
3139
|
-
const cont = (...args) => toThunk(() => fn(...args));
|
|
3140
|
-
return Object.assign(
|
|
3141
|
-
(...args) => __async(void 0, null, function* () {
|
|
3142
|
-
let result = yield fn(...args);
|
|
3143
|
-
while (isThunk(result) && typeof result === "function") {
|
|
3144
|
-
result = yield result();
|
|
3145
|
-
}
|
|
3146
|
-
return result;
|
|
3147
|
-
}),
|
|
3148
|
-
{ cont }
|
|
3149
|
-
);
|
|
3150
|
-
};
|
|
3151
|
-
var getMSB = (value) => {
|
|
3152
|
-
if (value <= 0) {
|
|
3153
|
-
return 0;
|
|
3154
|
-
}
|
|
3155
|
-
return 1 << 31 - Math.clz32(value);
|
|
3156
|
-
};
|
|
3157
|
-
|
|
3158
3513
|
// src/data-structures/heap/heap.ts
|
|
3159
3514
|
var Heap = class _Heap {
|
|
3160
3515
|
constructor(options) {
|
|
@@ -5794,46 +6149,6 @@ var dataStructureTyped = (() => {
|
|
|
5794
6149
|
}
|
|
5795
6150
|
};
|
|
5796
6151
|
|
|
5797
|
-
// src/types/data-structures/binary-tree/binary-tree.ts
|
|
5798
|
-
var IterationType = /* @__PURE__ */ ((IterationType3) => {
|
|
5799
|
-
IterationType3["ITERATIVE"] = "ITERATIVE";
|
|
5800
|
-
IterationType3["RECURSIVE"] = "RECURSIVE";
|
|
5801
|
-
return IterationType3;
|
|
5802
|
-
})(IterationType || {});
|
|
5803
|
-
var FamilyPosition = /* @__PURE__ */ ((FamilyPosition2) => {
|
|
5804
|
-
FamilyPosition2["ROOT"] = "ROOT";
|
|
5805
|
-
FamilyPosition2["LEFT"] = "LEFT";
|
|
5806
|
-
FamilyPosition2["RIGHT"] = "RIGHT";
|
|
5807
|
-
FamilyPosition2["ROOT_LEFT"] = "ROOT_LEFT";
|
|
5808
|
-
FamilyPosition2["ROOT_RIGHT"] = "ROOT_RIGHT";
|
|
5809
|
-
FamilyPosition2["ISOLATED"] = "ISOLATED";
|
|
5810
|
-
FamilyPosition2["MAL_NODE"] = "MAL_NODE";
|
|
5811
|
-
return FamilyPosition2;
|
|
5812
|
-
})(FamilyPosition || {});
|
|
5813
|
-
|
|
5814
|
-
// src/types/data-structures/binary-tree/rb-tree.ts
|
|
5815
|
-
var RBTNColor = /* @__PURE__ */ ((RBTNColor2) => {
|
|
5816
|
-
RBTNColor2[RBTNColor2["RED"] = 1] = "RED";
|
|
5817
|
-
RBTNColor2[RBTNColor2["BLACK"] = 0] = "BLACK";
|
|
5818
|
-
return RBTNColor2;
|
|
5819
|
-
})(RBTNColor || {});
|
|
5820
|
-
|
|
5821
|
-
// src/types/data-structures/graph/directed-graph.ts
|
|
5822
|
-
var TopologicalProperty = /* @__PURE__ */ ((TopologicalProperty2) => {
|
|
5823
|
-
TopologicalProperty2["VAL"] = "VAL";
|
|
5824
|
-
TopologicalProperty2["NODE"] = "NODE";
|
|
5825
|
-
TopologicalProperty2["ID"] = "ID";
|
|
5826
|
-
return TopologicalProperty2;
|
|
5827
|
-
})(TopologicalProperty || {});
|
|
5828
|
-
|
|
5829
|
-
// src/types/helpers.ts
|
|
5830
|
-
var CP = /* @__PURE__ */ ((CP2) => {
|
|
5831
|
-
CP2["lt"] = "lt";
|
|
5832
|
-
CP2["eq"] = "eq";
|
|
5833
|
-
CP2["gt"] = "gt";
|
|
5834
|
-
return CP2;
|
|
5835
|
-
})(CP || {});
|
|
5836
|
-
|
|
5837
6152
|
// src/data-structures/binary-tree/binary-tree.ts
|
|
5838
6153
|
var BinaryTreeNode = class {
|
|
5839
6154
|
/**
|