-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashMap.js
59 lines (54 loc) · 1.56 KB
/
HashMap.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
const LinkedList = require('./SinglyLinkedList');
const Node = require('./Node');
//Hashmap class that utilizes separate chaining for collisions
class HashMap {
// creates an array of linked lists
constructor(size = 0) {
this.hashmap = new Array(size)
.fill(null)
.map(() => new LinkedList());
}
//hashing method
hash(key) {
let hashCode = 0;
for (let i = 0; i < key.length; i++) {
hashCode += hashCode + key.charCodeAt(i);
}
return hashCode % this.hashmap.length;
}
// method assigns key and value to proper index according to hashing method
assign(key, value) {
const arrayIndex = this.hash(key);
const linkedList = this.hashmap[arrayIndex];
console.log(`Storing ${value} at index ${arrayIndex}`);
if (linkedList.head === null) {
linkedList.addToHead({ key, value });
return;
}
let current = linkedList.head;
while (current) {
if (current.data.key === key) {
current.data = { key, value };
}
if (!current.next) {
current.next = new Node({ key, value });
break;
}
current = current.next;
}
}
//method retrieves the value at the specified key in the array
retrieve(key) {
const arrayIndex = this.hash(key);
let current = this.hashmap[arrayIndex].head;
while (current) {
if (current.data.key === key) {
console.log(`\nRetrieving ${current.data.value} from index ${arrayIndex}`);
return current.data.value;
}
current = current.next;
}
return null;
}
}
module.exports = HashMap;