-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs_hash.js
More file actions
191 lines (159 loc) · 5.03 KB
/
js_hash.js
File metadata and controls
191 lines (159 loc) · 5.03 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//import linked list
let LinkedList = require('./js_linked_list');
class ValuePair{
constructor(key, value){
this.key = key;
this.value = value;
}
toString() {
return '[' + this.key + ' - ' + this.value + ']';
}
}
//Separate Chaining Hash implementation
class HashTableSeparateChaining {
constructor(){
this.table = [];
this._loseloseHashCode = this._loseloseHashCode.bind(this);
this._djb2HashCode = this._djb2HashCode.bind(this);
this._hashCode = this._hashCode.bind(this);
}
_loseloseHashCode(key) {
let hash = 0;
for (let i = 0; i < key.length; i++) {
hash += key.charCodeAt(i);
}
return hash % 37;
}
_djb2HashCode(key) {
let hash = 5381;
for (let i = 0; i < key.length; i++) {
hash = hash * 33 + key.charCodeAt(i);
}
return hash % 1013;
}
_hashCode(key){
return this._djb2HashCode(key);
}
put(key,value){
let position = this._hashCode(key);
console.log(position + ' - ' + key);
if (this.table[position] == undefined) {
this.table[position] = new LinkedList();
}
this.table[position].append(new ValuePair(key, value));
}
get(key){
let position = this._hashCode(key);
if (this.table[position] !== undefined && !this.table[position].isEmpty()){
//iterate linked list to find key/value
let current = this.table[position].getHead();
do {
if (current.element.key === key){
return current.element.value;
}
current = current.next;
} while(current);
}
return undefined;
}
remove(key){
let position = this._hashCode(key);
if (this.table[position] !== undefined){
//iterate linked list to find key/value
let current = this.table[position].getHead();
do {
if (current.element.key === key){
this.table[position].remove(current.element);
if (this.table[position].isEmpty()){
this.table[position] = undefined;
}
return true;
}
current = current.next;
} while(current);
}
return false;
}
print(){
for (let i = 0; i < this.table.length; ++i) {
if (this.table[i] !== undefined) {
console.log(this.table[i].toString());
}
}
}
}
//examples of hashSC
let hashSC = new HashTableSeparateChaining();
hashSC.put('ron','ron@email.com');
hashSC.put('jenny','jenny@email.com');
hashSC.print();
console.log(hashSC.get('jenny'));
hashSC.remove('ron');
hashSC.print();
//Linear Probing Hash implementation
class HashTableLinearProbing extends HashTableSeparateChaining {
constructor(){
super();
}
put(key,value){
let position = this._hashCode(key);
console.log(position + ' - ' + key);
if (this.table[position] == undefined) {
this.table[position] = new ValuePair(key, value);
} else {
let index = ++position;
while (this.table[index] != undefined){
index++;
}
this.table[index] = new ValuePair(key, value);
}
}
get(key){
let position = this._hashCode(key);
if (this.table[position] !== undefined){
if (this.table[position].key === key) {
return this.table[position].value;
} else {
let index = ++position;
while (this.table[index] !== undefined && (this.table[index] && this.table[index].key !== key)){
index++;
}
if (this.table[index] && this.table[index].key === key) {
return this.table[index].value;
}
}
}
return undefined;
}
remove(key){
let position = this._hashCode(key);
if (this.table[position] !== undefined){
if (this.table[position].key === key) {
this.table[position] = undefined;
} else {
let index = ++position;
while (this.table[index] === undefined || this.table[index].key !== key){
index++;
}
if (this.table[index].key === key) {
this.table[index] = undefined;
}
}
}
}
print(){
for (let i = 0; i < this.table.length; ++i) {
if (this.table[i] !== undefined) {
console.log(i + ' -> ' + this.table[i].toString());
}
}
}
}
let hashLP = new HashTableLinearProbing();
console.log('***linearProbing***');
hashLP.put('shiv','shiv@email.com');
hashLP.put('reed','reed@gmail.com');
hashLP.put('slee','slee@gmail.com');
console.log(hashLP.get('reed'));
hashLP.remove('reed');
hashLP.print();