forked from knaxus/problem-solving-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
109 lines (92 loc) · 2.51 KB
/
index.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
const { Node } = require('../LinkedList');
class HashTable {
constructor(slots) {
// init with a default set of slots
this.slot = slots || 17;
// size to hold the current size
// and help to resize when the table is half filled
this.size = 0;
// the main bucket
this.bucket = new Array(this.slot);
// fill the bucket with null
for (let i = 0; i < this.slot; i += 1) this.bucket[i] = null;
}
_hash(key) {
// convert the key to String;
const stringKey = String(key);
let index = 0;
const PRIME = 1801;
// loop till the length of the key or mamx 100
const loopTill = Math.min(stringKey.length, 100);
for (let i = 0; i < loopTill; i += 1) {
const char = stringKey[i];
const value = char.charCodeAt(0) - 96;
index = (index + PRIME + value) % this.bucket.length;
}
return index;
}
_push(index, value) {
/**
* Util to add a SSL to the index in case of more than once
* value for the same key exixts
*/
const node = new Node(value);
if (!this.bucket[index]) {
this.bucket[index] = node;
this.size += 1;
return index;
}
let head = this.bucket[index];
// traverse to the end
while (head.next !== null) {
head = head.next;
}
head.next = node;
this.size += 1;
return index;
}
_values(index, key) {
/**
* Util to return the values as an array
*/
const res = [];
let head = this.bucket[index];
while (head !== null) {
if (head.data.key === key) {
res.push(head.data.value);
}
head = head.next;
}
return res;
}
set(key, value) {
// eslint-disable-next-line no-underscore-dangle
const index = this._hash(key);
// storing value as an key-value pair
// eslint-disable-next-line no-underscore-dangle
this._push(index, { key, value });
}
get(key) {
// get the index
// eslint-disable-next-line no-underscore-dangle
const index = this._hash(key);
if (!this.bucket[index]) return null;
// eslint-disable-next-line no-underscore-dangle
return this._values(index, key);
}
getSize() {
return this.size;
}
isEmpty() {
return this.getSize() === 0;
}
}
// const ht = new HashTable();
// ht.set('hello', 'I am a new value');
// ht.set('hello', 'I am a yet another value');
// ht.set('maroon', 'I maroon');
// ht.set('yellow', 'I am yellow');
// console.log(ht.get('hello'));
// console.log(ht.get('maroon'));
// console.log(ht.bucket);
module.exports = HashTable;