Make it possible to add objects to LinkedList.

This commit is contained in:
Oleksii Trekhleb
2018-04-09 12:15:28 +03:00
parent cdf72208b3
commit 8c46dbfb6d
10 changed files with 206 additions and 191 deletions

View File

@@ -21,16 +21,32 @@ export default class HashTable {
insert(key, value) {
const bucketLinkedList = this.buckets[this.hash(key)];
bucketLinkedList.appendUnique({ key, value });
const node = bucketLinkedList.find({ callback: nodeValue => nodeValue.key === key });
if (!node) {
// Insert new node.
bucketLinkedList.append({ key, value });
} else {
// Update value of existing node.
node.value.value = value;
}
}
delete(key) {
const bucketLinkedList = this.buckets[this.hash(key)];
return bucketLinkedList.deleteByKey(key);
const node = bucketLinkedList.find({ callback: nodeValue => nodeValue.key === key });
if (node) {
return bucketLinkedList.delete(node.value);
}
return null;
}
get(key) {
const bucketLinkedList = this.buckets[this.hash(key)];
return bucketLinkedList.findByKey(key);
const node = bucketLinkedList.find({ callback: nodeValue => nodeValue.key === key });
return node ? node.value.value : null;
}
}