data-structure-typed 1.35.1 → 1.40.0-rc
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/binary-tree/aa-tree.d.ts +2 -0
- package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +527 -0
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +323 -0
- package/dist/data-structures/binary-tree/abstract-binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/avl-tree.d.ts +94 -0
- package/dist/data-structures/binary-tree/avl-tree.js +90 -3
- package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/b-tree.d.ts +2 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +46 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +36 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-tree.d.ts +31 -0
- package/dist/data-structures/binary-tree/binary-tree.js +21 -0
- package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/bst.d.ts +133 -0
- package/dist/data-structures/binary-tree/bst.js +114 -0
- package/dist/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/data-structures/binary-tree/index.d.ts +12 -0
- package/dist/data-structures/binary-tree/rb-tree.d.ts +13 -0
- package/dist/data-structures/binary-tree/segment-tree.d.ts +83 -0
- package/dist/data-structures/binary-tree/segment-tree.js +45 -0
- package/dist/data-structures/binary-tree/segment-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/splay-tree.d.ts +2 -0
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +209 -0
- package/dist/data-structures/binary-tree/tree-multiset.js +178 -0
- package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/data-structures/binary-tree/two-three-tree.d.ts +2 -0
- package/dist/data-structures/graph/abstract-graph.d.ts +332 -0
- package/dist/data-structures/graph/abstract-graph.js +269 -4
- package/dist/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/data-structures/graph/directed-graph.d.ts +200 -0
- package/dist/data-structures/graph/directed-graph.js +167 -0
- package/dist/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/data-structures/graph/index.d.ts +4 -0
- package/dist/data-structures/graph/map-graph.d.ts +79 -0
- package/dist/data-structures/graph/map-graph.js +54 -0
- package/dist/data-structures/graph/map-graph.js.map +1 -1
- package/dist/data-structures/graph/undirected-graph.d.ts +127 -0
- package/dist/data-structures/graph/undirected-graph.js +105 -0
- package/dist/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/data-structures/hash/coordinate-map.d.ts +45 -0
- package/dist/data-structures/hash/coordinate-map.js +35 -0
- package/dist/data-structures/hash/coordinate-map.js.map +1 -1
- package/dist/data-structures/hash/coordinate-set.d.ts +37 -0
- package/dist/data-structures/hash/coordinate-set.js +28 -0
- package/dist/data-structures/hash/coordinate-set.js.map +1 -1
- package/dist/data-structures/hash/hash-map.d.ts +56 -0
- package/dist/data-structures/hash/hash-map.js +29 -1
- package/dist/data-structures/hash/hash-map.js.map +1 -1
- package/dist/data-structures/hash/hash-table.d.ts +106 -0
- package/dist/data-structures/hash/hash-table.js +88 -6
- package/dist/data-structures/hash/hash-table.js.map +1 -1
- package/dist/data-structures/hash/index.d.ts +7 -0
- package/dist/data-structures/hash/pair.d.ts +2 -0
- package/dist/data-structures/hash/tree-map.d.ts +2 -0
- package/dist/data-structures/hash/tree-set.d.ts +2 -0
- package/dist/data-structures/heap/heap.d.ts +83 -0
- package/dist/data-structures/heap/heap.js +62 -0
- package/dist/data-structures/heap/heap.js.map +1 -1
- package/dist/data-structures/heap/index.d.ts +3 -0
- package/dist/data-structures/heap/max-heap.d.ts +23 -0
- package/dist/data-structures/heap/max-heap.js +16 -0
- package/dist/data-structures/heap/max-heap.js.map +1 -1
- package/dist/data-structures/heap/min-heap.d.ts +24 -0
- package/dist/data-structures/heap/min-heap.js +17 -0
- package/dist/data-structures/heap/min-heap.js.map +1 -1
- package/dist/data-structures/index.d.ts +11 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +234 -0
- package/dist/data-structures/linked-list/doubly-linked-list.js +202 -0
- package/dist/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/data-structures/linked-list/index.d.ts +3 -0
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +157 -0
- package/dist/data-structures/linked-list/singly-linked-list.js +135 -0
- package/dist/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +61 -0
- package/dist/data-structures/linked-list/skip-linked-list.js +36 -0
- package/dist/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/data-structures/matrix/index.d.ts +4 -0
- package/dist/data-structures/matrix/matrix.d.ts +21 -0
- package/dist/data-structures/matrix/matrix.js +15 -0
- package/dist/data-structures/matrix/matrix.js.map +1 -1
- package/dist/data-structures/matrix/matrix2d.d.ts +108 -0
- package/dist/data-structures/matrix/matrix2d.js +91 -2
- package/dist/data-structures/matrix/matrix2d.js.map +1 -1
- package/dist/data-structures/matrix/navigator.d.ts +52 -0
- package/dist/data-structures/matrix/navigator.js +28 -0
- package/dist/data-structures/matrix/navigator.js.map +1 -1
- package/dist/data-structures/matrix/vector2d.d.ts +201 -0
- package/dist/data-structures/matrix/vector2d.js +188 -1
- package/dist/data-structures/matrix/vector2d.js.map +1 -1
- package/dist/data-structures/priority-queue/index.d.ts +3 -0
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +15 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +18 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +15 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +19 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/data-structures/priority-queue/priority-queue.d.ts +180 -0
- package/dist/data-structures/priority-queue/priority-queue.js +141 -0
- package/dist/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/data-structures/queue/deque.d.ts +165 -0
- package/dist/data-structures/queue/deque.js +124 -0
- package/dist/data-structures/queue/deque.js.map +1 -1
- package/dist/data-structures/queue/index.d.ts +2 -0
- package/dist/data-structures/queue/queue.d.ts +107 -0
- package/dist/data-structures/queue/queue.js +80 -0
- package/dist/data-structures/queue/queue.js.map +1 -1
- package/dist/data-structures/stack/index.d.ts +1 -0
- package/dist/data-structures/stack/stack.d.ts +63 -0
- package/dist/data-structures/stack/stack.js +50 -0
- package/dist/data-structures/stack/stack.js.map +1 -1
- package/dist/data-structures/tree/index.d.ts +1 -0
- package/dist/data-structures/tree/tree.d.ts +14 -0
- package/dist/data-structures/tree/tree.js +1 -0
- package/dist/data-structures/tree/tree.js.map +1 -1
- package/dist/data-structures/trie/index.d.ts +1 -0
- package/dist/data-structures/trie/trie.d.ts +61 -0
- package/dist/data-structures/trie/trie.js +36 -0
- package/dist/data-structures/trie/trie.js.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/interfaces/abstract-binary-tree.d.ts +7 -0
- package/dist/interfaces/abstract-graph.d.ts +5 -0
- package/dist/interfaces/avl-tree.d.ts +7 -0
- package/dist/interfaces/binary-tree.d.ts +6 -0
- package/dist/interfaces/bst.d.ts +6 -0
- package/dist/interfaces/directed-graph.d.ts +3 -0
- package/dist/interfaces/doubly-linked-list.d.ts +1 -0
- package/dist/interfaces/heap.d.ts +1 -0
- package/dist/interfaces/index.d.ts +15 -0
- package/dist/interfaces/navigator.d.ts +1 -0
- package/dist/interfaces/priority-queue.d.ts +1 -0
- package/dist/interfaces/rb-tree.d.ts +6 -0
- package/dist/interfaces/segment-tree.d.ts +1 -0
- package/dist/interfaces/singly-linked-list.d.ts +1 -0
- package/dist/interfaces/tree-multiset.d.ts +6 -0
- package/dist/interfaces/undirected-graph.d.ts +3 -0
- package/dist/types/data-structures/abstract-binary-tree.d.ts +34 -0
- package/dist/types/data-structures/abstract-binary-tree.js +6 -0
- package/dist/types/data-structures/abstract-binary-tree.js.map +1 -1
- package/dist/types/data-structures/abstract-graph.d.ts +11 -0
- package/dist/types/data-structures/avl-tree.d.ts +4 -0
- package/dist/types/data-structures/binary-tree.d.ts +4 -0
- package/dist/types/data-structures/bst.d.ts +13 -0
- package/dist/types/data-structures/directed-graph.d.ts +6 -0
- package/dist/types/data-structures/doubly-linked-list.d.ts +1 -0
- package/dist/types/data-structures/hash.d.ts +1 -0
- package/dist/types/data-structures/heap.d.ts +3 -0
- package/dist/types/data-structures/index.d.ts +16 -0
- package/dist/types/data-structures/map-graph.d.ts +1 -0
- package/dist/types/data-structures/navigator.d.ts +14 -0
- package/dist/types/data-structures/priority-queue.d.ts +7 -0
- package/dist/types/data-structures/rb-tree.d.ts +8 -0
- package/dist/types/data-structures/segment-tree.d.ts +1 -0
- package/dist/types/data-structures/singly-linked-list.d.ts +1 -0
- package/dist/types/data-structures/tree-multiset.d.ts +4 -0
- package/dist/types/helpers.d.ts +1 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/utils/index.d.ts +2 -0
- package/dist/types/utils/utils.d.ts +7 -0
- package/dist/types/utils/validate-type.d.ts +19 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/utils.d.ts +19 -0
- package/package.json +10 -6
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { HashFunction } from '../../types';
|
|
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
|
+
export declare class HashMap<K, V> {
|
|
10
|
+
get hashFn(): HashFunction<K>;
|
|
11
|
+
set hashFn(value: HashFunction<K>);
|
|
12
|
+
get table(): Array<Array<[K, V]>>;
|
|
13
|
+
set table(value: Array<Array<[K, V]>>);
|
|
14
|
+
get capacityMultiplier(): number;
|
|
15
|
+
set capacityMultiplier(value: number);
|
|
16
|
+
get loadFactor(): number;
|
|
17
|
+
set loadFactor(value: number);
|
|
18
|
+
get initialCapacity(): number;
|
|
19
|
+
set initialCapacity(value: number);
|
|
20
|
+
get size(): number;
|
|
21
|
+
set size(value: number);
|
|
22
|
+
private _initialCapacity;
|
|
23
|
+
private _loadFactor;
|
|
24
|
+
private _capacityMultiplier;
|
|
25
|
+
private _size;
|
|
26
|
+
private _table;
|
|
27
|
+
private _hashFn;
|
|
28
|
+
/**
|
|
29
|
+
* The constructor initializes the properties of a hash table, including the initial capacity, load factor, capacity
|
|
30
|
+
* multiplier, size, table array, and hash function.
|
|
31
|
+
* @param [initialCapacity=16] - The initial capacity is the initial size of the hash table. It determines the number of
|
|
32
|
+
* buckets or slots available for storing key-value pairs. The default value is 16.
|
|
33
|
+
* @param [loadFactor=0.75] - The load factor is a measure of how full the hash table can be before it is resized. It is
|
|
34
|
+
* a value between 0 and 1, where 1 means the hash table is completely full and 0 means it is completely empty. When the
|
|
35
|
+
* load factor is reached, the hash table will
|
|
36
|
+
* @param [hashFn] - The `hashFn` parameter is an optional parameter that represents the hash function used to calculate
|
|
37
|
+
* the index of a key in the hash table. If a custom hash function is not provided, a default hash function is used. The
|
|
38
|
+
* default hash function converts the key to a string, calculates the sum of the
|
|
39
|
+
*/
|
|
40
|
+
constructor(initialCapacity?: number, loadFactor?: number, hashFn?: HashFunction<K>);
|
|
41
|
+
private _hash;
|
|
42
|
+
/**
|
|
43
|
+
* The `resizeTable` function resizes the table used in a hash map by creating a new table with a specified capacity and
|
|
44
|
+
* rehashing the key-value pairs from the old table into the new table.
|
|
45
|
+
* @param {number} newCapacity - The newCapacity parameter is the desired capacity for the resized table. It represents
|
|
46
|
+
* the number of buckets that the new table should have.
|
|
47
|
+
*/
|
|
48
|
+
private resizeTable;
|
|
49
|
+
set(key: K, value: V): void;
|
|
50
|
+
get(key: K): V | undefined;
|
|
51
|
+
remove(key: K): void;
|
|
52
|
+
entries(): IterableIterator<[K, V]>;
|
|
53
|
+
[Symbol.iterator](): IterableIterator<[K, V]>;
|
|
54
|
+
clear(): void;
|
|
55
|
+
isEmpty(): boolean;
|
|
56
|
+
}
|
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.HashMap = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* data-structure-typed
|
|
6
|
+
*
|
|
7
|
+
* @author Tyler Zeng
|
|
8
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
9
|
+
* @license MIT License
|
|
10
|
+
*/
|
|
4
11
|
class HashMap {
|
|
5
12
|
get hashFn() {
|
|
6
13
|
return this._hashFn;
|
|
@@ -38,6 +45,18 @@ class HashMap {
|
|
|
38
45
|
set size(value) {
|
|
39
46
|
this._size = value;
|
|
40
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* The constructor initializes the properties of a hash table, including the initial capacity, load factor, capacity
|
|
50
|
+
* multiplier, size, table array, and hash function.
|
|
51
|
+
* @param [initialCapacity=16] - The initial capacity is the initial size of the hash table. It determines the number of
|
|
52
|
+
* buckets or slots available for storing key-value pairs. The default value is 16.
|
|
53
|
+
* @param [loadFactor=0.75] - The load factor is a measure of how full the hash table can be before it is resized. It is
|
|
54
|
+
* a value between 0 and 1, where 1 means the hash table is completely full and 0 means it is completely empty. When the
|
|
55
|
+
* load factor is reached, the hash table will
|
|
56
|
+
* @param [hashFn] - The `hashFn` parameter is an optional parameter that represents the hash function used to calculate
|
|
57
|
+
* the index of a key in the hash table. If a custom hash function is not provided, a default hash function is used. The
|
|
58
|
+
* default hash function converts the key to a string, calculates the sum of the
|
|
59
|
+
*/
|
|
41
60
|
constructor(initialCapacity = 16, loadFactor = 0.75, hashFn) {
|
|
42
61
|
this._initialCapacity = initialCapacity;
|
|
43
62
|
this._loadFactor = loadFactor;
|
|
@@ -58,9 +77,16 @@ class HashMap {
|
|
|
58
77
|
_hash(key) {
|
|
59
78
|
return this._hashFn(key);
|
|
60
79
|
}
|
|
80
|
+
/**
|
|
81
|
+
* The `resizeTable` function resizes the table used in a hash map by creating a new table with a specified capacity and
|
|
82
|
+
* rehashing the key-value pairs from the old table into the new table.
|
|
83
|
+
* @param {number} newCapacity - The newCapacity parameter is the desired capacity for the resized table. It represents
|
|
84
|
+
* the number of buckets that the new table should have.
|
|
85
|
+
*/
|
|
61
86
|
resizeTable(newCapacity) {
|
|
62
87
|
const newTable = new Array(newCapacity);
|
|
63
88
|
for (const bucket of this._table) {
|
|
89
|
+
// Note that this is this._table
|
|
64
90
|
if (bucket) {
|
|
65
91
|
for (const [key, value] of bucket) {
|
|
66
92
|
const newIndex = this._hash(key) % newCapacity;
|
|
@@ -71,7 +97,7 @@ class HashMap {
|
|
|
71
97
|
}
|
|
72
98
|
}
|
|
73
99
|
}
|
|
74
|
-
this._table = newTable;
|
|
100
|
+
this._table = newTable; // Again, here is this._table
|
|
75
101
|
}
|
|
76
102
|
set(key, value) {
|
|
77
103
|
const loadFactor = this.size / this.table.length;
|
|
@@ -82,6 +108,7 @@ class HashMap {
|
|
|
82
108
|
if (!this.table[index]) {
|
|
83
109
|
this.table[index] = [];
|
|
84
110
|
}
|
|
111
|
+
// Check if the key already exists in the bucket
|
|
85
112
|
for (let i = 0; i < this.table[index].length; i++) {
|
|
86
113
|
if (this.table[index][i][0] === key) {
|
|
87
114
|
this.table[index][i][1] = value;
|
|
@@ -112,6 +139,7 @@ class HashMap {
|
|
|
112
139
|
if (this.table[index][i][0] === key) {
|
|
113
140
|
this.table[index].splice(i, 1);
|
|
114
141
|
this.size--;
|
|
142
|
+
// Check if the table needs to be resized down
|
|
115
143
|
const loadFactor = this.size / this.table.length;
|
|
116
144
|
if (loadFactor < this.loadFactor / this.capacityMultiplier) {
|
|
117
145
|
this.resizeTable(this.table.length / this.capacityMultiplier);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hash-map.js","sourceRoot":"","sources":["../../../src/data-structures/hash/hash-map.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"hash-map.js","sourceRoot":"","sources":["../../../src/data-structures/hash/hash-map.ts"],"names":[],"mappings":";;;AAEA;;;;;;GAMG;AACH,MAAa,OAAO;IAClB,IAAI,MAAM;QACR,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;IAED,IAAI,MAAM,CAAC,KAAsB;QAC/B,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC;IACvB,CAAC;IACD,IAAI,KAAK;QACP,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED,IAAI,KAAK,CAAC,KAA2B;QACnC,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;IACtB,CAAC;IAED,IAAI,kBAAkB;QACpB,OAAO,IAAI,CAAC,mBAAmB,CAAC;IAClC,CAAC;IAED,IAAI,kBAAkB,CAAC,KAAa;QAClC,IAAI,CAAC,mBAAmB,GAAG,KAAK,CAAC;IACnC,CAAC;IAED,IAAI,UAAU;QACZ,OAAO,IAAI,CAAC,WAAW,CAAC;IAC1B,CAAC;IAED,IAAI,UAAU,CAAC,KAAa;QAC1B,IAAI,CAAC,WAAW,GAAG,KAAK,CAAC;IAC3B,CAAC;IAED,IAAI,eAAe;QACjB,OAAO,IAAI,CAAC,gBAAgB,CAAC;IAC/B,CAAC;IAED,IAAI,eAAe,CAAC,KAAa;QAC/B,IAAI,CAAC,gBAAgB,GAAG,KAAK,CAAC;IAChC,CAAC;IAED,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;IAED,IAAI,IAAI,CAAC,KAAa;QACpB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACrB,CAAC;IASD;;;;;;;;;;;OAWG;IACH,YAAY,eAAe,GAAG,EAAE,EAAE,UAAU,GAAG,IAAI,EAAE,MAAwB;QAC3E,IAAI,CAAC,gBAAgB,GAAG,eAAe,C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I,MAAM,EAAE;oBACjC,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;iBACpB;aACF;SACF;IACH,CAAC;IAED,CAAC,MAAM,CAAC,QAAQ,CAAC;QACf,OAAO,IAAI,CAAC,OAAO,EAAE,CAAC;IACxB,CAAC;IAED,KAAK;QACH,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC;QACd,IAAI,CAAC,KAAK,GAAG,IAAI,KAAK,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC;IAC/C,CAAC;IAED,OAAO;QACL,OAAO,IAAI,CAAC,IAAI,KAAK,CAAC,CAAC;IACzB,CAAC;CACF;AAjMD,0BAiMC"}
|
|
@@ -0,0 +1,106 @@
|
|
|
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
|
+
val: V;
|
|
11
|
+
next: HashTableNode<K, V> | null;
|
|
12
|
+
constructor(key: K, val: V);
|
|
13
|
+
}
|
|
14
|
+
import { HashFunction } from '../../types';
|
|
15
|
+
export declare class HashTable<K, V> {
|
|
16
|
+
get hashFn(): HashFunction<K>;
|
|
17
|
+
set hashFn(value: HashFunction<K>);
|
|
18
|
+
get buckets(): Array<HashTableNode<K, V> | null>;
|
|
19
|
+
set buckets(value: Array<HashTableNode<K, V> | null>);
|
|
20
|
+
get capacity(): number;
|
|
21
|
+
set capacity(value: number);
|
|
22
|
+
private static readonly DEFAULT_CAPACITY;
|
|
23
|
+
private static readonly LOAD_FACTOR;
|
|
24
|
+
private _capacity;
|
|
25
|
+
private _size;
|
|
26
|
+
private _buckets;
|
|
27
|
+
private _hashFn;
|
|
28
|
+
constructor(capacity?: number, hashFn?: HashFunction<K>);
|
|
29
|
+
/**
|
|
30
|
+
* The function `_defaultHashFn` calculates the hash value of a given key and returns the remainder when divided by the
|
|
31
|
+
* capacity of the data structure.
|
|
32
|
+
* @param {K} key - The `key` parameter is the input value that needs to be hashed. It can be of any type, but in this
|
|
33
|
+
* code snippet, it is checked whether the key is a string or an object. If it is a string, the `_murmurStringHashFn`
|
|
34
|
+
* function is used to
|
|
35
|
+
* @returns the hash value of the key modulo the capacity of the data structure.
|
|
36
|
+
*/
|
|
37
|
+
protected _defaultHashFn(key: K): number;
|
|
38
|
+
/**
|
|
39
|
+
* The `_multiplicativeStringHashFn` function calculates a hash value for a given string key using the multiplicative
|
|
40
|
+
* string hash function.
|
|
41
|
+
* @param {K} key - The `key` parameter is the input value for which we want to calculate the hash. It can be of any
|
|
42
|
+
* type, as it is generic (`K`). The function converts the `key` to a string using the `String()` function.
|
|
43
|
+
* @returns a number, which is the result of the multiplicative string hash function applied to the input key.
|
|
44
|
+
*/
|
|
45
|
+
protected _multiplicativeStringHashFn<K>(key: K): number;
|
|
46
|
+
/**
|
|
47
|
+
* The function `_murmurStringHashFn` calculates a hash value for a given string key using the MurmurHash algorithm.
|
|
48
|
+
* @param {K} key - The `key` parameter is the input value for which you want to calculate the hash. It can be of any
|
|
49
|
+
* type, but it will be converted to a string using the `String()` function before calculating the hash.
|
|
50
|
+
* @returns a number, which is the hash value calculated for the given key.
|
|
51
|
+
*/
|
|
52
|
+
protected _murmurStringHashFn<K>(key: K): number;
|
|
53
|
+
/**
|
|
54
|
+
* The _hash function takes a key and returns a number.
|
|
55
|
+
* @param {K} key - The parameter "key" is of type K, which represents the type of the key that will be hashed.
|
|
56
|
+
* @returns The hash function is returning a number.
|
|
57
|
+
*/
|
|
58
|
+
protected _hash(key: K): number;
|
|
59
|
+
/**
|
|
60
|
+
* The function calculates a hash value for a given string using the djb2 algorithm.
|
|
61
|
+
* @param {string} key - The `key` parameter in the `stringHash` function is a string value that represents the input for
|
|
62
|
+
* which we want to calculate the hash value.
|
|
63
|
+
* @returns a number, which is the hash value of the input string.
|
|
64
|
+
*/
|
|
65
|
+
protected _stringHash(key: string): number;
|
|
66
|
+
/**
|
|
67
|
+
* The function `_objectHash` takes a key and returns a hash value, using a custom hash function for objects.
|
|
68
|
+
* @param {K} key - The parameter "key" is of type "K", which means it can be any type. It could be a string, number,
|
|
69
|
+
* boolean, object, or any other type of value. The purpose of the objectHash function is to generate a hash value for
|
|
70
|
+
* the key, which can be used for
|
|
71
|
+
* @returns a number, which is the hash value of the key.
|
|
72
|
+
*/
|
|
73
|
+
protected _objectHash(key: K): number;
|
|
74
|
+
/**
|
|
75
|
+
* The set function adds a key-value pair to the hash table, handling collisions and resizing if necessary.
|
|
76
|
+
* @param {K} key - The key parameter represents the key of the key-value pair that you want to insert into the hash
|
|
77
|
+
* table. It is of type K, which is a generic type representing the key's data type.
|
|
78
|
+
* @param {V} val - The parameter `val` represents the value that you want to associate with the given key in the hash
|
|
79
|
+
* table.
|
|
80
|
+
* @returns Nothing is being returned. The return type of the `put` method is `void`, which means it does not return any
|
|
81
|
+
* value.
|
|
82
|
+
*/
|
|
83
|
+
set(key: K, val: V): void;
|
|
84
|
+
/**
|
|
85
|
+
* The `get` function retrieves the value associated with a given key from a hash table.
|
|
86
|
+
* @param {K} key - The `key` parameter represents the key of the element that we want to retrieve from the data
|
|
87
|
+
* structure.
|
|
88
|
+
* @returns The method is returning the value associated with the given key if it exists in the hash table. If the key is
|
|
89
|
+
* not found, it returns `undefined`.
|
|
90
|
+
*/
|
|
91
|
+
get(key: K): V | undefined;
|
|
92
|
+
/**
|
|
93
|
+
* The remove function removes a key-value pair from a hash table.
|
|
94
|
+
* @param {K} key - The `key` parameter represents the key of the key-value pair that needs to be removed from the hash
|
|
95
|
+
* table.
|
|
96
|
+
* @returns Nothing is being returned. The `remove` method has a return type of `void`, which means it does not return
|
|
97
|
+
* any value.
|
|
98
|
+
*/
|
|
99
|
+
remove(key: K): void;
|
|
100
|
+
/**
|
|
101
|
+
* The `expand` function increases the capacity of a hash table by creating a new array of buckets with double the
|
|
102
|
+
* capacity and rehashing all the existing key-value pairs into the new buckets.
|
|
103
|
+
*/
|
|
104
|
+
protected _expand(): void;
|
|
105
|
+
get size(): number;
|
|
106
|
+
}
|
|
@@ -1,4 +1,11 @@
|
|
|
1
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
|
+
*/
|
|
2
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
10
|
exports.HashTable = exports.HashTableNode = void 0;
|
|
4
11
|
class HashTableNode {
|
|
@@ -34,21 +41,44 @@ class HashTable {
|
|
|
34
41
|
this._size = 0;
|
|
35
42
|
this._buckets = new Array(this._capacity).fill(null);
|
|
36
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* The function `_defaultHashFn` calculates the hash value of a given key and returns the remainder when divided by the
|
|
46
|
+
* capacity of the data structure.
|
|
47
|
+
* @param {K} key - The `key` parameter is the input value that needs to be hashed. It can be of any type, but in this
|
|
48
|
+
* code snippet, it is checked whether the key is a string or an object. If it is a string, the `_murmurStringHashFn`
|
|
49
|
+
* function is used to
|
|
50
|
+
* @returns the hash value of the key modulo the capacity of the data structure.
|
|
51
|
+
*/
|
|
37
52
|
_defaultHashFn(key) {
|
|
53
|
+
// Can be replaced with other hash functions as needed
|
|
38
54
|
const hashValue = typeof key === 'string' ? this._murmurStringHashFn(key) : this._objectHash(key);
|
|
39
55
|
return hashValue % this._capacity;
|
|
40
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* The `_multiplicativeStringHashFn` function calculates a hash value for a given string key using the multiplicative
|
|
59
|
+
* string hash function.
|
|
60
|
+
* @param {K} key - The `key` parameter is the input value for which we want to calculate the hash. It can be of any
|
|
61
|
+
* type, as it is generic (`K`). The function converts the `key` to a string using the `String()` function.
|
|
62
|
+
* @returns a number, which is the result of the multiplicative string hash function applied to the input key.
|
|
63
|
+
*/
|
|
41
64
|
_multiplicativeStringHashFn(key) {
|
|
42
65
|
const keyString = String(key);
|
|
43
66
|
let hash = 0;
|
|
44
67
|
for (let i = 0; i < keyString.length; i++) {
|
|
45
68
|
const charCode = keyString.charCodeAt(i);
|
|
69
|
+
// Some constants for adjusting the hash function
|
|
46
70
|
const A = 0.618033988749895;
|
|
47
|
-
const M = 1 << 30;
|
|
71
|
+
const M = 1 << 30; // 2^30
|
|
48
72
|
hash = (hash * A + charCode) % M;
|
|
49
73
|
}
|
|
50
|
-
return Math.abs(hash);
|
|
51
|
-
}
|
|
74
|
+
return Math.abs(hash); // Take absolute value to ensure non-negative numbers
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* The function `_murmurStringHashFn` calculates a hash value for a given string key using the MurmurHash algorithm.
|
|
78
|
+
* @param {K} key - The `key` parameter is the input value for which you want to calculate the hash. It can be of any
|
|
79
|
+
* type, but it will be converted to a string using the `String()` function before calculating the hash.
|
|
80
|
+
* @returns a number, which is the hash value calculated for the given key.
|
|
81
|
+
*/
|
|
52
82
|
_murmurStringHashFn(key) {
|
|
53
83
|
const keyString = String(key);
|
|
54
84
|
const seed = 0;
|
|
@@ -61,9 +91,20 @@ class HashTable {
|
|
|
61
91
|
}
|
|
62
92
|
return Math.abs(hash);
|
|
63
93
|
}
|
|
94
|
+
/**
|
|
95
|
+
* The _hash function takes a key and returns a number.
|
|
96
|
+
* @param {K} key - The parameter "key" is of type K, which represents the type of the key that will be hashed.
|
|
97
|
+
* @returns The hash function is returning a number.
|
|
98
|
+
*/
|
|
64
99
|
_hash(key) {
|
|
65
100
|
return this.hashFn(key);
|
|
66
101
|
}
|
|
102
|
+
/**
|
|
103
|
+
* The function calculates a hash value for a given string using the djb2 algorithm.
|
|
104
|
+
* @param {string} key - The `key` parameter in the `stringHash` function is a string value that represents the input for
|
|
105
|
+
* which we want to calculate the hash value.
|
|
106
|
+
* @returns a number, which is the hash value of the input string.
|
|
107
|
+
*/
|
|
67
108
|
_stringHash(key) {
|
|
68
109
|
let hash = 0;
|
|
69
110
|
for (let i = 0; i < key.length; i++) {
|
|
@@ -71,9 +112,28 @@ class HashTable {
|
|
|
71
112
|
}
|
|
72
113
|
return hash;
|
|
73
114
|
}
|
|
115
|
+
/**
|
|
116
|
+
* The function `_objectHash` takes a key and returns a hash value, using a custom hash function for objects.
|
|
117
|
+
* @param {K} key - The parameter "key" is of type "K", which means it can be any type. It could be a string, number,
|
|
118
|
+
* boolean, object, or any other type of value. The purpose of the objectHash function is to generate a hash value for
|
|
119
|
+
* the key, which can be used for
|
|
120
|
+
* @returns a number, which is the hash value of the key.
|
|
121
|
+
*/
|
|
74
122
|
_objectHash(key) {
|
|
123
|
+
// If the key is an object, you can write a custom hash function
|
|
124
|
+
// For example, convert the object's properties to a string and use string hashing
|
|
125
|
+
// This is just an example; you should write a specific object hash function as needed
|
|
75
126
|
return this._stringHash(JSON.stringify(key));
|
|
76
127
|
}
|
|
128
|
+
/**
|
|
129
|
+
* The set function adds a key-value pair to the hash table, handling collisions and resizing if necessary.
|
|
130
|
+
* @param {K} key - The key parameter represents the key of the key-value pair that you want to insert into the hash
|
|
131
|
+
* table. It is of type K, which is a generic type representing the key's data type.
|
|
132
|
+
* @param {V} val - The parameter `val` represents the value that you want to associate with the given key in the hash
|
|
133
|
+
* table.
|
|
134
|
+
* @returns Nothing is being returned. The return type of the `put` method is `void`, which means it does not return any
|
|
135
|
+
* value.
|
|
136
|
+
*/
|
|
77
137
|
set(key, val) {
|
|
78
138
|
const index = this._hash(key);
|
|
79
139
|
const newNode = new HashTableNode(key, val);
|
|
@@ -81,9 +141,11 @@ class HashTable {
|
|
|
81
141
|
this._buckets[index] = newNode;
|
|
82
142
|
}
|
|
83
143
|
else {
|
|
144
|
+
// Handle collisions, consider using open addressing, etc.
|
|
84
145
|
let currentNode = this._buckets[index];
|
|
85
146
|
while (currentNode) {
|
|
86
147
|
if (currentNode.key === key) {
|
|
148
|
+
// If the key already exists, update the value
|
|
87
149
|
currentNode.val = val;
|
|
88
150
|
return;
|
|
89
151
|
}
|
|
@@ -92,13 +154,22 @@ class HashTable {
|
|
|
92
154
|
}
|
|
93
155
|
currentNode = currentNode.next;
|
|
94
156
|
}
|
|
157
|
+
// Add to the end of the linked list
|
|
95
158
|
currentNode.next = newNode;
|
|
96
159
|
}
|
|
97
160
|
this._size++;
|
|
161
|
+
// If the load factor is too high, resize the hash table
|
|
98
162
|
if (this._size / this._capacity >= HashTable.LOAD_FACTOR) {
|
|
99
163
|
this._expand();
|
|
100
164
|
}
|
|
101
165
|
}
|
|
166
|
+
/**
|
|
167
|
+
* The `get` function retrieves the value associated with a given key from a hash table.
|
|
168
|
+
* @param {K} key - The `key` parameter represents the key of the element that we want to retrieve from the data
|
|
169
|
+
* structure.
|
|
170
|
+
* @returns The method is returning the value associated with the given key if it exists in the hash table. If the key is
|
|
171
|
+
* not found, it returns `undefined`.
|
|
172
|
+
*/
|
|
102
173
|
get(key) {
|
|
103
174
|
const index = this._hash(key);
|
|
104
175
|
let currentNode = this._buckets[index];
|
|
@@ -108,8 +179,15 @@ class HashTable {
|
|
|
108
179
|
}
|
|
109
180
|
currentNode = currentNode.next;
|
|
110
181
|
}
|
|
111
|
-
return undefined;
|
|
112
|
-
}
|
|
182
|
+
return undefined; // Key not found
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* The remove function removes a key-value pair from a hash table.
|
|
186
|
+
* @param {K} key - The `key` parameter represents the key of the key-value pair that needs to be removed from the hash
|
|
187
|
+
* table.
|
|
188
|
+
* @returns Nothing is being returned. The `remove` method has a return type of `void`, which means it does not return
|
|
189
|
+
* any value.
|
|
190
|
+
*/
|
|
113
191
|
remove(key) {
|
|
114
192
|
const index = this._hash(key);
|
|
115
193
|
let currentNode = this._buckets[index];
|
|
@@ -123,13 +201,17 @@ class HashTable {
|
|
|
123
201
|
this._buckets[index] = currentNode.next;
|
|
124
202
|
}
|
|
125
203
|
this._size--;
|
|
126
|
-
currentNode.next = null;
|
|
204
|
+
currentNode.next = null; // Release memory
|
|
127
205
|
return;
|
|
128
206
|
}
|
|
129
207
|
prevNode = currentNode;
|
|
130
208
|
currentNode = currentNode.next;
|
|
131
209
|
}
|
|
132
210
|
}
|
|
211
|
+
/**
|
|
212
|
+
* The `expand` function increases the capacity of a hash table by creating a new array of buckets with double the
|
|
213
|
+
* capacity and rehashing all the existing key-value pairs into the new buckets.
|
|
214
|
+
*/
|
|
133
215
|
_expand() {
|
|
134
216
|
const newCapacity = this._capacity * 2;
|
|
135
217
|
const newBuckets = new Array(newCapacity).fill(null);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hash-table.js","sourceRoot":"","sources":["../../../src/data-structures/hash/hash-table.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"hash-table.js","sourceRoot":"","sources":["../../../src/data-structures/hash/hash-table.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;;AAEH,MAAa,aAAa;IAKxB,YAAY,GAAM,EAAE,GAAM;QACxB,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;CACF;AAVD,sCAUC;AAID,MAAa,SAAS;IACpB,IAAI,MAAM;QACR,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;IAED,IAAI,MAAM,CAAC,KAAsB;QAC/B,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC;IACvB,CAAC;IAED,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;IAED,IAAI,OAAO,CAAC,KAAwC;QAClD,IAAI,CAAC,QAAQ,GAAG,KAAK,CAAC;IACxB,CAAC;IAED,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,SAAS,CAAC;IACxB,CAAC;IAED,IAAI,QA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aAAa,CAAO,WAAW,CAAC,GAAG,EAAE,WAAW,CAAC,GAAG,CAAC,CAAC;gBAE1E,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE;oBACzB,UAAU,CAAC,QAAQ,CAAC,GAAG,OAAO,CAAC;iBAChC;qBAAM;oBACL,IAAI,cAAc,GAAG,UAAU,CAAC,QAAQ,CAAE,CAAC;oBAC3C,OAAO,cAAc,CAAC,IAAI,EAAE;wBAC1B,cAAc,GAAG,cAAc,CAAC,IAAI,CAAC;qBACtC;oBACD,cAAc,CAAC,IAAI,GAAG,OAAO,CAAC;iBAC/B;gBACD,WAAW,GAAG,WAAW,CAAC,IAAI,CAAC;aAChC;SACF;QAED,IAAI,CAAC,QAAQ,GAAG,UAAU,CAAC;QAC3B,IAAI,CAAC,SAAS,GAAG,WAAW,CAAC;IAC/B,CAAC;IAED,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;;AA7PH,8BA8PC;AArOyB,0BAAgB,GAAG,EAAE,CAAC;AACtB,qBAAW,GAAG,IAAI,CAAC"}
|
|
@@ -0,0 +1,83 @@
|
|
|
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 { PriorityQueue } from '../priority-queue';
|
|
9
|
+
import type { HeapOptions } from '../../types';
|
|
10
|
+
export declare class HeapItem<V = any> {
|
|
11
|
+
/**
|
|
12
|
+
* The constructor function initializes an instance of a class with a priority and a value.
|
|
13
|
+
* @param {number} priority - The `priority` parameter is a number that represents the priority of the value. It is
|
|
14
|
+
* optional and has a default value of `NaN`.
|
|
15
|
+
* @param {V | null} [val=null] - The `val` parameter is of type `V | null`, which means it can accept a value of type
|
|
16
|
+
* `V` or `null`.
|
|
17
|
+
*/
|
|
18
|
+
constructor(priority?: number, val?: V | null);
|
|
19
|
+
private _priority;
|
|
20
|
+
get priority(): number;
|
|
21
|
+
set priority(value: number);
|
|
22
|
+
private _val;
|
|
23
|
+
get val(): V | null;
|
|
24
|
+
set val(value: V | null);
|
|
25
|
+
}
|
|
26
|
+
export declare abstract class Heap<V = any> {
|
|
27
|
+
/**
|
|
28
|
+
* The function is a constructor for a class that initializes a priority callback function based on the
|
|
29
|
+
* options provided.
|
|
30
|
+
* @param [options] - An optional object that contains configuration options for the Heap.
|
|
31
|
+
*/
|
|
32
|
+
protected constructor(options?: HeapOptions<V>);
|
|
33
|
+
protected abstract _pq: PriorityQueue<HeapItem<V>>;
|
|
34
|
+
get pq(): PriorityQueue<HeapItem<V>>;
|
|
35
|
+
protected _priorityExtractor: (val: V) => number;
|
|
36
|
+
get priorityExtractor(): (val: V) => number;
|
|
37
|
+
/**
|
|
38
|
+
* The function returns the size of a priority queue.
|
|
39
|
+
* @returns The size of the priority queue.
|
|
40
|
+
*/
|
|
41
|
+
get size(): number;
|
|
42
|
+
/**
|
|
43
|
+
* The function checks if a priority queue is empty.
|
|
44
|
+
* @returns {boolean} A boolean value indicating whether the size of the priority queue is less than 1.
|
|
45
|
+
*/
|
|
46
|
+
isEmpty(): boolean;
|
|
47
|
+
peek(isItem?: undefined): V | undefined;
|
|
48
|
+
peek(isItem: false): V | undefined;
|
|
49
|
+
peek(isItem: true): HeapItem<V> | null;
|
|
50
|
+
peekLast(isItem?: undefined): V | undefined;
|
|
51
|
+
peekLast(isItem: false): V | undefined;
|
|
52
|
+
peekLast(isItem: true): HeapItem<V> | null;
|
|
53
|
+
/**
|
|
54
|
+
* The `add` function adds an val to a priority queue with an optional priority value.
|
|
55
|
+
* @param {V} val - The `val` parameter represents the value that you want to add to the heap. It can be of any
|
|
56
|
+
* type.
|
|
57
|
+
* @param {number} [priority] - The `priority` parameter is an optional number that represents the priority of the
|
|
58
|
+
* val being added to the heap. If the `val` parameter is a number, then the `priority` parameter is set to
|
|
59
|
+
* the value of `val`. If the `val` parameter is not a number, then the
|
|
60
|
+
* @returns The `add` method returns the instance of the `Heap` class.
|
|
61
|
+
* @throws {Error} if priority is not a valid number
|
|
62
|
+
*/
|
|
63
|
+
add(priority: number, val?: V): Heap<V>;
|
|
64
|
+
poll(isItem?: undefined): V | undefined;
|
|
65
|
+
poll(isItem: false): V | undefined;
|
|
66
|
+
poll(isItem: true): HeapItem<V> | null;
|
|
67
|
+
/**
|
|
68
|
+
* The function checks if a given node or value exists in the priority queue.
|
|
69
|
+
* @param {V | HeapItem<V>} node - The parameter `node` can be of type `V` or `HeapItem<V>`.
|
|
70
|
+
* @returns a boolean value.
|
|
71
|
+
*/
|
|
72
|
+
has(node: V | HeapItem<V>): boolean;
|
|
73
|
+
toArray(isItem?: undefined): (V | undefined)[];
|
|
74
|
+
toArray(isItem: false): (V | undefined)[];
|
|
75
|
+
toArray(isItem: true): (HeapItem<V> | null)[];
|
|
76
|
+
sort(isItem?: undefined): (V | undefined)[];
|
|
77
|
+
sort(isItem: false): (V | undefined)[];
|
|
78
|
+
sort(isItem: true): (HeapItem<V> | null)[];
|
|
79
|
+
/**
|
|
80
|
+
* The clear function clears the priority queue.
|
|
81
|
+
*/
|
|
82
|
+
clear(): void;
|
|
83
|
+
}
|