-
Notifications
You must be signed in to change notification settings - Fork 500
/
hash-table.js
60 lines (58 loc) · 1.69 KB
/
hash-table.js
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
function HashTable(size) {
this.values = {};
this.numberOfValues = 0;
this.size = size;
}
HashTable.prototype.add = function(key, value) {
var hash = this.calculateHash(key);
if(!this.values.hasOwnProperty(hash)) {
this.values[hash] = {};
}
if(!this.values[hash].hasOwnProperty(key)) {
this.numberOfValues++;
}
this.values[hash][key] = value;
};
HashTable.prototype.remove = function(key) {
var hash = this.calculateHash(key);
if(this.values.hasOwnProperty(hash) && this.values[hash].hasOwnProperty(key)) {
delete this.values[hash][key];
this.numberOfValues--;
}
};
HashTable.prototype.calculateHash = function(key) {
return key.toString().length % this.size;
};
HashTable.prototype.search = function(key) {
var hash = this.calculateHash(key);
if(this.values.hasOwnProperty(hash) && this.values[hash].hasOwnProperty(key)) {
return this.values[hash][key];
} else {
return null;
}
};
HashTable.prototype.length = function() {
return this.numberOfValues;
};
HashTable.prototype.print = function() {
var string = '';
for(var value in this.values) {
for(var key in this.values[value]) {
string += this.values[value][key] + ' ';
}
}
console.log(string.trim());
};
var hashTable = new HashTable(3);
hashTable.add('first', 1);
hashTable.add('second', 2);
hashTable.add('third', 3);
hashTable.add('fourth', 4);
hashTable.add('fifth', 5);
hashTable.print(); // => 2 4 1 3 5
console.log('length gives 5:', hashTable.length()); // => 5
console.log('search second gives 2:', hashTable.search('second')); // => 2
hashTable.remove('fourth');
hashTable.remove('first');
hashTable.print(); // => 2 3 5
console.log('length gives 3:', hashTable.length()); // => 3