tree-set-typed 2.3.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/.eslintrc.js +61 -0
- package/.prettierignore +6 -0
- package/.prettierrc.js +16 -0
- package/LICENSE +21 -0
- package/README.md +482 -0
- package/coverage/clover.xml +13 -0
- package/coverage/coverage-final.json +96 -0
- package/coverage/coverage-summary.json +60 -0
- package/coverage/lcov-report/base.css +403 -0
- package/coverage/lcov-report/block-navigation.js +87 -0
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +119 -0
- package/coverage/lcov-report/index.ts.html +109 -0
- package/coverage/lcov-report/prettify.css +1 -0
- package/coverage/lcov-report/prettify.js +2 -0
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +206 -0
- package/coverage/lcov.info +14 -0
- package/dist/cjs/index.cjs +12 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs-legacy/index.cjs +12 -0
- package/dist/cjs-legacy/index.cjs.map +1 -0
- package/dist/esm/index.mjs +3 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/esm-legacy/index.mjs +3 -0
- package/dist/esm-legacy/index.mjs.map +1 -0
- package/dist/types/common/index.d.ts +12 -0
- package/dist/types/constants/index.d.ts +4 -0
- package/dist/types/data-structures/base/index.d.ts +2 -0
- package/dist/types/data-structures/base/iterable-element-base.d.ts +219 -0
- package/dist/types/data-structures/base/iterable-entry-base.d.ts +150 -0
- package/dist/types/data-structures/base/linear-base.d.ts +335 -0
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +236 -0
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +197 -0
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +440 -0
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +174 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +807 -0
- package/dist/types/data-structures/binary-tree/bst.d.ts +645 -0
- package/dist/types/data-structures/binary-tree/index.d.ts +10 -0
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +312 -0
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +160 -0
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +243 -0
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +333 -0
- package/dist/types/data-structures/graph/abstract-graph.d.ts +340 -0
- package/dist/types/data-structures/graph/directed-graph.d.ts +332 -0
- package/dist/types/data-structures/graph/index.d.ts +4 -0
- package/dist/types/data-structures/graph/map-graph.d.ts +78 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +347 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +428 -0
- package/dist/types/data-structures/hash/index.d.ts +1 -0
- package/dist/types/data-structures/heap/heap.d.ts +552 -0
- package/dist/types/data-structures/heap/index.d.ts +3 -0
- package/dist/types/data-structures/heap/max-heap.d.ts +32 -0
- package/dist/types/data-structures/heap/min-heap.d.ts +33 -0
- package/dist/types/data-structures/index.d.ts +12 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +437 -0
- package/dist/types/data-structures/linked-list/index.d.ts +3 -0
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +567 -0
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +27 -0
- package/dist/types/data-structures/matrix/index.d.ts +2 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +168 -0
- package/dist/types/data-structures/matrix/navigator.d.ts +55 -0
- package/dist/types/data-structures/priority-queue/index.d.ts +3 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +27 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +26 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +15 -0
- package/dist/types/data-structures/queue/deque.d.ts +459 -0
- package/dist/types/data-structures/queue/index.d.ts +2 -0
- package/dist/types/data-structures/queue/queue.d.ts +364 -0
- package/dist/types/data-structures/stack/index.d.ts +1 -0
- package/dist/types/data-structures/stack/stack.d.ts +324 -0
- package/dist/types/data-structures/tree/index.d.ts +1 -0
- package/dist/types/data-structures/tree/tree.d.ts +62 -0
- package/dist/types/data-structures/trie/index.d.ts +1 -0
- package/dist/types/data-structures/trie/trie.d.ts +412 -0
- package/dist/types/index.d.ts +23 -0
- package/dist/types/interfaces/binary-tree.d.ts +60 -0
- package/dist/types/interfaces/doubly-linked-list.d.ts +1 -0
- package/dist/types/interfaces/graph.d.ts +21 -0
- package/dist/types/interfaces/heap.d.ts +1 -0
- package/dist/types/interfaces/index.d.ts +8 -0
- package/dist/types/interfaces/navigator.d.ts +1 -0
- package/dist/types/interfaces/priority-queue.d.ts +1 -0
- package/dist/types/interfaces/segment-tree.d.ts +1 -0
- package/dist/types/interfaces/singly-linked-list.d.ts +1 -0
- package/dist/types/types/common.d.ts +15 -0
- package/dist/types/types/data-structures/base/base.d.ts +13 -0
- package/dist/types/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/avl-tree.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/binary-tree.d.ts +29 -0
- package/dist/types/types/data-structures/binary-tree/bst.d.ts +12 -0
- package/dist/types/types/data-structures/binary-tree/index.d.ts +9 -0
- package/dist/types/types/data-structures/binary-tree/red-black-tree.d.ts +3 -0
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -0
- package/dist/types/types/data-structures/graph/abstract-graph.d.ts +14 -0
- package/dist/types/types/data-structures/graph/directed-graph.d.ts +1 -0
- package/dist/types/types/data-structures/graph/index.d.ts +3 -0
- package/dist/types/types/data-structures/graph/map-graph.d.ts +1 -0
- package/dist/types/types/data-structures/graph/undirected-graph.d.ts +1 -0
- package/dist/types/types/data-structures/hash/hash-map.d.ts +19 -0
- package/dist/types/types/data-structures/hash/index.d.ts +2 -0
- package/dist/types/types/data-structures/heap/heap.d.ts +5 -0
- package/dist/types/types/data-structures/heap/index.d.ts +1 -0
- package/dist/types/types/data-structures/heap/max-heap.d.ts +1 -0
- package/dist/types/types/data-structures/heap/min-heap.d.ts +1 -0
- package/dist/types/types/data-structures/index.d.ts +12 -0
- package/dist/types/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -0
- package/dist/types/types/data-structures/linked-list/index.d.ts +3 -0
- package/dist/types/types/data-structures/linked-list/singly-linked-list.d.ts +2 -0
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +4 -0
- package/dist/types/types/data-structures/matrix/index.d.ts +2 -0
- package/dist/types/types/data-structures/matrix/matrix.d.ts +7 -0
- package/dist/types/types/data-structures/matrix/navigator.d.ts +14 -0
- package/dist/types/types/data-structures/priority-queue/index.d.ts +3 -0
- package/dist/types/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -0
- package/dist/types/types/data-structures/priority-queue/min-priority-queue.d.ts +1 -0
- package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +2 -0
- package/dist/types/types/data-structures/queue/deque.d.ts +4 -0
- package/dist/types/types/data-structures/queue/index.d.ts +2 -0
- package/dist/types/types/data-structures/queue/queue.d.ts +4 -0
- package/dist/types/types/data-structures/stack/index.d.ts +1 -0
- package/dist/types/types/data-structures/stack/stack.d.ts +2 -0
- package/dist/types/types/data-structures/tree/index.d.ts +1 -0
- package/dist/types/types/data-structures/tree/tree.d.ts +1 -0
- package/dist/types/types/data-structures/trie/index.d.ts +1 -0
- package/dist/types/types/data-structures/trie/trie.d.ts +4 -0
- package/dist/types/types/index.d.ts +3 -0
- package/dist/types/types/utils/index.d.ts +2 -0
- package/dist/types/types/utils/utils.d.ts +22 -0
- package/dist/types/types/utils/validate-type.d.ts +19 -0
- package/dist/types/utils/index.d.ts +2 -0
- package/dist/types/utils/number.d.ts +14 -0
- package/dist/types/utils/utils.d.ts +209 -0
- package/dist/umd/red-black-tree-typed.js +14578 -0
- package/dist/umd/red-black-tree-typed.js.map +1 -0
- package/dist/umd/red-black-tree-typed.min.js +44 -0
- package/dist/umd/red-black-tree-typed.min.js.map +1 -0
- package/docs/.nojekyll +1 -0
- package/docs/assets/highlight.css +92 -0
- package/docs/assets/main.js +59 -0
- package/docs/assets/navigation.js +1 -0
- package/docs/assets/search.js +1 -0
- package/docs/assets/style.css +1383 -0
- package/docs/classes/AVLTree.html +2046 -0
- package/docs/classes/AVLTreeNode.html +263 -0
- package/docs/index.html +523 -0
- package/docs/modules.html +45 -0
- package/jest.config.js +8 -0
- package/package.json +113 -0
- package/src/common/index.ts +23 -0
- package/src/constants/index.ts +4 -0
- package/src/data-structures/base/index.ts +2 -0
- package/src/data-structures/base/iterable-element-base.ts +352 -0
- package/src/data-structures/base/iterable-entry-base.ts +246 -0
- package/src/data-structures/base/linear-base.ts +643 -0
- package/src/data-structures/binary-tree/avl-tree-counter.ts +539 -0
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +438 -0
- package/src/data-structures/binary-tree/avl-tree.ts +840 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +331 -0
- package/src/data-structures/binary-tree/binary-tree.ts +2492 -0
- package/src/data-structures/binary-tree/bst.ts +2024 -0
- package/src/data-structures/binary-tree/index.ts +10 -0
- package/src/data-structures/binary-tree/red-black-tree.ts +767 -0
- package/src/data-structures/binary-tree/segment-tree.ts +324 -0
- package/src/data-structures/binary-tree/tree-counter.ts +575 -0
- package/src/data-structures/binary-tree/tree-multi-map.ts +549 -0
- package/src/data-structures/graph/abstract-graph.ts +1081 -0
- package/src/data-structures/graph/directed-graph.ts +715 -0
- package/src/data-structures/graph/index.ts +4 -0
- package/src/data-structures/graph/map-graph.ts +132 -0
- package/src/data-structures/graph/undirected-graph.ts +626 -0
- package/src/data-structures/hash/hash-map.ts +813 -0
- package/src/data-structures/hash/index.ts +1 -0
- package/src/data-structures/heap/heap.ts +1020 -0
- package/src/data-structures/heap/index.ts +3 -0
- package/src/data-structures/heap/max-heap.ts +47 -0
- package/src/data-structures/heap/min-heap.ts +36 -0
- package/src/data-structures/index.ts +12 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +876 -0
- package/src/data-structures/linked-list/index.ts +3 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +1050 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +173 -0
- package/src/data-structures/matrix/index.ts +2 -0
- package/src/data-structures/matrix/matrix.ts +491 -0
- package/src/data-structures/matrix/navigator.ts +124 -0
- package/src/data-structures/priority-queue/index.ts +3 -0
- package/src/data-structures/priority-queue/max-priority-queue.ts +42 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +29 -0
- package/src/data-structures/priority-queue/priority-queue.ts +19 -0
- package/src/data-structures/queue/deque.ts +1001 -0
- package/src/data-structures/queue/index.ts +2 -0
- package/src/data-structures/queue/queue.ts +592 -0
- package/src/data-structures/stack/index.ts +1 -0
- package/src/data-structures/stack/stack.ts +469 -0
- package/src/data-structures/tree/index.ts +1 -0
- package/src/data-structures/tree/tree.ts +115 -0
- package/src/data-structures/trie/index.ts +1 -0
- package/src/data-structures/trie/trie.ts +756 -0
- package/src/index.ts +24 -0
- package/src/interfaces/binary-tree.ts +252 -0
- package/src/interfaces/doubly-linked-list.ts +1 -0
- package/src/interfaces/graph.ts +44 -0
- package/src/interfaces/heap.ts +1 -0
- package/src/interfaces/index.ts +8 -0
- package/src/interfaces/navigator.ts +1 -0
- package/src/interfaces/priority-queue.ts +1 -0
- package/src/interfaces/segment-tree.ts +1 -0
- package/src/interfaces/singly-linked-list.ts +1 -0
- package/src/types/common.ts +25 -0
- package/src/types/data-structures/base/base.ts +34 -0
- package/src/types/data-structures/base/index.ts +1 -0
- package/src/types/data-structures/binary-tree/avl-tree-counter.ts +3 -0
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +3 -0
- package/src/types/data-structures/binary-tree/avl-tree.ts +3 -0
- package/src/types/data-structures/binary-tree/binary-indexed-tree.ts +1 -0
- package/src/types/data-structures/binary-tree/binary-tree.ts +31 -0
- package/src/types/data-structures/binary-tree/bst.ts +19 -0
- package/src/types/data-structures/binary-tree/index.ts +9 -0
- package/src/types/data-structures/binary-tree/red-black-tree.ts +5 -0
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -0
- package/src/types/data-structures/binary-tree/tree-counter.ts +3 -0
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +3 -0
- package/src/types/data-structures/graph/abstract-graph.ts +18 -0
- package/src/types/data-structures/graph/directed-graph.ts +2 -0
- package/src/types/data-structures/graph/index.ts +3 -0
- package/src/types/data-structures/graph/map-graph.ts +1 -0
- package/src/types/data-structures/graph/undirected-graph.ts +1 -0
- package/src/types/data-structures/hash/hash-map.ts +19 -0
- package/src/types/data-structures/hash/index.ts +3 -0
- package/src/types/data-structures/heap/heap.ts +6 -0
- package/src/types/data-structures/heap/index.ts +1 -0
- package/src/types/data-structures/heap/max-heap.ts +1 -0
- package/src/types/data-structures/heap/min-heap.ts +1 -0
- package/src/types/data-structures/index.ts +12 -0
- package/src/types/data-structures/linked-list/doubly-linked-list.ts +3 -0
- package/src/types/data-structures/linked-list/index.ts +3 -0
- package/src/types/data-structures/linked-list/singly-linked-list.ts +3 -0
- package/src/types/data-structures/linked-list/skip-linked-list.ts +1 -0
- package/src/types/data-structures/matrix/index.ts +2 -0
- package/src/types/data-structures/matrix/matrix.ts +7 -0
- package/src/types/data-structures/matrix/navigator.ts +14 -0
- package/src/types/data-structures/priority-queue/index.ts +3 -0
- package/src/types/data-structures/priority-queue/max-priority-queue.ts +1 -0
- package/src/types/data-structures/priority-queue/min-priority-queue.ts +1 -0
- package/src/types/data-structures/priority-queue/priority-queue.ts +3 -0
- package/src/types/data-structures/queue/deque.ts +5 -0
- package/src/types/data-structures/queue/index.ts +2 -0
- package/src/types/data-structures/queue/queue.ts +5 -0
- package/src/types/data-structures/stack/index.ts +1 -0
- package/src/types/data-structures/stack/stack.ts +3 -0
- package/src/types/data-structures/tree/index.ts +1 -0
- package/src/types/data-structures/tree/tree.ts +1 -0
- package/src/types/data-structures/trie/index.ts +1 -0
- package/src/types/data-structures/trie/trie.ts +3 -0
- package/src/types/index.ts +3 -0
- package/src/types/utils/index.ts +2 -0
- package/src/types/utils/utils.ts +33 -0
- package/src/types/utils/validate-type.ts +35 -0
- package/src/utils/index.ts +2 -0
- package/src/utils/number.ts +22 -0
- package/src/utils/utils.ts +350 -0
- package/test/index.test.ts +111 -0
- package/tsconfig.base.json +23 -0
- package/tsconfig.json +12 -0
- package/tsconfig.test.json +8 -0
- package/tsconfig.types.json +15 -0
- package/tsup.config.js +28 -0
- package/tsup.node.config.js +71 -0
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import type { EntryCallback, ReduceEntryCallback } from '../../types';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Iterable view over key-value entries.
|
|
5
|
+
* @template K - Key type.
|
|
6
|
+
* @template V - Value type.
|
|
7
|
+
* @remarks Time O(1), Space O(1)
|
|
8
|
+
*/
|
|
9
|
+
export abstract class IterableEntryBase<K = any, V = any> {
|
|
10
|
+
/**
|
|
11
|
+
* Total number of entries.
|
|
12
|
+
* @returns Entry count.
|
|
13
|
+
* @remarks Time O(1), Space O(1)
|
|
14
|
+
*/
|
|
15
|
+
abstract get size(): number;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Default iterator yielding `[key, value]` entries.
|
|
19
|
+
* @returns Iterator of `[K, V]`.
|
|
20
|
+
* @remarks Time O(n) to iterate, Space O(1)
|
|
21
|
+
*/
|
|
22
|
+
*[Symbol.iterator](...args: any[]): IterableIterator<[K, V]> {
|
|
23
|
+
yield* this._getIterator(...args);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Iterate over `[key, value]` pairs (may yield `undefined` values).
|
|
28
|
+
* @returns Iterator of `[K, V | undefined]`.
|
|
29
|
+
* @remarks Time O(n), Space O(1)
|
|
30
|
+
*/
|
|
31
|
+
*entries(): IterableIterator<[K, V | undefined]> {
|
|
32
|
+
for (const item of this) {
|
|
33
|
+
yield item;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Iterate over keys only.
|
|
39
|
+
* @returns Iterator of keys.
|
|
40
|
+
* @remarks Time O(n), Space O(1)
|
|
41
|
+
*/
|
|
42
|
+
*keys(): IterableIterator<K> {
|
|
43
|
+
for (const item of this) {
|
|
44
|
+
yield item[0];
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Iterate over values only.
|
|
50
|
+
* @returns Iterator of values.
|
|
51
|
+
* @remarks Time O(n), Space O(1)
|
|
52
|
+
*/
|
|
53
|
+
*values(): IterableIterator<V> {
|
|
54
|
+
for (const item of this) {
|
|
55
|
+
yield item[1];
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Test whether all entries satisfy the predicate.
|
|
61
|
+
* @param predicate - `(key, value, index, self) => boolean`.
|
|
62
|
+
* @param thisArg - Optional `this` for callback.
|
|
63
|
+
* @returns `true` if all pass; otherwise `false`.
|
|
64
|
+
* @remarks Time O(n), Space O(1)
|
|
65
|
+
*/
|
|
66
|
+
every(predicate: EntryCallback<K, V, boolean>, thisArg?: any): boolean {
|
|
67
|
+
let index = 0;
|
|
68
|
+
for (const item of this) {
|
|
69
|
+
if (!predicate.call(thisArg, item[1], item[0], index++, this)) {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Test whether any entry satisfies the predicate.
|
|
78
|
+
* @param predicate - `(key, value, index, self) => boolean`.
|
|
79
|
+
* @param thisArg - Optional `this` for callback.
|
|
80
|
+
* @returns `true` if any passes; otherwise `false`.
|
|
81
|
+
* @remarks Time O(n), Space O(1)
|
|
82
|
+
*/
|
|
83
|
+
some(predicate: EntryCallback<K, V, boolean>, thisArg?: any): boolean {
|
|
84
|
+
let index = 0;
|
|
85
|
+
for (const item of this) {
|
|
86
|
+
if (predicate.call(thisArg, item[1], item[0], index++, this)) {
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Visit each entry, left-to-right.
|
|
95
|
+
* @param callbackfn - `(key, value, index, self) => void`.
|
|
96
|
+
* @param thisArg - Optional `this` for callback.
|
|
97
|
+
* @remarks Time O(n), Space O(1)
|
|
98
|
+
*/
|
|
99
|
+
forEach(callbackfn: EntryCallback<K, V, void>, thisArg?: any): void {
|
|
100
|
+
let index = 0;
|
|
101
|
+
for (const item of this) {
|
|
102
|
+
const [key, value] = item;
|
|
103
|
+
callbackfn.call(thisArg, value, key, index++, this);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Find the first entry that matches a predicate.
|
|
109
|
+
* @param callbackfn - `(key, value, index, self) => boolean`.
|
|
110
|
+
* @param thisArg - Optional `this` for callback.
|
|
111
|
+
* @returns Matching `[key, value]` or `undefined`.
|
|
112
|
+
* @remarks Time O(n), Space O(1)
|
|
113
|
+
*/
|
|
114
|
+
find(callbackfn: EntryCallback<K, V, boolean>, thisArg?: any): [K, V] | undefined {
|
|
115
|
+
let index = 0;
|
|
116
|
+
for (const item of this) {
|
|
117
|
+
const [key, value] = item;
|
|
118
|
+
if (callbackfn.call(thisArg, value, key, index++, this)) return item;
|
|
119
|
+
}
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Whether the given key exists.
|
|
125
|
+
* @param key - Key to test.
|
|
126
|
+
* @returns `true` if found; otherwise `false`.
|
|
127
|
+
* @remarks Time O(n) generic, Space O(1)
|
|
128
|
+
*/
|
|
129
|
+
has(key: K): boolean {
|
|
130
|
+
for (const item of this) {
|
|
131
|
+
const [itemKey] = item;
|
|
132
|
+
if (itemKey === key) return true;
|
|
133
|
+
}
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Whether there exists an entry with the given value.
|
|
139
|
+
* @param value - Value to test.
|
|
140
|
+
* @returns `true` if found; otherwise `false`.
|
|
141
|
+
* @remarks Time O(n), Space O(1)
|
|
142
|
+
*/
|
|
143
|
+
hasValue(value: V): boolean {
|
|
144
|
+
for (const [, elementValue] of this) {
|
|
145
|
+
if (elementValue === value) return true;
|
|
146
|
+
}
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Get the value under a key.
|
|
152
|
+
* @param key - Key to look up.
|
|
153
|
+
* @returns Value or `undefined`.
|
|
154
|
+
* @remarks Time O(n) generic, Space O(1)
|
|
155
|
+
*/
|
|
156
|
+
get(key: K): V | undefined {
|
|
157
|
+
for (const item of this) {
|
|
158
|
+
const [itemKey, value] = item;
|
|
159
|
+
if (itemKey === key) return value;
|
|
160
|
+
}
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Reduce entries into a single accumulator.
|
|
166
|
+
* @param callbackfn - `(acc, value, key, index, self) => acc`.
|
|
167
|
+
* @param initialValue - Initial accumulator.
|
|
168
|
+
* @returns Final accumulator.
|
|
169
|
+
* @remarks Time O(n), Space O(1)
|
|
170
|
+
*/
|
|
171
|
+
reduce<U>(callbackfn: ReduceEntryCallback<K, V, U>, initialValue: U): U {
|
|
172
|
+
let accumulator = initialValue;
|
|
173
|
+
let index = 0;
|
|
174
|
+
for (const item of this) {
|
|
175
|
+
const [key, value] = item;
|
|
176
|
+
accumulator = callbackfn(accumulator, value, key, index++, this);
|
|
177
|
+
}
|
|
178
|
+
return accumulator;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Converts data structure to `[key, value]` pairs.
|
|
183
|
+
* @returns Array of entries.
|
|
184
|
+
* @remarks Time O(n), Space O(n)
|
|
185
|
+
*/
|
|
186
|
+
toArray() {
|
|
187
|
+
return [...this];
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Visualize the iterable as an array of `[key, value]` pairs (or a custom string).
|
|
192
|
+
* @returns Array of entries (default) or a string.
|
|
193
|
+
* @remarks Time O(n), Space O(n)
|
|
194
|
+
*/
|
|
195
|
+
toVisual(): [K, V][] | string {
|
|
196
|
+
return [...this];
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Print a human-friendly representation to the console.
|
|
201
|
+
* @remarks Time O(n), Space O(n)
|
|
202
|
+
*/
|
|
203
|
+
print(): void {
|
|
204
|
+
console.log(this.toVisual());
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Whether there are no entries.
|
|
209
|
+
* @returns `true` if empty; `false` otherwise.
|
|
210
|
+
* @remarks Time O(1) typical, Space O(1)
|
|
211
|
+
*/
|
|
212
|
+
abstract isEmpty(): boolean;
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Remove all entries.
|
|
216
|
+
* @remarks Time O(n) typical, Space O(1)
|
|
217
|
+
*/
|
|
218
|
+
abstract clear(): void;
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Deep clone preserving the concrete subtype.
|
|
222
|
+
* @returns A new instance of the same concrete class (`this` type).
|
|
223
|
+
* @remarks Time O(n) typical, Space O(n)
|
|
224
|
+
*/
|
|
225
|
+
abstract clone(): this;
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Map entries using an implementation-specific strategy.
|
|
229
|
+
* @remarks Time O(n), Space O(n)
|
|
230
|
+
*/
|
|
231
|
+
abstract map(...args: any[]): any;
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Filter entries and return the same-species structure.
|
|
235
|
+
* @returns A new instance of the same concrete class (`this` type).
|
|
236
|
+
* @remarks Time O(n), Space O(n)
|
|
237
|
+
*/
|
|
238
|
+
abstract filter(...args: any[]): this;
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Underlying iterator for the default iteration protocol.
|
|
242
|
+
* @returns Iterator of `[K, V]`.
|
|
243
|
+
* @remarks Time O(n), Space O(1)
|
|
244
|
+
*/
|
|
245
|
+
protected abstract _getIterator(...args: any[]): IterableIterator<[K, V]>;
|
|
246
|
+
}
|