-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathhash-table.js
More file actions
38 lines (30 loc) · 734 Bytes
/
hash-table.js
File metadata and controls
38 lines (30 loc) · 734 Bytes
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
// Generating a hash table with string keys
function HashTable() {
this.table = new Array(137);
this.hashFunction = hashFunction;
this.showDistro = showDistro;
this.put = put;
};
function put(data) {
var pos = this.hashFunction(data);
this.table[pos] = data;
};
function hashFunction(string) {
var total = 0;
const H = 37;
for (var i = 0; i < string.length; i++) {
total += H * total + string.charCodeAt(i);
}
total = total % this.table.length;
if (total < 0) {
total += this.table.length - 1
}
return parseInt(total);
};
function showDistro() {
for (var i = 0; i < this.table.length; i++) {
if (this.table[i] != undefined) {
console.log(i + ": " + this.table[i]);
}
}
};