heap-typed 1.38.0 → 1.38.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/dist/data-structures/binary-tree/avl-tree.d.ts +9 -9
  2. package/dist/data-structures/binary-tree/avl-tree.js +22 -22
  3. package/dist/data-structures/binary-tree/binary-tree.d.ts +31 -31
  4. package/dist/data-structures/binary-tree/binary-tree.js +32 -32
  5. package/dist/data-structures/binary-tree/rb-tree.d.ts +1 -1
  6. package/dist/data-structures/binary-tree/tree-multiset.d.ts +9 -9
  7. package/dist/data-structures/binary-tree/tree-multiset.js +23 -23
  8. package/dist/data-structures/hash/hash-map.d.ts +25 -25
  9. package/dist/data-structures/hash/hash-map.js +59 -59
  10. package/dist/data-structures/hash/hash-table.d.ts +34 -34
  11. package/dist/data-structures/hash/hash-table.js +99 -99
  12. package/dist/data-structures/heap/heap.d.ts +66 -66
  13. package/dist/data-structures/heap/heap.js +167 -167
  14. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +1 -1
  15. package/dist/data-structures/linked-list/doubly-linked-list.js +3 -3
  16. package/dist/data-structures/linked-list/skip-linked-list.d.ts +17 -17
  17. package/dist/data-structures/linked-list/skip-linked-list.js +34 -34
  18. package/dist/data-structures/matrix/matrix2d.d.ts +7 -7
  19. package/dist/data-structures/matrix/matrix2d.js +9 -9
  20. package/dist/data-structures/trie/trie.d.ts +2 -2
  21. package/dist/data-structures/trie/trie.js +6 -6
  22. package/dist/index.d.ts +3 -3
  23. package/dist/index.js +3 -3
  24. package/package.json +1 -4
  25. package/src/data-structures/binary-tree/avl-tree.ts +28 -28
  26. package/src/data-structures/binary-tree/binary-indexed-tree.ts +1 -1
  27. package/src/data-structures/binary-tree/binary-tree.ts +57 -57
  28. package/src/data-structures/binary-tree/bst.ts +4 -0
  29. package/src/data-structures/binary-tree/rb-tree.ts +2 -2
  30. package/src/data-structures/binary-tree/tree-multiset.ts +30 -31
  31. package/src/data-structures/graph/abstract-graph.ts +10 -11
  32. package/src/data-structures/graph/directed-graph.ts +1 -2
  33. package/src/data-structures/graph/undirected-graph.ts +4 -5
  34. package/src/data-structures/hash/hash-map.ts +82 -76
  35. package/src/data-structures/hash/hash-table.ts +112 -109
  36. package/src/data-structures/hash/tree-map.ts +2 -1
  37. package/src/data-structures/hash/tree-set.ts +2 -1
  38. package/src/data-structures/heap/heap.ts +182 -181
  39. package/src/data-structures/linked-list/doubly-linked-list.ts +4 -4
  40. package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
  41. package/src/data-structures/linked-list/skip-linked-list.ts +45 -38
  42. package/src/data-structures/matrix/matrix.ts +1 -1
  43. package/src/data-structures/matrix/matrix2d.ts +10 -10
  44. package/src/data-structures/matrix/vector2d.ts +2 -1
  45. package/src/data-structures/queue/deque.ts +5 -4
  46. package/src/data-structures/queue/queue.ts +1 -1
  47. package/src/data-structures/trie/trie.ts +9 -9
  48. package/src/index.ts +3 -3
  49. package/src/types/data-structures/matrix/navigator.ts +1 -1
  50. package/src/types/helpers.ts +5 -1
  51. package/src/types/utils/utils.ts +1 -1
  52. package/src/types/utils/validate-type.ts +2 -2
@@ -8,28 +8,47 @@ import {HashFunction} from '../../types';
8
8
  * @license MIT License
9
9
  */
10
10
  export class HashMap<K, V> {
11
- get hashFn(): HashFunction<K> {
12
- return this._hashFn;
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
+ });
13
39
  }
14
40
 
15
- set hashFn(value: HashFunction<K>) {
16
- this._hashFn = value;
17
- }
18
- get table(): Array<Array<[K, V]>> {
19
- return this._table;
20
- }
41
+ private _initialCapacity: number;
21
42
 
22
- set table(value: Array<Array<[K, V]>>) {
23
- this._table = value;
43
+ get initialCapacity(): number {
44
+ return this._initialCapacity;
24
45
  }
25
46
 
26
- get capacityMultiplier(): number {
27
- return this._capacityMultiplier;
47
+ set initialCapacity(value: number) {
48
+ this._initialCapacity = value;
28
49
  }
29
50
 
30
- set capacityMultiplier(value: number) {
31
- this._capacityMultiplier = value;
32
- }
51
+ private _loadFactor: number;
33
52
 
34
53
  get loadFactor(): number {
35
54
  return this._loadFactor;
@@ -39,14 +58,18 @@ export class HashMap<K, V> {
39
58
  this._loadFactor = value;
40
59
  }
41
60
 
42
- get initialCapacity(): number {
43
- return this._initialCapacity;
61
+ private _capacityMultiplier: number;
62
+
63
+ get capacityMultiplier(): number {
64
+ return this._capacityMultiplier;
44
65
  }
45
66
 
46
- set initialCapacity(value: number) {
47
- this._initialCapacity = value;
67
+ set capacityMultiplier(value: number) {
68
+ this._capacityMultiplier = value;
48
69
  }
49
70
 
71
+ private _size: number;
72
+
50
73
  get size(): number {
51
74
  return this._size;
52
75
  }
@@ -55,68 +78,24 @@ export class HashMap<K, V> {
55
78
  this._size = value;
56
79
  }
57
80
 
58
- private _initialCapacity: number;
59
- private _loadFactor: number;
60
- private _capacityMultiplier: number;
61
- private _size: number;
62
81
  private _table: Array<Array<[K, V]>>;
63
- private _hashFn: HashFunction<K>;
64
82
 
65
- /**
66
- * The constructor initializes the properties of a hash table, including the initial capacity, load factor, capacity
67
- * multiplier, size, table array, and hash function.
68
- * @param [initialCapacity=16] - The initial capacity is the initial size of the hash table. It determines the number of
69
- * buckets or slots available for storing key-value pairs. The default value is 16.
70
- * @param [loadFactor=0.75] - The load factor is a measure of how full the hash table can be before it is resized. It is
71
- * a value between 0 and 1, where 1 means the hash table is completely full and 0 means it is completely empty. When the
72
- * load factor is reached, the hash table will
73
- * @param [hashFn] - The `hashFn` parameter is an optional parameter that represents the hash function used to calculate
74
- * the index of a key in the hash table. If a custom hash function is not provided, a default hash function is used. The
75
- * default hash function converts the key to a string, calculates the sum of the
76
- */
77
- constructor(initialCapacity = 16, loadFactor = 0.75, hashFn?: HashFunction<K>) {
78
- this._initialCapacity = initialCapacity;
79
- this._loadFactor = loadFactor;
80
- this._capacityMultiplier = 2;
81
- this._size = 0;
82
- this._table = new Array(initialCapacity);
83
- this._hashFn =
84
- hashFn ||
85
- ((key: K) => {
86
- const strKey = String(key);
87
- let hash = 0;
88
- for (let i = 0; i < strKey.length; i++) {
89
- hash += strKey.charCodeAt(i);
90
- }
91
- return hash % this.table.length;
92
- });
83
+ get table(): Array<Array<[K, V]>> {
84
+ return this._table;
93
85
  }
94
86
 
95
- private _hash(key: K): number {
96
- return this._hashFn(key);
87
+ set table(value: Array<Array<[K, V]>>) {
88
+ this._table = value;
97
89
  }
98
90
 
99
- /**
100
- * The `resizeTable` function resizes the table used in a hash map by creating a new table with a specified capacity and
101
- * rehashing the key-value pairs from the old table into the new table.
102
- * @param {number} newCapacity - The newCapacity parameter is the desired capacity for the resized table. It represents
103
- * the number of buckets that the new table should have.
104
- */
105
- private resizeTable(newCapacity: number): void {
106
- const newTable = new Array(newCapacity);
107
- for (const bucket of this._table) {
108
- // Note that this is this._table
109
- if (bucket) {
110
- for (const [key, value] of bucket) {
111
- const newIndex = this._hash(key) % newCapacity;
112
- if (!newTable[newIndex]) {
113
- newTable[newIndex] = [];
114
- }
115
- newTable[newIndex].push([key, value]);
116
- }
117
- }
118
- }
119
- this._table = newTable; // Again, here is this._table
91
+ private _hashFn: HashFunction<K>;
92
+
93
+ get hashFn(): HashFunction<K> {
94
+ return this._hashFn;
95
+ }
96
+
97
+ set hashFn(value: HashFunction<K>) {
98
+ this._hashFn = value;
120
99
  }
121
100
 
122
101
  set(key: K, value: V): void {
@@ -178,7 +157,7 @@ export class HashMap<K, V> {
178
157
  }
179
158
  }
180
159
 
181
- *entries(): IterableIterator<[K, V]> {
160
+ * entries(): IterableIterator<[K, V]> {
182
161
  for (const bucket of this.table) {
183
162
  if (bucket) {
184
163
  for (const [key, value] of bucket) {
@@ -200,4 +179,31 @@ export class HashMap<K, V> {
200
179
  isEmpty(): boolean {
201
180
  return this.size === 0;
202
181
  }
182
+
183
+ private _hash(key: K): number {
184
+ return this._hashFn(key);
185
+ }
186
+
187
+ /**
188
+ * The `resizeTable` function resizes the table used in a hash map by creating a new table with a specified capacity and
189
+ * rehashing the key-value pairs from the old table into the new table.
190
+ * @param {number} newCapacity - The newCapacity parameter is the desired capacity for the resized table. It represents
191
+ * the number of buckets that the new table should have.
192
+ */
193
+ private resizeTable(newCapacity: number): void {
194
+ const newTable = new Array(newCapacity);
195
+ for (const bucket of this._table) {
196
+ // Note that this is this._table
197
+ if (bucket) {
198
+ for (const [key, value] of bucket) {
199
+ const newIndex = this._hash(key) % newCapacity;
200
+ if (!newTable[newIndex]) {
201
+ newTable[newIndex] = [];
202
+ }
203
+ newTable[newIndex].push([key, value]);
204
+ }
205
+ }
206
+ }
207
+ this._table = newTable; // Again, here is this._table
208
+ }
203
209
  }
@@ -21,21 +21,17 @@ export class HashTableNode<K, V> {
21
21
  import {HashFunction} from '../../types';
22
22
 
23
23
  export class HashTable<K, V> {
24
- get hashFn(): HashFunction<K> {
25
- return this._hashFn;
26
- }
27
-
28
- set hashFn(value: HashFunction<K>) {
29
- this._hashFn = value;
30
- }
24
+ private static readonly DEFAULT_CAPACITY = 16;
25
+ private static readonly LOAD_FACTOR = 0.75;
31
26
 
32
- get buckets(): Array<HashTableNode<K, V> | null> {
33
- return this._buckets;
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> | null>(this._capacity).fill(null);
34
32
  }
35
33
 
36
- set buckets(value: Array<HashTableNode<K, V> | null>) {
37
- this._buckets = value;
38
- }
34
+ private _capacity: number;
39
35
 
40
36
  get capacity(): number {
41
37
  return this._capacity;
@@ -45,111 +41,30 @@ export class HashTable<K, V> {
45
41
  this._capacity = value;
46
42
  }
47
43
 
48
- private static readonly DEFAULT_CAPACITY = 16;
49
- private static readonly LOAD_FACTOR = 0.75;
50
-
51
- private _capacity: number;
52
44
  private _size: number;
53
- private _buckets: Array<HashTableNode<K, V> | null>;
54
- private _hashFn: HashFunction<K>;
55
45
 
56
- constructor(capacity: number = HashTable.DEFAULT_CAPACITY, hashFn?: HashFunction<K>) {
57
- this._hashFn = hashFn || this._defaultHashFn;
58
- this._capacity = Math.max(capacity, HashTable.DEFAULT_CAPACITY);
59
- this._size = 0;
60
- this._buckets = new Array<HashTableNode<K, V> | null>(this._capacity).fill(null);
46
+ get size(): number {
47
+ return this._size;
61
48
  }
62
49
 
63
- /**
64
- * The function `_defaultHashFn` calculates the hash value of a given key and returns the remainder when divided by the
65
- * capacity of the data structure.
66
- * @param {K} key - The `key` parameter is the input value that needs to be hashed. It can be of any type, but in this
67
- * code snippet, it is checked whether the key is a string or an object. If it is a string, the `_murmurStringHashFn`
68
- * function is used to
69
- * @returns the hash value of the key modulo the capacity of the data structure.
70
- */
71
- protected _defaultHashFn(key: K): number {
72
- // Can be replaced with other hash functions as needed
73
- const hashValue = typeof key === 'string' ? this._murmurStringHashFn(key) : this._objectHash(key);
74
- return hashValue % this._capacity;
75
- }
50
+ private _buckets: Array<HashTableNode<K, V> | null>;
76
51
 
77
- /**
78
- * The `_multiplicativeStringHashFn` function calculates a hash value for a given string key using the multiplicative
79
- * string hash function.
80
- * @param {K} key - The `key` parameter is the input value for which we want to calculate the hash. It can be of any
81
- * type, as it is generic (`K`). The function converts the `key` to a string using the `String()` function.
82
- * @returns a number, which is the result of the multiplicative string hash function applied to the input key.
83
- */
84
- protected _multiplicativeStringHashFn<K>(key: K): number {
85
- const keyString = String(key);
86
- let hash = 0;
87
- for (let i = 0; i < keyString.length; i++) {
88
- const charCode = keyString.charCodeAt(i);
89
- // Some constants for adjusting the hash function
90
- const A = 0.618033988749895;
91
- const M = 1 << 30; // 2^30
92
- hash = (hash * A + charCode) % M;
93
- }
94
- return Math.abs(hash); // Take absolute value to ensure non-negative numbers
52
+ get buckets(): Array<HashTableNode<K, V> | null> {
53
+ return this._buckets;
95
54
  }
96
55
 
97
- /**
98
- * The function `_murmurStringHashFn` calculates a hash value for a given string key using the MurmurHash algorithm.
99
- * @param {K} key - The `key` parameter is the input value for which you want to calculate the hash. It can be of any
100
- * type, but it will be converted to a string using the `String()` function before calculating the hash.
101
- * @returns a number, which is the hash value calculated for the given key.
102
- */
103
- protected _murmurStringHashFn<K>(key: K): number {
104
- const keyString = String(key);
105
- const seed = 0;
106
- let hash = seed;
107
-
108
- for (let i = 0; i < keyString.length; i++) {
109
- const char = keyString.charCodeAt(i);
110
- hash = (hash ^ char) * 0x5bd1e995;
111
- hash = (hash ^ (hash >>> 15)) * 0x27d4eb2d;
112
- hash = hash ^ (hash >>> 15);
113
- }
114
-
115
- return Math.abs(hash);
56
+ set buckets(value: Array<HashTableNode<K, V> | null>) {
57
+ this._buckets = value;
116
58
  }
117
59
 
118
- /**
119
- * The _hash function takes a key and returns a number.
120
- * @param {K} key - The parameter "key" is of type K, which represents the type of the key that will be hashed.
121
- * @returns The hash function is returning a number.
122
- */
123
- protected _hash(key: K): number {
124
- return this.hashFn(key);
125
- }
60
+ private _hashFn: HashFunction<K>;
126
61
 
127
- /**
128
- * The function calculates a hash value for a given string using the djb2 algorithm.
129
- * @param {string} key - The `key` parameter in the `stringHash` function is a string value that represents the input for
130
- * which we want to calculate the hash value.
131
- * @returns a number, which is the hash value of the input string.
132
- */
133
- protected _stringHash(key: string): number {
134
- let hash = 0;
135
- for (let i = 0; i < key.length; i++) {
136
- hash = (hash * 31 + key.charCodeAt(i)) & 0xffffffff;
137
- }
138
- return hash;
62
+ get hashFn(): HashFunction<K> {
63
+ return this._hashFn;
139
64
  }
140
65
 
141
- /**
142
- * The function `_objectHash` takes a key and returns a hash value, using a custom hash function for objects.
143
- * @param {K} key - The parameter "key" is of type "K", which means it can be any type. It could be a string, number,
144
- * boolean, object, or any other type of value. The purpose of the objectHash function is to generate a hash value for
145
- * the key, which can be used for
146
- * @returns a number, which is the hash value of the key.
147
- */
148
- protected _objectHash(key: K): number {
149
- // If the key is an object, you can write a custom hash function
150
- // For example, convert the object's properties to a string and use string hashing
151
- // This is just an example; you should write a specific object hash function as needed
152
- return this._stringHash(JSON.stringify(key));
66
+ set hashFn(value: HashFunction<K>) {
67
+ this._hashFn = value;
153
68
  }
154
69
 
155
70
  /**
@@ -240,6 +155,98 @@ export class HashTable<K, V> {
240
155
  }
241
156
  }
242
157
 
158
+ /**
159
+ * The function `_defaultHashFn` calculates the hash value of a given key and returns the remainder when divided by the
160
+ * capacity of the data structure.
161
+ * @param {K} key - The `key` parameter is the input value that needs to be hashed. It can be of any type, but in this
162
+ * code snippet, it is checked whether the key is a string or an object. If it is a string, the `_murmurStringHashFn`
163
+ * function is used to
164
+ * @returns the hash value of the key modulo the capacity of the data structure.
165
+ */
166
+ protected _defaultHashFn(key: K): number {
167
+ // Can be replaced with other hash functions as needed
168
+ const hashValue = typeof key === 'string' ? this._murmurStringHashFn(key) : this._objectHash(key);
169
+ return hashValue % this._capacity;
170
+ }
171
+
172
+ /**
173
+ * The `_multiplicativeStringHashFn` function calculates a hash value for a given string key using the multiplicative
174
+ * string hash function.
175
+ * @param {K} key - The `key` parameter is the input value for which we want to calculate the hash. It can be of any
176
+ * type, as it is generic (`K`). The function converts the `key` to a string using the `String()` function.
177
+ * @returns a number, which is the result of the multiplicative string hash function applied to the input key.
178
+ */
179
+ protected _multiplicativeStringHashFn<K>(key: K): number {
180
+ const keyString = String(key);
181
+ let hash = 0;
182
+ for (let i = 0; i < keyString.length; i++) {
183
+ const charCode = keyString.charCodeAt(i);
184
+ // Some constants for adjusting the hash function
185
+ const A = 0.618033988749895;
186
+ const M = 1 << 30; // 2^30
187
+ hash = (hash * A + charCode) % M;
188
+ }
189
+ return Math.abs(hash); // Take absolute value to ensure non-negative numbers
190
+ }
191
+
192
+ /**
193
+ * The function `_murmurStringHashFn` calculates a hash value for a given string key using the MurmurHash algorithm.
194
+ * @param {K} key - The `key` parameter is the input value for which you want to calculate the hash. It can be of any
195
+ * type, but it will be converted to a string using the `String()` function before calculating the hash.
196
+ * @returns a number, which is the hash value calculated for the given key.
197
+ */
198
+ protected _murmurStringHashFn<K>(key: K): number {
199
+ const keyString = String(key);
200
+ const seed = 0;
201
+ let hash = seed;
202
+
203
+ for (let i = 0; i < keyString.length; i++) {
204
+ const char = keyString.charCodeAt(i);
205
+ hash = (hash ^ char) * 0x5bd1e995;
206
+ hash = (hash ^ (hash >>> 15)) * 0x27d4eb2d;
207
+ hash = hash ^ (hash >>> 15);
208
+ }
209
+
210
+ return Math.abs(hash);
211
+ }
212
+
213
+ /**
214
+ * The _hash function takes a key and returns a number.
215
+ * @param {K} key - The parameter "key" is of type K, which represents the type of the key that will be hashed.
216
+ * @returns The hash function is returning a number.
217
+ */
218
+ protected _hash(key: K): number {
219
+ return this.hashFn(key);
220
+ }
221
+
222
+ /**
223
+ * The function calculates a hash value for a given string using the djb2 algorithm.
224
+ * @param {string} key - The `key` parameter in the `stringHash` function is a string value that represents the input for
225
+ * which we want to calculate the hash value.
226
+ * @returns a number, which is the hash value of the input string.
227
+ */
228
+ protected _stringHash(key: string): number {
229
+ let hash = 0;
230
+ for (let i = 0; i < key.length; i++) {
231
+ hash = (hash * 31 + key.charCodeAt(i)) & 0xffffffff;
232
+ }
233
+ return hash;
234
+ }
235
+
236
+ /**
237
+ * The function `_objectHash` takes a key and returns a hash value, using a custom hash function for objects.
238
+ * @param {K} key - The parameter "key" is of type "K", which means it can be any type. It could be a string, number,
239
+ * boolean, object, or any other type of value. The purpose of the objectHash function is to generate a hash value for
240
+ * the key, which can be used for
241
+ * @returns a number, which is the hash value of the key.
242
+ */
243
+ protected _objectHash(key: K): number {
244
+ // If the key is an object, you can write a custom hash function
245
+ // For example, convert the object's properties to a string and use string hashing
246
+ // This is just an example; you should write a specific object hash function as needed
247
+ return this._stringHash(JSON.stringify(key));
248
+ }
249
+
243
250
  /**
244
251
  * The `expand` function increases the capacity of a hash table by creating a new array of buckets with double the
245
252
  * capacity and rehashing all the existing key-value pairs into the new buckets.
@@ -270,8 +277,4 @@ export class HashTable<K, V> {
270
277
  this._buckets = newBuckets;
271
278
  this._capacity = newCapacity;
272
279
  }
273
-
274
- get size(): number {
275
- return this._size;
276
- }
277
280
  }
@@ -1 +1,2 @@
1
- export class TreeMap {}
1
+ export class TreeMap {
2
+ }
@@ -1 +1,2 @@
1
- export class TreeSet {}
1
+ export class TreeSet {
2
+ }