-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTable.js
More file actions
37 lines (31 loc) · 866 Bytes
/
HashTable.js
File metadata and controls
37 lines (31 loc) · 866 Bytes
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
class HashTable {
constructor() {
this.table = [];
}
static loseloseHashCode(key) {
let hash = 0;
for (let codePoint of key) {
hash += codePoint.charCodeAt();
}
return hash % 37;
}
put(key, value) {
const position = HashTable.loseloseHashCode(key);
console.log(`${position} - ${key}`);
this.table[position] = value;
}
get(key) {
return this.table[HashTable.loseloseHashCode(key)];
}
remove(key) {
this.table[HashTable.loseloseHashCode(key)] = undefined;
}
}
const hash = new HashTable();
hash.put("Surmon", "surmon.me@email.com"); // 19 - Surmon
hash.put("John", "johnsnow@email.com"); // 29 - John
hash.put("Tyrion", "tyrion@email.com"); // 16 - Tyrion
// 测试get方法
console.log(hash.get("Surmon")); // surmon.me@email.com
console.log(hash.get("Loiane")); // undefined
console.log(hash);