Uh oh!
There was an error while loading. Please reload this page.
forked from trekhleb/javascript-algorithms
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTable.js
More file actions
Latest commit
108 lines (94 loc) · 2.85 KB
/
Copy pathHashTable.js
File metadata and controls
108 lines (94 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
importLinkedListfrom'../linked-list/LinkedList';
// Hash table size directly affects on the number of collisions.
// The bigger the hash table size the less collisions you'll get.
// For demonstrating purposes hash table size is small to show how collisions
// are being handled.
constdefaultHashTableSize=32;
exportdefaultclassHashTable{
/**
* @param {number} hashTableSize
*/
constructor(hashTableSize=defaultHashTableSize){
// Create hash table of certain size and fill each bucket with empty linked list.
this.buckets=Array(hashTableSize).fill(null).map(()=>newLinkedList());
// Just to keep track of all actual keys in a fast way.
this.keys={};
}
/**
* Converts key string to hash number.
*
* @param {string} key
* @return {number}
*/
hash(key){
// For simplicity reasons we will just use character codes sum of all characters of the key
// to calculate the hash.
//
// But you may also use more sophisticated approaches like polynomial string hash to reduce the
// number of collisions:
//
// hash = charCodeAt(0) * PRIME^(n-1) + charCodeAt(1) * PRIME^(n-2) + ... + charCodeAt(n-1)
//
// where charCodeAt(i) is the i-th character code of the key, n is the length of the key and
// PRIME is just any prime number like 31.
consthash=Array.from(key).reduce(
(hashAccumulator,keySymbol)=>(hashAccumulator+keySymbol.charCodeAt(0)),
0,
);
// Reduce hash number so it would fit hash table size.
returnhash%this.buckets.length;
}
/**
* @param {string} key
* @param {*} value
*/
set(key,value){
constkeyHash=this.hash(key);
this.keys[key]=keyHash;
constbucketLinkedList=this.buckets[keyHash];
constnode=bucketLinkedList.find({callback: nodeValue=>nodeValue.key===key});
if(!node){
// Insert new node.
bucketLinkedList.append({ key, value });
}else{
// Update value of existing node.
node.value.value=value;
}
}
/**
* @param {string} key
* @return {*}
*/
delete(key){
constkeyHash=this.hash(key);
deletethis.keys[key];
constbucketLinkedList=this.buckets[keyHash];
constnode=bucketLinkedList.find({callback: nodeValue=>nodeValue.key===key});
if(node){
returnbucketLinkedList.delete(node.value);
}
returnnull;
}
/**
* @param {string} key
* @return {*}
*/
get(key){
constbucketLinkedList=this.buckets[this.hash(key)];
constnode=bucketLinkedList.find({callback: nodeValue=>nodeValue.key===key});
returnnode ? node.value.value : undefined;
}
/**
* @param {string} key
* @return {boolean}
*/
has(key){
returnObject.hasOwnProperty.call(this.keys,key);
}
/**
* @return {string[]}
*/
getKeys(){
returnObject.keys(this.keys);
}
}