-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashTable.ts
165 lines (133 loc) · 3.66 KB
/
hashTable.ts
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
export default class HashTable {
private dataStore: Array<HashTableLinkedList>;
constructor(size = 53) {
this.dataStore = new Array(size);
}
/**
* Hashes the specified key to a number between 0 (zero) and the specified `upperLimit` value (excluding the `upperLimit`)
* @param key the string to be hashed
* @param upperLimit the upper limit of the resulting hash number
*/
private _hash(key: string) {
let newIndex = 0;
let RANDOM_PRIME = 17;
for (let i = 0; i < Math.min(key.length, 100); i++) {
let encoding = key[i].charCodeAt(0) - 96;
newIndex = (newIndex * RANDOM_PRIME + encoding) % this.dataStore.length;
}
return newIndex;
}
public getLength() {
return this.dataStore.length;
}
set(key: string, value: any) {
let index = this._hash(key);
if (this.dataStore[index]) {
this.dataStore[index].set(key, value);
} else {
this.dataStore[index] = new HashTableLinkedList(key, value);
}
}
get(key: string) {
let index = this._hash(key);
return this.dataStore[index]?.get(key)?.value;
}
keys() {
let allKeys: Array<string | number> = [];
this.dataStore.forEach((list) => {
for (let item of list) {
if (item) allKeys.push(item.key);
}
});
return allKeys;
}
values() {
let allValues: Array<any> = [];
this.dataStore.forEach((list) => {
for (let item of list) {
if (item) allValues.push(item.value);
}
});
return allValues;
}
}
class HashTableNode {
key: string;
value: any;
next: HashTableNode | null;
constructor(key: HashTableNode['key'], value: HashTableNode['value']) {
this.key = key;
this.value = value;
this.next = null;
}
}
class HashTableLinkedList {
head: HashTableNode | null;
tail: HashTableNode | null;
length: number;
constructor(key: HashTableNode['key'], value: HashTableNode['value']) {
this.head = new HashTableNode(key, value);
this.tail = this.head;
this.length = 0;
}
/**
* Return the node at the given position in the list
* @param index the position of the node in the list
*/
get(key: string): HashTableNode | undefined {
if (!this.head) return undefined;
let currentNode: HashTableNode | null = this.head;
let foundNode: HashTableNode | undefined = undefined;
while (currentNode?.next || !foundNode) {
if (currentNode?.key === key) {
foundNode = currentNode;
} else {
currentNode = currentNode?.next ?? null;
}
}
return foundNode;
}
/**
* Adds/replaces an item in the list. Time Complexity: O(N)
* @param key the key of the node
* @param value the value of the node
*/
set(key: HashTableNode['key'], value: HashTableNode['value']) {
let foundNode = this.get(key);
if (foundNode) {
foundNode.value = value;
} else {
this.#push(key, value);
}
}
/**
* Add a new node to the end of the list. The newly added node becomes the tail
* @param value the value to be added to the list
*/
#push(key: string, value: HashTableNode['value']): HashTableLinkedList {
let newNode = new HashTableNode(key, value);
if (this.tail) {
this.tail.next = newNode;
this.tail = newNode;
} else {
this.head = newNode;
this.tail = this.head;
}
this.length++;
return this;
}
[Symbol.iterator]() {
let currentNode = this.head;
return {
next: () => {
if (currentNode === null) return { done: true };
let returnNode = currentNode;
currentNode = currentNode?.next;
return {
value: returnNode,
done: !!currentNode,
};
},
};
}
}