directed-graph-typed 2.1.1 → 2.1.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/dist/cjs/index.cjs +2798 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/esm/index.mjs +2789 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/types/data-structures/base/index.d.ts +2 -1
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +182 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +135 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +291 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +174 -1
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +754 -29
- package/dist/types/data-structures/binary-tree/bst.d.ts +413 -12
- package/dist/types/data-structures/binary-tree/index.d.ts +3 -2
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +208 -3
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +160 -1
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +190 -2
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +270 -2
- package/dist/types/data-structures/graph/abstract-graph.d.ts +340 -14
- package/dist/types/data-structures/graph/directed-graph.d.ts +207 -1
- package/dist/types/data-structures/graph/index.d.ts +2 -1
- package/dist/types/data-structures/graph/map-graph.d.ts +78 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +188 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +345 -19
- package/dist/types/data-structures/hash/index.d.ts +0 -1
- package/dist/types/data-structures/heap/heap.d.ts +503 -5
- package/dist/types/data-structures/heap/index.d.ts +2 -0
- package/dist/types/data-structures/heap/max-heap.d.ts +32 -1
- package/dist/types/data-structures/heap/min-heap.d.ts +33 -1
- package/dist/types/data-structures/index.d.ts +7 -7
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +769 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +451 -2
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +27 -4
- package/dist/types/data-structures/matrix/index.d.ts +1 -1
- package/dist/types/data-structures/matrix/matrix.d.ts +168 -7
- package/dist/types/data-structures/matrix/navigator.d.ts +54 -13
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +27 -1
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +26 -1
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +15 -2
- package/dist/types/data-structures/queue/deque.d.ts +431 -4
- package/dist/types/data-structures/queue/queue.d.ts +308 -4
- package/dist/types/data-structures/stack/stack.d.ts +306 -2
- package/dist/types/data-structures/tree/tree.d.ts +62 -1
- package/dist/types/data-structures/trie/trie.d.ts +350 -4
- package/dist/types/index.d.ts +13 -2
- package/dist/{interfaces → types/interfaces}/binary-tree.d.ts +1 -1
- 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/{data-structures → types/types/data-structures}/binary-tree/index.d.ts +2 -3
- 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/{data-structures → types/types/data-structures}/graph/index.d.ts +1 -2
- 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/linked-list/doubly-linked-list.d.ts +2 -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/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/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/queue.d.ts +4 -0
- package/dist/types/types/data-structures/stack/stack.d.ts +2 -0
- package/dist/types/types/data-structures/tree/tree.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/utils/index.d.ts +1 -1
- package/dist/types/utils/utils.d.ts +209 -22
- package/dist/umd/directed-graph-typed.js +2791 -0
- package/dist/umd/directed-graph-typed.js.map +1 -0
- package/dist/umd/directed-graph-typed.min.js +9 -0
- package/dist/umd/directed-graph-typed.min.js.map +1 -0
- package/package.json +25 -5
- package/src/data-structures/binary-tree/avl-tree-counter.ts +4 -4
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +4 -4
- package/src/data-structures/binary-tree/bst.ts +1 -1
- package/src/data-structures/binary-tree/red-black-tree.ts +2 -2
- package/src/data-structures/binary-tree/tree-counter.ts +4 -4
- package/src/data-structures/binary-tree/tree-multi-map.ts +1 -1
- package/src/data-structures/heap/heap.ts +5 -5
- package/src/data-structures/linked-list/singly-linked-list.ts +2 -2
- package/src/interfaces/binary-tree.ts +1 -1
- package/tsconfig.base.json +23 -0
- package/tsconfig.json +8 -34
- package/tsconfig.test.json +8 -0
- package/tsconfig.types.json +15 -0
- package/tsup.config.js +28 -0
- package/tsup.node.config.js +37 -0
- package/dist/common/index.js +0 -28
- package/dist/constants/index.js +0 -8
- package/dist/data-structures/base/index.d.ts +0 -2
- package/dist/data-structures/base/index.js +0 -18
- package/dist/data-structures/base/iterable-element-base.js +0 -243
- package/dist/data-structures/base/iterable-entry-base.js +0 -183
- package/dist/data-structures/base/linear-base.js +0 -415
- package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +0 -182
- package/dist/data-structures/binary-tree/avl-tree-counter.js +0 -374
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -135
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +0 -250
- package/dist/data-structures/binary-tree/avl-tree.d.ts +0 -291
- package/dist/data-structures/binary-tree/avl-tree.js +0 -611
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -174
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +0 -294
- package/dist/data-structures/binary-tree/binary-tree.d.ts +0 -754
- package/dist/data-structures/binary-tree/binary-tree.js +0 -1925
- package/dist/data-structures/binary-tree/bst.d.ts +0 -413
- package/dist/data-structures/binary-tree/bst.js +0 -903
- package/dist/data-structures/binary-tree/index.js +0 -26
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +0 -208
- package/dist/data-structures/binary-tree/red-black-tree.js +0 -546
- package/dist/data-structures/binary-tree/segment-tree.d.ts +0 -160
- package/dist/data-structures/binary-tree/segment-tree.js +0 -297
- package/dist/data-structures/binary-tree/tree-counter.d.ts +0 -190
- package/dist/data-structures/binary-tree/tree-counter.js +0 -413
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +0 -270
- package/dist/data-structures/binary-tree/tree-multi-map.js +0 -384
- package/dist/data-structures/graph/abstract-graph.d.ts +0 -340
- package/dist/data-structures/graph/abstract-graph.js +0 -896
- package/dist/data-structures/graph/directed-graph.d.ts +0 -207
- package/dist/data-structures/graph/directed-graph.js +0 -525
- package/dist/data-structures/graph/index.js +0 -20
- package/dist/data-structures/graph/map-graph.d.ts +0 -78
- package/dist/data-structures/graph/map-graph.js +0 -107
- package/dist/data-structures/graph/undirected-graph.d.ts +0 -188
- package/dist/data-structures/graph/undirected-graph.js +0 -424
- package/dist/data-structures/hash/hash-map.d.ts +0 -345
- package/dist/data-structures/hash/hash-map.js +0 -692
- package/dist/data-structures/hash/index.d.ts +0 -1
- package/dist/data-structures/hash/index.js +0 -17
- package/dist/data-structures/heap/heap.d.ts +0 -503
- package/dist/data-structures/heap/heap.js +0 -901
- package/dist/data-structures/heap/index.d.ts +0 -3
- package/dist/data-structures/heap/index.js +0 -19
- package/dist/data-structures/heap/max-heap.d.ts +0 -32
- package/dist/data-structures/heap/max-heap.js +0 -40
- package/dist/data-structures/heap/min-heap.d.ts +0 -33
- package/dist/data-structures/heap/min-heap.js +0 -31
- package/dist/data-structures/index.js +0 -28
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +0 -769
- package/dist/data-structures/linked-list/doubly-linked-list.js +0 -1111
- package/dist/data-structures/linked-list/index.js +0 -19
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +0 -451
- package/dist/data-structures/linked-list/singly-linked-list.js +0 -850
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -27
- package/dist/data-structures/linked-list/skip-linked-list.js +0 -144
- package/dist/data-structures/matrix/index.js +0 -18
- package/dist/data-structures/matrix/matrix.d.ts +0 -168
- package/dist/data-structures/matrix/matrix.js +0 -448
- package/dist/data-structures/matrix/navigator.d.ts +0 -55
- package/dist/data-structures/matrix/navigator.js +0 -111
- package/dist/data-structures/priority-queue/index.js +0 -19
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +0 -27
- package/dist/data-structures/priority-queue/max-priority-queue.js +0 -34
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +0 -26
- package/dist/data-structures/priority-queue/min-priority-queue.js +0 -24
- package/dist/data-structures/priority-queue/priority-queue.d.ts +0 -15
- package/dist/data-structures/priority-queue/priority-queue.js +0 -20
- package/dist/data-structures/queue/deque.d.ts +0 -431
- package/dist/data-structures/queue/deque.js +0 -879
- package/dist/data-structures/queue/index.js +0 -18
- package/dist/data-structures/queue/queue.d.ts +0 -308
- package/dist/data-structures/queue/queue.js +0 -473
- package/dist/data-structures/stack/index.js +0 -17
- package/dist/data-structures/stack/stack.d.ts +0 -306
- package/dist/data-structures/stack/stack.js +0 -401
- package/dist/data-structures/tree/index.js +0 -17
- package/dist/data-structures/tree/tree.d.ts +0 -62
- package/dist/data-structures/tree/tree.js +0 -107
- package/dist/data-structures/trie/index.js +0 -17
- package/dist/data-structures/trie/trie.d.ts +0 -350
- package/dist/data-structures/trie/trie.js +0 -610
- package/dist/index.d.ts +0 -14
- package/dist/index.js +0 -30
- package/dist/interfaces/binary-tree.js +0 -2
- package/dist/interfaces/doubly-linked-list.js +0 -2
- package/dist/interfaces/graph.js +0 -2
- package/dist/interfaces/heap.js +0 -2
- package/dist/interfaces/index.js +0 -24
- package/dist/interfaces/navigator.js +0 -2
- package/dist/interfaces/priority-queue.js +0 -2
- package/dist/interfaces/segment-tree.js +0 -2
- package/dist/interfaces/singly-linked-list.js +0 -2
- package/dist/types/common.js +0 -2
- package/dist/types/data-structures/base/base.js +0 -2
- package/dist/types/data-structures/base/index.js +0 -17
- package/dist/types/data-structures/binary-tree/avl-tree-counter.js +0 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.js +0 -2
- package/dist/types/data-structures/binary-tree/avl-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/binary-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/bst.js +0 -2
- package/dist/types/data-structures/binary-tree/index.js +0 -25
- package/dist/types/data-structures/binary-tree/red-black-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/segment-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/tree-counter.js +0 -2
- package/dist/types/data-structures/binary-tree/tree-multi-map.js +0 -2
- package/dist/types/data-structures/graph/abstract-graph.js +0 -2
- package/dist/types/data-structures/graph/directed-graph.js +0 -2
- package/dist/types/data-structures/graph/index.js +0 -19
- package/dist/types/data-structures/graph/map-graph.js +0 -2
- package/dist/types/data-structures/graph/undirected-graph.js +0 -2
- package/dist/types/data-structures/hash/hash-map.js +0 -2
- package/dist/types/data-structures/hash/index.js +0 -17
- package/dist/types/data-structures/heap/heap.js +0 -2
- package/dist/types/data-structures/heap/index.js +0 -17
- package/dist/types/data-structures/heap/max-heap.js +0 -2
- package/dist/types/data-structures/heap/min-heap.js +0 -2
- package/dist/types/data-structures/index.js +0 -28
- package/dist/types/data-structures/linked-list/doubly-linked-list.js +0 -2
- package/dist/types/data-structures/linked-list/index.js +0 -19
- package/dist/types/data-structures/linked-list/singly-linked-list.js +0 -2
- package/dist/types/data-structures/linked-list/skip-linked-list.js +0 -2
- package/dist/types/data-structures/matrix/index.js +0 -18
- package/dist/types/data-structures/matrix/matrix.js +0 -2
- package/dist/types/data-structures/matrix/navigator.js +0 -2
- package/dist/types/data-structures/priority-queue/index.js +0 -19
- package/dist/types/data-structures/priority-queue/max-priority-queue.js +0 -2
- package/dist/types/data-structures/priority-queue/min-priority-queue.js +0 -2
- package/dist/types/data-structures/priority-queue/priority-queue.js +0 -2
- package/dist/types/data-structures/queue/deque.js +0 -2
- package/dist/types/data-structures/queue/index.js +0 -18
- package/dist/types/data-structures/queue/queue.js +0 -2
- package/dist/types/data-structures/stack/index.js +0 -17
- package/dist/types/data-structures/stack/stack.js +0 -2
- package/dist/types/data-structures/tree/index.js +0 -17
- package/dist/types/data-structures/tree/tree.js +0 -2
- package/dist/types/data-structures/trie/index.js +0 -17
- package/dist/types/data-structures/trie/trie.js +0 -2
- package/dist/types/index.js +0 -19
- package/dist/types/utils/index.js +0 -18
- package/dist/types/utils/utils.js +0 -2
- package/dist/types/utils/validate-type.js +0 -2
- package/dist/utils/index.d.ts +0 -2
- package/dist/utils/index.js +0 -18
- package/dist/utils/number.js +0 -24
- package/dist/utils/utils.d.ts +0 -209
- package/dist/utils/utils.js +0 -353
- package/dist/{common → types/common}/index.d.ts +0 -0
- package/dist/{constants → types/constants}/index.d.ts +0 -0
- package/dist/{data-structures → types/data-structures}/base/iterable-element-base.d.ts +0 -0
- package/dist/{data-structures → types/data-structures}/base/iterable-entry-base.d.ts +0 -0
- package/dist/{data-structures → types/data-structures}/base/linear-base.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/doubly-linked-list.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/graph.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/heap.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/index.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/navigator.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/priority-queue.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/segment-tree.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/singly-linked-list.d.ts +0 -0
- package/dist/types/{common.d.ts → types/common.d.ts} +0 -0
- package/dist/types/{data-structures → types/data-structures}/base/base.d.ts +0 -0
- package/dist/{data-structures → types/types/data-structures}/index.d.ts +7 -7
- package/dist/{data-structures → types/types/data-structures}/linked-list/index.d.ts +0 -0
- package/dist/{data-structures → types/types/data-structures}/matrix/index.d.ts +1 -1
- /package/dist/{data-structures → types/types/data-structures}/priority-queue/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/queue/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/stack/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/tree/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/trie/index.d.ts +0 -0
- /package/dist/types/{utils → types/utils}/validate-type.d.ts +0 -0
- /package/dist/{utils → types/utils}/number.d.ts +0 -0
|
@@ -1,692 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* data-structure-typed
|
|
4
|
-
*
|
|
5
|
-
* @author Pablo Zeng
|
|
6
|
-
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
7
|
-
* @license MIT License
|
|
8
|
-
*/
|
|
9
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
-
exports.LinkedHashMap = exports.HashMap = void 0;
|
|
11
|
-
const base_1 = require("../base");
|
|
12
|
-
const utils_1 = require("../../utils");
|
|
13
|
-
/**
|
|
14
|
-
* Hash-based map. Supports object keys and custom hashing; offers O(1) average set/get/has.
|
|
15
|
-
* @remarks Time O(1), Space O(1)
|
|
16
|
-
* @template K
|
|
17
|
-
* @template V
|
|
18
|
-
* @template R
|
|
19
|
-
* 1. Key-Value Pair Storage: HashMap stores key-value pairs. Each key map to a value.
|
|
20
|
-
* 2. Fast Lookup: It's used when you need to quickly find, insert, or delete entries based on a key.
|
|
21
|
-
* 3. Unique Keys: Keys are unique.
|
|
22
|
-
* If you try to insert another entry with the same key, the new one will replace the old entry.
|
|
23
|
-
* 4. Unordered Collection: HashMap does not guarantee the order of entries, and the order may change over time.
|
|
24
|
-
* @example
|
|
25
|
-
* // should maintain insertion order
|
|
26
|
-
* const linkedHashMap = new LinkedHashMap<number, string>();
|
|
27
|
-
* linkedHashMap.set(1, 'A');
|
|
28
|
-
* linkedHashMap.set(2, 'B');
|
|
29
|
-
* linkedHashMap.set(3, 'C');
|
|
30
|
-
*
|
|
31
|
-
* const result = Array.from(linkedHashMap);
|
|
32
|
-
* console.log(result); // [
|
|
33
|
-
* // [1, 'A'],
|
|
34
|
-
* // [2, 'B'],
|
|
35
|
-
* // [3, 'C']
|
|
36
|
-
* // ]
|
|
37
|
-
* @example
|
|
38
|
-
* // fast lookup of values by key
|
|
39
|
-
* const hashMap = new HashMap<number, string>();
|
|
40
|
-
* hashMap.set(1, 'A');
|
|
41
|
-
* hashMap.set(2, 'B');
|
|
42
|
-
* hashMap.set(3, 'C');
|
|
43
|
-
*
|
|
44
|
-
* console.log(hashMap.get(1)); // 'A'
|
|
45
|
-
* console.log(hashMap.get(2)); // 'B'
|
|
46
|
-
* console.log(hashMap.get(3)); // 'C'
|
|
47
|
-
* console.log(hashMap.get(99)); // undefined
|
|
48
|
-
* @example
|
|
49
|
-
* // remove duplicates when adding multiple entries
|
|
50
|
-
* const hashMap = new HashMap<number, string>();
|
|
51
|
-
* hashMap.set(1, 'A');
|
|
52
|
-
* hashMap.set(2, 'B');
|
|
53
|
-
* hashMap.set(1, 'C'); // Update value for key 1
|
|
54
|
-
*
|
|
55
|
-
* console.log(hashMap.size); // 2
|
|
56
|
-
* console.log(hashMap.get(1)); // 'C'
|
|
57
|
-
* console.log(hashMap.get(2)); // 'B'
|
|
58
|
-
* @example
|
|
59
|
-
* // count occurrences of keys
|
|
60
|
-
* const data = [1, 2, 1, 3, 2, 1];
|
|
61
|
-
*
|
|
62
|
-
* const countMap = new HashMap<number, number>();
|
|
63
|
-
* for (const key of data) {
|
|
64
|
-
* countMap.set(key, (countMap.get(key) || 0) + 1);
|
|
65
|
-
* }
|
|
66
|
-
*
|
|
67
|
-
* console.log(countMap.get(1)); // 3
|
|
68
|
-
* console.log(countMap.get(2)); // 2
|
|
69
|
-
* console.log(countMap.get(3)); // 1
|
|
70
|
-
*/
|
|
71
|
-
class HashMap extends base_1.IterableEntryBase {
|
|
72
|
-
/**
|
|
73
|
-
* Create a HashMap and optionally bulk-insert entries.
|
|
74
|
-
* @remarks Time O(N), Space O(N)
|
|
75
|
-
* @param [entryOrRawElements] - Iterable of entries or raw elements to insert.
|
|
76
|
-
* @param [options] - Options: hash function and optional record-to-entry converter.
|
|
77
|
-
* @returns New HashMap instance.
|
|
78
|
-
*/
|
|
79
|
-
constructor(entryOrRawElements = [], options) {
|
|
80
|
-
super();
|
|
81
|
-
this._store = {};
|
|
82
|
-
this._objMap = new Map();
|
|
83
|
-
this._size = 0;
|
|
84
|
-
this._hashFn = (key) => String(key);
|
|
85
|
-
if (options) {
|
|
86
|
-
const { hashFn, toEntryFn } = options;
|
|
87
|
-
if (hashFn)
|
|
88
|
-
this._hashFn = hashFn;
|
|
89
|
-
if (toEntryFn)
|
|
90
|
-
this._toEntryFn = toEntryFn;
|
|
91
|
-
}
|
|
92
|
-
if (entryOrRawElements)
|
|
93
|
-
this.setMany(entryOrRawElements);
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Get the internal store for non-object keys.
|
|
97
|
-
* @remarks Time O(1), Space O(1)
|
|
98
|
-
* @returns Internal record of string→{key,value}.
|
|
99
|
-
*/
|
|
100
|
-
get store() {
|
|
101
|
-
return this._store;
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* Get the internal Map used for object/function keys.
|
|
105
|
-
* @remarks Time O(1), Space O(1)
|
|
106
|
-
* @returns Map of object→value.
|
|
107
|
-
*/
|
|
108
|
-
get objMap() {
|
|
109
|
-
return this._objMap;
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* Get the raw→entry converter function if present.
|
|
113
|
-
* @remarks Time O(1), Space O(1)
|
|
114
|
-
* @returns Converter function or undefined.
|
|
115
|
-
*/
|
|
116
|
-
get toEntryFn() {
|
|
117
|
-
return this._toEntryFn;
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
* Get the number of distinct keys stored.
|
|
121
|
-
* @remarks Time O(1), Space O(1)
|
|
122
|
-
* @returns Current size.
|
|
123
|
-
*/
|
|
124
|
-
get size() {
|
|
125
|
-
return this._size;
|
|
126
|
-
}
|
|
127
|
-
/**
|
|
128
|
-
* Get the current hash function for non-object keys.
|
|
129
|
-
* @remarks Time O(1), Space O(1)
|
|
130
|
-
* @returns Hash function.
|
|
131
|
-
*/
|
|
132
|
-
get hashFn() {
|
|
133
|
-
return this._hashFn;
|
|
134
|
-
}
|
|
135
|
-
/**
|
|
136
|
-
* Check whether the map is empty.
|
|
137
|
-
* @remarks Time O(1), Space O(1)
|
|
138
|
-
* @returns True if size is 0.
|
|
139
|
-
*/
|
|
140
|
-
isEmpty() {
|
|
141
|
-
return this._size === 0;
|
|
142
|
-
}
|
|
143
|
-
/**
|
|
144
|
-
* Remove all entries and reset counters.
|
|
145
|
-
* @remarks Time O(N), Space O(1)
|
|
146
|
-
* @returns void
|
|
147
|
-
*/
|
|
148
|
-
clear() {
|
|
149
|
-
this._store = {};
|
|
150
|
-
this._objMap.clear();
|
|
151
|
-
this._size = 0;
|
|
152
|
-
}
|
|
153
|
-
/**
|
|
154
|
-
* Type guard: check if a raw value is a [key, value] entry.
|
|
155
|
-
* @remarks Time O(1), Space O(1)
|
|
156
|
-
* @returns True if the value is a 2-tuple.
|
|
157
|
-
*/
|
|
158
|
-
isEntry(rawElement) {
|
|
159
|
-
return Array.isArray(rawElement) && rawElement.length === 2;
|
|
160
|
-
}
|
|
161
|
-
/**
|
|
162
|
-
* Insert or replace a single entry.
|
|
163
|
-
* @remarks Time O(1), Space O(1)
|
|
164
|
-
* @param key - Key.
|
|
165
|
-
* @param value - Value.
|
|
166
|
-
* @returns True when the operation succeeds.
|
|
167
|
-
*/
|
|
168
|
-
set(key, value) {
|
|
169
|
-
if (this._isObjKey(key)) {
|
|
170
|
-
if (!this.objMap.has(key))
|
|
171
|
-
this._size++;
|
|
172
|
-
this.objMap.set(key, value);
|
|
173
|
-
}
|
|
174
|
-
else {
|
|
175
|
-
const strKey = this._getNoObjKey(key);
|
|
176
|
-
if (this.store[strKey] === undefined)
|
|
177
|
-
this._size++;
|
|
178
|
-
this._store[strKey] = { key, value };
|
|
179
|
-
}
|
|
180
|
-
return true;
|
|
181
|
-
}
|
|
182
|
-
/**
|
|
183
|
-
* Insert many entries from an iterable.
|
|
184
|
-
* @remarks Time O(N), Space O(N)
|
|
185
|
-
* @param entryOrRawElements - Iterable of entries or raw elements to insert.
|
|
186
|
-
* @returns Array of per-entry results.
|
|
187
|
-
*/
|
|
188
|
-
setMany(entryOrRawElements) {
|
|
189
|
-
const results = [];
|
|
190
|
-
for (const rawEle of entryOrRawElements) {
|
|
191
|
-
let key, value;
|
|
192
|
-
if (this.isEntry(rawEle))
|
|
193
|
-
[key, value] = rawEle;
|
|
194
|
-
else if (this._toEntryFn)
|
|
195
|
-
[key, value] = this._toEntryFn(rawEle);
|
|
196
|
-
if (key !== undefined && value !== undefined)
|
|
197
|
-
results.push(this.set(key, value));
|
|
198
|
-
}
|
|
199
|
-
return results;
|
|
200
|
-
}
|
|
201
|
-
/**
|
|
202
|
-
* Get the value for a key.
|
|
203
|
-
* @remarks Time O(1), Space O(1)
|
|
204
|
-
* @param key - Key to look up.
|
|
205
|
-
* @returns Value or undefined.
|
|
206
|
-
*/
|
|
207
|
-
get(key) {
|
|
208
|
-
var _a;
|
|
209
|
-
if (this._isObjKey(key))
|
|
210
|
-
return this.objMap.get(key);
|
|
211
|
-
const strKey = this._getNoObjKey(key);
|
|
212
|
-
return (_a = this._store[strKey]) === null || _a === void 0 ? void 0 : _a.value;
|
|
213
|
-
}
|
|
214
|
-
/**
|
|
215
|
-
* Check if a key exists.
|
|
216
|
-
* @remarks Time O(1), Space O(1)
|
|
217
|
-
* @param key - Key to test.
|
|
218
|
-
* @returns True if present.
|
|
219
|
-
*/
|
|
220
|
-
has(key) {
|
|
221
|
-
if (this._isObjKey(key))
|
|
222
|
-
return this.objMap.has(key);
|
|
223
|
-
const strKey = this._getNoObjKey(key);
|
|
224
|
-
return strKey in this.store;
|
|
225
|
-
}
|
|
226
|
-
/**
|
|
227
|
-
* Delete an entry by key.
|
|
228
|
-
* @remarks Time O(1), Space O(1)
|
|
229
|
-
* @param key - Key to delete.
|
|
230
|
-
* @returns True if the key was found and removed.
|
|
231
|
-
*/
|
|
232
|
-
delete(key) {
|
|
233
|
-
if (this._isObjKey(key)) {
|
|
234
|
-
if (this.objMap.has(key))
|
|
235
|
-
this._size--;
|
|
236
|
-
return this.objMap.delete(key);
|
|
237
|
-
}
|
|
238
|
-
const strKey = this._getNoObjKey(key);
|
|
239
|
-
if (strKey in this.store) {
|
|
240
|
-
delete this.store[strKey];
|
|
241
|
-
this._size--;
|
|
242
|
-
return true;
|
|
243
|
-
}
|
|
244
|
-
return false;
|
|
245
|
-
}
|
|
246
|
-
/**
|
|
247
|
-
* Replace the hash function and rehash the non-object store.
|
|
248
|
-
* @remarks Time O(N), Space O(N)
|
|
249
|
-
* @param fn - New hash function for non-object keys.
|
|
250
|
-
* @returns This map instance.
|
|
251
|
-
*/
|
|
252
|
-
setHashFn(fn) {
|
|
253
|
-
if (this._hashFn === fn)
|
|
254
|
-
return this;
|
|
255
|
-
this._hashFn = fn;
|
|
256
|
-
this._rehashNoObj();
|
|
257
|
-
return this;
|
|
258
|
-
}
|
|
259
|
-
/**
|
|
260
|
-
* Deep clone this map, preserving hashing behavior.
|
|
261
|
-
* @remarks Time O(N), Space O(N)
|
|
262
|
-
* @returns A new map with the same content.
|
|
263
|
-
*/
|
|
264
|
-
clone() {
|
|
265
|
-
const opts = { hashFn: this._hashFn, toEntryFn: this._toEntryFn };
|
|
266
|
-
return this._createLike(this, opts);
|
|
267
|
-
}
|
|
268
|
-
/**
|
|
269
|
-
* Map values to a new map with the same keys.
|
|
270
|
-
* @remarks Time O(N), Space O(N)
|
|
271
|
-
* @template VM
|
|
272
|
-
* @param callbackfn - Mapping function (key, value, index, map) → newValue.
|
|
273
|
-
* @param [thisArg] - Value for `this` inside the callback.
|
|
274
|
-
* @returns A new map with transformed values.
|
|
275
|
-
*/
|
|
276
|
-
map(callbackfn, thisArg) {
|
|
277
|
-
const out = this._createLike();
|
|
278
|
-
let index = 0;
|
|
279
|
-
for (const [key, value] of this)
|
|
280
|
-
out.set(key, callbackfn.call(thisArg, key, value, index++, this));
|
|
281
|
-
return out;
|
|
282
|
-
}
|
|
283
|
-
/**
|
|
284
|
-
* Filter entries into a new map.
|
|
285
|
-
* @remarks Time O(N), Space O(N)
|
|
286
|
-
* @param predicate - Predicate (key, value, index, map) → boolean.
|
|
287
|
-
* @param [thisArg] - Value for `this` inside the predicate.
|
|
288
|
-
* @returns A new map containing entries that satisfied the predicate.
|
|
289
|
-
*/
|
|
290
|
-
filter(predicate, thisArg) {
|
|
291
|
-
const out = this._createLike();
|
|
292
|
-
let index = 0;
|
|
293
|
-
for (const [key, value] of this)
|
|
294
|
-
if (predicate.call(thisArg, key, value, index++, this))
|
|
295
|
-
out.set(key, value);
|
|
296
|
-
return out;
|
|
297
|
-
}
|
|
298
|
-
/**
|
|
299
|
-
* (Protected) Create a like-kind instance and seed it from an iterable.
|
|
300
|
-
* @remarks Time O(N), Space O(N)
|
|
301
|
-
* @template TK
|
|
302
|
-
* @template TV
|
|
303
|
-
* @template TR
|
|
304
|
-
* @param [entries] - Iterable used to seed the new map.
|
|
305
|
-
* @param [options] - Options forwarded to the constructor.
|
|
306
|
-
* @returns A like-kind map instance.
|
|
307
|
-
*/
|
|
308
|
-
_createLike(entries = [], options) {
|
|
309
|
-
const Ctor = this.constructor;
|
|
310
|
-
return new Ctor(entries, options);
|
|
311
|
-
}
|
|
312
|
-
_rehashNoObj() {
|
|
313
|
-
const fresh = {};
|
|
314
|
-
for (const { key, value } of Object.values(this._store)) {
|
|
315
|
-
const sk = this._getNoObjKey(key);
|
|
316
|
-
fresh[sk] = { key, value };
|
|
317
|
-
}
|
|
318
|
-
this._store = fresh;
|
|
319
|
-
}
|
|
320
|
-
*_getIterator() {
|
|
321
|
-
for (const node of Object.values(this.store))
|
|
322
|
-
yield [node.key, node.value];
|
|
323
|
-
for (const node of this.objMap)
|
|
324
|
-
yield node;
|
|
325
|
-
}
|
|
326
|
-
_isObjKey(key) {
|
|
327
|
-
const keyType = typeof key;
|
|
328
|
-
return (keyType === 'object' || keyType === 'function') && key !== null;
|
|
329
|
-
}
|
|
330
|
-
_getNoObjKey(key) {
|
|
331
|
-
const keyType = typeof key;
|
|
332
|
-
let strKey;
|
|
333
|
-
if (keyType !== 'string' && keyType !== 'number' && keyType !== 'symbol') {
|
|
334
|
-
strKey = this._hashFn(key);
|
|
335
|
-
}
|
|
336
|
-
else {
|
|
337
|
-
if (keyType === 'number') {
|
|
338
|
-
strKey = key;
|
|
339
|
-
}
|
|
340
|
-
else {
|
|
341
|
-
strKey = key;
|
|
342
|
-
}
|
|
343
|
-
}
|
|
344
|
-
return strKey;
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
exports.HashMap = HashMap;
|
|
348
|
-
/**
|
|
349
|
-
* Hash-based map that preserves insertion order via a doubly-linked list.
|
|
350
|
-
* @remarks Time O(1), Space O(1)
|
|
351
|
-
* @template K
|
|
352
|
-
* @template V
|
|
353
|
-
* @template R
|
|
354
|
-
* @example examples will be generated by unit test
|
|
355
|
-
*/
|
|
356
|
-
class LinkedHashMap extends base_1.IterableEntryBase {
|
|
357
|
-
/**
|
|
358
|
-
* Create a LinkedHashMap and optionally bulk-insert entries.
|
|
359
|
-
* @remarks Time O(N), Space O(N)
|
|
360
|
-
* @param [entryOrRawElements] - Iterable of entries or raw elements to insert.
|
|
361
|
-
* @param [options] - Options: hash functions and optional record-to-entry converter.
|
|
362
|
-
* @returns New LinkedHashMap instance.
|
|
363
|
-
*/
|
|
364
|
-
constructor(entryOrRawElements = [], options) {
|
|
365
|
-
super();
|
|
366
|
-
this._hashFn = (key) => String(key);
|
|
367
|
-
this._objHashFn = (key) => key;
|
|
368
|
-
this._noObjMap = {};
|
|
369
|
-
this._objMap = new WeakMap();
|
|
370
|
-
this._toEntryFn = (rawElement) => {
|
|
371
|
-
if (this.isEntry(rawElement)) {
|
|
372
|
-
return rawElement;
|
|
373
|
-
}
|
|
374
|
-
throw new Error('If `entryOrRawElements` does not adhere to [key,value], provide `options.toEntryFn` to transform raw records.');
|
|
375
|
-
};
|
|
376
|
-
this._size = 0;
|
|
377
|
-
this._sentinel = {};
|
|
378
|
-
this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
|
|
379
|
-
if (options) {
|
|
380
|
-
const { hashFn, objHashFn, toEntryFn } = options;
|
|
381
|
-
if (hashFn)
|
|
382
|
-
this._hashFn = hashFn;
|
|
383
|
-
if (objHashFn)
|
|
384
|
-
this._objHashFn = objHashFn;
|
|
385
|
-
if (toEntryFn)
|
|
386
|
-
this._toEntryFn = toEntryFn;
|
|
387
|
-
}
|
|
388
|
-
if (entryOrRawElements)
|
|
389
|
-
this.setMany(entryOrRawElements);
|
|
390
|
-
}
|
|
391
|
-
get hashFn() {
|
|
392
|
-
return this._hashFn;
|
|
393
|
-
}
|
|
394
|
-
/**
|
|
395
|
-
* Get the hash function for object/weak keys.
|
|
396
|
-
* @remarks Time O(1), Space O(1)
|
|
397
|
-
* @returns Object-hash function.
|
|
398
|
-
*/
|
|
399
|
-
get objHashFn() {
|
|
400
|
-
return this._objHashFn;
|
|
401
|
-
}
|
|
402
|
-
/**
|
|
403
|
-
* Get the internal record for non-object keys.
|
|
404
|
-
* @remarks Time O(1), Space O(1)
|
|
405
|
-
* @returns Record of hash→node.
|
|
406
|
-
*/
|
|
407
|
-
get noObjMap() {
|
|
408
|
-
return this._noObjMap;
|
|
409
|
-
}
|
|
410
|
-
get objMap() {
|
|
411
|
-
return this._objMap;
|
|
412
|
-
}
|
|
413
|
-
/**
|
|
414
|
-
* Get the head node (first entry) sentinel link.
|
|
415
|
-
* @remarks Time O(1), Space O(1)
|
|
416
|
-
* @returns Head node or sentinel.
|
|
417
|
-
*/
|
|
418
|
-
get head() {
|
|
419
|
-
return this._head;
|
|
420
|
-
}
|
|
421
|
-
/**
|
|
422
|
-
* Get the tail node (last entry) sentinel link.
|
|
423
|
-
* @remarks Time O(1), Space O(1)
|
|
424
|
-
* @returns Tail node or sentinel.
|
|
425
|
-
*/
|
|
426
|
-
get tail() {
|
|
427
|
-
return this._tail;
|
|
428
|
-
}
|
|
429
|
-
get toEntryFn() {
|
|
430
|
-
return this._toEntryFn;
|
|
431
|
-
}
|
|
432
|
-
get size() {
|
|
433
|
-
return this._size;
|
|
434
|
-
}
|
|
435
|
-
/**
|
|
436
|
-
* Get the first [key, value] pair.
|
|
437
|
-
* @remarks Time O(1), Space O(1)
|
|
438
|
-
* @returns First entry or undefined when empty.
|
|
439
|
-
*/
|
|
440
|
-
get first() {
|
|
441
|
-
if (this._size === 0)
|
|
442
|
-
return;
|
|
443
|
-
return [this.head.key, this.head.value];
|
|
444
|
-
}
|
|
445
|
-
/**
|
|
446
|
-
* Get the last [key, value] pair.
|
|
447
|
-
* @remarks Time O(1), Space O(1)
|
|
448
|
-
* @returns Last entry or undefined when empty.
|
|
449
|
-
*/
|
|
450
|
-
get last() {
|
|
451
|
-
if (this._size === 0)
|
|
452
|
-
return;
|
|
453
|
-
return [this.tail.key, this.tail.value];
|
|
454
|
-
}
|
|
455
|
-
/**
|
|
456
|
-
* Iterate from head → tail.
|
|
457
|
-
* @remarks Time O(N), Space O(1)
|
|
458
|
-
* @returns Iterator of [key, value].
|
|
459
|
-
*/
|
|
460
|
-
*begin() {
|
|
461
|
-
let node = this.head;
|
|
462
|
-
while (node !== this._sentinel) {
|
|
463
|
-
yield [node.key, node.value];
|
|
464
|
-
node = node.next;
|
|
465
|
-
}
|
|
466
|
-
}
|
|
467
|
-
/**
|
|
468
|
-
* Iterate from tail → head.
|
|
469
|
-
* @remarks Time O(N), Space O(1)
|
|
470
|
-
* @returns Iterator of [key, value].
|
|
471
|
-
*/
|
|
472
|
-
*reverseBegin() {
|
|
473
|
-
let node = this.tail;
|
|
474
|
-
while (node !== this._sentinel) {
|
|
475
|
-
yield [node.key, node.value];
|
|
476
|
-
node = node.prev;
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
|
-
/**
|
|
480
|
-
* Insert or replace a single entry; preserves insertion order.
|
|
481
|
-
* @remarks Time O(1), Space O(1)
|
|
482
|
-
* @param key - Key.
|
|
483
|
-
* @param [value] - Value.
|
|
484
|
-
* @returns True when the operation succeeds.
|
|
485
|
-
*/
|
|
486
|
-
set(key, value) {
|
|
487
|
-
let node;
|
|
488
|
-
const isNewKey = !this.has(key);
|
|
489
|
-
if ((0, utils_1.isWeakKey)(key)) {
|
|
490
|
-
const hash = this._objHashFn(key);
|
|
491
|
-
node = this.objMap.get(hash);
|
|
492
|
-
if (!node && isNewKey) {
|
|
493
|
-
node = { key: hash, value, prev: this.tail, next: this._sentinel };
|
|
494
|
-
this.objMap.set(hash, node);
|
|
495
|
-
}
|
|
496
|
-
else if (node) {
|
|
497
|
-
node.value = value;
|
|
498
|
-
}
|
|
499
|
-
}
|
|
500
|
-
else {
|
|
501
|
-
const hash = this._hashFn(key);
|
|
502
|
-
node = this.noObjMap[hash];
|
|
503
|
-
if (!node && isNewKey) {
|
|
504
|
-
this.noObjMap[hash] = node = { key, value, prev: this.tail, next: this._sentinel };
|
|
505
|
-
}
|
|
506
|
-
else if (node) {
|
|
507
|
-
node.value = value;
|
|
508
|
-
}
|
|
509
|
-
}
|
|
510
|
-
if (node && isNewKey) {
|
|
511
|
-
if (this._size === 0) {
|
|
512
|
-
this._head = node;
|
|
513
|
-
this._sentinel.next = node;
|
|
514
|
-
}
|
|
515
|
-
else {
|
|
516
|
-
this.tail.next = node;
|
|
517
|
-
node.prev = this.tail;
|
|
518
|
-
}
|
|
519
|
-
this._tail = node;
|
|
520
|
-
this._sentinel.prev = node;
|
|
521
|
-
this._size++;
|
|
522
|
-
}
|
|
523
|
-
return true;
|
|
524
|
-
}
|
|
525
|
-
setMany(entryOrRawElements) {
|
|
526
|
-
const results = [];
|
|
527
|
-
for (const rawEle of entryOrRawElements) {
|
|
528
|
-
let key, value;
|
|
529
|
-
if (this.isEntry(rawEle))
|
|
530
|
-
[key, value] = rawEle;
|
|
531
|
-
else if (this._toEntryFn)
|
|
532
|
-
[key, value] = this._toEntryFn(rawEle);
|
|
533
|
-
if (key !== undefined && value !== undefined)
|
|
534
|
-
results.push(this.set(key, value));
|
|
535
|
-
}
|
|
536
|
-
return results;
|
|
537
|
-
}
|
|
538
|
-
has(key) {
|
|
539
|
-
if ((0, utils_1.isWeakKey)(key)) {
|
|
540
|
-
const hash = this._objHashFn(key);
|
|
541
|
-
return this.objMap.has(hash);
|
|
542
|
-
}
|
|
543
|
-
const hash = this._hashFn(key);
|
|
544
|
-
return hash in this.noObjMap;
|
|
545
|
-
}
|
|
546
|
-
get(key) {
|
|
547
|
-
if ((0, utils_1.isWeakKey)(key)) {
|
|
548
|
-
const hash = this._objHashFn(key);
|
|
549
|
-
const node = this.objMap.get(hash);
|
|
550
|
-
return node ? node.value : undefined;
|
|
551
|
-
}
|
|
552
|
-
const hash = this._hashFn(key);
|
|
553
|
-
const node = this.noObjMap[hash];
|
|
554
|
-
return node ? node.value : undefined;
|
|
555
|
-
}
|
|
556
|
-
/**
|
|
557
|
-
* Get the value at a given index in insertion order.
|
|
558
|
-
* @remarks Time O(N), Space O(1)
|
|
559
|
-
* @param index - Zero-based index.
|
|
560
|
-
* @returns Value at the index.
|
|
561
|
-
*/
|
|
562
|
-
at(index) {
|
|
563
|
-
(0, utils_1.rangeCheck)(index, 0, this._size - 1);
|
|
564
|
-
let node = this.head;
|
|
565
|
-
while (index--)
|
|
566
|
-
node = node.next;
|
|
567
|
-
return node.value;
|
|
568
|
-
}
|
|
569
|
-
delete(key) {
|
|
570
|
-
let node;
|
|
571
|
-
if ((0, utils_1.isWeakKey)(key)) {
|
|
572
|
-
const hash = this._objHashFn(key);
|
|
573
|
-
node = this.objMap.get(hash);
|
|
574
|
-
if (!node)
|
|
575
|
-
return false;
|
|
576
|
-
this.objMap.delete(hash);
|
|
577
|
-
}
|
|
578
|
-
else {
|
|
579
|
-
const hash = this._hashFn(key);
|
|
580
|
-
node = this.noObjMap[hash];
|
|
581
|
-
if (!node)
|
|
582
|
-
return false;
|
|
583
|
-
delete this.noObjMap[hash];
|
|
584
|
-
}
|
|
585
|
-
return this._deleteNode(node);
|
|
586
|
-
}
|
|
587
|
-
/**
|
|
588
|
-
* Delete the first entry that matches a predicate.
|
|
589
|
-
* @remarks Time O(N), Space O(1)
|
|
590
|
-
* @param predicate - Function (key, value, index, map) → boolean to decide deletion.
|
|
591
|
-
* @returns True if an entry was removed.
|
|
592
|
-
*/
|
|
593
|
-
deleteWhere(predicate) {
|
|
594
|
-
let node = this._head;
|
|
595
|
-
let i = 0;
|
|
596
|
-
while (node !== this._sentinel) {
|
|
597
|
-
const cur = node;
|
|
598
|
-
node = node.next;
|
|
599
|
-
if (predicate(cur.key, cur.value, i++, this)) {
|
|
600
|
-
if ((0, utils_1.isWeakKey)(cur.key)) {
|
|
601
|
-
this._objMap.delete(cur.key);
|
|
602
|
-
}
|
|
603
|
-
else {
|
|
604
|
-
const hash = this._hashFn(cur.key);
|
|
605
|
-
delete this._noObjMap[hash];
|
|
606
|
-
}
|
|
607
|
-
return this._deleteNode(cur);
|
|
608
|
-
}
|
|
609
|
-
}
|
|
610
|
-
return false;
|
|
611
|
-
}
|
|
612
|
-
/**
|
|
613
|
-
* Delete the entry at a given index.
|
|
614
|
-
* @remarks Time O(N), Space O(1)
|
|
615
|
-
* @param index - Zero-based index.
|
|
616
|
-
* @returns True if removed.
|
|
617
|
-
*/
|
|
618
|
-
deleteAt(index) {
|
|
619
|
-
(0, utils_1.rangeCheck)(index, 0, this._size - 1);
|
|
620
|
-
let node = this.head;
|
|
621
|
-
while (index--)
|
|
622
|
-
node = node.next;
|
|
623
|
-
return this._deleteNode(node);
|
|
624
|
-
}
|
|
625
|
-
isEmpty() {
|
|
626
|
-
return this._size === 0;
|
|
627
|
-
}
|
|
628
|
-
isEntry(rawElement) {
|
|
629
|
-
return Array.isArray(rawElement) && rawElement.length === 2;
|
|
630
|
-
}
|
|
631
|
-
clear() {
|
|
632
|
-
this._noObjMap = {};
|
|
633
|
-
this._size = 0;
|
|
634
|
-
this._head = this._tail = this._sentinel.prev = this._sentinel.next = this._sentinel;
|
|
635
|
-
}
|
|
636
|
-
clone() {
|
|
637
|
-
const opts = { hashFn: this._hashFn, objHashFn: this._objHashFn };
|
|
638
|
-
return this._createLike(this, opts);
|
|
639
|
-
}
|
|
640
|
-
filter(predicate, thisArg) {
|
|
641
|
-
const out = this._createLike();
|
|
642
|
-
let index = 0;
|
|
643
|
-
for (const [key, value] of this) {
|
|
644
|
-
if (predicate.call(thisArg, key, value, index, this))
|
|
645
|
-
out.set(key, value);
|
|
646
|
-
index++;
|
|
647
|
-
}
|
|
648
|
-
return out;
|
|
649
|
-
}
|
|
650
|
-
/**
|
|
651
|
-
* Map each entry to a new [key, value] pair and preserve order.
|
|
652
|
-
* @remarks Time O(N), Space O(N)
|
|
653
|
-
* @template MK
|
|
654
|
-
* @template MV
|
|
655
|
-
* @param callback - Mapping function (key, value, index, map) → [newKey, newValue].
|
|
656
|
-
* @param [thisArg] - Value for `this` inside the callback.
|
|
657
|
-
* @returns A new map of the same class with transformed entries.
|
|
658
|
-
*/
|
|
659
|
-
map(callback, thisArg) {
|
|
660
|
-
const out = this._createLike();
|
|
661
|
-
let index = 0;
|
|
662
|
-
for (const [key, value] of this) {
|
|
663
|
-
const [newKey, newValue] = callback.call(thisArg, key, value, index, this);
|
|
664
|
-
out.set(newKey, newValue);
|
|
665
|
-
index++;
|
|
666
|
-
}
|
|
667
|
-
return out;
|
|
668
|
-
}
|
|
669
|
-
*_getIterator() {
|
|
670
|
-
let node = this.head;
|
|
671
|
-
while (node !== this._sentinel) {
|
|
672
|
-
yield [node.key, node.value];
|
|
673
|
-
node = node.next;
|
|
674
|
-
}
|
|
675
|
-
}
|
|
676
|
-
_deleteNode(node) {
|
|
677
|
-
const { prev, next } = node;
|
|
678
|
-
prev.next = next;
|
|
679
|
-
next.prev = prev;
|
|
680
|
-
if (node === this.head)
|
|
681
|
-
this._head = next;
|
|
682
|
-
if (node === this.tail)
|
|
683
|
-
this._tail = prev;
|
|
684
|
-
this._size -= 1;
|
|
685
|
-
return true;
|
|
686
|
-
}
|
|
687
|
-
_createLike(entries = [], options) {
|
|
688
|
-
const Ctor = this.constructor;
|
|
689
|
-
return new Ctor(entries, options);
|
|
690
|
-
}
|
|
691
|
-
}
|
|
692
|
-
exports.LinkedHashMap = LinkedHashMap;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './hash-map';
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./hash-map"), exports);
|