red-black-tree-typed 1.47.3
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 +713 -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/data-structures/binary-tree/avl-tree.d.ts +173 -0
- package/dist/data-structures/binary-tree/avl-tree.js +429 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +149 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +269 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +515 -0
- package/dist/data-structures/binary-tree/binary-tree.js +1665 -0
- package/dist/data-structures/binary-tree/bst.d.ts +253 -0
- package/dist/data-structures/binary-tree/bst.js +651 -0
- package/dist/data-structures/binary-tree/index.d.ts +7 -0
- package/dist/data-structures/binary-tree/index.js +23 -0
- package/dist/data-structures/binary-tree/rb-tree.d.ts +169 -0
- package/dist/data-structures/binary-tree/rb-tree.js +524 -0
- package/dist/data-structures/binary-tree/segment-tree.d.ts +67 -0
- package/dist/data-structures/binary-tree/segment-tree.js +180 -0
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +162 -0
- package/dist/data-structures/binary-tree/tree-multimap.js +407 -0
- package/dist/data-structures/graph/abstract-graph.d.ts +450 -0
- package/dist/data-structures/graph/abstract-graph.js +1047 -0
- package/dist/data-structures/graph/directed-graph.d.ts +320 -0
- package/dist/data-structures/graph/directed-graph.js +530 -0
- package/dist/data-structures/graph/index.d.ts +4 -0
- package/dist/data-structures/graph/index.js +20 -0
- package/dist/data-structures/graph/map-graph.d.ts +73 -0
- package/dist/data-structures/graph/map-graph.js +93 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +183 -0
- package/dist/data-structures/graph/undirected-graph.js +302 -0
- package/dist/data-structures/hash/hash-map.d.ts +186 -0
- package/dist/data-structures/hash/hash-map.js +367 -0
- package/dist/data-structures/hash/hash-table.d.ts +103 -0
- package/dist/data-structures/hash/hash-table.js +236 -0
- package/dist/data-structures/hash/index.d.ts +2 -0
- package/dist/data-structures/hash/index.js +18 -0
- package/dist/data-structures/heap/heap.d.ts +410 -0
- package/dist/data-structures/heap/heap.js +697 -0
- package/dist/data-structures/heap/index.d.ts +3 -0
- package/dist/data-structures/heap/index.js +19 -0
- package/dist/data-structures/heap/max-heap.d.ts +15 -0
- package/dist/data-structures/heap/max-heap.js +26 -0
- package/dist/data-structures/heap/min-heap.d.ts +15 -0
- package/dist/data-structures/heap/min-heap.js +26 -0
- package/dist/data-structures/index.d.ts +11 -0
- package/dist/data-structures/index.js +27 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +456 -0
- package/dist/data-structures/linked-list/doubly-linked-list.js +772 -0
- package/dist/data-structures/linked-list/index.d.ts +3 -0
- package/dist/data-structures/linked-list/index.js +19 -0
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +414 -0
- package/dist/data-structures/linked-list/singly-linked-list.js +715 -0
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +144 -0
- package/dist/data-structures/linked-list/skip-linked-list.js +251 -0
- package/dist/data-structures/matrix/index.d.ts +4 -0
- package/dist/data-structures/matrix/index.js +20 -0
- package/dist/data-structures/matrix/matrix.d.ts +21 -0
- package/dist/data-structures/matrix/matrix.js +28 -0
- package/dist/data-structures/matrix/matrix2d.d.ts +107 -0
- package/dist/data-structures/matrix/matrix2d.js +199 -0
- package/dist/data-structures/matrix/navigator.d.ts +52 -0
- package/dist/data-structures/matrix/navigator.js +106 -0
- package/dist/data-structures/matrix/vector2d.d.ts +200 -0
- package/dist/data-structures/matrix/vector2d.js +290 -0
- package/dist/data-structures/priority-queue/index.d.ts +3 -0
- package/dist/data-structures/priority-queue/index.js +19 -0
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +15 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +26 -0
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +15 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +26 -0
- package/dist/data-structures/priority-queue/priority-queue.d.ts +15 -0
- package/dist/data-structures/priority-queue/priority-queue.js +17 -0
- package/dist/data-structures/queue/deque.d.ts +572 -0
- package/dist/data-structures/queue/deque.js +990 -0
- package/dist/data-structures/queue/index.d.ts +2 -0
- package/dist/data-structures/queue/index.js +18 -0
- package/dist/data-structures/queue/queue.d.ts +209 -0
- package/dist/data-structures/queue/queue.js +274 -0
- package/dist/data-structures/stack/index.d.ts +1 -0
- package/dist/data-structures/stack/index.js +17 -0
- package/dist/data-structures/stack/stack.d.ts +106 -0
- package/dist/data-structures/stack/stack.js +136 -0
- package/dist/data-structures/tree/index.d.ts +1 -0
- package/dist/data-structures/tree/index.js +17 -0
- package/dist/data-structures/tree/tree.d.ts +8 -0
- package/dist/data-structures/tree/tree.js +40 -0
- package/dist/data-structures/trie/index.d.ts +1 -0
- package/dist/data-structures/trie/index.js +17 -0
- package/dist/data-structures/trie/trie.d.ts +155 -0
- package/dist/data-structures/trie/trie.js +326 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +27 -0
- package/dist/interfaces/binary-tree.d.ts +7 -0
- package/dist/interfaces/binary-tree.js +2 -0
- package/dist/interfaces/doubly-linked-list.d.ts +1 -0
- package/dist/interfaces/doubly-linked-list.js +2 -0
- package/dist/interfaces/graph.d.ts +5 -0
- package/dist/interfaces/graph.js +2 -0
- package/dist/interfaces/heap.d.ts +1 -0
- package/dist/interfaces/heap.js +2 -0
- package/dist/interfaces/index.d.ts +8 -0
- package/dist/interfaces/index.js +24 -0
- package/dist/interfaces/navigator.d.ts +1 -0
- package/dist/interfaces/navigator.js +2 -0
- package/dist/interfaces/priority-queue.d.ts +1 -0
- package/dist/interfaces/priority-queue.js +2 -0
- package/dist/interfaces/segment-tree.d.ts +1 -0
- package/dist/interfaces/segment-tree.js +2 -0
- package/dist/interfaces/singly-linked-list.d.ts +1 -0
- package/dist/interfaces/singly-linked-list.js +2 -0
- package/dist/types/common.d.ts +20 -0
- package/dist/types/common.js +9 -0
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +5 -0
- package/dist/types/data-structures/binary-tree/avl-tree.js +2 -0
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -0
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.js +2 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +31 -0
- package/dist/types/data-structures/binary-tree/binary-tree.js +24 -0
- package/dist/types/data-structures/binary-tree/bst.d.ts +8 -0
- package/dist/types/data-structures/binary-tree/bst.js +2 -0
- package/dist/types/data-structures/binary-tree/index.d.ts +6 -0
- package/dist/types/data-structures/binary-tree/index.js +22 -0
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +9 -0
- package/dist/types/data-structures/binary-tree/rb-tree.js +8 -0
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +1 -0
- package/dist/types/data-structures/binary-tree/segment-tree.js +2 -0
- package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +5 -0
- package/dist/types/data-structures/binary-tree/tree-multimap.js +2 -0
- package/dist/types/data-structures/graph/abstract-graph.d.ts +10 -0
- package/dist/types/data-structures/graph/abstract-graph.js +2 -0
- package/dist/types/data-structures/graph/directed-graph.d.ts +1 -0
- package/dist/types/data-structures/graph/directed-graph.js +2 -0
- package/dist/types/data-structures/graph/index.d.ts +3 -0
- package/dist/types/data-structures/graph/index.js +19 -0
- package/dist/types/data-structures/graph/map-graph.d.ts +1 -0
- package/dist/types/data-structures/graph/map-graph.js +2 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +1 -0
- package/dist/types/data-structures/graph/undirected-graph.js +2 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +11 -0
- package/dist/types/data-structures/hash/hash-map.js +2 -0
- package/dist/types/data-structures/hash/hash-table.d.ts +1 -0
- package/dist/types/data-structures/hash/hash-table.js +2 -0
- package/dist/types/data-structures/hash/index.d.ts +3 -0
- package/dist/types/data-structures/hash/index.js +18 -0
- package/dist/types/data-structures/heap/heap.d.ts +1 -0
- package/dist/types/data-structures/heap/heap.js +2 -0
- package/dist/types/data-structures/heap/index.d.ts +1 -0
- package/dist/types/data-structures/heap/index.js +17 -0
- package/dist/types/data-structures/heap/max-heap.d.ts +1 -0
- package/dist/types/data-structures/heap/max-heap.js +2 -0
- package/dist/types/data-structures/heap/min-heap.d.ts +1 -0
- package/dist/types/data-structures/heap/min-heap.js +2 -0
- package/dist/types/data-structures/index.d.ts +11 -0
- package/dist/types/data-structures/index.js +27 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +1 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.js +2 -0
- package/dist/types/data-structures/linked-list/index.d.ts +2 -0
- package/dist/types/data-structures/linked-list/index.js +18 -0
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +1 -0
- package/dist/types/data-structures/linked-list/singly-linked-list.js +2 -0
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +1 -0
- package/dist/types/data-structures/linked-list/skip-linked-list.js +2 -0
- package/dist/types/data-structures/matrix/index.d.ts +1 -0
- package/dist/types/data-structures/matrix/index.js +17 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +1 -0
- package/dist/types/data-structures/matrix/matrix.js +2 -0
- package/dist/types/data-structures/matrix/matrix2d.d.ts +1 -0
- package/dist/types/data-structures/matrix/matrix2d.js +2 -0
- package/dist/types/data-structures/matrix/navigator.d.ts +14 -0
- package/dist/types/data-structures/matrix/navigator.js +2 -0
- package/dist/types/data-structures/matrix/vector2d.d.ts +1 -0
- package/dist/types/data-structures/matrix/vector2d.js +2 -0
- package/dist/types/data-structures/priority-queue/index.d.ts +3 -0
- package/dist/types/data-structures/priority-queue/index.js +19 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.js +2 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +1 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.js +2 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
- package/dist/types/data-structures/priority-queue/priority-queue.js +2 -0
- package/dist/types/data-structures/queue/deque.d.ts +1 -0
- package/dist/types/data-structures/queue/deque.js +2 -0
- package/dist/types/data-structures/queue/index.d.ts +2 -0
- package/dist/types/data-structures/queue/index.js +18 -0
- package/dist/types/data-structures/queue/queue.d.ts +1 -0
- package/dist/types/data-structures/queue/queue.js +2 -0
- package/dist/types/data-structures/stack/index.d.ts +1 -0
- package/dist/types/data-structures/stack/index.js +17 -0
- package/dist/types/data-structures/stack/stack.d.ts +1 -0
- package/dist/types/data-structures/stack/stack.js +2 -0
- package/dist/types/data-structures/tree/index.d.ts +1 -0
- package/dist/types/data-structures/tree/index.js +17 -0
- package/dist/types/data-structures/tree/tree.d.ts +1 -0
- package/dist/types/data-structures/tree/tree.js +2 -0
- package/dist/types/data-structures/trie/index.d.ts +1 -0
- package/dist/types/data-structures/trie/index.js +17 -0
- package/dist/types/data-structures/trie/trie.d.ts +1 -0
- package/dist/types/data-structures/trie/trie.js +2 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/index.js +19 -0
- package/dist/types/utils/index.d.ts +2 -0
- package/dist/types/utils/index.js +18 -0
- package/dist/types/utils/utils.d.ts +7 -0
- package/dist/types/utils/utils.js +2 -0
- package/dist/types/utils/validate-type.d.ts +19 -0
- package/dist/types/utils/validate-type.js +2 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +17 -0
- package/dist/utils/utils.d.ts +24 -0
- package/dist/utils/utils.js +89 -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 +147 -0
- package/src/data-structures/binary-tree/avl-tree.ts +443 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +306 -0
- package/src/data-structures/binary-tree/binary-tree.ts +1974 -0
- package/src/data-structures/binary-tree/bst.ts +676 -0
- package/src/data-structures/binary-tree/index.ts +7 -0
- package/src/data-structures/binary-tree/rb-tree.ts +585 -0
- package/src/data-structures/binary-tree/segment-tree.ts +190 -0
- package/src/data-structures/binary-tree/tree-multimap.ts +435 -0
- package/src/data-structures/graph/abstract-graph.ts +1181 -0
- package/src/data-structures/graph/directed-graph.ts +593 -0
- package/src/data-structures/graph/index.ts +4 -0
- package/src/data-structures/graph/map-graph.ts +106 -0
- package/src/data-structures/graph/undirected-graph.ts +331 -0
- package/src/data-structures/hash/hash-map.ts +401 -0
- package/src/data-structures/hash/hash-table.ts +268 -0
- package/src/data-structures/hash/index.ts +2 -0
- package/src/data-structures/heap/heap.ts +790 -0
- package/src/data-structures/heap/index.ts +3 -0
- package/src/data-structures/heap/max-heap.ts +26 -0
- package/src/data-structures/heap/min-heap.ts +26 -0
- package/src/data-structures/index.ts +11 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +837 -0
- package/src/data-structures/linked-list/index.ts +3 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +784 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +295 -0
- package/src/data-structures/matrix/index.ts +4 -0
- package/src/data-structures/matrix/matrix.ts +27 -0
- package/src/data-structures/matrix/matrix2d.ts +211 -0
- package/src/data-structures/matrix/navigator.ts +121 -0
- package/src/data-structures/matrix/vector2d.ts +315 -0
- package/src/data-structures/priority-queue/index.ts +3 -0
- package/src/data-structures/priority-queue/max-priority-queue.ts +25 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +25 -0
- package/src/data-structures/priority-queue/priority-queue.ts +16 -0
- package/src/data-structures/queue/deque.ts +1073 -0
- package/src/data-structures/queue/index.ts +2 -0
- package/src/data-structures/queue/queue.ts +308 -0
- package/src/data-structures/stack/index.ts +1 -0
- package/src/data-structures/stack/stack.ts +150 -0
- package/src/data-structures/tree/index.ts +1 -0
- package/src/data-structures/tree/tree.ts +41 -0
- package/src/data-structures/trie/index.ts +1 -0
- package/src/data-structures/trie/trie.ts +345 -0
- package/src/index.ts +11 -0
- package/src/interfaces/binary-tree.ts +10 -0
- package/src/interfaces/doubly-linked-list.ts +1 -0
- package/src/interfaces/graph.ts +7 -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 +23 -0
- package/src/types/data-structures/binary-tree/avl-tree.ts +9 -0
- package/src/types/data-structures/binary-tree/binary-indexed-tree.ts +1 -0
- package/src/types/data-structures/binary-tree/binary-tree.ts +35 -0
- package/src/types/data-structures/binary-tree/bst.ts +13 -0
- package/src/types/data-structures/binary-tree/index.ts +6 -0
- package/src/types/data-structures/binary-tree/rb-tree.ts +10 -0
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -0
- package/src/types/data-structures/binary-tree/tree-multimap.ts +8 -0
- package/src/types/data-structures/graph/abstract-graph.ts +11 -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 +12 -0
- package/src/types/data-structures/hash/hash-table.ts +1 -0
- package/src/types/data-structures/hash/index.ts +4 -0
- package/src/types/data-structures/heap/heap.ts +1 -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 +11 -0
- package/src/types/data-structures/linked-list/doubly-linked-list.ts +1 -0
- package/src/types/data-structures/linked-list/index.ts +2 -0
- package/src/types/data-structures/linked-list/singly-linked-list.ts +1 -0
- package/src/types/data-structures/linked-list/skip-linked-list.ts +1 -0
- package/src/types/data-structures/matrix/index.ts +1 -0
- package/src/types/data-structures/matrix/matrix.ts +1 -0
- package/src/types/data-structures/matrix/matrix2d.ts +1 -0
- package/src/types/data-structures/matrix/navigator.ts +14 -0
- package/src/types/data-structures/matrix/vector2d.ts +1 -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 +1 -0
- package/src/types/data-structures/queue/deque.ts +1 -0
- package/src/types/data-structures/queue/index.ts +2 -0
- package/src/types/data-structures/queue/queue.ts +1 -0
- package/src/types/data-structures/stack/index.ts +1 -0
- package/src/types/data-structures/stack/stack.ts +1 -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 +1 -0
- package/src/types/index.ts +3 -0
- package/src/types/utils/index.ts +2 -0
- package/src/types/utils/utils.ts +6 -0
- package/src/types/utils/validate-type.ts +35 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/utils.ts +101 -0
- package/test/index.test.ts +111 -0
- package/tsconfig.json +38 -0
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Tyler Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
7
|
+
*/
|
|
8
|
+
import { HashMapLinkedNode, HashMapOptions } from '../../types';
|
|
9
|
+
export declare class HashMap<K = any, V = any> {
|
|
10
|
+
protected _noObjMap: Record<string, HashMapLinkedNode<K, V | undefined>>;
|
|
11
|
+
protected _objMap: WeakMap<object, HashMapLinkedNode<K, V | undefined>>;
|
|
12
|
+
protected _head: HashMapLinkedNode<K, V | undefined>;
|
|
13
|
+
protected _tail: HashMapLinkedNode<K, V | undefined>;
|
|
14
|
+
protected readonly _sentinel: HashMapLinkedNode<K, V | undefined>;
|
|
15
|
+
protected _hashFn: (key: K) => string;
|
|
16
|
+
protected _objHashFn: (key: K) => object;
|
|
17
|
+
/**
|
|
18
|
+
* The constructor initializes a HashMapLinkedNode with an optional iterable of key-value pairs.
|
|
19
|
+
* @param options - The `options` parameter is an object that contains the `elements` property. The
|
|
20
|
+
* `elements` property is an iterable that contains key-value pairs represented as arrays `[K, V]`.
|
|
21
|
+
*/
|
|
22
|
+
constructor(options?: HashMapOptions<K, V>);
|
|
23
|
+
protected _size: number;
|
|
24
|
+
get size(): number;
|
|
25
|
+
/**
|
|
26
|
+
* Time Complexity: O(1)
|
|
27
|
+
* Space Complexity: O(1)
|
|
28
|
+
*
|
|
29
|
+
* The function returns the key-value pair at the front of a data structure.
|
|
30
|
+
* @returns The front element of the data structure, represented as a tuple with a key (K) and a
|
|
31
|
+
* value (V).
|
|
32
|
+
*/
|
|
33
|
+
get first(): [K, V] | undefined;
|
|
34
|
+
/**
|
|
35
|
+
* Time Complexity: O(1)
|
|
36
|
+
* Space Complexity: O(1)
|
|
37
|
+
*
|
|
38
|
+
* The function returns the key-value pair at the end of a data structure.
|
|
39
|
+
* @returns The method is returning an array containing the key-value pair of the tail element in the
|
|
40
|
+
* data structure.
|
|
41
|
+
*/
|
|
42
|
+
get last(): [K, V] | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* The `begin()` function in TypeScript iterates over a linked list and yields key-value pairs.
|
|
45
|
+
*/
|
|
46
|
+
begin(): Generator<(K | V | undefined)[], void, unknown>;
|
|
47
|
+
/**
|
|
48
|
+
* The function `reverseBegin()` iterates over a linked list in reverse order, yielding each node's
|
|
49
|
+
* key and value.
|
|
50
|
+
*/
|
|
51
|
+
reverseBegin(): Generator<(K | V | undefined)[], void, unknown>;
|
|
52
|
+
/**
|
|
53
|
+
* Time Complexity: O(1)
|
|
54
|
+
* Space Complexity: O(1)
|
|
55
|
+
*
|
|
56
|
+
* The `set` function adds a new key-value pair to a data structure, either using an object key or a
|
|
57
|
+
* string key.
|
|
58
|
+
* @param {K} key - The `key` parameter is the key to be set in the data structure. It can be of any
|
|
59
|
+
* type, but typically it is a string or symbol.
|
|
60
|
+
* @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the
|
|
61
|
+
* value associated with the key being set in the data structure.
|
|
62
|
+
* @returns the size of the data structure after the key-value pair has been set.
|
|
63
|
+
*/
|
|
64
|
+
set(key: K, value?: V): number;
|
|
65
|
+
/**
|
|
66
|
+
* Time Complexity: O(1)
|
|
67
|
+
* Space Complexity: O(1)
|
|
68
|
+
*
|
|
69
|
+
* The function `get` retrieves the value associated with a given key from a map, either by using the
|
|
70
|
+
* key directly or by using an index stored in the key object.
|
|
71
|
+
* @param {K} key - The `key` parameter is the key used to retrieve a value from the map. It can be
|
|
72
|
+
* of any type, but typically it is a string or symbol.
|
|
73
|
+
* @returns The value associated with the given key is being returned. If the key is an object key,
|
|
74
|
+
* the value is retrieved from the `_nodes` array using the index stored in the `OBJ_KEY_INDEX`
|
|
75
|
+
* property of the key. If the key is a string key, the value is retrieved from the `_noObjMap` object
|
|
76
|
+
* using the key itself. If the key is not found, `undefined` is
|
|
77
|
+
*/
|
|
78
|
+
get(key: K): V | undefined;
|
|
79
|
+
/**
|
|
80
|
+
* Time Complexity: O(n), where n is the index.
|
|
81
|
+
* Space Complexity: O(1)
|
|
82
|
+
*
|
|
83
|
+
* The function `getAt` retrieves the key-value pair at a specified index in a linked list.
|
|
84
|
+
* @param {number} index - The index parameter is a number that represents the position of the
|
|
85
|
+
* element we want to retrieve from the data structure.
|
|
86
|
+
* @returns The method `getAt(index: number)` is returning an array containing the key-value pair at
|
|
87
|
+
* the specified index in the data structure. The key-value pair is represented as a tuple `[K, V]`,
|
|
88
|
+
* where `K` is the key and `V` is the value.
|
|
89
|
+
*/
|
|
90
|
+
getAt(index: number): [K, V];
|
|
91
|
+
/**
|
|
92
|
+
* Time Complexity: O(1)
|
|
93
|
+
* Space Complexity: O(1)
|
|
94
|
+
*
|
|
95
|
+
* The `delete` function removes a key-value pair from a map-like data structure.
|
|
96
|
+
* @param {K} key - The `key` parameter is the key that you want to delete from the data structure.
|
|
97
|
+
* It can be of any type, but typically it is a string or an object.
|
|
98
|
+
* @returns a boolean value. It returns `true` if the deletion was successful, and `false` if the key
|
|
99
|
+
* was not found.
|
|
100
|
+
*/
|
|
101
|
+
delete(key: K): boolean;
|
|
102
|
+
/**
|
|
103
|
+
* Time Complexity: O(n), where n is the index.
|
|
104
|
+
* Space Complexity: O(1)
|
|
105
|
+
*
|
|
106
|
+
* The `deleteAt` function deletes a node at a specified index in a linked list.
|
|
107
|
+
* @param {number} index - The index parameter represents the position at which the node should be
|
|
108
|
+
* deleted in the linked list.
|
|
109
|
+
* @returns The size of the list after deleting the element at the specified index.
|
|
110
|
+
*/
|
|
111
|
+
deleteAt(index: number): number;
|
|
112
|
+
/**
|
|
113
|
+
* Time Complexity: O(1)
|
|
114
|
+
* Space Complexity: O(1)
|
|
115
|
+
*
|
|
116
|
+
* The function checks if a data structure is empty by comparing its size to zero.
|
|
117
|
+
* @returns The method is returning a boolean value indicating whether the size of the object is 0 or
|
|
118
|
+
* not.
|
|
119
|
+
*/
|
|
120
|
+
isEmpty(): boolean;
|
|
121
|
+
/**
|
|
122
|
+
* Time Complexity: O(1)
|
|
123
|
+
* Space Complexity: O(1)
|
|
124
|
+
*
|
|
125
|
+
* The `clear` function clears all the elements in a data structure and resets its properties.
|
|
126
|
+
*/
|
|
127
|
+
clear(): void;
|
|
128
|
+
/**
|
|
129
|
+
* Time Complexity: O(n), where n is the number of elements in the HashMap.
|
|
130
|
+
* Space Complexity: O(1)
|
|
131
|
+
*
|
|
132
|
+
* The `forEach` function iterates over each element in a HashMap and executes a callback function on
|
|
133
|
+
* each element.
|
|
134
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
135
|
+
* HashMap. It takes three arguments:
|
|
136
|
+
*/
|
|
137
|
+
forEach(callback: (element: [K, V], index: number, hashMap: HashMap<K, V>) => void): void;
|
|
138
|
+
/**
|
|
139
|
+
* The `filter` function takes a predicate function and returns a new HashMap containing only the
|
|
140
|
+
* key-value pairs that satisfy the predicate.
|
|
141
|
+
* @param predicate - The `predicate` parameter is a function that takes two arguments: `element` and
|
|
142
|
+
* `map`.
|
|
143
|
+
* @returns a new HashMap object that contains the key-value pairs from the original HashMap that
|
|
144
|
+
* satisfy the given predicate function.
|
|
145
|
+
*/
|
|
146
|
+
filter(predicate: (element: [K, V], map: HashMap<K, V>) => boolean): HashMap<K, V>;
|
|
147
|
+
/**
|
|
148
|
+
* The `map` function takes a callback function and returns a new HashMap with the values transformed
|
|
149
|
+
* by the callback.
|
|
150
|
+
* @param callback - The `callback` parameter is a function that takes two arguments: `element` and
|
|
151
|
+
* `map`.
|
|
152
|
+
* @returns a new HashMap object with the values mapped according to the provided callback function.
|
|
153
|
+
*/
|
|
154
|
+
map<NV>(callback: (element: [K, V], map: HashMap<K, V>) => NV): HashMap<K, NV>;
|
|
155
|
+
/**
|
|
156
|
+
* The `reduce` function iterates over the elements of a HashMap and applies a callback function to
|
|
157
|
+
* each element, accumulating a single value.
|
|
158
|
+
* @param callback - The callback parameter is a function that takes three arguments: accumulator,
|
|
159
|
+
* element, and map. It is called for each element in the HashMap and is used to accumulate a single
|
|
160
|
+
* result.
|
|
161
|
+
* @param {A} initialValue - The `initialValue` parameter is the initial value of the accumulator. It
|
|
162
|
+
* is the value that will be passed as the first argument to the `callback` function when reducing
|
|
163
|
+
* the elements of the map.
|
|
164
|
+
* @returns The `reduce` function is returning the final value of the accumulator after iterating
|
|
165
|
+
* over all the elements in the HashMap and applying the callback function to each element.
|
|
166
|
+
*/
|
|
167
|
+
reduce<A>(callback: (accumulator: A, element: [K, V], map: HashMap<K, V>) => A, initialValue: A): A;
|
|
168
|
+
/**
|
|
169
|
+
* Time Complexity: O(n), where n is the number of elements in the HashMap.
|
|
170
|
+
* Space Complexity: O(1)
|
|
171
|
+
*
|
|
172
|
+
* The above function is an iterator that yields key-value pairs from a linked list.
|
|
173
|
+
*/
|
|
174
|
+
[Symbol.iterator](): Generator<[K, V], void, unknown>;
|
|
175
|
+
/**
|
|
176
|
+
* Time Complexity: O(1)
|
|
177
|
+
* Space Complexity: O(1)
|
|
178
|
+
*
|
|
179
|
+
* The `_deleteNode` function removes a node from a doubly linked list and updates the head and tail
|
|
180
|
+
* pointers if necessary.
|
|
181
|
+
* @param node - The `node` parameter is an instance of the `HashMapLinkedNode` class, which
|
|
182
|
+
* represents a node in a linked list. It contains a key-value pair and references to the previous
|
|
183
|
+
* and next nodes in the list.
|
|
184
|
+
*/
|
|
185
|
+
protected _deleteNode(node: HashMapLinkedNode<K, V | undefined>): void;
|
|
186
|
+
}
|
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* data-structure-typed
|
|
4
|
+
*
|
|
5
|
+
* @author Tyler Zeng
|
|
6
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
7
|
+
* @license MIT License
|
|
8
|
+
*/
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.HashMap = void 0;
|
|
11
|
+
const utils_1 = require("../../utils");
|
|
12
|
+
class HashMap {
|
|
13
|
+
/**
|
|
14
|
+
* The constructor initializes a HashMapLinkedNode with an optional iterable of key-value pairs.
|
|
15
|
+
* @param options - The `options` parameter is an object that contains the `elements` property. The
|
|
16
|
+
* `elements` property is an iterable that contains key-value pairs represented as arrays `[K, V]`.
|
|
17
|
+
*/
|
|
18
|
+
constructor(options = {
|
|
19
|
+
elements: [],
|
|
20
|
+
hashFn: (key) => String(key),
|
|
21
|
+
objHashFn: (key) => key
|
|
22
|
+
}) {
|
|
23
|
+
this._noObjMap = {};
|
|
24
|
+
this._objMap = new WeakMap();
|
|
25
|
+
this._size = 0;
|
|
26
|
+
this._sentinel = {};
|
|
27
|
+
this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
|
|
28
|
+
const { elements, hashFn, objHashFn } = options;
|
|
29
|
+
this._hashFn = hashFn;
|
|
30
|
+
this._objHashFn = objHashFn;
|
|
31
|
+
if (elements) {
|
|
32
|
+
for (const el of elements) {
|
|
33
|
+
this.set(el[0], el[1]);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
get size() {
|
|
38
|
+
return this._size;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Time Complexity: O(1)
|
|
42
|
+
* Space Complexity: O(1)
|
|
43
|
+
*
|
|
44
|
+
* The function returns the key-value pair at the front of a data structure.
|
|
45
|
+
* @returns The front element of the data structure, represented as a tuple with a key (K) and a
|
|
46
|
+
* value (V).
|
|
47
|
+
*/
|
|
48
|
+
get first() {
|
|
49
|
+
if (this._size === 0)
|
|
50
|
+
return;
|
|
51
|
+
return [this._head.key, this._head.value];
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Time Complexity: O(1)
|
|
55
|
+
* Space Complexity: O(1)
|
|
56
|
+
*
|
|
57
|
+
* The function returns the key-value pair at the end of a data structure.
|
|
58
|
+
* @returns The method is returning an array containing the key-value pair of the tail element in the
|
|
59
|
+
* data structure.
|
|
60
|
+
*/
|
|
61
|
+
get last() {
|
|
62
|
+
if (this._size === 0)
|
|
63
|
+
return;
|
|
64
|
+
return [this._tail.key, this._tail.value];
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* The `begin()` function in TypeScript iterates over a linked list and yields key-value pairs.
|
|
68
|
+
*/
|
|
69
|
+
*begin() {
|
|
70
|
+
let node = this._head;
|
|
71
|
+
while (node !== this._sentinel) {
|
|
72
|
+
yield [node.key, node.value];
|
|
73
|
+
node = node.next;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* The function `reverseBegin()` iterates over a linked list in reverse order, yielding each node's
|
|
78
|
+
* key and value.
|
|
79
|
+
*/
|
|
80
|
+
*reverseBegin() {
|
|
81
|
+
let node = this._tail;
|
|
82
|
+
while (node !== this._sentinel) {
|
|
83
|
+
yield [node.key, node.value];
|
|
84
|
+
node = node.prev;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Time Complexity: O(1)
|
|
89
|
+
* Space Complexity: O(1)
|
|
90
|
+
*
|
|
91
|
+
* The `set` function adds a new key-value pair to a data structure, either using an object key or a
|
|
92
|
+
* string key.
|
|
93
|
+
* @param {K} key - The `key` parameter is the key to be set in the data structure. It can be of any
|
|
94
|
+
* type, but typically it is a string or symbol.
|
|
95
|
+
* @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the
|
|
96
|
+
* value associated with the key being set in the data structure.
|
|
97
|
+
* @returns the size of the data structure after the key-value pair has been set.
|
|
98
|
+
*/
|
|
99
|
+
set(key, value) {
|
|
100
|
+
let node;
|
|
101
|
+
if ((0, utils_1.isWeakKey)(key)) {
|
|
102
|
+
const hash = this._objHashFn(key);
|
|
103
|
+
node = this._objMap.get(hash);
|
|
104
|
+
if (node) {
|
|
105
|
+
// If the node already exists, update its value
|
|
106
|
+
node.value = value;
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
// Create new node
|
|
110
|
+
node = { key: hash, value, prev: this._tail, next: this._sentinel };
|
|
111
|
+
// Add new nodes to _objMap and linked list
|
|
112
|
+
this._objMap.set(hash, node);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
const hash = this._hashFn(key);
|
|
117
|
+
// Non-object keys are handled in the same way as the original implementation
|
|
118
|
+
node = this._noObjMap[hash];
|
|
119
|
+
if (node) {
|
|
120
|
+
node.value = value;
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
this._noObjMap[hash] = node = {
|
|
124
|
+
key,
|
|
125
|
+
value,
|
|
126
|
+
prev: this._tail,
|
|
127
|
+
next: this._sentinel
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
if (this._size === 0) {
|
|
132
|
+
this._head = node;
|
|
133
|
+
this._sentinel.next = node;
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
this._tail.next = node;
|
|
137
|
+
}
|
|
138
|
+
this._tail = node;
|
|
139
|
+
this._sentinel.prev = node;
|
|
140
|
+
this._size++;
|
|
141
|
+
return this._size;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Time Complexity: O(1)
|
|
145
|
+
* Space Complexity: O(1)
|
|
146
|
+
*
|
|
147
|
+
* The function `get` retrieves the value associated with a given key from a map, either by using the
|
|
148
|
+
* key directly or by using an index stored in the key object.
|
|
149
|
+
* @param {K} key - The `key` parameter is the key used to retrieve a value from the map. It can be
|
|
150
|
+
* of any type, but typically it is a string or symbol.
|
|
151
|
+
* @returns The value associated with the given key is being returned. If the key is an object key,
|
|
152
|
+
* the value is retrieved from the `_nodes` array using the index stored in the `OBJ_KEY_INDEX`
|
|
153
|
+
* property of the key. If the key is a string key, the value is retrieved from the `_noObjMap` object
|
|
154
|
+
* using the key itself. If the key is not found, `undefined` is
|
|
155
|
+
*/
|
|
156
|
+
get(key) {
|
|
157
|
+
if ((0, utils_1.isWeakKey)(key)) {
|
|
158
|
+
const hash = this._objHashFn(key);
|
|
159
|
+
const node = this._objMap.get(hash);
|
|
160
|
+
return node ? node.value : undefined;
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
const hash = this._hashFn(key);
|
|
164
|
+
const node = this._noObjMap[hash];
|
|
165
|
+
return node ? node.value : undefined;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Time Complexity: O(n), where n is the index.
|
|
170
|
+
* Space Complexity: O(1)
|
|
171
|
+
*
|
|
172
|
+
* The function `getAt` retrieves the key-value pair at a specified index in a linked list.
|
|
173
|
+
* @param {number} index - The index parameter is a number that represents the position of the
|
|
174
|
+
* element we want to retrieve from the data structure.
|
|
175
|
+
* @returns The method `getAt(index: number)` is returning an array containing the key-value pair at
|
|
176
|
+
* the specified index in the data structure. The key-value pair is represented as a tuple `[K, V]`,
|
|
177
|
+
* where `K` is the key and `V` is the value.
|
|
178
|
+
*/
|
|
179
|
+
getAt(index) {
|
|
180
|
+
(0, utils_1.rangeCheck)(index, 0, this._size - 1);
|
|
181
|
+
let node = this._head;
|
|
182
|
+
while (index--) {
|
|
183
|
+
node = node.next;
|
|
184
|
+
}
|
|
185
|
+
return [node.key, node.value];
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Time Complexity: O(1)
|
|
189
|
+
* Space Complexity: O(1)
|
|
190
|
+
*
|
|
191
|
+
* The `delete` function removes a key-value pair from a map-like data structure.
|
|
192
|
+
* @param {K} key - The `key` parameter is the key that you want to delete from the data structure.
|
|
193
|
+
* It can be of any type, but typically it is a string or an object.
|
|
194
|
+
* @returns a boolean value. It returns `true` if the deletion was successful, and `false` if the key
|
|
195
|
+
* was not found.
|
|
196
|
+
*/
|
|
197
|
+
delete(key) {
|
|
198
|
+
let node;
|
|
199
|
+
if ((0, utils_1.isWeakKey)(key)) {
|
|
200
|
+
const hash = this._objHashFn(key);
|
|
201
|
+
// Get nodes from WeakMap
|
|
202
|
+
node = this._objMap.get(hash);
|
|
203
|
+
if (!node) {
|
|
204
|
+
return false; // If the node does not exist, return false
|
|
205
|
+
}
|
|
206
|
+
// Remove nodes from WeakMap
|
|
207
|
+
this._objMap.delete(hash);
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
const hash = this._hashFn(key);
|
|
211
|
+
// Get nodes from noObjMap
|
|
212
|
+
node = this._noObjMap[hash];
|
|
213
|
+
if (!node) {
|
|
214
|
+
return false; // If the node does not exist, return false
|
|
215
|
+
}
|
|
216
|
+
// Remove nodes from orgMap
|
|
217
|
+
delete this._noObjMap[hash];
|
|
218
|
+
}
|
|
219
|
+
// Remove node from doubly linked list
|
|
220
|
+
this._deleteNode(node);
|
|
221
|
+
return true;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Time Complexity: O(n), where n is the index.
|
|
225
|
+
* Space Complexity: O(1)
|
|
226
|
+
*
|
|
227
|
+
* The `deleteAt` function deletes a node at a specified index in a linked list.
|
|
228
|
+
* @param {number} index - The index parameter represents the position at which the node should be
|
|
229
|
+
* deleted in the linked list.
|
|
230
|
+
* @returns The size of the list after deleting the element at the specified index.
|
|
231
|
+
*/
|
|
232
|
+
deleteAt(index) {
|
|
233
|
+
(0, utils_1.rangeCheck)(index, 0, this._size - 1);
|
|
234
|
+
let node = this._head;
|
|
235
|
+
while (index--) {
|
|
236
|
+
node = node.next;
|
|
237
|
+
}
|
|
238
|
+
this._deleteNode(node);
|
|
239
|
+
return this._size;
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Time Complexity: O(1)
|
|
243
|
+
* Space Complexity: O(1)
|
|
244
|
+
*
|
|
245
|
+
* The function checks if a data structure is empty by comparing its size to zero.
|
|
246
|
+
* @returns The method is returning a boolean value indicating whether the size of the object is 0 or
|
|
247
|
+
* not.
|
|
248
|
+
*/
|
|
249
|
+
isEmpty() {
|
|
250
|
+
return this._size === 0;
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Time Complexity: O(1)
|
|
254
|
+
* Space Complexity: O(1)
|
|
255
|
+
*
|
|
256
|
+
* The `clear` function clears all the elements in a data structure and resets its properties.
|
|
257
|
+
*/
|
|
258
|
+
clear() {
|
|
259
|
+
this._noObjMap = {};
|
|
260
|
+
this._size = 0;
|
|
261
|
+
this._head = this._tail = this._sentinel.prev = this._sentinel.next = this._sentinel;
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Time Complexity: O(n), where n is the number of elements in the HashMap.
|
|
265
|
+
* Space Complexity: O(1)
|
|
266
|
+
*
|
|
267
|
+
* The `forEach` function iterates over each element in a HashMap and executes a callback function on
|
|
268
|
+
* each element.
|
|
269
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
270
|
+
* HashMap. It takes three arguments:
|
|
271
|
+
*/
|
|
272
|
+
forEach(callback) {
|
|
273
|
+
let index = 0;
|
|
274
|
+
let node = this._head;
|
|
275
|
+
while (node !== this._sentinel) {
|
|
276
|
+
callback([node.key, node.value], index++, this);
|
|
277
|
+
node = node.next;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* The `filter` function takes a predicate function and returns a new HashMap containing only the
|
|
282
|
+
* key-value pairs that satisfy the predicate.
|
|
283
|
+
* @param predicate - The `predicate` parameter is a function that takes two arguments: `element` and
|
|
284
|
+
* `map`.
|
|
285
|
+
* @returns a new HashMap object that contains the key-value pairs from the original HashMap that
|
|
286
|
+
* satisfy the given predicate function.
|
|
287
|
+
*/
|
|
288
|
+
filter(predicate) {
|
|
289
|
+
const filteredMap = new HashMap();
|
|
290
|
+
for (const [key, value] of this) {
|
|
291
|
+
if (predicate([key, value], this)) {
|
|
292
|
+
filteredMap.set(key, value);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
return filteredMap;
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* The `map` function takes a callback function and returns a new HashMap with the values transformed
|
|
299
|
+
* by the callback.
|
|
300
|
+
* @param callback - The `callback` parameter is a function that takes two arguments: `element` and
|
|
301
|
+
* `map`.
|
|
302
|
+
* @returns a new HashMap object with the values mapped according to the provided callback function.
|
|
303
|
+
*/
|
|
304
|
+
map(callback) {
|
|
305
|
+
const mappedMap = new HashMap();
|
|
306
|
+
for (const [key, value] of this) {
|
|
307
|
+
const newValue = callback([key, value], this);
|
|
308
|
+
mappedMap.set(key, newValue);
|
|
309
|
+
}
|
|
310
|
+
return mappedMap;
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* The `reduce` function iterates over the elements of a HashMap and applies a callback function to
|
|
314
|
+
* each element, accumulating a single value.
|
|
315
|
+
* @param callback - The callback parameter is a function that takes three arguments: accumulator,
|
|
316
|
+
* element, and map. It is called for each element in the HashMap and is used to accumulate a single
|
|
317
|
+
* result.
|
|
318
|
+
* @param {A} initialValue - The `initialValue` parameter is the initial value of the accumulator. It
|
|
319
|
+
* is the value that will be passed as the first argument to the `callback` function when reducing
|
|
320
|
+
* the elements of the map.
|
|
321
|
+
* @returns The `reduce` function is returning the final value of the accumulator after iterating
|
|
322
|
+
* over all the elements in the HashMap and applying the callback function to each element.
|
|
323
|
+
*/
|
|
324
|
+
reduce(callback, initialValue) {
|
|
325
|
+
let accumulator = initialValue;
|
|
326
|
+
for (const element of this) {
|
|
327
|
+
accumulator = callback(accumulator, element, this);
|
|
328
|
+
}
|
|
329
|
+
return accumulator;
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* Time Complexity: O(n), where n is the number of elements in the HashMap.
|
|
333
|
+
* Space Complexity: O(1)
|
|
334
|
+
*
|
|
335
|
+
* The above function is an iterator that yields key-value pairs from a linked list.
|
|
336
|
+
*/
|
|
337
|
+
*[Symbol.iterator]() {
|
|
338
|
+
let node = this._head;
|
|
339
|
+
while (node !== this._sentinel) {
|
|
340
|
+
yield [node.key, node.value];
|
|
341
|
+
node = node.next;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* Time Complexity: O(1)
|
|
346
|
+
* Space Complexity: O(1)
|
|
347
|
+
*
|
|
348
|
+
* The `_deleteNode` function removes a node from a doubly linked list and updates the head and tail
|
|
349
|
+
* pointers if necessary.
|
|
350
|
+
* @param node - The `node` parameter is an instance of the `HashMapLinkedNode` class, which
|
|
351
|
+
* represents a node in a linked list. It contains a key-value pair and references to the previous
|
|
352
|
+
* and next nodes in the list.
|
|
353
|
+
*/
|
|
354
|
+
_deleteNode(node) {
|
|
355
|
+
const { prev, next } = node;
|
|
356
|
+
prev.next = next;
|
|
357
|
+
next.prev = prev;
|
|
358
|
+
if (node === this._head) {
|
|
359
|
+
this._head = next;
|
|
360
|
+
}
|
|
361
|
+
if (node === this._tail) {
|
|
362
|
+
this._tail = prev;
|
|
363
|
+
}
|
|
364
|
+
this._size -= 1;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
exports.HashMap = HashMap;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Tyler Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
7
|
+
*/
|
|
8
|
+
export declare class HashTableNode<K, V> {
|
|
9
|
+
key: K;
|
|
10
|
+
value: V;
|
|
11
|
+
next: HashTableNode<K, V> | null;
|
|
12
|
+
constructor(key: K, value: V);
|
|
13
|
+
}
|
|
14
|
+
import { HashFunction } from '../../types';
|
|
15
|
+
export declare class HashTable<K, V> {
|
|
16
|
+
protected static readonly DEFAULT_CAPACITY = 16;
|
|
17
|
+
protected static readonly LOAD_FACTOR = 0.75;
|
|
18
|
+
constructor(capacity?: number, hashFn?: HashFunction<K>);
|
|
19
|
+
protected _capacity: number;
|
|
20
|
+
get capacity(): number;
|
|
21
|
+
protected _size: number;
|
|
22
|
+
get size(): number;
|
|
23
|
+
protected _buckets: Array<HashTableNode<K, V> | null>;
|
|
24
|
+
get buckets(): Array<HashTableNode<K, V> | null>;
|
|
25
|
+
protected _hashFn: HashFunction<K>;
|
|
26
|
+
get hashFn(): HashFunction<K>;
|
|
27
|
+
/**
|
|
28
|
+
* The set function adds a key-value pair to the hash table, handling collisions and resizing if necessary.
|
|
29
|
+
* @param {K} key - The key parameter represents the key of the key-value pair that you want to insert into the hash
|
|
30
|
+
* table. It is of type K, which is a generic type representing the key's data type.
|
|
31
|
+
* @param {V} value - The parameter `value` represents the value that you want to associate with the given key in the hash
|
|
32
|
+
* table.
|
|
33
|
+
* @returns Nothing is being returned. The return type of the `put` method is `void`, which means it does not return any
|
|
34
|
+
* value.
|
|
35
|
+
*/
|
|
36
|
+
set(key: K, value: V): void;
|
|
37
|
+
/**
|
|
38
|
+
* The `get` function retrieves the value associated with a given key from a hash table.
|
|
39
|
+
* @param {K} key - The `key` parameter represents the key of the element that we want to retrieve from the data
|
|
40
|
+
* structure.
|
|
41
|
+
* @returns The method is returning the value associated with the given key if it exists in the hash table. If the key is
|
|
42
|
+
* not found, it returns `undefined`.
|
|
43
|
+
*/
|
|
44
|
+
get(key: K): V | undefined;
|
|
45
|
+
/**
|
|
46
|
+
* The delete function removes a key-value pair from a hash table.
|
|
47
|
+
* @param {K} key - The `key` parameter represents the key of the key-value pair that needs to be removed from the hash
|
|
48
|
+
* table.
|
|
49
|
+
* @returns Nothing is being returned. The `delete` method has a return type of `void`, which means it does not return
|
|
50
|
+
* any value.
|
|
51
|
+
*/
|
|
52
|
+
delete(key: K): void;
|
|
53
|
+
/**
|
|
54
|
+
* The function `_defaultHashFn` calculates the hash value of a given key and returns the remainder when divided by the
|
|
55
|
+
* capacity of the data structure.
|
|
56
|
+
* @param {K} key - The `key` parameter is the input value that needs to be hashed. It can be of any type, but in this
|
|
57
|
+
* code snippet, it is checked whether the key is a string or an object. If it is a string, the `_murmurStringHashFn`
|
|
58
|
+
* function is used to
|
|
59
|
+
* @returns the hash value of the key modulo the capacity of the data structure.
|
|
60
|
+
*/
|
|
61
|
+
protected _defaultHashFn(key: K): number;
|
|
62
|
+
/**
|
|
63
|
+
* The `_multiplicativeStringHashFn` function calculates a hash value for a given string key using the multiplicative
|
|
64
|
+
* string hash function.
|
|
65
|
+
* @param {K} key - The `key` parameter is the input value for which we want to calculate the hash. It can be of any
|
|
66
|
+
* type, as it is generic (`K`). The function converts the `key` to a string using the `String()` function.
|
|
67
|
+
* @returns a number, which is the result of the multiplicative string hash function applied to the input key.
|
|
68
|
+
*/
|
|
69
|
+
protected _multiplicativeStringHashFn<K>(key: K): number;
|
|
70
|
+
/**
|
|
71
|
+
* The function `_murmurStringHashFn` calculates a hash value for a given string key using the MurmurHash algorithm.
|
|
72
|
+
* @param {K} key - The `key` parameter is the input value for which you want to calculate the hash. It can be of any
|
|
73
|
+
* type, but it will be converted to a string using the `String()` function before calculating the hash.
|
|
74
|
+
* @returns a number, which is the hash value calculated for the given key.
|
|
75
|
+
*/
|
|
76
|
+
protected _murmurStringHashFn<K>(key: K): number;
|
|
77
|
+
/**
|
|
78
|
+
* The _hash function takes a key and returns a number.
|
|
79
|
+
* @param {K} key - The parameter "key" is of type K, which represents the type of the key that will be hashed.
|
|
80
|
+
* @returns The hash function is returning a number.
|
|
81
|
+
*/
|
|
82
|
+
protected _hash(key: K): number;
|
|
83
|
+
/**
|
|
84
|
+
* The function calculates a hash value for a given string using the djb2 algorithm.
|
|
85
|
+
* @param {string} key - The `key` parameter in the `stringHash` function is a string value that represents the input for
|
|
86
|
+
* which we want to calculate the hash value.
|
|
87
|
+
* @returns a number, which is the hash value of the input string.
|
|
88
|
+
*/
|
|
89
|
+
protected _stringHash(key: string): number;
|
|
90
|
+
/**
|
|
91
|
+
* The function `_objectHash` takes a key and returns a hash value, using a custom hash function for objects.
|
|
92
|
+
* @param {K} key - The parameter "key" is of type "K", which means it can be any type. It could be a string, number,
|
|
93
|
+
* boolean, object, or any other type of value. The purpose of the objectHash function is to generate a hash value for
|
|
94
|
+
* the key, which can be used for
|
|
95
|
+
* @returns a number, which is the hash value of the key.
|
|
96
|
+
*/
|
|
97
|
+
protected _objectHash(key: K): number;
|
|
98
|
+
/**
|
|
99
|
+
* The `expand` function increases the capacity of a hash table by creating a new array of buckets with double the
|
|
100
|
+
* capacity and rehashing all the existing key-value pairs into the new buckets.
|
|
101
|
+
*/
|
|
102
|
+
protected _expand(): void;
|
|
103
|
+
}
|