-
Notifications
You must be signed in to change notification settings - Fork 167
/
Chapter11(HashTables).js
143 lines (106 loc) · 3.46 KB
/
Chapter11(HashTables).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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
localStorage.setItem("testKey", "testValue");
location = location; // refreshes the page
//-----------------------------------
localStorage.getItem("testKey"); // prints "testValue"
// Start of: Linear Probing -----------------------------------
function HashTable(size) {
this.size = size;
this.keys = this.initArray(size);
this.values = this.initArray(size);
this.limit = 0;
}
HashTable.prototype.initArray = function(size) {
var array = [];
for (var i = 0; i < size; i++) {
array.push(null);
}
return array;
}
HashTable.prototype.put = function(key, value) {
if (this.limit >= this.size) throw 'hash table is full'
var hashedIndex = this.hash(key);
// Linear probing
while (this.keys[hashedIndex] != null) {
hashedIndex++;
hashedIndex = hashedIndex % this.size;
}
this.keys[hashedIndex] = key;
this.values[hashedIndex] = value;
this.limit++;
}
HashTable.prototype.get = function(key) {
var hashedIndex = this.hash(key);
while (this.keys[hashedIndex] != key) {
hashedIndex++;
hashedIndex = hashedIndex % this.size;
}
return this.values[hashedIndex];
}
HashTable.prototype.hash = function(key) {
// Check if int
if (!Number.isInteger(key)) throw 'must be int';
return key % this.size;
}
var exampletable = new HashTable(13);
exampletable.put(7, "hi");
exampletable.put(20, "hello");
exampletable.put(33, "sunny");
exampletable.put(46, "weather");
exampletable.put(59, "wow");
exampletable.put(72, "fourty");
exampletable.put(85, "happy");
exampletable.put(98, "sad");
// End of: Linear Probing ------------------------------------0
// Start of: Quadratic Probing --------------------------------
HashTable.prototype.put = function(key, value) {
if (this.limit >= this.size) throw 'hash table is full'
var hashedIndex = this.hash(key),
squareIndex = 1;
// quadratic probing
while (this.keys[hashedIndex % this.size] != null) {
hashedIndex += Math.pow(squareIndex, 2);
squareIndex++;
}
this.keys[hashedIndex % this.size] = key;
this.values[hashedIndex % this.size] = value;
this.limit++;
}
HashTable.prototype.get = function(key) {
var hashedIndex = this.hash(key),
squareIndex = 1;
while (this.keys[hashedIndex % this.size] != key) {
hashedIndex += Math.pow(squareIndex, 2);
hashedIndex = hashedIndex % this.size;
squareIndex++;
}
return this.values[hashedIndex % this.size];
}
// End of: Quadratic Probing ----------------------------------
// Star of: Double Hashing with Linear Probing ----------------
HashTable.prototype.put = function(key, value) {
if (this.limit >= this.size) throw 'hash table is full'
var hashedIndex = this.hash(key);
while (this.keys[hashedIndex] != null) {
hashedIndex++;
hashedIndex = hashedIndex % this.size;
}
this.keys[hashedIndex] = key;
this.values[hashedIndex] = value;
this.limit++;
}
HashTable.prototype.get = function(key) {
var hashedIndex = this.hash(key);
while (this.keys[hashedIndex] != key) {
hashedIndex++;
hashedIndex = hashedIndex % this.size;
}
return this.values[hashedIndex];
}
HashTable.prototype.hash = function(key) {
if (!Number.isInteger(key)) throw 'must be int'; // check if int
return this.secondHash(key);
}
HashTable.prototype.secondHash = function(hashedKey) {
var R = this.size - 2;
return R - hashedKey % R;
}