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
| class Node { constructor(value) { this.next = null this.value = value } }
class Hash_Table { constructor() { this.size = 1024 this.data = new Array(this.size) }
_hash(string) { let hash = 5381 string .toString() .split("") .map((i) => (hash = (hash << 5) + hash + i.charCodeAt(0))) return hash }
insert(key, value) { this.data[this._hash(key) % this.size] = value }
get(key) { let value = this.data[this._hash(key) % this.size] return value === undefined ? new Error("Doesn't exist.") : value }
delete(key) { this.data[this._hash(key) % this.size] = undefined } }
let hash = new Hash_Table() hash.insert("test", 3) console.log(hash.get("test")) hash.delete("test") console.log(hash.get("test")) hash.insert(12, 44) console.log(hash.get(12))
|