min-heap-typed 1.49.6 → 1.49.7
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/avl-tree.d.ts +3 -3
- package/dist/data-structures/binary-tree/avl-tree.js +5 -5
- package/dist/data-structures/binary-tree/binary-tree.d.ts +3 -3
- package/dist/data-structures/binary-tree/binary-tree.js +7 -9
- package/dist/data-structures/binary-tree/bst.d.ts +3 -3
- package/dist/data-structures/binary-tree/bst.js +6 -7
- package/dist/data-structures/binary-tree/rb-tree.d.ts +2 -2
- package/dist/data-structures/binary-tree/rb-tree.js +4 -4
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +1 -1
- package/dist/data-structures/binary-tree/tree-multimap.js +3 -3
- package/dist/data-structures/hash/hash-map.d.ts +24 -27
- package/dist/data-structures/hash/hash-map.js +35 -35
- package/dist/data-structures/hash/index.d.ts +0 -1
- package/dist/data-structures/hash/index.js +0 -1
- package/dist/data-structures/heap/heap.d.ts +2 -1
- package/dist/data-structures/heap/heap.js +13 -13
- package/dist/data-structures/heap/max-heap.js +1 -1
- package/dist/data-structures/heap/min-heap.js +1 -1
- package/dist/data-structures/linked-list/doubly-linked-list.js +1 -1
- package/dist/data-structures/linked-list/singly-linked-list.js +1 -3
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -8
- package/dist/data-structures/linked-list/skip-linked-list.js +15 -18
- package/dist/data-structures/matrix/matrix.d.ts +2 -7
- package/dist/data-structures/matrix/matrix.js +0 -7
- package/dist/data-structures/priority-queue/max-priority-queue.js +1 -1
- package/dist/data-structures/priority-queue/min-priority-queue.js +1 -1
- package/dist/data-structures/priority-queue/priority-queue.js +1 -1
- package/dist/data-structures/queue/deque.d.ts +2 -11
- package/dist/data-structures/queue/deque.js +9 -13
- package/dist/data-structures/queue/queue.d.ts +13 -13
- package/dist/data-structures/queue/queue.js +29 -25
- package/dist/data-structures/stack/stack.js +2 -3
- package/dist/data-structures/trie/trie.d.ts +2 -2
- package/dist/data-structures/trie/trie.js +9 -5
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +1 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +5 -2
- package/dist/types/data-structures/hash/index.d.ts +0 -1
- package/dist/types/data-structures/hash/index.js +0 -1
- package/dist/types/data-structures/heap/heap.d.ts +1 -1
- package/dist/types/data-structures/linked-list/index.d.ts +1 -0
- package/dist/types/data-structures/linked-list/index.js +1 -0
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +4 -1
- package/dist/types/data-structures/matrix/index.d.ts +1 -0
- package/dist/types/data-structures/matrix/index.js +1 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +7 -1
- package/dist/types/data-structures/queue/deque.d.ts +3 -1
- package/dist/types/data-structures/trie/trie.d.ts +3 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +4 -4
- package/src/data-structures/binary-tree/binary-tree.ts +6 -10
- package/src/data-structures/binary-tree/bst.ts +5 -7
- package/src/data-structures/binary-tree/rb-tree.ts +3 -3
- package/src/data-structures/binary-tree/tree-multimap.ts +2 -2
- package/src/data-structures/hash/hash-map.ts +46 -50
- package/src/data-structures/hash/index.ts +0 -1
- package/src/data-structures/heap/heap.ts +20 -19
- package/src/data-structures/heap/max-heap.ts +1 -1
- package/src/data-structures/heap/min-heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +2 -5
- package/src/data-structures/linked-list/skip-linked-list.ts +15 -16
- package/src/data-structures/matrix/matrix.ts +2 -10
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/data-structures/queue/deque.ts +11 -15
- package/src/data-structures/queue/queue.ts +29 -28
- package/src/data-structures/stack/stack.ts +3 -6
- package/src/data-structures/trie/trie.ts +10 -11
- package/src/types/data-structures/binary-tree/binary-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/bst.ts +1 -1
- package/src/types/data-structures/binary-tree/tree-multimap.ts +1 -1
- package/src/types/data-structures/hash/hash-map.ts +6 -2
- package/src/types/data-structures/hash/index.ts +0 -1
- package/src/types/data-structures/heap/heap.ts +1 -1
- package/src/types/data-structures/linked-list/index.ts +1 -0
- package/src/types/data-structures/linked-list/skip-linked-list.ts +1 -1
- package/src/types/data-structures/matrix/index.ts +1 -0
- package/src/types/data-structures/matrix/matrix.ts +7 -1
- package/src/types/data-structures/queue/deque.ts +1 -1
- package/src/types/data-structures/trie/trie.ts +1 -1
- package/dist/data-structures/hash/hash-table.d.ts +0 -108
- package/dist/data-structures/hash/hash-table.js +0 -281
- package/dist/types/data-structures/hash/hash-table.d.ts +0 -1
- package/dist/types/data-structures/hash/hash-table.js +0 -2
- package/dist/types/data-structures/matrix/matrix2d.d.ts +0 -1
- package/dist/types/data-structures/matrix/matrix2d.js +0 -2
- package/dist/types/data-structures/matrix/vector2d.d.ts +0 -1
- package/dist/types/data-structures/matrix/vector2d.js +0 -2
- package/src/data-structures/hash/hash-table.ts +0 -318
- package/src/types/data-structures/hash/hash-table.ts +0 -1
- package/src/types/data-structures/matrix/matrix2d.ts +0 -1
- package/src/types/data-structures/matrix/vector2d.ts +0 -1
|
@@ -1,281 +0,0 @@
|
|
|
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.HashTable = exports.HashTableNode = void 0;
|
|
11
|
-
class HashTableNode {
|
|
12
|
-
constructor(key, value) {
|
|
13
|
-
this.key = key;
|
|
14
|
-
this.value = value;
|
|
15
|
-
this.next = undefined;
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
exports.HashTableNode = HashTableNode;
|
|
19
|
-
class HashTable {
|
|
20
|
-
constructor(capacity = HashTable.DEFAULT_CAPACITY, hashFn) {
|
|
21
|
-
this._hashFn = hashFn || this._defaultHashFn;
|
|
22
|
-
this._capacity = Math.max(capacity, HashTable.DEFAULT_CAPACITY);
|
|
23
|
-
this._size = 0;
|
|
24
|
-
this._buckets = new Array(this._capacity).fill(undefined);
|
|
25
|
-
}
|
|
26
|
-
get capacity() {
|
|
27
|
-
return this._capacity;
|
|
28
|
-
}
|
|
29
|
-
get size() {
|
|
30
|
-
return this._size;
|
|
31
|
-
}
|
|
32
|
-
get buckets() {
|
|
33
|
-
return this._buckets;
|
|
34
|
-
}
|
|
35
|
-
get hashFn() {
|
|
36
|
-
return this._hashFn;
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* The set function adds a key-value pair to the hash table, handling collisions and resizing if necessary.
|
|
40
|
-
* @param {K} key - The key parameter represents the key of the key-value pair that you want to insert into the hash
|
|
41
|
-
* table. It is of type K, which is a generic type representing the key's data type.
|
|
42
|
-
* @param {V} value - The parameter `value` represents the value that you want to associate with the given key in the hash
|
|
43
|
-
* table.
|
|
44
|
-
* @returns Nothing is being returned. The return type of the `put` method is `void`, which means it does not return any
|
|
45
|
-
* value.
|
|
46
|
-
*/
|
|
47
|
-
set(key, value) {
|
|
48
|
-
const index = this._hash(key);
|
|
49
|
-
const newNode = new HashTableNode(key, value);
|
|
50
|
-
if (!this._buckets[index]) {
|
|
51
|
-
this._buckets[index] = newNode;
|
|
52
|
-
}
|
|
53
|
-
else {
|
|
54
|
-
// Handle collisions, consider using open addressing, etc.
|
|
55
|
-
let currentNode = this._buckets[index];
|
|
56
|
-
while (currentNode) {
|
|
57
|
-
if (currentNode.key === key) {
|
|
58
|
-
// If the key already exists, update the value
|
|
59
|
-
currentNode.value = value;
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
|
-
if (!currentNode.next) {
|
|
63
|
-
break;
|
|
64
|
-
}
|
|
65
|
-
currentNode = currentNode.next;
|
|
66
|
-
}
|
|
67
|
-
// Add to the end of the linked list
|
|
68
|
-
currentNode.next = newNode;
|
|
69
|
-
}
|
|
70
|
-
this._size++;
|
|
71
|
-
// If the load factor is too high, resize the hash table
|
|
72
|
-
if (this._size / this._capacity >= HashTable.LOAD_FACTOR) {
|
|
73
|
-
this._expand();
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* The `get` function retrieves the value associated with a given key from a hash table.
|
|
78
|
-
* @param {K} key - The `key` parameter represents the key of the element that we want to retrieve from the data
|
|
79
|
-
* structure.
|
|
80
|
-
* @returns The method is returning the value associated with the given key if it exists in the hash table. If the key is
|
|
81
|
-
* not found, it returns `undefined`.
|
|
82
|
-
*/
|
|
83
|
-
get(key) {
|
|
84
|
-
const index = this._hash(key);
|
|
85
|
-
let currentNode = this._buckets[index];
|
|
86
|
-
while (currentNode) {
|
|
87
|
-
if (currentNode.key === key) {
|
|
88
|
-
return currentNode.value;
|
|
89
|
-
}
|
|
90
|
-
currentNode = currentNode.next;
|
|
91
|
-
}
|
|
92
|
-
return undefined; // Key not found
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* The delete function removes a key-value pair from a hash table.
|
|
96
|
-
* @param {K} key - The `key` parameter represents the key of the key-value pair that needs to be removed from the hash
|
|
97
|
-
* table.
|
|
98
|
-
* @returns Nothing is being returned. The `delete` method has a return type of `void`, which means it does not return
|
|
99
|
-
* any value.
|
|
100
|
-
*/
|
|
101
|
-
delete(key) {
|
|
102
|
-
const index = this._hash(key);
|
|
103
|
-
let currentNode = this._buckets[index];
|
|
104
|
-
let prevNode = undefined;
|
|
105
|
-
while (currentNode) {
|
|
106
|
-
if (currentNode.key === key) {
|
|
107
|
-
if (prevNode) {
|
|
108
|
-
prevNode.next = currentNode.next;
|
|
109
|
-
}
|
|
110
|
-
else {
|
|
111
|
-
this._buckets[index] = currentNode.next;
|
|
112
|
-
}
|
|
113
|
-
this._size--;
|
|
114
|
-
currentNode.next = undefined; // Release memory
|
|
115
|
-
return;
|
|
116
|
-
}
|
|
117
|
-
prevNode = currentNode;
|
|
118
|
-
currentNode = currentNode.next;
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
*[Symbol.iterator]() {
|
|
122
|
-
for (const bucket of this._buckets) {
|
|
123
|
-
let currentNode = bucket;
|
|
124
|
-
while (currentNode) {
|
|
125
|
-
yield [currentNode.key, currentNode.value];
|
|
126
|
-
currentNode = currentNode.next;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
forEach(callback) {
|
|
131
|
-
let index = 0;
|
|
132
|
-
for (const entry of this) {
|
|
133
|
-
callback(entry, index, this);
|
|
134
|
-
index++;
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
filter(predicate) {
|
|
138
|
-
const newTable = new HashTable();
|
|
139
|
-
let index = 0;
|
|
140
|
-
for (const [key, value] of this) {
|
|
141
|
-
if (predicate([key, value], index, this)) {
|
|
142
|
-
newTable.set(key, value);
|
|
143
|
-
}
|
|
144
|
-
index++;
|
|
145
|
-
}
|
|
146
|
-
return newTable;
|
|
147
|
-
}
|
|
148
|
-
map(callback) {
|
|
149
|
-
const newTable = new HashTable();
|
|
150
|
-
let index = 0;
|
|
151
|
-
for (const [key, value] of this) {
|
|
152
|
-
newTable.set(key, callback([key, value], index, this));
|
|
153
|
-
index++;
|
|
154
|
-
}
|
|
155
|
-
return newTable;
|
|
156
|
-
}
|
|
157
|
-
reduce(callback, initialValue) {
|
|
158
|
-
let accumulator = initialValue;
|
|
159
|
-
let index = 0;
|
|
160
|
-
for (const entry of this) {
|
|
161
|
-
accumulator = callback(accumulator, entry, index, this);
|
|
162
|
-
index++;
|
|
163
|
-
}
|
|
164
|
-
return accumulator;
|
|
165
|
-
}
|
|
166
|
-
/**
|
|
167
|
-
* The function `_defaultHashFn` calculates the hash value of a given key and returns the remainder when divided by the
|
|
168
|
-
* capacity of the data structure.
|
|
169
|
-
* @param {K} key - The `key` parameter is the input value that needs to be hashed. It can be of any type, but in this
|
|
170
|
-
* code snippet, it is checked whether the key is a string or an object. If it is a string, the `_murmurStringHashFn`
|
|
171
|
-
* function is used to
|
|
172
|
-
* @returns the hash value of the key modulo the capacity of the data structure.
|
|
173
|
-
*/
|
|
174
|
-
_defaultHashFn(key) {
|
|
175
|
-
// Can be replaced with other hash functions as needed
|
|
176
|
-
const hashValue = typeof key === 'string' ? this._murmurStringHashFn(key) : this._objectHash(key);
|
|
177
|
-
return hashValue % this._capacity;
|
|
178
|
-
}
|
|
179
|
-
/**
|
|
180
|
-
* The `_multiplicativeStringHashFn` function calculates a hash value for a given string key using the multiplicative
|
|
181
|
-
* string hash function.
|
|
182
|
-
* @param {K} key - The `key` parameter is the input value for which we want to calculate the hash. It can be of any
|
|
183
|
-
* type, as it is generic (`K`). The function converts the `key` to a string using the `String()` function.
|
|
184
|
-
* @returns a number, which is the result of the multiplicative string hash function applied to the input key.
|
|
185
|
-
*/
|
|
186
|
-
_multiplicativeStringHashFn(key) {
|
|
187
|
-
const keyString = String(key);
|
|
188
|
-
let hash = 0;
|
|
189
|
-
for (let i = 0; i < keyString.length; i++) {
|
|
190
|
-
const charCode = keyString.charCodeAt(i);
|
|
191
|
-
// Some constants for adjusting the hash function
|
|
192
|
-
const A = 0.618033988749895;
|
|
193
|
-
const M = 1 << 30; // 2^30
|
|
194
|
-
hash = (hash * A + charCode) % M;
|
|
195
|
-
}
|
|
196
|
-
return Math.abs(hash); // Take absolute value to ensure non-negative numbers
|
|
197
|
-
}
|
|
198
|
-
/**
|
|
199
|
-
* The function `_murmurStringHashFn` calculates a hash value for a given string key using the MurmurHash algorithm.
|
|
200
|
-
* @param {K} key - The `key` parameter is the input value for which you want to calculate the hash. It can be of any
|
|
201
|
-
* type, but it will be converted to a string using the `String()` function before calculating the hash.
|
|
202
|
-
* @returns a number, which is the hash value calculated for the given key.
|
|
203
|
-
*/
|
|
204
|
-
_murmurStringHashFn(key) {
|
|
205
|
-
const keyString = String(key);
|
|
206
|
-
const seed = 0;
|
|
207
|
-
let hash = seed;
|
|
208
|
-
for (let i = 0; i < keyString.length; i++) {
|
|
209
|
-
const char = keyString.charCodeAt(i);
|
|
210
|
-
hash = (hash ^ char) * 0x5bd1e995;
|
|
211
|
-
hash = (hash ^ (hash >>> 15)) * 0x27d4eb2d;
|
|
212
|
-
hash = hash ^ (hash >>> 15);
|
|
213
|
-
}
|
|
214
|
-
return Math.abs(hash);
|
|
215
|
-
}
|
|
216
|
-
/**
|
|
217
|
-
* The _hash function takes a key and returns a number.
|
|
218
|
-
* @param {K} key - The parameter "key" is of type K, which represents the type of the key that will be hashed.
|
|
219
|
-
* @returns The hash function is returning a number.
|
|
220
|
-
*/
|
|
221
|
-
_hash(key) {
|
|
222
|
-
return this.hashFn(key);
|
|
223
|
-
}
|
|
224
|
-
/**
|
|
225
|
-
* The function calculates a hash value for a given string using the djb2 algorithm.
|
|
226
|
-
* @param {string} key - The `key` parameter in the `stringHash` function is a string value that represents the input for
|
|
227
|
-
* which we want to calculate the hash value.
|
|
228
|
-
* @returns a number, which is the hash value of the input string.
|
|
229
|
-
*/
|
|
230
|
-
_stringHash(key) {
|
|
231
|
-
let hash = 0;
|
|
232
|
-
for (let i = 0; i < key.length; i++) {
|
|
233
|
-
hash = (hash * 31 + key.charCodeAt(i)) & 0xffffffff;
|
|
234
|
-
}
|
|
235
|
-
return hash;
|
|
236
|
-
}
|
|
237
|
-
/**
|
|
238
|
-
* The function `_objectHash` takes a key and returns a hash value, using a custom hash function for objects.
|
|
239
|
-
* @param {K} key - The parameter "key" is of type "K", which means it can be any type. It could be a string, number,
|
|
240
|
-
* boolean, object, or any other type of value. The purpose of the objectHash function is to generate a hash value for
|
|
241
|
-
* the key, which can be used for
|
|
242
|
-
* @returns a number, which is the hash value of the key.
|
|
243
|
-
*/
|
|
244
|
-
_objectHash(key) {
|
|
245
|
-
// If the key is an object, you can write a custom hash function
|
|
246
|
-
// For example, convert the object's properties to a string and use string hashing
|
|
247
|
-
// This is just an example; you should write a specific object hash function as needed
|
|
248
|
-
return this._stringHash(JSON.stringify(key));
|
|
249
|
-
}
|
|
250
|
-
/**
|
|
251
|
-
* The `expand` function increases the capacity of a hash table by creating a new array of buckets with double the
|
|
252
|
-
* capacity and rehashing all the existing key-value pairs into the new buckets.
|
|
253
|
-
*/
|
|
254
|
-
_expand() {
|
|
255
|
-
const newCapacity = this._capacity * 2;
|
|
256
|
-
const newBuckets = new Array(newCapacity).fill(undefined);
|
|
257
|
-
for (const bucket of this._buckets) {
|
|
258
|
-
let currentNode = bucket;
|
|
259
|
-
while (currentNode) {
|
|
260
|
-
const newIndex = this._hash(currentNode.key);
|
|
261
|
-
const newNode = new HashTableNode(currentNode.key, currentNode.value);
|
|
262
|
-
if (!newBuckets[newIndex]) {
|
|
263
|
-
newBuckets[newIndex] = newNode;
|
|
264
|
-
}
|
|
265
|
-
else {
|
|
266
|
-
let currentNewNode = newBuckets[newIndex];
|
|
267
|
-
while (currentNewNode.next) {
|
|
268
|
-
currentNewNode = currentNewNode.next;
|
|
269
|
-
}
|
|
270
|
-
currentNewNode.next = newNode;
|
|
271
|
-
}
|
|
272
|
-
currentNode = currentNode.next;
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
this._buckets = newBuckets;
|
|
276
|
-
this._capacity = newCapacity;
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
exports.HashTable = HashTable;
|
|
280
|
-
HashTable.DEFAULT_CAPACITY = 16;
|
|
281
|
-
HashTable.LOAD_FACTOR = 0.75;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,318 +0,0 @@
|
|
|
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
|
-
|
|
9
|
-
import type { HashFunction } from '../../types';
|
|
10
|
-
|
|
11
|
-
export class HashTableNode<K = any, V = any> {
|
|
12
|
-
key: K;
|
|
13
|
-
value: V;
|
|
14
|
-
next: HashTableNode<K, V> | undefined;
|
|
15
|
-
|
|
16
|
-
constructor(key: K, value: V) {
|
|
17
|
-
this.key = key;
|
|
18
|
-
this.value = value;
|
|
19
|
-
this.next = undefined;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export class HashTable<K = any, V = any> {
|
|
24
|
-
protected static readonly DEFAULT_CAPACITY = 16;
|
|
25
|
-
protected static readonly LOAD_FACTOR = 0.75;
|
|
26
|
-
|
|
27
|
-
constructor(capacity: number = HashTable.DEFAULT_CAPACITY, hashFn?: HashFunction<K>) {
|
|
28
|
-
this._hashFn = hashFn || this._defaultHashFn;
|
|
29
|
-
this._capacity = Math.max(capacity, HashTable.DEFAULT_CAPACITY);
|
|
30
|
-
this._size = 0;
|
|
31
|
-
this._buckets = new Array<HashTableNode<K, V> | undefined>(this._capacity).fill(undefined);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
protected _capacity: number;
|
|
35
|
-
|
|
36
|
-
get capacity(): number {
|
|
37
|
-
return this._capacity;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
protected _size: number;
|
|
41
|
-
|
|
42
|
-
get size(): number {
|
|
43
|
-
return this._size;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
protected _buckets: Array<HashTableNode<K, V> | undefined>;
|
|
47
|
-
|
|
48
|
-
get buckets(): Array<HashTableNode<K, V> | undefined> {
|
|
49
|
-
return this._buckets;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
protected _hashFn: HashFunction<K>;
|
|
53
|
-
|
|
54
|
-
get hashFn(): HashFunction<K> {
|
|
55
|
-
return this._hashFn;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* The set function adds a key-value pair to the hash table, handling collisions and resizing if necessary.
|
|
60
|
-
* @param {K} key - The key parameter represents the key of the key-value pair that you want to insert into the hash
|
|
61
|
-
* table. It is of type K, which is a generic type representing the key's data type.
|
|
62
|
-
* @param {V} value - The parameter `value` represents the value that you want to associate with the given key in the hash
|
|
63
|
-
* table.
|
|
64
|
-
* @returns Nothing is being returned. The return type of the `put` method is `void`, which means it does not return any
|
|
65
|
-
* value.
|
|
66
|
-
*/
|
|
67
|
-
set(key: K, value: V): void {
|
|
68
|
-
const index = this._hash(key);
|
|
69
|
-
const newNode = new HashTableNode<K, V>(key, value);
|
|
70
|
-
|
|
71
|
-
if (!this._buckets[index]) {
|
|
72
|
-
this._buckets[index] = newNode;
|
|
73
|
-
} else {
|
|
74
|
-
// Handle collisions, consider using open addressing, etc.
|
|
75
|
-
let currentNode = this._buckets[index]!;
|
|
76
|
-
while (currentNode) {
|
|
77
|
-
if (currentNode.key === key) {
|
|
78
|
-
// If the key already exists, update the value
|
|
79
|
-
currentNode.value = value;
|
|
80
|
-
return;
|
|
81
|
-
}
|
|
82
|
-
if (!currentNode.next) {
|
|
83
|
-
break;
|
|
84
|
-
}
|
|
85
|
-
currentNode = currentNode.next;
|
|
86
|
-
}
|
|
87
|
-
// Add to the end of the linked list
|
|
88
|
-
currentNode.next = newNode;
|
|
89
|
-
}
|
|
90
|
-
this._size++;
|
|
91
|
-
|
|
92
|
-
// If the load factor is too high, resize the hash table
|
|
93
|
-
if (this._size / this._capacity >= HashTable.LOAD_FACTOR) {
|
|
94
|
-
this._expand();
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* The `get` function retrieves the value associated with a given key from a hash table.
|
|
100
|
-
* @param {K} key - The `key` parameter represents the key of the element that we want to retrieve from the data
|
|
101
|
-
* structure.
|
|
102
|
-
* @returns The method is returning the value associated with the given key if it exists in the hash table. If the key is
|
|
103
|
-
* not found, it returns `undefined`.
|
|
104
|
-
*/
|
|
105
|
-
get(key: K): V | undefined {
|
|
106
|
-
const index = this._hash(key);
|
|
107
|
-
let currentNode = this._buckets[index];
|
|
108
|
-
|
|
109
|
-
while (currentNode) {
|
|
110
|
-
if (currentNode.key === key) {
|
|
111
|
-
return currentNode.value;
|
|
112
|
-
}
|
|
113
|
-
currentNode = currentNode.next;
|
|
114
|
-
}
|
|
115
|
-
return undefined; // Key not found
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* The delete function removes a key-value pair from a hash table.
|
|
120
|
-
* @param {K} key - The `key` parameter represents the key of the key-value pair that needs to be removed from the hash
|
|
121
|
-
* table.
|
|
122
|
-
* @returns Nothing is being returned. The `delete` method has a return type of `void`, which means it does not return
|
|
123
|
-
* any value.
|
|
124
|
-
*/
|
|
125
|
-
delete(key: K): void {
|
|
126
|
-
const index = this._hash(key);
|
|
127
|
-
let currentNode = this._buckets[index];
|
|
128
|
-
let prevNode: HashTableNode<K, V> | undefined = undefined;
|
|
129
|
-
|
|
130
|
-
while (currentNode) {
|
|
131
|
-
if (currentNode.key === key) {
|
|
132
|
-
if (prevNode) {
|
|
133
|
-
prevNode.next = currentNode.next;
|
|
134
|
-
} else {
|
|
135
|
-
this._buckets[index] = currentNode.next;
|
|
136
|
-
}
|
|
137
|
-
this._size--;
|
|
138
|
-
currentNode.next = undefined; // Release memory
|
|
139
|
-
return;
|
|
140
|
-
}
|
|
141
|
-
prevNode = currentNode;
|
|
142
|
-
currentNode = currentNode.next;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
* [Symbol.iterator](): Generator<[K, V], void, undefined> {
|
|
147
|
-
for (const bucket of this._buckets) {
|
|
148
|
-
let currentNode = bucket;
|
|
149
|
-
while (currentNode) {
|
|
150
|
-
yield [currentNode.key, currentNode.value];
|
|
151
|
-
currentNode = currentNode.next;
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
forEach(callback: (entry: [K, V], index: number, table: HashTable<K, V>) => void): void {
|
|
157
|
-
let index = 0;
|
|
158
|
-
for (const entry of this) {
|
|
159
|
-
callback(entry, index, this);
|
|
160
|
-
index++;
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
filter(predicate: (entry: [K, V], index: number, table: HashTable<K, V>) => boolean): HashTable<K, V> {
|
|
165
|
-
const newTable = new HashTable<K, V>();
|
|
166
|
-
let index = 0;
|
|
167
|
-
for (const [key, value] of this) {
|
|
168
|
-
if (predicate([key, value], index, this)) {
|
|
169
|
-
newTable.set(key, value);
|
|
170
|
-
}
|
|
171
|
-
index++;
|
|
172
|
-
}
|
|
173
|
-
return newTable;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
map<T>(callback: (entry: [K, V], index: number, table: HashTable<K, V>) => T): HashTable<K, T> {
|
|
177
|
-
const newTable = new HashTable<K, T>();
|
|
178
|
-
let index = 0;
|
|
179
|
-
for (const [key, value] of this) {
|
|
180
|
-
newTable.set(key, callback([key, value], index, this));
|
|
181
|
-
index++;
|
|
182
|
-
}
|
|
183
|
-
return newTable;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
reduce<T>(callback: (accumulator: T, entry: [K, V], index: number, table: HashTable<K, V>) => T, initialValue: T): T {
|
|
187
|
-
let accumulator = initialValue;
|
|
188
|
-
let index = 0;
|
|
189
|
-
for (const entry of this) {
|
|
190
|
-
accumulator = callback(accumulator, entry, index, this);
|
|
191
|
-
index++;
|
|
192
|
-
}
|
|
193
|
-
return accumulator;
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
/**
|
|
197
|
-
* The function `_defaultHashFn` calculates the hash value of a given key and returns the remainder when divided by the
|
|
198
|
-
* capacity of the data structure.
|
|
199
|
-
* @param {K} key - The `key` parameter is the input value that needs to be hashed. It can be of any type, but in this
|
|
200
|
-
* code snippet, it is checked whether the key is a string or an object. If it is a string, the `_murmurStringHashFn`
|
|
201
|
-
* function is used to
|
|
202
|
-
* @returns the hash value of the key modulo the capacity of the data structure.
|
|
203
|
-
*/
|
|
204
|
-
protected _defaultHashFn(key: K): number {
|
|
205
|
-
// Can be replaced with other hash functions as needed
|
|
206
|
-
const hashValue = typeof key === 'string' ? this._murmurStringHashFn(key) : this._objectHash(key);
|
|
207
|
-
return hashValue % this._capacity;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
/**
|
|
211
|
-
* The `_multiplicativeStringHashFn` function calculates a hash value for a given string key using the multiplicative
|
|
212
|
-
* string hash function.
|
|
213
|
-
* @param {K} key - The `key` parameter is the input value for which we want to calculate the hash. It can be of any
|
|
214
|
-
* type, as it is generic (`K`). The function converts the `key` to a string using the `String()` function.
|
|
215
|
-
* @returns a number, which is the result of the multiplicative string hash function applied to the input key.
|
|
216
|
-
*/
|
|
217
|
-
protected _multiplicativeStringHashFn<K>(key: K): number {
|
|
218
|
-
const keyString = String(key);
|
|
219
|
-
let hash = 0;
|
|
220
|
-
for (let i = 0; i < keyString.length; i++) {
|
|
221
|
-
const charCode = keyString.charCodeAt(i);
|
|
222
|
-
// Some constants for adjusting the hash function
|
|
223
|
-
const A = 0.618033988749895;
|
|
224
|
-
const M = 1 << 30; // 2^30
|
|
225
|
-
hash = (hash * A + charCode) % M;
|
|
226
|
-
}
|
|
227
|
-
return Math.abs(hash); // Take absolute value to ensure non-negative numbers
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
/**
|
|
231
|
-
* The function `_murmurStringHashFn` calculates a hash value for a given string key using the MurmurHash algorithm.
|
|
232
|
-
* @param {K} key - The `key` parameter is the input value for which you want to calculate the hash. It can be of any
|
|
233
|
-
* type, but it will be converted to a string using the `String()` function before calculating the hash.
|
|
234
|
-
* @returns a number, which is the hash value calculated for the given key.
|
|
235
|
-
*/
|
|
236
|
-
protected _murmurStringHashFn<K>(key: K): number {
|
|
237
|
-
const keyString = String(key);
|
|
238
|
-
const seed = 0;
|
|
239
|
-
let hash = seed;
|
|
240
|
-
|
|
241
|
-
for (let i = 0; i < keyString.length; i++) {
|
|
242
|
-
const char = keyString.charCodeAt(i);
|
|
243
|
-
hash = (hash ^ char) * 0x5bd1e995;
|
|
244
|
-
hash = (hash ^ (hash >>> 15)) * 0x27d4eb2d;
|
|
245
|
-
hash = hash ^ (hash >>> 15);
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
return Math.abs(hash);
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
/**
|
|
252
|
-
* The _hash function takes a key and returns a number.
|
|
253
|
-
* @param {K} key - The parameter "key" is of type K, which represents the type of the key that will be hashed.
|
|
254
|
-
* @returns The hash function is returning a number.
|
|
255
|
-
*/
|
|
256
|
-
protected _hash(key: K): number {
|
|
257
|
-
return this.hashFn(key);
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
/**
|
|
261
|
-
* The function calculates a hash value for a given string using the djb2 algorithm.
|
|
262
|
-
* @param {string} key - The `key` parameter in the `stringHash` function is a string value that represents the input for
|
|
263
|
-
* which we want to calculate the hash value.
|
|
264
|
-
* @returns a number, which is the hash value of the input string.
|
|
265
|
-
*/
|
|
266
|
-
protected _stringHash(key: string): number {
|
|
267
|
-
let hash = 0;
|
|
268
|
-
for (let i = 0; i < key.length; i++) {
|
|
269
|
-
hash = (hash * 31 + key.charCodeAt(i)) & 0xffffffff;
|
|
270
|
-
}
|
|
271
|
-
return hash;
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
/**
|
|
275
|
-
* The function `_objectHash` takes a key and returns a hash value, using a custom hash function for objects.
|
|
276
|
-
* @param {K} key - The parameter "key" is of type "K", which means it can be any type. It could be a string, number,
|
|
277
|
-
* boolean, object, or any other type of value. The purpose of the objectHash function is to generate a hash value for
|
|
278
|
-
* the key, which can be used for
|
|
279
|
-
* @returns a number, which is the hash value of the key.
|
|
280
|
-
*/
|
|
281
|
-
protected _objectHash(key: K): number {
|
|
282
|
-
// If the key is an object, you can write a custom hash function
|
|
283
|
-
// For example, convert the object's properties to a string and use string hashing
|
|
284
|
-
// This is just an example; you should write a specific object hash function as needed
|
|
285
|
-
return this._stringHash(JSON.stringify(key));
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
/**
|
|
289
|
-
* The `expand` function increases the capacity of a hash table by creating a new array of buckets with double the
|
|
290
|
-
* capacity and rehashing all the existing key-value pairs into the new buckets.
|
|
291
|
-
*/
|
|
292
|
-
protected _expand(): void {
|
|
293
|
-
const newCapacity = this._capacity * 2;
|
|
294
|
-
const newBuckets = new Array<HashTableNode<K, V> | undefined>(newCapacity).fill(undefined);
|
|
295
|
-
|
|
296
|
-
for (const bucket of this._buckets) {
|
|
297
|
-
let currentNode = bucket;
|
|
298
|
-
while (currentNode) {
|
|
299
|
-
const newIndex = this._hash(currentNode.key);
|
|
300
|
-
const newNode = new HashTableNode<K, V>(currentNode.key, currentNode.value);
|
|
301
|
-
|
|
302
|
-
if (!newBuckets[newIndex]) {
|
|
303
|
-
newBuckets[newIndex] = newNode;
|
|
304
|
-
} else {
|
|
305
|
-
let currentNewNode = newBuckets[newIndex]!;
|
|
306
|
-
while (currentNewNode.next) {
|
|
307
|
-
currentNewNode = currentNewNode.next;
|
|
308
|
-
}
|
|
309
|
-
currentNewNode.next = newNode;
|
|
310
|
-
}
|
|
311
|
-
currentNode = currentNode.next;
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
this._buckets = newBuckets;
|
|
316
|
-
this._capacity = newCapacity;
|
|
317
|
-
}
|
|
318
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|