data-structure-typed 2.5.0 → 2.5.2
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/.vitepress/cache/deps_temp_51f5f1b0/chunk-7OIKW5WK.js +12984 -0
- package/.vitepress/cache/deps_temp_51f5f1b0/package.json +3 -0
- package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vue_devtools-api.js +4505 -0
- package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vueuse_core.js +9731 -0
- package/.vitepress/cache/deps_temp_51f5f1b0/vue.js +347 -0
- package/CHANGELOG.md +5 -1
- package/README.md +124 -29
- package/dist/cjs/binary-tree.cjs +26282 -0
- package/dist/cjs/graph.cjs +5422 -0
- package/dist/cjs/hash.cjs +1310 -0
- package/dist/cjs/heap.cjs +1602 -0
- package/dist/cjs/index.cjs +31257 -14673
- package/dist/cjs/linked-list.cjs +4576 -0
- package/dist/cjs/matrix.cjs +1080 -0
- package/dist/cjs/priority-queue.cjs +1376 -0
- package/dist/cjs/queue.cjs +4264 -0
- package/dist/cjs/stack.cjs +907 -0
- package/dist/cjs/trie.cjs +1223 -0
- package/dist/cjs-legacy/binary-tree.cjs +26319 -0
- package/dist/cjs-legacy/graph.cjs +5420 -0
- package/dist/cjs-legacy/hash.cjs +1310 -0
- package/dist/cjs-legacy/heap.cjs +1599 -0
- package/dist/cjs-legacy/index.cjs +31268 -14679
- package/dist/cjs-legacy/linked-list.cjs +4582 -0
- package/dist/cjs-legacy/matrix.cjs +1083 -0
- package/dist/cjs-legacy/priority-queue.cjs +1374 -0
- package/dist/cjs-legacy/queue.cjs +4262 -0
- package/dist/cjs-legacy/stack.cjs +907 -0
- package/dist/cjs-legacy/trie.cjs +1222 -0
- package/dist/esm/binary-tree.mjs +26267 -0
- package/dist/esm/graph.mjs +5409 -0
- package/dist/esm/hash.mjs +1307 -0
- package/dist/esm/heap.mjs +1596 -0
- package/dist/esm/index.mjs +31254 -14674
- package/dist/esm/linked-list.mjs +4569 -0
- package/dist/esm/matrix.mjs +1076 -0
- package/dist/esm/priority-queue.mjs +1372 -0
- package/dist/esm/queue.mjs +4260 -0
- package/dist/esm/stack.mjs +905 -0
- package/dist/esm/trie.mjs +1220 -0
- package/dist/esm-legacy/binary-tree.mjs +26304 -0
- package/dist/esm-legacy/graph.mjs +5407 -0
- package/dist/esm-legacy/hash.mjs +1307 -0
- package/dist/esm-legacy/heap.mjs +1593 -0
- package/dist/esm-legacy/index.mjs +31265 -14680
- package/dist/esm-legacy/linked-list.mjs +4575 -0
- package/dist/esm-legacy/matrix.mjs +1079 -0
- package/dist/esm-legacy/priority-queue.mjs +1370 -0
- package/dist/esm-legacy/queue.mjs +4258 -0
- package/dist/esm-legacy/stack.mjs +905 -0
- package/dist/esm-legacy/trie.mjs +1219 -0
- package/dist/types/common/error.d.ts +9 -0
- package/dist/types/common/index.d.ts +1 -1
- package/dist/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
- package/dist/types/data-structures/base/linear-base.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +288 -0
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +336 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +618 -18
- package/dist/types/data-structures/binary-tree/bst.d.ts +676 -1
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +456 -0
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +144 -1
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +3307 -399
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3285 -360
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2674 -325
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +3072 -287
- package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
- package/dist/types/data-structures/graph/directed-graph.d.ts +240 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +216 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +274 -10
- package/dist/types/data-structures/heap/heap.d.ts +336 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +411 -3
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +363 -3
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +434 -2
- package/dist/types/data-structures/matrix/matrix.d.ts +192 -0
- package/dist/types/data-structures/queue/deque.d.ts +364 -4
- package/dist/types/data-structures/queue/queue.d.ts +288 -0
- package/dist/types/data-structures/stack/stack.d.ts +240 -0
- package/dist/types/data-structures/trie/trie.d.ts +292 -4
- package/dist/types/interfaces/graph.d.ts +1 -1
- package/dist/types/types/common.d.ts +2 -2
- package/dist/types/types/data-structures/binary-tree/bst.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/tree-map.d.ts +5 -0
- package/dist/types/types/data-structures/binary-tree/tree-multi-set.d.ts +4 -0
- package/dist/types/types/data-structures/binary-tree/tree-set.d.ts +4 -0
- package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
- package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
- package/dist/types/types/utils/validate-type.d.ts +4 -4
- package/dist/umd/data-structure-typed.js +31196 -14608
- package/dist/umd/data-structure-typed.min.js +11 -5
- package/docs-site-docusaurus/README.md +41 -0
- package/docs-site-docusaurus/docs/api/README.md +52 -0
- package/docs-site-docusaurus/docs/api/classes/AVLTree.md +6644 -0
- package/docs-site-docusaurus/docs/api/classes/AVLTreeNode.md +282 -0
- package/docs-site-docusaurus/docs/api/classes/AbstractGraph.md +2266 -0
- package/docs-site-docusaurus/docs/api/classes/BST.md +6293 -0
- package/docs-site-docusaurus/docs/api/classes/BSTNode.md +333 -0
- package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +455 -0
- package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +4647 -0
- package/docs-site-docusaurus/docs/api/classes/BinaryTreeNode.md +331 -0
- package/docs-site-docusaurus/docs/api/classes/Deque.md +2767 -0
- package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +2999 -0
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +2685 -0
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedListNode.md +221 -0
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +253 -0
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +21 -0
- package/docs-site-docusaurus/docs/api/classes/HashMap.md +1333 -0
- package/docs-site-docusaurus/docs/api/classes/Heap.md +1881 -0
- package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +800 -0
- package/docs-site-docusaurus/docs/api/classes/IterableEntryBase.md +644 -0
- package/docs-site-docusaurus/docs/api/classes/LinearBase.md +1632 -0
- package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +1853 -0
- package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +1108 -0
- package/docs-site-docusaurus/docs/api/classes/LinkedListNode.md +156 -0
- package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +2824 -0
- package/docs-site-docusaurus/docs/api/classes/MapGraph.md +2929 -0
- package/docs-site-docusaurus/docs/api/classes/Matrix.md +1026 -0
- package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +1866 -0
- package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +1883 -0
- package/docs-site-docusaurus/docs/api/classes/MinHeap.md +1879 -0
- package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +1882 -0
- package/docs-site-docusaurus/docs/api/classes/Navigator.md +109 -0
- package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +1839 -0
- package/docs-site-docusaurus/docs/api/classes/Queue.md +2244 -0
- package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +6888 -0
- package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +372 -0
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +2897 -0
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedListNode.md +169 -0
- package/docs-site-docusaurus/docs/api/classes/SkipList.md +1229 -0
- package/docs-site-docusaurus/docs/api/classes/Stack.md +1573 -0
- package/docs-site-docusaurus/docs/api/classes/TreeMap.md +1389 -0
- package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +1591 -0
- package/docs-site-docusaurus/docs/api/classes/TreeSet.md +1246 -0
- package/docs-site-docusaurus/docs/api/classes/Trie.md +1708 -0
- package/docs-site-docusaurus/docs/api/classes/TrieNode.md +199 -0
- package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +2979 -0
- package/docs-site-docusaurus/docs/guide/_category_.json +6 -0
- package/docs-site-docusaurus/docs/guide/architecture.md +615 -0
- package/docs-site-docusaurus/docs/guide/concepts.md +451 -0
- package/docs-site-docusaurus/docs/guide/faq.md +180 -0
- package/docs-site-docusaurus/docs/guide/guides.md +597 -0
- package/docs-site-docusaurus/docs/guide/installation.md +62 -0
- package/docs-site-docusaurus/docs/guide/integrations.md +825 -0
- package/docs-site-docusaurus/docs/guide/overview.md +645 -0
- package/docs-site-docusaurus/docs/guide/performance.md +835 -0
- package/docs-site-docusaurus/docs/guide/quick-start.md +104 -0
- package/docs-site-docusaurus/docs/guide/use-cases/_category_.json +6 -0
- package/docs-site-docusaurus/docs/guide/use-cases/array-sort-alternative.md +158 -0
- package/docs-site-docusaurus/docs/guide/use-cases/heap-vs-sorting.md +92 -0
- package/docs-site-docusaurus/docs/guide/use-cases/map-vs-treemap.md +151 -0
- package/docs-site-docusaurus/docs/guide/use-cases/priority-queue-typescript.md +113 -0
- package/docs-site-docusaurus/docs/guide/use-cases/treemap-javascript.md +151 -0
- package/docs-site-docusaurus/docusaurus.config.ts +159 -0
- package/docs-site-docusaurus/fix-mdx-generics.mjs +75 -0
- package/docs-site-docusaurus/package-lock.json +18667 -0
- package/docs-site-docusaurus/package.json +50 -0
- package/docs-site-docusaurus/prefix-class-to-methods.mjs +48 -0
- package/docs-site-docusaurus/sidebars.ts +23 -0
- package/docs-site-docusaurus/sort-protected.mjs +87 -0
- package/docs-site-docusaurus/src/css/custom.css +96 -0
- package/docs-site-docusaurus/src/pages/index.module.css +13 -0
- package/docs-site-docusaurus/src/pages/index.tsx +120 -0
- package/docs-site-docusaurus/src/pages/markdown-page.md +7 -0
- package/docs-site-docusaurus/src/theme/TOCItems/index.tsx +34 -0
- package/docs-site-docusaurus/static/.nojekyll +0 -0
- package/docs-site-docusaurus/static/img/docusaurus-social-card.jpg +0 -0
- package/docs-site-docusaurus/static/img/docusaurus.png +0 -0
- package/docs-site-docusaurus/static/img/favicon.ico +0 -0
- package/docs-site-docusaurus/static/img/favicon.png +0 -0
- package/docs-site-docusaurus/static/img/logo-180.png +0 -0
- package/docs-site-docusaurus/static/img/logo.jpg +0 -0
- package/docs-site-docusaurus/static/img/logo.png +0 -0
- package/docs-site-docusaurus/static/img/logo.svg +1 -0
- package/docs-site-docusaurus/static/img/og-image.png +0 -0
- package/docs-site-docusaurus/static/img/undraw_docusaurus_mountain.svg +171 -0
- package/docs-site-docusaurus/static/img/undraw_docusaurus_react.svg +170 -0
- package/docs-site-docusaurus/static/img/undraw_docusaurus_tree.svg +40 -0
- package/docs-site-docusaurus/static/llms.txt +37 -0
- package/docs-site-docusaurus/static/robots.txt +4 -0
- package/docs-site-docusaurus/typedoc.json +23 -0
- package/llms.txt +37 -0
- package/package.json +159 -55
- package/src/common/error.ts +19 -1
- package/src/common/index.ts +1 -1
- package/src/data-structures/base/index.ts +1 -0
- package/src/data-structures/base/iterable-element-base.ts +3 -2
- package/src/data-structures/base/iterable-entry-base.ts +8 -8
- package/src/data-structures/base/linear-base.ts +3 -3
- package/src/data-structures/binary-tree/avl-tree.ts +287 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +327 -5
- package/src/data-structures/binary-tree/binary-tree.ts +581 -6
- package/src/data-structures/binary-tree/bst.ts +922 -7
- package/src/data-structures/binary-tree/red-black-tree.ts +453 -0
- package/src/data-structures/binary-tree/segment-tree.ts +139 -2
- package/src/data-structures/binary-tree/tree-map.ts +3300 -495
- package/src/data-structures/binary-tree/tree-multi-map.ts +3384 -563
- package/src/data-structures/binary-tree/tree-multi-set.ts +2757 -493
- package/src/data-structures/binary-tree/tree-set.ts +3122 -440
- package/src/data-structures/graph/abstract-graph.ts +6 -6
- package/src/data-structures/graph/directed-graph.ts +230 -0
- package/src/data-structures/graph/undirected-graph.ts +207 -0
- package/src/data-structures/hash/hash-map.ts +270 -19
- package/src/data-structures/heap/heap.ts +326 -4
- package/src/data-structures/heap/max-heap.ts +2 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +394 -3
- package/src/data-structures/linked-list/singly-linked-list.ts +348 -3
- package/src/data-structures/linked-list/skip-linked-list.ts +421 -7
- package/src/data-structures/matrix/matrix.ts +194 -10
- package/src/data-structures/priority-queue/max-priority-queue.ts +2 -2
- package/src/data-structures/queue/deque.ts +350 -5
- package/src/data-structures/queue/queue.ts +276 -0
- package/src/data-structures/stack/stack.ts +230 -0
- package/src/data-structures/trie/trie.ts +283 -7
- package/src/interfaces/graph.ts +1 -1
- package/src/types/common.ts +2 -2
- package/src/types/data-structures/binary-tree/bst.ts +1 -0
- package/src/types/data-structures/binary-tree/tree-map.ts +6 -0
- package/src/types/data-structures/binary-tree/tree-multi-set.ts +5 -0
- package/src/types/data-structures/binary-tree/tree-set.ts +5 -0
- package/src/types/data-structures/heap/heap.ts +1 -0
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
- package/src/types/utils/validate-type.ts +4 -4
- package/vercel.json +6 -0
- package/dist/leetcode/avl-tree-counter.mjs +0 -2957
- package/dist/leetcode/avl-tree-multi-map.mjs +0 -2889
- package/dist/leetcode/avl-tree.mjs +0 -2720
- package/dist/leetcode/binary-tree.mjs +0 -1594
- package/dist/leetcode/bst.mjs +0 -2398
- package/dist/leetcode/deque.mjs +0 -683
- package/dist/leetcode/directed-graph.mjs +0 -1733
- package/dist/leetcode/doubly-linked-list.mjs +0 -709
- package/dist/leetcode/hash-map.mjs +0 -493
- package/dist/leetcode/heap.mjs +0 -542
- package/dist/leetcode/max-heap.mjs +0 -375
- package/dist/leetcode/max-priority-queue.mjs +0 -383
- package/dist/leetcode/min-heap.mjs +0 -363
- package/dist/leetcode/min-priority-queue.mjs +0 -371
- package/dist/leetcode/priority-queue.mjs +0 -363
- package/dist/leetcode/queue.mjs +0 -943
- package/dist/leetcode/red-black-tree.mjs +0 -2765
- package/dist/leetcode/singly-linked-list.mjs +0 -754
- package/dist/leetcode/stack.mjs +0 -217
- package/dist/leetcode/tree-counter.mjs +0 -3039
- package/dist/leetcode/tree-multi-map.mjs +0 -2913
- package/dist/leetcode/trie.mjs +0 -413
- package/dist/leetcode/undirected-graph.mjs +0 -1650
|
@@ -0,0 +1,1307 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
4
|
+
// src/data-structures/base/iterable-entry-base.ts
|
|
5
|
+
var IterableEntryBase = class {
|
|
6
|
+
static {
|
|
7
|
+
__name(this, "IterableEntryBase");
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Default iterator yielding `[key, value]` entries.
|
|
11
|
+
* @returns Iterator of `[K, V]`.
|
|
12
|
+
* @remarks Time O(n) to iterate, Space O(1)
|
|
13
|
+
*/
|
|
14
|
+
*[Symbol.iterator](...args) {
|
|
15
|
+
yield* this._getIterator(...args);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Iterate over `[key, value]` pairs (may yield `undefined` values).
|
|
19
|
+
* @returns Iterator of `[K, V | undefined]`.
|
|
20
|
+
* @remarks Time O(n), Space O(1)
|
|
21
|
+
*/
|
|
22
|
+
*entries() {
|
|
23
|
+
for (const item of this) {
|
|
24
|
+
yield item;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Iterate over keys only.
|
|
29
|
+
* @returns Iterator of keys.
|
|
30
|
+
* @remarks Time O(n), Space O(1)
|
|
31
|
+
*/
|
|
32
|
+
*keys() {
|
|
33
|
+
for (const item of this) {
|
|
34
|
+
yield item[0];
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Iterate over values only.
|
|
39
|
+
* @returns Iterator of values.
|
|
40
|
+
* @remarks Time O(n), Space O(1)
|
|
41
|
+
*/
|
|
42
|
+
*values() {
|
|
43
|
+
for (const item of this) {
|
|
44
|
+
yield item[1];
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Test whether all entries satisfy the predicate.
|
|
49
|
+
* @param predicate - `(key, value, index, self) => boolean`.
|
|
50
|
+
* @param thisArg - Optional `this` for callback.
|
|
51
|
+
* @returns `true` if all pass; otherwise `false`.
|
|
52
|
+
* @remarks Time O(n), Space O(1)
|
|
53
|
+
*/
|
|
54
|
+
every(predicate, thisArg) {
|
|
55
|
+
let index = 0;
|
|
56
|
+
for (const item of this) {
|
|
57
|
+
if (!predicate.call(thisArg, item[1], item[0], index++, this)) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Test whether any entry satisfies the predicate.
|
|
65
|
+
* @param predicate - `(key, value, index, self) => boolean`.
|
|
66
|
+
* @param thisArg - Optional `this` for callback.
|
|
67
|
+
* @returns `true` if any passes; otherwise `false`.
|
|
68
|
+
* @remarks Time O(n), Space O(1)
|
|
69
|
+
*/
|
|
70
|
+
some(predicate, thisArg) {
|
|
71
|
+
let index = 0;
|
|
72
|
+
for (const item of this) {
|
|
73
|
+
if (predicate.call(thisArg, item[1], item[0], index++, this)) {
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Visit each entry, left-to-right.
|
|
81
|
+
* @param callbackfn - `(key, value, index, self) => void`.
|
|
82
|
+
* @param thisArg - Optional `this` for callback.
|
|
83
|
+
* @remarks Time O(n), Space O(1)
|
|
84
|
+
*/
|
|
85
|
+
forEach(callbackfn, thisArg) {
|
|
86
|
+
let index = 0;
|
|
87
|
+
for (const item of this) {
|
|
88
|
+
const [key, value] = item;
|
|
89
|
+
callbackfn.call(thisArg, value, key, index++, this);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Find the first entry that matches a predicate.
|
|
94
|
+
* @param callbackfn - `(key, value, index, self) => boolean`.
|
|
95
|
+
* @param thisArg - Optional `this` for callback.
|
|
96
|
+
* @returns Matching `[key, value]` or `undefined`.
|
|
97
|
+
* @remarks Time O(n), Space O(1)
|
|
98
|
+
*/
|
|
99
|
+
find(callbackfn, thisArg) {
|
|
100
|
+
let index = 0;
|
|
101
|
+
for (const item of this) {
|
|
102
|
+
const [key, value] = item;
|
|
103
|
+
if (callbackfn.call(thisArg, value, key, index++, this)) return item;
|
|
104
|
+
}
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Whether the given key exists.
|
|
109
|
+
* @param key - Key to test.
|
|
110
|
+
* @returns `true` if found; otherwise `false`.
|
|
111
|
+
* @remarks Time O(n) generic, Space O(1)
|
|
112
|
+
*/
|
|
113
|
+
has(key) {
|
|
114
|
+
for (const item of this) {
|
|
115
|
+
const [itemKey] = item;
|
|
116
|
+
if (itemKey === key) return true;
|
|
117
|
+
}
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Whether there exists an entry with the given value.
|
|
122
|
+
* @param value - Value to test.
|
|
123
|
+
* @returns `true` if found; otherwise `false`.
|
|
124
|
+
* @remarks Time O(n), Space O(1)
|
|
125
|
+
*/
|
|
126
|
+
hasValue(value) {
|
|
127
|
+
for (const [, elementValue] of this) {
|
|
128
|
+
if (elementValue === value) return true;
|
|
129
|
+
}
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Get the value under a key.
|
|
134
|
+
* @param key - Key to look up.
|
|
135
|
+
* @returns Value or `undefined`.
|
|
136
|
+
* @remarks Time O(n) generic, Space O(1)
|
|
137
|
+
*/
|
|
138
|
+
get(key) {
|
|
139
|
+
for (const item of this) {
|
|
140
|
+
const [itemKey, value] = item;
|
|
141
|
+
if (itemKey === key) return value;
|
|
142
|
+
}
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Reduce entries into a single accumulator.
|
|
147
|
+
* @param callbackfn - `(acc, value, key, index, self) => acc`.
|
|
148
|
+
* @param initialValue - Initial accumulator.
|
|
149
|
+
* @returns Final accumulator.
|
|
150
|
+
* @remarks Time O(n), Space O(1)
|
|
151
|
+
*/
|
|
152
|
+
reduce(callbackfn, initialValue) {
|
|
153
|
+
let accumulator = initialValue;
|
|
154
|
+
let index = 0;
|
|
155
|
+
for (const item of this) {
|
|
156
|
+
const [key, value] = item;
|
|
157
|
+
accumulator = callbackfn(accumulator, value, key, index++, this);
|
|
158
|
+
}
|
|
159
|
+
return accumulator;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Converts data structure to `[key, value]` pairs.
|
|
163
|
+
* @returns Array of entries.
|
|
164
|
+
* @remarks Time O(n), Space O(n)
|
|
165
|
+
*/
|
|
166
|
+
toArray() {
|
|
167
|
+
return [...this];
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Visualize the iterable as an array of `[key, value]` pairs (or a custom string).
|
|
171
|
+
* @returns Array of entries (default) or a string.
|
|
172
|
+
* @remarks Time O(n), Space O(n)
|
|
173
|
+
*/
|
|
174
|
+
toVisual() {
|
|
175
|
+
return [...this];
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Print a human-friendly representation to the console.
|
|
179
|
+
* @remarks Time O(n), Space O(n)
|
|
180
|
+
*/
|
|
181
|
+
print() {
|
|
182
|
+
console.log(this.toVisual());
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
// src/common/error.ts
|
|
187
|
+
function raise(ErrorClass, message) {
|
|
188
|
+
throw new ErrorClass(message);
|
|
189
|
+
}
|
|
190
|
+
__name(raise, "raise");
|
|
191
|
+
var ERR = {
|
|
192
|
+
// Range / index
|
|
193
|
+
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
194
|
+
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
195
|
+
// Type / argument
|
|
196
|
+
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
197
|
+
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
198
|
+
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
199
|
+
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
200
|
+
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
201
|
+
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
202
|
+
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
203
|
+
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
204
|
+
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
205
|
+
// State / operation
|
|
206
|
+
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
207
|
+
// Matrix
|
|
208
|
+
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
209
|
+
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
210
|
+
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
211
|
+
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
212
|
+
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch"),
|
|
213
|
+
// Order statistic
|
|
214
|
+
orderStatisticNotEnabled: /* @__PURE__ */ __name((method, ctx) => `${ctx ? ctx + ": " : ""}${method}() requires enableOrderStatistic: true.`, "orderStatisticNotEnabled")
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
// src/utils/utils.ts
|
|
218
|
+
var rangeCheck = /* @__PURE__ */ __name((index, min, max, message) => {
|
|
219
|
+
if (index < min || index > max) {
|
|
220
|
+
throw new RangeError(message ?? `Index ${index} is out of range [${min}, ${max}].`);
|
|
221
|
+
}
|
|
222
|
+
}, "rangeCheck");
|
|
223
|
+
var isWeakKey = /* @__PURE__ */ __name((input) => {
|
|
224
|
+
const inputType = typeof input;
|
|
225
|
+
return inputType === "object" && input !== null || inputType === "function";
|
|
226
|
+
}, "isWeakKey");
|
|
227
|
+
|
|
228
|
+
// src/data-structures/hash/hash-map.ts
|
|
229
|
+
var HashMap = class extends IterableEntryBase {
|
|
230
|
+
static {
|
|
231
|
+
__name(this, "HashMap");
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Create a HashMap and optionally bulk-insert entries.
|
|
235
|
+
* @remarks Time O(N), Space O(N)
|
|
236
|
+
* @param [entryOrRawElements] - Iterable of entries or raw elements to insert.
|
|
237
|
+
* @param [options] - Options: hash function and optional record-to-entry converter.
|
|
238
|
+
* @returns New HashMap instance.
|
|
239
|
+
*/
|
|
240
|
+
constructor(entryOrRawElements = [], options) {
|
|
241
|
+
super();
|
|
242
|
+
if (options) {
|
|
243
|
+
const { hashFn, toEntryFn } = options;
|
|
244
|
+
if (hashFn) this._hashFn = hashFn;
|
|
245
|
+
if (toEntryFn) this._toEntryFn = toEntryFn;
|
|
246
|
+
}
|
|
247
|
+
if (entryOrRawElements) this.setMany(entryOrRawElements);
|
|
248
|
+
}
|
|
249
|
+
_store = {};
|
|
250
|
+
/**
|
|
251
|
+
* Get the internal store for non-object keys.
|
|
252
|
+
* @remarks Time O(1), Space O(1)
|
|
253
|
+
* @returns Internal record of string→{key,value}.
|
|
254
|
+
*/
|
|
255
|
+
get store() {
|
|
256
|
+
return this._store;
|
|
257
|
+
}
|
|
258
|
+
_objMap = /* @__PURE__ */ new Map();
|
|
259
|
+
/**
|
|
260
|
+
* Get the internal Map used for object/function keys.
|
|
261
|
+
* @remarks Time O(1), Space O(1)
|
|
262
|
+
* @returns Map of object→value.
|
|
263
|
+
*/
|
|
264
|
+
get objMap() {
|
|
265
|
+
return this._objMap;
|
|
266
|
+
}
|
|
267
|
+
_toEntryFn;
|
|
268
|
+
/**
|
|
269
|
+
* Get the raw→entry converter function if present.
|
|
270
|
+
* @remarks Time O(1), Space O(1)
|
|
271
|
+
* @returns Converter function or undefined.
|
|
272
|
+
*/
|
|
273
|
+
get toEntryFn() {
|
|
274
|
+
return this._toEntryFn;
|
|
275
|
+
}
|
|
276
|
+
_size = 0;
|
|
277
|
+
/**
|
|
278
|
+
* Get the number of distinct keys stored.
|
|
279
|
+
* @remarks Time O(1), Space O(1)
|
|
280
|
+
* @returns Current size.
|
|
281
|
+
*/
|
|
282
|
+
get size() {
|
|
283
|
+
return this._size;
|
|
284
|
+
}
|
|
285
|
+
_hashFn = /* @__PURE__ */ __name((key) => String(key), "_hashFn");
|
|
286
|
+
/**
|
|
287
|
+
* Get the current hash function for non-object keys.
|
|
288
|
+
* @remarks Time O(1), Space O(1)
|
|
289
|
+
* @returns Hash function.
|
|
290
|
+
*/
|
|
291
|
+
get hashFn() {
|
|
292
|
+
return this._hashFn;
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Check whether the map is empty.
|
|
296
|
+
* @remarks Time O(1), Space O(1)
|
|
297
|
+
* @returns True if size is 0.
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
* @example
|
|
332
|
+
* // Check if empty
|
|
333
|
+
* const map = new HashMap();
|
|
334
|
+
* console.log(map.isEmpty()); // true;
|
|
335
|
+
*/
|
|
336
|
+
isEmpty() {
|
|
337
|
+
return this._size === 0;
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* Remove all entries and reset counters.
|
|
341
|
+
* @remarks Time O(N), Space O(1)
|
|
342
|
+
* @returns void
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
* @example
|
|
377
|
+
* // Remove all entries
|
|
378
|
+
* const map = new HashMap<string, number>([['a', 1], ['b', 2]]);
|
|
379
|
+
* map.clear();
|
|
380
|
+
* console.log(map.isEmpty()); // true;
|
|
381
|
+
*/
|
|
382
|
+
clear() {
|
|
383
|
+
this._store = {};
|
|
384
|
+
this._objMap.clear();
|
|
385
|
+
this._size = 0;
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
388
|
+
* Type guard: check if a raw value is a [key, value] entry.
|
|
389
|
+
* @remarks Time O(1), Space O(1)
|
|
390
|
+
* @returns True if the value is a 2-tuple.
|
|
391
|
+
*/
|
|
392
|
+
isEntry(rawElement) {
|
|
393
|
+
return Array.isArray(rawElement) && rawElement.length === 2;
|
|
394
|
+
}
|
|
395
|
+
/**
|
|
396
|
+
* Insert or replace a single entry.
|
|
397
|
+
* @remarks Time O(1), Space O(1)
|
|
398
|
+
* @param key - Key.
|
|
399
|
+
* @param value - Value.
|
|
400
|
+
* @returns True when the operation succeeds.
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
|
|
428
|
+
|
|
429
|
+
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
|
|
433
|
+
|
|
434
|
+
|
|
435
|
+
|
|
436
|
+
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
|
|
453
|
+
|
|
454
|
+
|
|
455
|
+
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
|
|
459
|
+
|
|
460
|
+
|
|
461
|
+
|
|
462
|
+
|
|
463
|
+
|
|
464
|
+
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
|
|
468
|
+
|
|
469
|
+
|
|
470
|
+
|
|
471
|
+
* @example
|
|
472
|
+
* // basic HashMap creation and set operation
|
|
473
|
+
* // Create a simple HashMap with key-value pairs
|
|
474
|
+
* const map = new HashMap<number, string>([
|
|
475
|
+
* [1, 'one'],
|
|
476
|
+
* [2, 'two'],
|
|
477
|
+
* [3, 'three']
|
|
478
|
+
* ]);
|
|
479
|
+
*
|
|
480
|
+
* // Verify size
|
|
481
|
+
* console.log(map.size); // 3;
|
|
482
|
+
*
|
|
483
|
+
* // Set a new key-value pair
|
|
484
|
+
* map.set(4, 'four');
|
|
485
|
+
* console.log(map.size); // 4;
|
|
486
|
+
*
|
|
487
|
+
* // Verify entries
|
|
488
|
+
* console.log([...map.entries()]); // length: 4;
|
|
489
|
+
*/
|
|
490
|
+
set(key, value) {
|
|
491
|
+
if (this._isObjKey(key)) {
|
|
492
|
+
if (!this.objMap.has(key)) this._size++;
|
|
493
|
+
this.objMap.set(key, value);
|
|
494
|
+
} else {
|
|
495
|
+
const strKey = this._getNoObjKey(key);
|
|
496
|
+
if (this.store[strKey] === void 0) this._size++;
|
|
497
|
+
this._store[strKey] = { key, value };
|
|
498
|
+
}
|
|
499
|
+
return true;
|
|
500
|
+
}
|
|
501
|
+
/**
|
|
502
|
+
* Insert many entries from an iterable.
|
|
503
|
+
* @remarks Time O(N), Space O(N)
|
|
504
|
+
* @param entryOrRawElements - Iterable of entries or raw elements to insert.
|
|
505
|
+
* @returns Array of per-entry results.
|
|
506
|
+
|
|
507
|
+
|
|
508
|
+
|
|
509
|
+
|
|
510
|
+
|
|
511
|
+
|
|
512
|
+
|
|
513
|
+
|
|
514
|
+
|
|
515
|
+
|
|
516
|
+
|
|
517
|
+
|
|
518
|
+
|
|
519
|
+
|
|
520
|
+
|
|
521
|
+
|
|
522
|
+
|
|
523
|
+
|
|
524
|
+
|
|
525
|
+
|
|
526
|
+
|
|
527
|
+
|
|
528
|
+
|
|
529
|
+
|
|
530
|
+
|
|
531
|
+
|
|
532
|
+
|
|
533
|
+
|
|
534
|
+
|
|
535
|
+
|
|
536
|
+
|
|
537
|
+
|
|
538
|
+
|
|
539
|
+
* @example
|
|
540
|
+
* // Add multiple entries
|
|
541
|
+
* const map = new HashMap<string, number>();
|
|
542
|
+
* map.setMany([['a', 1], ['b', 2], ['c', 3]]);
|
|
543
|
+
* console.log(map.size); // 3;
|
|
544
|
+
*/
|
|
545
|
+
setMany(entryOrRawElements) {
|
|
546
|
+
const results = [];
|
|
547
|
+
for (const rawEle of entryOrRawElements) {
|
|
548
|
+
let key, value;
|
|
549
|
+
if (this.isEntry(rawEle)) [key, value] = rawEle;
|
|
550
|
+
else if (this._toEntryFn) [key, value] = this._toEntryFn(rawEle);
|
|
551
|
+
if (key !== void 0 && value !== void 0) results.push(this.set(key, value));
|
|
552
|
+
}
|
|
553
|
+
return results;
|
|
554
|
+
}
|
|
555
|
+
/**
|
|
556
|
+
* Get the value for a key.
|
|
557
|
+
* @remarks Time O(1), Space O(1)
|
|
558
|
+
* @param key - Key to look up.
|
|
559
|
+
* @returns Value or undefined.
|
|
560
|
+
|
|
561
|
+
|
|
562
|
+
|
|
563
|
+
|
|
564
|
+
|
|
565
|
+
|
|
566
|
+
|
|
567
|
+
|
|
568
|
+
|
|
569
|
+
|
|
570
|
+
|
|
571
|
+
|
|
572
|
+
|
|
573
|
+
|
|
574
|
+
|
|
575
|
+
|
|
576
|
+
|
|
577
|
+
|
|
578
|
+
|
|
579
|
+
|
|
580
|
+
|
|
581
|
+
|
|
582
|
+
|
|
583
|
+
|
|
584
|
+
|
|
585
|
+
|
|
586
|
+
|
|
587
|
+
|
|
588
|
+
|
|
589
|
+
|
|
590
|
+
|
|
591
|
+
|
|
592
|
+
|
|
593
|
+
|
|
594
|
+
|
|
595
|
+
* @example
|
|
596
|
+
* // HashMap get and has operations
|
|
597
|
+
* const map = new HashMap<string, number>([
|
|
598
|
+
* ['apple', 1],
|
|
599
|
+
* ['banana', 2],
|
|
600
|
+
* ['cherry', 3]
|
|
601
|
+
* ]);
|
|
602
|
+
*
|
|
603
|
+
* // Check if key exists
|
|
604
|
+
* console.log(map.has('apple')); // true;
|
|
605
|
+
* console.log(map.has('date')); // false;
|
|
606
|
+
*
|
|
607
|
+
* // Get value by key
|
|
608
|
+
* console.log(map.get('banana')); // 2;
|
|
609
|
+
* console.log(map.get('grape')); // undefined;
|
|
610
|
+
*
|
|
611
|
+
* // Get all keys and values
|
|
612
|
+
* const keys = [...map.keys()];
|
|
613
|
+
* const values = [...map.values()];
|
|
614
|
+
* console.log(keys); // contains 'apple';
|
|
615
|
+
* console.log(values); // contains 3;
|
|
616
|
+
*/
|
|
617
|
+
get(key) {
|
|
618
|
+
if (this._isObjKey(key)) return this.objMap.get(key);
|
|
619
|
+
const strKey = this._getNoObjKey(key);
|
|
620
|
+
return this._store[strKey]?.value;
|
|
621
|
+
}
|
|
622
|
+
/**
|
|
623
|
+
* Check if a key exists.
|
|
624
|
+
* @remarks Time O(1), Space O(1)
|
|
625
|
+
* @param key - Key to test.
|
|
626
|
+
* @returns True if present.
|
|
627
|
+
|
|
628
|
+
|
|
629
|
+
|
|
630
|
+
|
|
631
|
+
|
|
632
|
+
|
|
633
|
+
|
|
634
|
+
|
|
635
|
+
|
|
636
|
+
|
|
637
|
+
|
|
638
|
+
|
|
639
|
+
|
|
640
|
+
|
|
641
|
+
|
|
642
|
+
|
|
643
|
+
|
|
644
|
+
|
|
645
|
+
|
|
646
|
+
|
|
647
|
+
|
|
648
|
+
|
|
649
|
+
|
|
650
|
+
|
|
651
|
+
|
|
652
|
+
|
|
653
|
+
|
|
654
|
+
|
|
655
|
+
|
|
656
|
+
|
|
657
|
+
|
|
658
|
+
|
|
659
|
+
|
|
660
|
+
|
|
661
|
+
|
|
662
|
+
* @example
|
|
663
|
+
* // Check key existence
|
|
664
|
+
* const map = new HashMap<string, number>([['a', 1], ['b', 2]]);
|
|
665
|
+
*
|
|
666
|
+
* console.log(map.has('a')); // true;
|
|
667
|
+
* console.log(map.has('z')); // false;
|
|
668
|
+
*/
|
|
669
|
+
has(key) {
|
|
670
|
+
if (this._isObjKey(key)) return this.objMap.has(key);
|
|
671
|
+
const strKey = this._getNoObjKey(key);
|
|
672
|
+
return strKey in this.store;
|
|
673
|
+
}
|
|
674
|
+
/**
|
|
675
|
+
* Delete an entry by key.
|
|
676
|
+
* @remarks Time O(1), Space O(1)
|
|
677
|
+
* @param key - Key to delete.
|
|
678
|
+
* @returns True if the key was found and removed.
|
|
679
|
+
|
|
680
|
+
|
|
681
|
+
|
|
682
|
+
|
|
683
|
+
|
|
684
|
+
|
|
685
|
+
|
|
686
|
+
|
|
687
|
+
|
|
688
|
+
|
|
689
|
+
|
|
690
|
+
|
|
691
|
+
|
|
692
|
+
|
|
693
|
+
|
|
694
|
+
|
|
695
|
+
|
|
696
|
+
|
|
697
|
+
|
|
698
|
+
|
|
699
|
+
|
|
700
|
+
|
|
701
|
+
|
|
702
|
+
|
|
703
|
+
|
|
704
|
+
|
|
705
|
+
|
|
706
|
+
|
|
707
|
+
|
|
708
|
+
|
|
709
|
+
|
|
710
|
+
|
|
711
|
+
|
|
712
|
+
|
|
713
|
+
|
|
714
|
+
* @example
|
|
715
|
+
* // Remove entries by key
|
|
716
|
+
* const map = new HashMap<string, number>([['x', 10], ['y', 20], ['z', 30]]);
|
|
717
|
+
*
|
|
718
|
+
* console.log(map.delete('y')); // true;
|
|
719
|
+
* console.log(map.has('y')); // false;
|
|
720
|
+
* console.log(map.size); // 2;
|
|
721
|
+
*/
|
|
722
|
+
delete(key) {
|
|
723
|
+
if (this._isObjKey(key)) {
|
|
724
|
+
if (this.objMap.has(key)) this._size--;
|
|
725
|
+
return this.objMap.delete(key);
|
|
726
|
+
}
|
|
727
|
+
const strKey = this._getNoObjKey(key);
|
|
728
|
+
if (strKey in this.store) {
|
|
729
|
+
delete this.store[strKey];
|
|
730
|
+
this._size--;
|
|
731
|
+
return true;
|
|
732
|
+
}
|
|
733
|
+
return false;
|
|
734
|
+
}
|
|
735
|
+
/**
|
|
736
|
+
* Replace the hash function and rehash the non-object store.
|
|
737
|
+
* @remarks Time O(N), Space O(N)
|
|
738
|
+
* @param fn - New hash function for non-object keys.
|
|
739
|
+
* @returns This map instance.
|
|
740
|
+
*/
|
|
741
|
+
setHashFn(fn) {
|
|
742
|
+
if (this._hashFn === fn) return this;
|
|
743
|
+
this._hashFn = fn;
|
|
744
|
+
this._rehashNoObj();
|
|
745
|
+
return this;
|
|
746
|
+
}
|
|
747
|
+
/**
|
|
748
|
+
* Deep clone this map, preserving hashing behavior.
|
|
749
|
+
* @remarks Time O(N), Space O(N)
|
|
750
|
+
* @returns A new map with the same content.
|
|
751
|
+
|
|
752
|
+
|
|
753
|
+
|
|
754
|
+
|
|
755
|
+
|
|
756
|
+
|
|
757
|
+
|
|
758
|
+
|
|
759
|
+
|
|
760
|
+
|
|
761
|
+
|
|
762
|
+
|
|
763
|
+
|
|
764
|
+
|
|
765
|
+
|
|
766
|
+
|
|
767
|
+
|
|
768
|
+
|
|
769
|
+
|
|
770
|
+
|
|
771
|
+
|
|
772
|
+
|
|
773
|
+
|
|
774
|
+
|
|
775
|
+
|
|
776
|
+
|
|
777
|
+
|
|
778
|
+
|
|
779
|
+
|
|
780
|
+
|
|
781
|
+
|
|
782
|
+
|
|
783
|
+
|
|
784
|
+
* @example
|
|
785
|
+
* // Create independent copy
|
|
786
|
+
* const map = new HashMap<string, number>([['a', 1]]);
|
|
787
|
+
* const copy = map.clone();
|
|
788
|
+
* copy.set('a', 99);
|
|
789
|
+
* console.log(map.get('a')); // 1;
|
|
790
|
+
*/
|
|
791
|
+
clone() {
|
|
792
|
+
const opts = { hashFn: this._hashFn, toEntryFn: this._toEntryFn };
|
|
793
|
+
return this._createLike(this, opts);
|
|
794
|
+
}
|
|
795
|
+
/**
|
|
796
|
+
* Map values to a new map with the same keys.
|
|
797
|
+
* @remarks Time O(N), Space O(N)
|
|
798
|
+
* @template VM
|
|
799
|
+
* @param callbackfn - Mapping function (key, value, index, map) → newValue.
|
|
800
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
801
|
+
* @returns A new map with transformed values.
|
|
802
|
+
|
|
803
|
+
|
|
804
|
+
|
|
805
|
+
|
|
806
|
+
|
|
807
|
+
|
|
808
|
+
|
|
809
|
+
|
|
810
|
+
|
|
811
|
+
|
|
812
|
+
|
|
813
|
+
|
|
814
|
+
|
|
815
|
+
|
|
816
|
+
|
|
817
|
+
|
|
818
|
+
|
|
819
|
+
|
|
820
|
+
|
|
821
|
+
|
|
822
|
+
|
|
823
|
+
|
|
824
|
+
|
|
825
|
+
|
|
826
|
+
|
|
827
|
+
|
|
828
|
+
|
|
829
|
+
|
|
830
|
+
|
|
831
|
+
|
|
832
|
+
|
|
833
|
+
|
|
834
|
+
|
|
835
|
+
|
|
836
|
+
|
|
837
|
+
* @example
|
|
838
|
+
* // Transform all values
|
|
839
|
+
* const prices = new HashMap<string, number>([['apple', 1], ['banana', 2]]);
|
|
840
|
+
*
|
|
841
|
+
* const doubled = prices.map(v => (v ?? 0) * 2);
|
|
842
|
+
* console.log(doubled.get('apple')); // 2;
|
|
843
|
+
* console.log(doubled.get('banana')); // 4;
|
|
844
|
+
*/
|
|
845
|
+
map(callbackfn, thisArg) {
|
|
846
|
+
const out = this._createLike();
|
|
847
|
+
let index = 0;
|
|
848
|
+
for (const [key, value] of this) out.set(key, callbackfn.call(thisArg, value, key, index++, this));
|
|
849
|
+
return out;
|
|
850
|
+
}
|
|
851
|
+
/**
|
|
852
|
+
* Filter entries into a new map.
|
|
853
|
+
* @remarks Time O(N), Space O(N)
|
|
854
|
+
* @param predicate - Predicate (key, value, index, map) → boolean.
|
|
855
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
856
|
+
* @returns A new map containing entries that satisfied the predicate.
|
|
857
|
+
|
|
858
|
+
|
|
859
|
+
|
|
860
|
+
|
|
861
|
+
|
|
862
|
+
|
|
863
|
+
|
|
864
|
+
|
|
865
|
+
|
|
866
|
+
|
|
867
|
+
|
|
868
|
+
|
|
869
|
+
|
|
870
|
+
|
|
871
|
+
|
|
872
|
+
|
|
873
|
+
|
|
874
|
+
|
|
875
|
+
|
|
876
|
+
|
|
877
|
+
|
|
878
|
+
|
|
879
|
+
|
|
880
|
+
|
|
881
|
+
|
|
882
|
+
|
|
883
|
+
|
|
884
|
+
|
|
885
|
+
|
|
886
|
+
|
|
887
|
+
|
|
888
|
+
|
|
889
|
+
|
|
890
|
+
|
|
891
|
+
|
|
892
|
+
* @example
|
|
893
|
+
* // HashMap iteration and filter operations
|
|
894
|
+
* const map = new HashMap<number, string>([
|
|
895
|
+
* [1, 'Alice'],
|
|
896
|
+
* [2, 'Bob'],
|
|
897
|
+
* [3, 'Charlie'],
|
|
898
|
+
* [4, 'Diana'],
|
|
899
|
+
* [5, 'Eve']
|
|
900
|
+
* ]);
|
|
901
|
+
*
|
|
902
|
+
* // Iterate through entries
|
|
903
|
+
* const entries: [number, string][] = [];
|
|
904
|
+
* for (const [key, value] of map) {
|
|
905
|
+
* entries.push([key, value]);
|
|
906
|
+
* }
|
|
907
|
+
* console.log(entries); // length: 5;
|
|
908
|
+
*
|
|
909
|
+
* // Filter operation (for iteration with collection methods)
|
|
910
|
+
* const filtered = [...map].filter(([key]) => key > 2);
|
|
911
|
+
* console.log(filtered.length); // 3;
|
|
912
|
+
*
|
|
913
|
+
* // Map operation
|
|
914
|
+
* const values = [...map.values()].map(v => v.length);
|
|
915
|
+
* console.log(values); // contains 3; // 'Bob', 'Eve'
|
|
916
|
+
* console.log(values); // contains 7;
|
|
917
|
+
*/
|
|
918
|
+
filter(predicate, thisArg) {
|
|
919
|
+
const out = this._createLike();
|
|
920
|
+
let index = 0;
|
|
921
|
+
for (const [key, value] of this) if (predicate.call(thisArg, value, key, index++, this)) out.set(key, value);
|
|
922
|
+
return out;
|
|
923
|
+
}
|
|
924
|
+
/**
|
|
925
|
+
* (Protected) Create a like-kind instance and seed it from an iterable.
|
|
926
|
+
* @remarks Time O(N), Space O(N)
|
|
927
|
+
* @template TK
|
|
928
|
+
* @template TV
|
|
929
|
+
* @template TR
|
|
930
|
+
* @param [entries] - Iterable used to seed the new map.
|
|
931
|
+
* @param [options] - Options forwarded to the constructor.
|
|
932
|
+
* @returns A like-kind map instance.
|
|
933
|
+
*/
|
|
934
|
+
_createLike(entries = [], options) {
|
|
935
|
+
const Ctor = this.constructor;
|
|
936
|
+
return new Ctor(entries, options);
|
|
937
|
+
}
|
|
938
|
+
_rehashNoObj() {
|
|
939
|
+
const fresh = {};
|
|
940
|
+
for (const { key, value } of Object.values(this._store)) {
|
|
941
|
+
const sk = this._getNoObjKey(key);
|
|
942
|
+
fresh[sk] = { key, value };
|
|
943
|
+
}
|
|
944
|
+
this._store = fresh;
|
|
945
|
+
}
|
|
946
|
+
*_getIterator() {
|
|
947
|
+
for (const node of Object.values(this.store)) yield [node.key, node.value];
|
|
948
|
+
for (const node of this.objMap) yield node;
|
|
949
|
+
}
|
|
950
|
+
_isObjKey(key) {
|
|
951
|
+
const keyType = typeof key;
|
|
952
|
+
return (keyType === "object" || keyType === "function") && key !== null;
|
|
953
|
+
}
|
|
954
|
+
_getNoObjKey(key) {
|
|
955
|
+
const keyType = typeof key;
|
|
956
|
+
let strKey;
|
|
957
|
+
if (keyType !== "string" && keyType !== "number" && keyType !== "symbol") {
|
|
958
|
+
strKey = this._hashFn(key);
|
|
959
|
+
} else {
|
|
960
|
+
if (keyType === "number") {
|
|
961
|
+
strKey = key;
|
|
962
|
+
} else {
|
|
963
|
+
strKey = key;
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
return strKey;
|
|
967
|
+
}
|
|
968
|
+
};
|
|
969
|
+
var LinkedHashMap = class extends IterableEntryBase {
|
|
970
|
+
static {
|
|
971
|
+
__name(this, "LinkedHashMap");
|
|
972
|
+
}
|
|
973
|
+
_sentinel;
|
|
974
|
+
/**
|
|
975
|
+
* Create a LinkedHashMap and optionally bulk-insert entries.
|
|
976
|
+
* @remarks Time O(N), Space O(N)
|
|
977
|
+
* @param [entryOrRawElements] - Iterable of entries or raw elements to insert.
|
|
978
|
+
* @param [options] - Options: hash functions and optional record-to-entry converter.
|
|
979
|
+
* @returns New LinkedHashMap instance.
|
|
980
|
+
*/
|
|
981
|
+
constructor(entryOrRawElements = [], options) {
|
|
982
|
+
super();
|
|
983
|
+
this._sentinel = {};
|
|
984
|
+
this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
|
|
985
|
+
if (options) {
|
|
986
|
+
const { hashFn, objHashFn, toEntryFn } = options;
|
|
987
|
+
if (hashFn) this._hashFn = hashFn;
|
|
988
|
+
if (objHashFn) this._objHashFn = objHashFn;
|
|
989
|
+
if (toEntryFn) this._toEntryFn = toEntryFn;
|
|
990
|
+
}
|
|
991
|
+
if (entryOrRawElements) this.setMany(entryOrRawElements);
|
|
992
|
+
}
|
|
993
|
+
_hashFn = /* @__PURE__ */ __name((key) => String(key), "_hashFn");
|
|
994
|
+
get hashFn() {
|
|
995
|
+
return this._hashFn;
|
|
996
|
+
}
|
|
997
|
+
_objHashFn = /* @__PURE__ */ __name((key) => key, "_objHashFn");
|
|
998
|
+
/**
|
|
999
|
+
* Get the hash function for object/weak keys.
|
|
1000
|
+
* @remarks Time O(1), Space O(1)
|
|
1001
|
+
* @returns Object-hash function.
|
|
1002
|
+
*/
|
|
1003
|
+
get objHashFn() {
|
|
1004
|
+
return this._objHashFn;
|
|
1005
|
+
}
|
|
1006
|
+
_noObjMap = {};
|
|
1007
|
+
/**
|
|
1008
|
+
* Get the internal record for non-object keys.
|
|
1009
|
+
* @remarks Time O(1), Space O(1)
|
|
1010
|
+
* @returns Record of hash→node.
|
|
1011
|
+
*/
|
|
1012
|
+
get noObjMap() {
|
|
1013
|
+
return this._noObjMap;
|
|
1014
|
+
}
|
|
1015
|
+
_objMap = /* @__PURE__ */ new WeakMap();
|
|
1016
|
+
get objMap() {
|
|
1017
|
+
return this._objMap;
|
|
1018
|
+
}
|
|
1019
|
+
_head;
|
|
1020
|
+
/**
|
|
1021
|
+
* Get the head node (first entry) sentinel link.
|
|
1022
|
+
* @remarks Time O(1), Space O(1)
|
|
1023
|
+
* @returns Head node or sentinel.
|
|
1024
|
+
*/
|
|
1025
|
+
get head() {
|
|
1026
|
+
return this._head;
|
|
1027
|
+
}
|
|
1028
|
+
_tail;
|
|
1029
|
+
/**
|
|
1030
|
+
* Get the tail node (last entry) sentinel link.
|
|
1031
|
+
* @remarks Time O(1), Space O(1)
|
|
1032
|
+
* @returns Tail node or sentinel.
|
|
1033
|
+
*/
|
|
1034
|
+
get tail() {
|
|
1035
|
+
return this._tail;
|
|
1036
|
+
}
|
|
1037
|
+
_toEntryFn = /* @__PURE__ */ __name((rawElement) => {
|
|
1038
|
+
if (this.isEntry(rawElement)) {
|
|
1039
|
+
return rawElement;
|
|
1040
|
+
}
|
|
1041
|
+
raise(TypeError, ERR.invalidArgument("If elements do not adhere to [key, value], provide options.toEntryFn to transform raw records.", "HashMap"));
|
|
1042
|
+
}, "_toEntryFn");
|
|
1043
|
+
get toEntryFn() {
|
|
1044
|
+
return this._toEntryFn;
|
|
1045
|
+
}
|
|
1046
|
+
_size = 0;
|
|
1047
|
+
get size() {
|
|
1048
|
+
return this._size;
|
|
1049
|
+
}
|
|
1050
|
+
/**
|
|
1051
|
+
* Get the first [key, value] pair.
|
|
1052
|
+
* @remarks Time O(1), Space O(1)
|
|
1053
|
+
* @returns First entry or undefined when empty.
|
|
1054
|
+
*/
|
|
1055
|
+
get first() {
|
|
1056
|
+
if (this._size === 0) return;
|
|
1057
|
+
return [this.head.key, this.head.value];
|
|
1058
|
+
}
|
|
1059
|
+
/**
|
|
1060
|
+
* Get the last [key, value] pair.
|
|
1061
|
+
* @remarks Time O(1), Space O(1)
|
|
1062
|
+
* @returns Last entry or undefined when empty.
|
|
1063
|
+
*/
|
|
1064
|
+
get last() {
|
|
1065
|
+
if (this._size === 0) return;
|
|
1066
|
+
return [this.tail.key, this.tail.value];
|
|
1067
|
+
}
|
|
1068
|
+
/**
|
|
1069
|
+
* Iterate from head → tail.
|
|
1070
|
+
* @remarks Time O(N), Space O(1)
|
|
1071
|
+
* @returns Iterator of [key, value].
|
|
1072
|
+
*/
|
|
1073
|
+
*begin() {
|
|
1074
|
+
let node = this.head;
|
|
1075
|
+
while (node !== this._sentinel) {
|
|
1076
|
+
yield [node.key, node.value];
|
|
1077
|
+
node = node.next;
|
|
1078
|
+
}
|
|
1079
|
+
}
|
|
1080
|
+
/**
|
|
1081
|
+
* Iterate from tail → head.
|
|
1082
|
+
* @remarks Time O(N), Space O(1)
|
|
1083
|
+
* @returns Iterator of [key, value].
|
|
1084
|
+
*/
|
|
1085
|
+
*reverseBegin() {
|
|
1086
|
+
let node = this.tail;
|
|
1087
|
+
while (node !== this._sentinel) {
|
|
1088
|
+
yield [node.key, node.value];
|
|
1089
|
+
node = node.prev;
|
|
1090
|
+
}
|
|
1091
|
+
}
|
|
1092
|
+
/**
|
|
1093
|
+
* Insert or replace a single entry; preserves insertion order.
|
|
1094
|
+
* @remarks Time O(1), Space O(1)
|
|
1095
|
+
* @param key - Key.
|
|
1096
|
+
* @param [value] - Value.
|
|
1097
|
+
* @returns True when the operation succeeds.
|
|
1098
|
+
*/
|
|
1099
|
+
set(key, value) {
|
|
1100
|
+
let node;
|
|
1101
|
+
const isNewKey = !this.has(key);
|
|
1102
|
+
if (isWeakKey(key)) {
|
|
1103
|
+
const hash = this._objHashFn(key);
|
|
1104
|
+
node = this.objMap.get(hash);
|
|
1105
|
+
if (!node && isNewKey) {
|
|
1106
|
+
node = { key: hash, value, prev: this.tail, next: this._sentinel };
|
|
1107
|
+
this.objMap.set(hash, node);
|
|
1108
|
+
} else if (node) {
|
|
1109
|
+
node.value = value;
|
|
1110
|
+
}
|
|
1111
|
+
} else {
|
|
1112
|
+
const hash = this._hashFn(key);
|
|
1113
|
+
node = this.noObjMap[hash];
|
|
1114
|
+
if (!node && isNewKey) {
|
|
1115
|
+
this.noObjMap[hash] = node = { key, value, prev: this.tail, next: this._sentinel };
|
|
1116
|
+
} else if (node) {
|
|
1117
|
+
node.value = value;
|
|
1118
|
+
}
|
|
1119
|
+
}
|
|
1120
|
+
if (node && isNewKey) {
|
|
1121
|
+
if (this._size === 0) {
|
|
1122
|
+
this._head = node;
|
|
1123
|
+
this._sentinel.next = node;
|
|
1124
|
+
} else {
|
|
1125
|
+
this.tail.next = node;
|
|
1126
|
+
node.prev = this.tail;
|
|
1127
|
+
}
|
|
1128
|
+
this._tail = node;
|
|
1129
|
+
this._sentinel.prev = node;
|
|
1130
|
+
this._size++;
|
|
1131
|
+
}
|
|
1132
|
+
return true;
|
|
1133
|
+
}
|
|
1134
|
+
setMany(entryOrRawElements) {
|
|
1135
|
+
const results = [];
|
|
1136
|
+
for (const rawEle of entryOrRawElements) {
|
|
1137
|
+
let key, value;
|
|
1138
|
+
if (this.isEntry(rawEle)) [key, value] = rawEle;
|
|
1139
|
+
else if (this._toEntryFn) [key, value] = this._toEntryFn(rawEle);
|
|
1140
|
+
if (key !== void 0 && value !== void 0) results.push(this.set(key, value));
|
|
1141
|
+
}
|
|
1142
|
+
return results;
|
|
1143
|
+
}
|
|
1144
|
+
has(key) {
|
|
1145
|
+
if (isWeakKey(key)) {
|
|
1146
|
+
const hash2 = this._objHashFn(key);
|
|
1147
|
+
return this.objMap.has(hash2);
|
|
1148
|
+
}
|
|
1149
|
+
const hash = this._hashFn(key);
|
|
1150
|
+
return hash in this.noObjMap;
|
|
1151
|
+
}
|
|
1152
|
+
get(key) {
|
|
1153
|
+
if (isWeakKey(key)) {
|
|
1154
|
+
const hash2 = this._objHashFn(key);
|
|
1155
|
+
const node2 = this.objMap.get(hash2);
|
|
1156
|
+
return node2 ? node2.value : void 0;
|
|
1157
|
+
}
|
|
1158
|
+
const hash = this._hashFn(key);
|
|
1159
|
+
const node = this.noObjMap[hash];
|
|
1160
|
+
return node ? node.value : void 0;
|
|
1161
|
+
}
|
|
1162
|
+
/**
|
|
1163
|
+
* Get the value at a given index in insertion order.
|
|
1164
|
+
* @remarks Time O(N), Space O(1)
|
|
1165
|
+
* @param index - Zero-based index.
|
|
1166
|
+
* @returns Value at the index.
|
|
1167
|
+
*/
|
|
1168
|
+
at(index) {
|
|
1169
|
+
rangeCheck(index, 0, this._size - 1);
|
|
1170
|
+
let node = this.head;
|
|
1171
|
+
while (index--) node = node.next;
|
|
1172
|
+
return node.value;
|
|
1173
|
+
}
|
|
1174
|
+
delete(key) {
|
|
1175
|
+
let node;
|
|
1176
|
+
if (isWeakKey(key)) {
|
|
1177
|
+
const hash = this._objHashFn(key);
|
|
1178
|
+
node = this.objMap.get(hash);
|
|
1179
|
+
if (!node) return false;
|
|
1180
|
+
this.objMap.delete(hash);
|
|
1181
|
+
} else {
|
|
1182
|
+
const hash = this._hashFn(key);
|
|
1183
|
+
node = this.noObjMap[hash];
|
|
1184
|
+
if (!node) return false;
|
|
1185
|
+
delete this.noObjMap[hash];
|
|
1186
|
+
}
|
|
1187
|
+
return this._deleteNode(node);
|
|
1188
|
+
}
|
|
1189
|
+
/**
|
|
1190
|
+
* Delete the first entry that matches a predicate.
|
|
1191
|
+
* @remarks Time O(N), Space O(1)
|
|
1192
|
+
* @param predicate - Function (key, value, index, map) → boolean to decide deletion.
|
|
1193
|
+
* @returns True if an entry was removed.
|
|
1194
|
+
*/
|
|
1195
|
+
deleteWhere(predicate) {
|
|
1196
|
+
let node = this._head;
|
|
1197
|
+
let i = 0;
|
|
1198
|
+
while (node !== this._sentinel) {
|
|
1199
|
+
const cur = node;
|
|
1200
|
+
node = node.next;
|
|
1201
|
+
if (predicate(cur.key, cur.value, i++, this)) {
|
|
1202
|
+
const keyToCheck = cur.key;
|
|
1203
|
+
if (isWeakKey(keyToCheck)) {
|
|
1204
|
+
this._objMap.delete(keyToCheck);
|
|
1205
|
+
} else {
|
|
1206
|
+
const hash = this._hashFn(cur.key);
|
|
1207
|
+
delete this._noObjMap[hash];
|
|
1208
|
+
}
|
|
1209
|
+
return this._deleteNode(cur);
|
|
1210
|
+
}
|
|
1211
|
+
}
|
|
1212
|
+
return false;
|
|
1213
|
+
}
|
|
1214
|
+
/**
|
|
1215
|
+
* Delete the entry at a given index.
|
|
1216
|
+
* @remarks Time O(N), Space O(1)
|
|
1217
|
+
* @param index - Zero-based index.
|
|
1218
|
+
* @returns True if removed.
|
|
1219
|
+
*/
|
|
1220
|
+
deleteAt(index) {
|
|
1221
|
+
rangeCheck(index, 0, this._size - 1);
|
|
1222
|
+
let node = this.head;
|
|
1223
|
+
while (index--) node = node.next;
|
|
1224
|
+
return this._deleteNode(node);
|
|
1225
|
+
}
|
|
1226
|
+
isEmpty() {
|
|
1227
|
+
return this._size === 0;
|
|
1228
|
+
}
|
|
1229
|
+
isEntry(rawElement) {
|
|
1230
|
+
return Array.isArray(rawElement) && rawElement.length === 2;
|
|
1231
|
+
}
|
|
1232
|
+
clear() {
|
|
1233
|
+
this._noObjMap = {};
|
|
1234
|
+
this._size = 0;
|
|
1235
|
+
this._head = this._tail = this._sentinel.prev = this._sentinel.next = this._sentinel;
|
|
1236
|
+
}
|
|
1237
|
+
clone() {
|
|
1238
|
+
const opts = { hashFn: this._hashFn, objHashFn: this._objHashFn };
|
|
1239
|
+
return this._createLike(this, opts);
|
|
1240
|
+
}
|
|
1241
|
+
filter(predicate, thisArg) {
|
|
1242
|
+
const out = this._createLike();
|
|
1243
|
+
let index = 0;
|
|
1244
|
+
for (const [key, value] of this) {
|
|
1245
|
+
if (predicate.call(thisArg, value, key, index, this)) out.set(key, value);
|
|
1246
|
+
index++;
|
|
1247
|
+
}
|
|
1248
|
+
return out;
|
|
1249
|
+
}
|
|
1250
|
+
/**
|
|
1251
|
+
* Map each entry to a new [key, value] pair and preserve order.
|
|
1252
|
+
* @remarks Time O(N), Space O(N)
|
|
1253
|
+
* @template MK
|
|
1254
|
+
* @template MV
|
|
1255
|
+
* @param callback - Mapping function (key, value, index, map) → [newKey, newValue].
|
|
1256
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1257
|
+
* @returns A new map of the same class with transformed entries.
|
|
1258
|
+
*/
|
|
1259
|
+
map(callback, thisArg) {
|
|
1260
|
+
const out = this._createLike();
|
|
1261
|
+
let index = 0;
|
|
1262
|
+
for (const [key, value] of this) {
|
|
1263
|
+
const [newKey, newValue] = callback.call(thisArg, value, key, index, this);
|
|
1264
|
+
out.set(newKey, newValue);
|
|
1265
|
+
index++;
|
|
1266
|
+
}
|
|
1267
|
+
return out;
|
|
1268
|
+
}
|
|
1269
|
+
*_getIterator() {
|
|
1270
|
+
let node = this.head;
|
|
1271
|
+
while (node !== this._sentinel) {
|
|
1272
|
+
yield [node.key, node.value];
|
|
1273
|
+
node = node.next;
|
|
1274
|
+
}
|
|
1275
|
+
}
|
|
1276
|
+
_deleteNode(node) {
|
|
1277
|
+
const key = node.key;
|
|
1278
|
+
if (isWeakKey(key)) {
|
|
1279
|
+
this._objMap.delete(key);
|
|
1280
|
+
} else {
|
|
1281
|
+
const hash = this._hashFn(key);
|
|
1282
|
+
delete this._noObjMap[hash];
|
|
1283
|
+
}
|
|
1284
|
+
const { prev, next } = node;
|
|
1285
|
+
prev.next = next;
|
|
1286
|
+
next.prev = prev;
|
|
1287
|
+
if (node === this.head) this._head = next;
|
|
1288
|
+
if (node === this.tail) this._tail = prev;
|
|
1289
|
+
this._size -= 1;
|
|
1290
|
+
return true;
|
|
1291
|
+
}
|
|
1292
|
+
_createLike(entries = [], options) {
|
|
1293
|
+
const Ctor = this.constructor;
|
|
1294
|
+
return new Ctor(entries, options);
|
|
1295
|
+
}
|
|
1296
|
+
};
|
|
1297
|
+
/**
|
|
1298
|
+
* data-structure-typed
|
|
1299
|
+
*
|
|
1300
|
+
* @author Pablo Zeng
|
|
1301
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
1302
|
+
* @license MIT License
|
|
1303
|
+
*/
|
|
1304
|
+
|
|
1305
|
+
export { HashMap, LinkedHashMap };
|
|
1306
|
+
//# sourceMappingURL=hash.mjs.map
|
|
1307
|
+
//# sourceMappingURL=hash.mjs.map
|