-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
183 lines (145 loc) · 4.22 KB
/
script.js
File metadata and controls
183 lines (145 loc) · 4.22 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
import LinkedList from "./linkedList.js";
export default class HashMap {
constructor(loadFactor) {
this.buckets = Array.from({ length: 16 }, () => new LinkedList());
this.loadFactor = loadFactor;
this.capacity = 0;
}
resize() {
let totalBuckets = this.buckets.length;
//check if rezising is needed
if (this.capacity > totalBuckets * this.loadFactor) {
//double size
const newBucketCount = totalBuckets * 2;
const oldBuckets = this.buckets;
this.buckets = Array.from(
{ length: newBucketCount },
() => new LinkedList()
);
for (let bucket of oldBuckets) {
let currentNode = bucket.head;
while (currentNode !== null) {
const newIndex = this.hash(currentNode.key);
this.buckets[newIndex].append(currentNode.key, currentNode.value);
currentNode = currentNode.nextNode;
}
}
}
}
hash(key) {
let hashCode = 0;
const primeNumber = 31;
for (let i = 0; i < key.length; i++) {
hashCode =
(primeNumber * hashCode + key.charCodeAt(i)) % this.buckets.length;
}
return hashCode;
}
//helper method
_getBucket(key) {
const index = this.hash(key);
if (index < 0 || index >= this.buckets.length) {
throw new Error("Trying to access index out of bounds");
}
return this.buckets[index];
}
set(key, value) {
let bucket = this._getBucket(key);
let foundNode = bucket.findNode(key);
if (foundNode) {
//code that handles if key value pair already exists
foundNode.value = value;
} else {
//if bucket is empty, or collision
bucket.append(key, value);
this.capacity++;
}
//check if resizing is needed after adding the new element
this.resize();
console.log(bucket.toString());
}
get(key) {
let bucket = this._getBucket(key);
let foundNode = bucket.findNode(key);
return foundNode ? foundNode.value : null;
}
has(key) {
let bucket = this._getBucket(key);
let foundNode = bucket.findNode(key);
return foundNode ? true : false;
}
remove(key) {
let bucket = this._getBucket(key);
let foundIndexNode = bucket.findIndex(key);
if (foundIndexNode !== null) {
this.capacity--;
return bucket.removeAt(foundIndexNode);
}
return false;
}
length() {
return this.capacity;
}
clear() {
this.buckets = Array.from({ length: 16 }, () => new LinkedList());
}
keys() {
const keysArray = [];
for (let bucket of this.buckets) {
let currentNode = bucket.head;
while (currentNode !== null) {
keysArray.push(currentNode.key);
currentNode = currentNode.nextNode;
}
}
return keysArray;
}
values() {
const valuesArray = [];
for (let bucket of this.buckets) {
let currentNode = bucket.head;
while (currentNode !== null) {
valuesArray.push(currentNode.value);
currentNode = currentNode.nextNode;
}
}
return valuesArray;
}
entries() {
const keypairArray = [];
for (let bucket of this.buckets) {
let currentNode = bucket.head;
while (currentNode !== null) {
let keypair = [];
keypair.push(currentNode.key);
keypair.push(currentNode.value);
keypairArray.push(keypair);
currentNode = currentNode.nextNode;
}
}
return keypairArray;
}
}
// const hashMap = new HashMap(0.8);
// console.log(hashMap.hash("Carlos"));
// console.log(hashMap.hash("Carla"));
// console.log(hashMap.hash("Corbijn"));
// console.log(hashMap.hash("Corlas"));
// console.log(hashMap.hash("Neef"));
// hashMap.set("Carlos", "Test123");
// hashMap.set("Carla", "Test");
// hashMap.set("Carla", "Mogool");
// hashMap.set("Corlas", "Mogool");
// hashMap.set("Neef", "Mogool2");
// console.log(hashMap);
// console.log(`size: ${hashMap.capacity}`);
// console.log(hashMap.get("Corlas"));
// console.log(hashMap.has("Carlas"));
// // console.log(hashMap.remove("Carla"));
// // console.log(hashMap.remove("Carlos"));
// // console.log(hashMap.remove("Corlas"));
// hashMap.set("Corlas", "Mogool");
// console.log(hashMap.capacity);
// console.log(hashMap.keys());
// console.log(hashMap.values());
// console.log(hashMap.entries());