1
+ import { Map } from "../map" ;
2
+
1
3
/**
2
- * Represents a hash table .
4
+ * Represents a hash map .
3
5
* Time complexity:
4
6
* - Set, Get, Delete, Has: O(1) on average, O(n) in the worst case.
5
7
* - Clear: O(m) where m is the number of buckets.
6
8
* - Keys, Values, Entires: O(n + m).
7
9
*
8
10
* @template K The key type.
9
11
* @template V The value type.
10
- * @param size The size of the hash table .
12
+ * @param size The size of the hash map .
11
13
* @param buckets The buckets in which to store the key-value pairs.
12
- * @param loadFactor The load factor to determine when to resize the hash table .
14
+ * @param loadFactor The load factor to determine when to resize the hash map .
13
15
*/
14
- export class HashTable < K , V > {
16
+ export class HashMap < K , V > implements Map < K , V > {
15
17
private size ! : number ;
16
- private buckets ! : HashTableEntry < K , V > [ ] [ ] ;
18
+ private buckets ! : HashMapEntry < K , V > [ ] [ ] ;
17
19
private readonly loadFactor = 0.75 ;
18
20
19
21
constructor ( ) {
@@ -45,7 +47,7 @@ export class HashTable<K, V> {
45
47
const bucket = this . buckets [ index ] ;
46
48
47
49
if ( bucket . length === 0 ) {
48
- bucket . push ( new HashTableEntry ( key , value ) ) ;
50
+ bucket . push ( new HashMapEntry ( key , value ) ) ;
49
51
this . size ++ ;
50
52
return ;
51
53
}
@@ -57,7 +59,7 @@ export class HashTable<K, V> {
57
59
}
58
60
}
59
61
60
- bucket . push ( new HashTableEntry ( key , value ) ) ;
62
+ bucket . push ( new HashMapEntry ( key , value ) ) ;
61
63
this . size ++ ;
62
64
}
63
65
@@ -118,7 +120,7 @@ export class HashTable<K, V> {
118
120
}
119
121
120
122
/**
121
- * Clears the hash table .
123
+ * Clears the hash map .
122
124
*/
123
125
clear ( ) : void {
124
126
this . size = 0 ;
@@ -162,8 +164,8 @@ export class HashTable<K, V> {
162
164
*
163
165
* @returns The entries.
164
166
*/
165
- entries ( ) : HashTableEntry < K , V > [ ] {
166
- const entries : HashTableEntry < K , V > [ ] = [ ] ;
167
+ entries ( ) : HashMapEntry < K , V > [ ] {
168
+ const entries : HashMapEntry < K , V > [ ] = [ ] ;
167
169
for ( const bucket of this . buckets ) {
168
170
for ( const entry of bucket ) {
169
171
entries . push ( entry ) ;
@@ -204,7 +206,7 @@ export class HashTable<K, V> {
204
206
}
205
207
206
208
/**
207
- * Resizes the hash table by doubling the amount of buckets.
209
+ * Resizes the hash map by doubling the amount of buckets.
208
210
*/
209
211
private resize ( ) : void {
210
212
this . initializeBuckets ( this . buckets . length * 2 ) ;
@@ -224,7 +226,7 @@ export class HashTable<K, V> {
224
226
* @param key The key.
225
227
* @param value The value.
226
228
*/
227
- class HashTableEntry < K , V > {
229
+ export class HashMapEntry < K , V > {
228
230
key : K ;
229
231
value : V ;
230
232
0 commit comments