HashTable
Source
const LinkedList = require("./LinkedList");
const defaultHashTableSize = 32;
class HashTable {
constructor(hashTableSize = defaultHashTableSize) {
this.buckets = Array(hashTableSize)
.fill(null)
.map(() => new LinkedList());
this.keys = {};
}
hash(key) {
const hash = Array.from(key).reduce((acc, cur) => {
return acc + cur.charCodeAt(0);
}, 0);
return hash % this.buckets.length;
}
set(key, value) {
const hash = this.hash(key);
this.keys[key] = hash;
const bucket = this.buckets[hash];
const node = bucket.find({ callback: item => item.key === key });
if (!node) {
bucket.append({ key, value });
} else {
node.value.value = value;
}
}
delete(key) {
const hash = this.hash(key);
delete this.keys[hash];
const bucket = this.buckets[hash];
const node = bucket.find({ callback: item => item.key === key });
if (node) {
return bucket.delete(node.value);
}
return null;
}
get(key) {
const hash = this.hash(key);
const bucket = this.buckets[hash];
const node = bucket.find({ callback: item => item.key === key });
return node ? node.value.value : undefined;
}
has(key) {
return Object.hasOwnProperty.call(this.keys, key);
}
getKeys() {
return Object.keys(this.keys);
}
}
export default HashTable;