min-heap-typed 1.40.0-rc → 1.40.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.dependency-cruiser.js +422 -422
- package/.eslintrc.js +59 -59
- package/.prettierrc.js +14 -14
- package/README.md +20 -3
- package/coverage/clover.xml +11 -7
- package/coverage/coverage-final.json +95 -1
- package/coverage/coverage-summary.json +59 -2
- package/coverage/lcov-report/base.css +278 -99
- package/coverage/lcov-report/index.html +69 -65
- package/coverage/lcov-report/index.ts.html +36 -35
- package/coverage/lcov-report/sorter.js +15 -5
- package/dist/data-structures/binary-tree/avl-tree.d.ts +106 -0
- package/dist/data-structures/binary-tree/avl-tree.js +347 -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 +355 -0
- package/dist/data-structures/binary-tree/binary-tree.js +1115 -0
- package/dist/data-structures/binary-tree/bst.d.ts +167 -0
- package/dist/data-structures/binary-tree/bst.js +512 -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 +11 -0
- package/dist/data-structures/binary-tree/rb-tree.js +21 -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-multiset.d.ts +126 -0
- package/dist/data-structures/binary-tree/tree-multiset.js +355 -0
- package/dist/data-structures/graph/abstract-graph.d.ts +313 -0
- package/dist/data-structures/graph/abstract-graph.js +884 -0
- package/dist/data-structures/graph/directed-graph.d.ts +194 -0
- package/dist/data-structures/graph/directed-graph.js +404 -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 +120 -0
- package/dist/data-structures/graph/undirected-graph.js +239 -0
- package/dist/data-structures/hash/coordinate-map.d.ts +44 -0
- package/dist/data-structures/hash/coordinate-map.js +62 -0
- package/dist/data-structures/hash/coordinate-set.d.ts +36 -0
- package/dist/data-structures/hash/coordinate-set.js +52 -0
- package/dist/data-structures/hash/hash-map.d.ts +50 -0
- package/dist/data-structures/hash/hash-map.js +153 -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 +6 -0
- package/dist/data-structures/hash/index.js +22 -0
- package/dist/data-structures/hash/tree-map.d.ts +2 -0
- package/dist/data-structures/hash/tree-map.js +6 -0
- package/dist/data-structures/hash/tree-set.d.ts +2 -0
- package/dist/data-structures/hash/tree-set.js +6 -0
- package/dist/data-structures/heap/heap.d.ts +235 -0
- package/dist/data-structures/heap/heap.js +515 -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 +253 -0
- package/dist/data-structures/linked-list/doubly-linked-list.js +569 -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 +232 -0
- package/dist/data-structures/linked-list/singly-linked-list.js +533 -0
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +80 -0
- package/dist/data-structures/linked-list/skip-linked-list.js +187 -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 +161 -0
- package/dist/data-structures/queue/deque.js +264 -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 +122 -0
- package/dist/data-structures/queue/queue.js +187 -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 +64 -0
- package/dist/data-structures/stack/stack.js +94 -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 +79 -0
- package/dist/data-structures/trie/trie.js +251 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +18 -4
- 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/data-structures/binary-tree/avl-tree.d.ts +4 -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 +29 -0
- package/dist/types/data-structures/binary-tree/binary-tree.js +24 -0
- package/dist/types/data-structures/binary-tree/bst.d.ts +7 -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 +8 -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-multiset.d.ts +4 -0
- package/dist/types/data-structures/binary-tree/tree-multiset.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 +6 -0
- package/dist/types/data-structures/graph/directed-graph.js +9 -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/coordinate-map.d.ts +1 -0
- package/dist/types/data-structures/hash/coordinate-map.js +2 -0
- package/dist/types/data-structures/hash/coordinate-set.d.ts +1 -0
- package/dist/types/data-structures/hash/coordinate-set.js +2 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +1 -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 +1 -0
- package/dist/types/data-structures/hash/index.js +2 -0
- package/dist/types/data-structures/hash/tree-map.d.ts +1 -0
- package/dist/types/data-structures/hash/tree-map.js +2 -0
- package/dist/types/data-structures/hash/tree-set.d.ts +1 -0
- package/dist/types/data-structures/hash/tree-set.js +2 -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/helpers.d.ts +8 -0
- package/dist/types/helpers.js +9 -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 +20 -0
- package/dist/utils/utils.js +73 -0
- package/jest.config.js +6 -6
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +350 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +306 -0
- package/src/data-structures/binary-tree/binary-tree.ts +1262 -0
- package/src/data-structures/binary-tree/bst.ts +522 -0
- package/src/data-structures/binary-tree/index.ts +7 -0
- package/src/data-structures/binary-tree/rb-tree.ts +358 -0
- package/src/data-structures/binary-tree/segment-tree.ts +190 -0
- package/src/data-structures/binary-tree/tree-multiset.ts +379 -0
- package/src/data-structures/graph/abstract-graph.ts +1000 -0
- package/src/data-structures/graph/directed-graph.ts +449 -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 +259 -0
- package/src/data-structures/hash/coordinate-map.ts +63 -0
- package/src/data-structures/hash/coordinate-set.ts +52 -0
- package/src/data-structures/hash/hash-map.ts +185 -0
- package/src/data-structures/hash/hash-table.ts +268 -0
- package/src/data-structures/hash/index.ts +6 -0
- package/src/data-structures/hash/tree-map.ts +2 -0
- package/src/data-structures/hash/tree-set.ts +2 -0
- package/src/data-structures/heap/heap.ts +589 -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 +605 -0
- package/src/data-structures/linked-list/index.ts +3 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +576 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +221 -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 +282 -0
- package/src/data-structures/queue/index.ts +2 -0
- package/src/data-structures/queue/queue.ts +209 -0
- package/src/data-structures/stack/index.ts +1 -0
- package/src/data-structures/stack/stack.ts +102 -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 +262 -0
- package/src/index.ts +4 -1
- 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/data-structures/binary-tree/avl-tree.ts +5 -0
- package/src/types/data-structures/binary-tree/binary-indexed-tree.ts +1 -0
- package/src/types/data-structures/binary-tree/binary-tree.ts +31 -0
- package/src/types/data-structures/binary-tree/bst.ts +11 -0
- package/src/types/data-structures/binary-tree/index.ts +6 -0
- package/src/types/data-structures/binary-tree/rb-tree.ts +8 -0
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -0
- package/src/types/data-structures/binary-tree/tree-multiset.ts +6 -0
- package/src/types/data-structures/graph/abstract-graph.ts +11 -0
- package/src/types/data-structures/graph/directed-graph.ts +8 -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/coordinate-map.ts +1 -0
- package/src/types/data-structures/hash/coordinate-set.ts +1 -0
- package/src/types/data-structures/hash/hash-map.ts +1 -0
- package/src/types/data-structures/hash/hash-table.ts +1 -0
- package/src/types/data-structures/hash/index.ts +1 -0
- package/src/types/data-structures/hash/tree-map.ts +1 -0
- package/src/types/data-structures/hash/tree-set.ts +1 -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/helpers.ts +11 -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 +86 -0
- package/tsconfig.json +1 -2
|
@@ -0,0 +1,259 @@
|
|
|
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 {arrayRemove} from '../../utils';
|
|
9
|
+
import {AbstractEdge, AbstractGraph, AbstractVertex} from './abstract-graph';
|
|
10
|
+
import type {VertexKey} from '../../types';
|
|
11
|
+
import {IGraph} from '../../interfaces';
|
|
12
|
+
|
|
13
|
+
export class UndirectedVertex<V = any> extends AbstractVertex<V> {
|
|
14
|
+
/**
|
|
15
|
+
* The constructor function initializes a vertex with an optional value.
|
|
16
|
+
* @param {VertexKey} key - The `key` parameter is of type `VertexKey` and represents the identifier of the vertex. It is
|
|
17
|
+
* used to uniquely identify the vertex within a graph or network.
|
|
18
|
+
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It is used to initialize the value of the
|
|
19
|
+
* vertex. If no value is provided, the vertex will be initialized with a default value.
|
|
20
|
+
*/
|
|
21
|
+
constructor(key: VertexKey, value?: V) {
|
|
22
|
+
super(key, value);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export class UndirectedEdge<E = number> extends AbstractEdge<E> {
|
|
27
|
+
vertices: [VertexKey, VertexKey];
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The constructor function creates an instance of a class with two vertex IDs, an optional weight, and an optional
|
|
31
|
+
* value.
|
|
32
|
+
* @param {VertexKey} v1 - The first vertex ID of the edge.
|
|
33
|
+
* @param {VertexKey} v2 - The parameter `v2` is a `VertexKey`, which represents the identifier of the second vertex in a
|
|
34
|
+
* graph edge.
|
|
35
|
+
* @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge.
|
|
36
|
+
* @param {E} [value] - The "value" parameter is an optional parameter of type E. It is used to store a value associated
|
|
37
|
+
* with the edge.
|
|
38
|
+
*/
|
|
39
|
+
constructor(v1: VertexKey, v2: VertexKey, weight?: number, value?: E) {
|
|
40
|
+
super(weight, value);
|
|
41
|
+
this.vertices = [v1, v2];
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export class UndirectedGraph<
|
|
46
|
+
V = any,
|
|
47
|
+
E = any,
|
|
48
|
+
VO extends UndirectedVertex<V> = UndirectedVertex<V>,
|
|
49
|
+
EO extends UndirectedEdge<E> = UndirectedEdge<E>
|
|
50
|
+
>
|
|
51
|
+
extends AbstractGraph<V, E, VO, EO>
|
|
52
|
+
implements IGraph<V, E, VO, EO> {
|
|
53
|
+
/**
|
|
54
|
+
* The constructor initializes a new Map object to store edges.
|
|
55
|
+
*/
|
|
56
|
+
constructor() {
|
|
57
|
+
super();
|
|
58
|
+
this._edges = new Map<VO, EO[]>();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
protected _edges: Map<VO, EO[]>;
|
|
62
|
+
|
|
63
|
+
get edges(): Map<VO, EO[]> {
|
|
64
|
+
return this._edges;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* The function creates a new vertex with an optional value and returns it.
|
|
69
|
+
* @param {VertexKey} key - The `key` parameter is the unique identifier for the vertex. It is used to distinguish one
|
|
70
|
+
* vertex from another in the graph.
|
|
71
|
+
* @param [value] - The `value` parameter is an optional value that can be assigned to the vertex. If a value is provided,
|
|
72
|
+
* it will be used as the value of the vertex. If no value is provided, the `key` parameter will be used as the value of
|
|
73
|
+
* the vertex.
|
|
74
|
+
* @returns The method is returning a new instance of the `UndirectedVertex` class, casted as type `VO`.
|
|
75
|
+
*/
|
|
76
|
+
override createVertex(key: VertexKey, value?: VO['value']): VO {
|
|
77
|
+
return new UndirectedVertex(key, value ?? key) as VO;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The function creates an undirected edge between two vertices with an optional weight and value.
|
|
82
|
+
* @param {VertexKey} v1 - The parameter `v1` represents the first vertex of the edge.
|
|
83
|
+
* @param {VertexKey} v2 - The parameter `v2` represents the second vertex of the edge.
|
|
84
|
+
* @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge. If
|
|
85
|
+
* no weight is provided, it defaults to 1.
|
|
86
|
+
* @param [value] - The `value` parameter is an optional value that can be assigned to the edge. It can be of any type and
|
|
87
|
+
* is used to store additional information or data associated with the edge.
|
|
88
|
+
* @returns a new instance of the `UndirectedEdge` class, which is casted as type `EO`.
|
|
89
|
+
*/
|
|
90
|
+
override createEdge(v1: VertexKey, v2: VertexKey, weight?: number, value?: EO['value']): EO {
|
|
91
|
+
return new UndirectedEdge(v1, v2, weight ?? 1, value) as EO;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* The function `getEdge` returns the first edge that connects two vertices, or null if no such edge exists.
|
|
96
|
+
* @param {VO | VertexKey | null} v1 - The parameter `v1` represents a vertex or vertex ID. It can be of type `VO` (vertex
|
|
97
|
+
* object), `null`, or `VertexKey` (a string or number representing the ID of a vertex).
|
|
98
|
+
* @param {VO | VertexKey | null} v2 - The parameter `v2` represents a vertex or vertex ID. It can be of type `VO` (vertex
|
|
99
|
+
* object), `null`, or `VertexKey` (vertex ID).
|
|
100
|
+
* @returns an edge (EO) or null.
|
|
101
|
+
*/
|
|
102
|
+
getEdge(v1: VO | VertexKey | null, v2: VO | VertexKey | null): EO | null {
|
|
103
|
+
let edges: EO[] | undefined = [];
|
|
104
|
+
|
|
105
|
+
if (v1 !== null && v2 !== null) {
|
|
106
|
+
const vertex1: VO | null = this._getVertex(v1);
|
|
107
|
+
const vertex2: VO | null = this._getVertex(v2);
|
|
108
|
+
|
|
109
|
+
if (vertex1 && vertex2) {
|
|
110
|
+
edges = this._edges.get(vertex1)?.filter(e => e.vertices.includes(vertex2.key));
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return edges ? edges[0] || null : null;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* The function removes an edge between two vertices in a graph and returns the removed edge.
|
|
119
|
+
* @param {VO | VertexKey} v1 - The parameter `v1` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
|
|
120
|
+
* @param {VO | VertexKey} v2 - VO | VertexKey - This parameter can be either a vertex object (VO) or a vertex ID
|
|
121
|
+
* (VertexKey). It represents the second vertex of the edge that needs to be removed.
|
|
122
|
+
* @returns the removed edge (EO) if it exists, or null if either of the vertices (VO) does not exist.
|
|
123
|
+
*/
|
|
124
|
+
deleteEdgeBetween(v1: VO | VertexKey, v2: VO | VertexKey): EO | null {
|
|
125
|
+
const vertex1: VO | null = this._getVertex(v1);
|
|
126
|
+
const vertex2: VO | null = this._getVertex(v2);
|
|
127
|
+
|
|
128
|
+
if (!vertex1 || !vertex2) {
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const v1Edges = this._edges.get(vertex1);
|
|
133
|
+
let removed: EO | null = null;
|
|
134
|
+
if (v1Edges) {
|
|
135
|
+
removed = arrayRemove<EO>(v1Edges, (e: EO) => e.vertices.includes(vertex2.key))[0] || null;
|
|
136
|
+
}
|
|
137
|
+
const v2Edges = this._edges.get(vertex2);
|
|
138
|
+
if (v2Edges) {
|
|
139
|
+
arrayRemove<EO>(v2Edges, (e: EO) => e.vertices.includes(vertex1.key));
|
|
140
|
+
}
|
|
141
|
+
return removed;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* The deleteEdge function removes an edge between two vertices in a graph.
|
|
146
|
+
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
|
|
147
|
+
* @returns The method is returning either the removed edge (of type EO) or null if the edge was not found.
|
|
148
|
+
*/
|
|
149
|
+
deleteEdge(edge: EO): EO | null {
|
|
150
|
+
return this.deleteEdgeBetween(edge.vertices[0], edge.vertices[1]);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* The function `degreeOf` returns the degree of a vertex in a graph, which is the number of edges connected to that
|
|
155
|
+
* vertex.
|
|
156
|
+
* @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
|
|
157
|
+
* @returns The function `degreeOf` returns the degree of a vertex in a graph. The degree of a vertex is the number of
|
|
158
|
+
* edges connected to that vertex.
|
|
159
|
+
*/
|
|
160
|
+
degreeOf(vertexOrKey: VertexKey | VO): number {
|
|
161
|
+
const vertex = this._getVertex(vertexOrKey);
|
|
162
|
+
if (vertex) {
|
|
163
|
+
return this._edges.get(vertex)?.length || 0;
|
|
164
|
+
} else {
|
|
165
|
+
return 0;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* The function returns the edges of a given vertex or vertex ID.
|
|
171
|
+
* @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`. A `VertexKey` is a
|
|
172
|
+
* unique identifier for a vertex in a graph, while `VO` represents the type of the vertex.
|
|
173
|
+
* @returns an array of edges.
|
|
174
|
+
*/
|
|
175
|
+
edgesOf(vertexOrKey: VertexKey | VO): EO[] {
|
|
176
|
+
const vertex = this._getVertex(vertexOrKey);
|
|
177
|
+
if (vertex) {
|
|
178
|
+
return this._edges.get(vertex) || [];
|
|
179
|
+
} else {
|
|
180
|
+
return [];
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* The function "edgeSet" returns an array of unique edges from a set of edges.
|
|
186
|
+
* @returns The method `edgeSet()` returns an array of type `EO[]`.
|
|
187
|
+
*/
|
|
188
|
+
edgeSet(): EO[] {
|
|
189
|
+
const edgeSet: Set<EO> = new Set();
|
|
190
|
+
this._edges.forEach(edges => {
|
|
191
|
+
edges.forEach(edge => {
|
|
192
|
+
edgeSet.add(edge);
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
return [...edgeSet];
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* The function "getNeighbors" returns an array of neighboring vertices for a given vertex or vertex ID.
|
|
200
|
+
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
201
|
+
* (`VertexKey`).
|
|
202
|
+
* @returns an array of vertices (VO[]).
|
|
203
|
+
*/
|
|
204
|
+
getNeighbors(vertexOrKey: VO | VertexKey): VO[] {
|
|
205
|
+
const neighbors: VO[] = [];
|
|
206
|
+
const vertex = this._getVertex(vertexOrKey);
|
|
207
|
+
if (vertex) {
|
|
208
|
+
const neighborEdges = this.edgesOf(vertex);
|
|
209
|
+
for (const edge of neighborEdges) {
|
|
210
|
+
const neighbor = this._getVertex(edge.vertices.filter(e => e !== vertex.key)[0]);
|
|
211
|
+
if (neighbor) {
|
|
212
|
+
neighbors.push(neighbor);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return neighbors;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* The function "getEndsOfEdge" returns the vertices at the ends of an edge if the edge exists in the graph, otherwise
|
|
221
|
+
* it returns null.
|
|
222
|
+
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
|
|
223
|
+
* @returns The function `getEndsOfEdge` returns an array containing two vertices `[VO, VO]` if the edge exists in the
|
|
224
|
+
* graph. If the edge does not exist, it returns `null`.
|
|
225
|
+
*/
|
|
226
|
+
getEndsOfEdge(edge: EO): [VO, VO] | null {
|
|
227
|
+
if (!this.hasEdge(edge.vertices[0], edge.vertices[1])) {
|
|
228
|
+
return null;
|
|
229
|
+
}
|
|
230
|
+
const v1 = this._getVertex(edge.vertices[0]);
|
|
231
|
+
const v2 = this._getVertex(edge.vertices[1]);
|
|
232
|
+
if (v1 && v2) {
|
|
233
|
+
return [v1, v2];
|
|
234
|
+
} else {
|
|
235
|
+
return null;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* The function adds an edge to the graph by updating the adjacency list with the vertices of the edge.
|
|
241
|
+
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
|
|
242
|
+
* @returns a boolean value.
|
|
243
|
+
*/
|
|
244
|
+
protected _addEdgeOnly(edge: EO): boolean {
|
|
245
|
+
for (const end of edge.vertices) {
|
|
246
|
+
const endVertex = this._getVertex(end);
|
|
247
|
+
if (endVertex === null) return false;
|
|
248
|
+
if (endVertex) {
|
|
249
|
+
const edges = this._edges.get(endVertex);
|
|
250
|
+
if (edges) {
|
|
251
|
+
edges.push(edge);
|
|
252
|
+
} else {
|
|
253
|
+
this._edges.set(endVertex, [edge]);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return true;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
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 class CoordinateMap<V> extends Map<any, V> {
|
|
9
|
+
constructor(joint?: string) {
|
|
10
|
+
super();
|
|
11
|
+
if (joint !== undefined) this._joint = joint;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
protected _joint = '_';
|
|
15
|
+
|
|
16
|
+
get joint(): string {
|
|
17
|
+
return this._joint;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The "has" function overrides the base class's "has" function and checks if a key exists in the map by joining the
|
|
22
|
+
* key array with a specified delimiter.
|
|
23
|
+
* @param {number[]} key - The parameter "key" is an array of numbers.
|
|
24
|
+
* @returns The `has` method is being overridden to return the result of calling the `has` method of the superclass
|
|
25
|
+
* (`super.has`) with the `key` array joined together using the `_joint` property.
|
|
26
|
+
*/
|
|
27
|
+
override has(key: number[]) {
|
|
28
|
+
return super.has(key.join(this._joint));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The function overrides the set method of a Map object to convert the key from an array to a string using a specified
|
|
33
|
+
* delimiter before calling the original set method.
|
|
34
|
+
* @param {number[]} key - The key parameter is an array of numbers.
|
|
35
|
+
* @param {V} value - The value parameter is the value that you want to associate with the specified key.
|
|
36
|
+
* @returns The `set` method is returning the result of calling the `set` method of the superclass
|
|
37
|
+
* (`super.set(key.join(this._joint), value)`).
|
|
38
|
+
*/
|
|
39
|
+
override set(key: number[], value: V) {
|
|
40
|
+
return super.set(key.join(this._joint), value);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The function overrides the get method to join the key array with a specified joint and then calls the super get
|
|
45
|
+
* method.
|
|
46
|
+
* @param {number[]} key - An array of numbers
|
|
47
|
+
* @returns The code is returning the value associated with the specified key in the map.
|
|
48
|
+
*/
|
|
49
|
+
override get(key: number[]) {
|
|
50
|
+
return super.get(key.join(this._joint));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The function overrides the delete method and joins the key array using a specified joint character before calling
|
|
55
|
+
* the super delete method.
|
|
56
|
+
* @param {number[]} key - An array of numbers that represents the key to be deleted.
|
|
57
|
+
* @returns The `delete` method is returning the result of calling the `delete` method on the superclass, with the
|
|
58
|
+
* `key` array joined together using the `_joint` property.
|
|
59
|
+
*/
|
|
60
|
+
override delete(key: number[]) {
|
|
61
|
+
return super.delete(key.join(this._joint));
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
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 class CoordinateSet extends Set<any> {
|
|
9
|
+
constructor(joint?: string) {
|
|
10
|
+
super();
|
|
11
|
+
if (joint !== undefined) this._joint = joint;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
protected _joint = '_';
|
|
15
|
+
|
|
16
|
+
get joint(): string {
|
|
17
|
+
return this._joint;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The "has" function overrides the "has" method of the superclass and checks if a value exists in an array after
|
|
22
|
+
* joining its elements with a specified separator.
|
|
23
|
+
* @param {number[]} value - The parameter "value" is an array of numbers.
|
|
24
|
+
* @returns The overridden `has` method is returning the result of calling the `has` method of the superclass, passing
|
|
25
|
+
* in the joined value as an argument.
|
|
26
|
+
*/
|
|
27
|
+
override has(value: number[]) {
|
|
28
|
+
return super.has(value.join(this._joint));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The "add" function overrides the parent class's "add" function by joining the elements of the input array with a
|
|
33
|
+
* specified delimiter before calling the parent class's "add" function.
|
|
34
|
+
* @param {number[]} value - An array of numbers
|
|
35
|
+
* @returns The overridden `add` method is returning the result of calling the `add` method of the superclass
|
|
36
|
+
* (`super.add`) with the joined string representation of the `value` array (`value.join(this._joint)`).
|
|
37
|
+
*/
|
|
38
|
+
override add(value: number[]) {
|
|
39
|
+
return super.add(value.join(this._joint));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The function overrides the delete method and deletes an element from a Set by joining the elements of the input
|
|
44
|
+
* array with a specified joint and then calling the delete method of the parent class.
|
|
45
|
+
* @param {number[]} value - An array of numbers
|
|
46
|
+
* @returns The `delete` method is returning the result of calling the `delete` method of the superclass, with the
|
|
47
|
+
* `value` array joined together using the `_joint` property.
|
|
48
|
+
*/
|
|
49
|
+
override delete(value: number[]) {
|
|
50
|
+
return super.delete(value.join(this._joint));
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import {HashFunction} from '../../types';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* data-structure-typed
|
|
5
|
+
*
|
|
6
|
+
* @author Tyler Zeng
|
|
7
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
8
|
+
* @license MIT License
|
|
9
|
+
*/
|
|
10
|
+
export class HashMap<K, V> {
|
|
11
|
+
/**
|
|
12
|
+
* The constructor initializes the properties of a hash table, including the initial capacity, load factor, capacity
|
|
13
|
+
* multiplier, size, table array, and hash function.
|
|
14
|
+
* @param [initialCapacity=16] - The initial capacity is the initial size of the hash table. It determines the number of
|
|
15
|
+
* buckets or slots available for storing key-value pairs. The default value is 16.
|
|
16
|
+
* @param [loadFactor=0.75] - The load factor is a measure of how full the hash table can be before it is resized. It is
|
|
17
|
+
* a value between 0 and 1, where 1 means the hash table is completely full and 0 means it is completely empty. When the
|
|
18
|
+
* load factor is reached, the hash table will
|
|
19
|
+
* @param [hashFn] - The `hashFn` parameter is an optional parameter that represents the hash function used to calculate
|
|
20
|
+
* the index of a key in the hash table. If a custom hash function is not provided, a default hash function is used. The
|
|
21
|
+
* default hash function converts the key to a string, calculates the sum of the
|
|
22
|
+
*/
|
|
23
|
+
constructor(initialCapacity = 16, loadFactor = 0.75, hashFn?: HashFunction<K>) {
|
|
24
|
+
this._initialCapacity = initialCapacity;
|
|
25
|
+
this._loadFactor = loadFactor;
|
|
26
|
+
this._capacityMultiplier = 2;
|
|
27
|
+
this._size = 0;
|
|
28
|
+
this._table = new Array(initialCapacity);
|
|
29
|
+
this._hashFn =
|
|
30
|
+
hashFn ||
|
|
31
|
+
((key: K) => {
|
|
32
|
+
const strKey = String(key);
|
|
33
|
+
let hash = 0;
|
|
34
|
+
for (let i = 0; i < strKey.length; i++) {
|
|
35
|
+
hash += strKey.charCodeAt(i);
|
|
36
|
+
}
|
|
37
|
+
return hash % this.table.length;
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
protected _initialCapacity: number;
|
|
42
|
+
|
|
43
|
+
get initialCapacity(): number {
|
|
44
|
+
return this._initialCapacity;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
protected _loadFactor: number;
|
|
48
|
+
|
|
49
|
+
get loadFactor(): number {
|
|
50
|
+
return this._loadFactor;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
protected _capacityMultiplier: number;
|
|
54
|
+
|
|
55
|
+
get capacityMultiplier(): number {
|
|
56
|
+
return this._capacityMultiplier;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
protected _size: number;
|
|
60
|
+
|
|
61
|
+
get size(): number {
|
|
62
|
+
return this._size;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
protected _table: Array<Array<[K, V]>>;
|
|
66
|
+
|
|
67
|
+
get table(): Array<Array<[K, V]>> {
|
|
68
|
+
return this._table;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
protected _hashFn: HashFunction<K>;
|
|
72
|
+
|
|
73
|
+
get hashFn(): HashFunction<K> {
|
|
74
|
+
return this._hashFn;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
set(key: K, value: V): void {
|
|
78
|
+
const loadFactor = this.size / this.table.length;
|
|
79
|
+
if (loadFactor >= this.loadFactor) {
|
|
80
|
+
this.resizeTable(this.table.length * this.capacityMultiplier);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const index = this._hash(key);
|
|
84
|
+
if (!this.table[index]) {
|
|
85
|
+
this.table[index] = [];
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Check if the key already exists in the bucket
|
|
89
|
+
for (let i = 0; i < this.table[index].length; i++) {
|
|
90
|
+
if (this.table[index][i][0] === key) {
|
|
91
|
+
this.table[index][i][1] = value;
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
this.table[index].push([key, value]);
|
|
97
|
+
this._size++;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
get(key: K): V | undefined {
|
|
101
|
+
const index = this._hash(key);
|
|
102
|
+
if (!this.table[index]) {
|
|
103
|
+
return undefined;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
for (const [k, v] of this.table[index]) {
|
|
107
|
+
if (k === key) {
|
|
108
|
+
return v;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
delete(key: K): void {
|
|
116
|
+
const index = this._hash(key);
|
|
117
|
+
if (!this.table[index]) {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
for (let i = 0; i < this.table[index].length; i++) {
|
|
122
|
+
if (this.table[index][i][0] === key) {
|
|
123
|
+
this.table[index].splice(i, 1);
|
|
124
|
+
this._size--;
|
|
125
|
+
|
|
126
|
+
// Check if the table needs to be resized down
|
|
127
|
+
const loadFactor = this.size / this.table.length;
|
|
128
|
+
if (loadFactor < this.loadFactor / this.capacityMultiplier) {
|
|
129
|
+
this.resizeTable(this.table.length / this.capacityMultiplier);
|
|
130
|
+
}
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
* entries(): IterableIterator<[K, V]> {
|
|
137
|
+
for (const bucket of this.table) {
|
|
138
|
+
if (bucket) {
|
|
139
|
+
for (const [key, value] of bucket) {
|
|
140
|
+
yield [key, value];
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
[Symbol.iterator](): IterableIterator<[K, V]> {
|
|
147
|
+
return this.entries();
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
clear(): void {
|
|
151
|
+
this._size = 0;
|
|
152
|
+
this._table = new Array(this.initialCapacity);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
isEmpty(): boolean {
|
|
156
|
+
return this.size === 0;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
protected _hash(key: K): number {
|
|
160
|
+
return this._hashFn(key);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* The `resizeTable` function resizes the table used in a hash map by creating a new table with a specified capacity and
|
|
165
|
+
* rehashing the key-value pairs from the old table into the new table.
|
|
166
|
+
* @param {number} newCapacity - The newCapacity parameter is the desired capacity for the resized table. It represents
|
|
167
|
+
* the number of buckets that the new table should have.
|
|
168
|
+
*/
|
|
169
|
+
protected resizeTable(newCapacity: number): void {
|
|
170
|
+
const newTable = new Array(newCapacity);
|
|
171
|
+
for (const bucket of this._table) {
|
|
172
|
+
// Note that this is this._table
|
|
173
|
+
if (bucket) {
|
|
174
|
+
for (const [key, value] of bucket) {
|
|
175
|
+
const newIndex = this._hash(key) % newCapacity;
|
|
176
|
+
if (!newTable[newIndex]) {
|
|
177
|
+
newTable[newIndex] = [];
|
|
178
|
+
}
|
|
179
|
+
newTable[newIndex].push([key, value]);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
this._table = newTable; // Again, here is this._table
|
|
184
|
+
}
|
|
185
|
+
}
|