mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2026-03-13 08:51:02 +08:00
Add new hash table methods.
This commit is contained in:
@@ -1,14 +1,29 @@
|
||||
import LinkedList from '../linked-list/LinkedList';
|
||||
|
||||
// Hash table size directly affects on the number of collisions.
|
||||
// The bigger the hash table size the less collisions you'll get.
|
||||
// For demonstrating purposes hash table size is small to show how collisions
|
||||
// are being handled.
|
||||
const defaultHashTableSize = 32;
|
||||
|
||||
export default class HashTable {
|
||||
/**
|
||||
* @param {number} hashTableSize
|
||||
*/
|
||||
constructor(hashTableSize = defaultHashTableSize) {
|
||||
// Create hash table of certain size and fill each bucket with empty linked list.
|
||||
this.buckets = Array(hashTableSize).fill(null).map(() => new LinkedList());
|
||||
|
||||
// Just to keep track of all actual keys in a fast way.
|
||||
this.keys = {};
|
||||
}
|
||||
|
||||
// Converts key string to hash number.
|
||||
/**
|
||||
* Converts key string to hash number.
|
||||
*
|
||||
* @param {string} key
|
||||
* @return {number}
|
||||
*/
|
||||
hash(key) {
|
||||
const hash = Array.from(key).reduce(
|
||||
(hashAccumulator, keySymbol) => (hashAccumulator + keySymbol.charCodeAt(0)),
|
||||
@@ -19,8 +34,14 @@ export default class HashTable {
|
||||
return hash % this.buckets.length;
|
||||
}
|
||||
|
||||
insert(key, value) {
|
||||
const bucketLinkedList = this.buckets[this.hash(key)];
|
||||
/**
|
||||
* @param {string} key
|
||||
* @param {*} value
|
||||
*/
|
||||
set(key, value) {
|
||||
const keyHash = this.hash(key);
|
||||
this.keys[key] = keyHash;
|
||||
const bucketLinkedList = this.buckets[keyHash];
|
||||
const node = bucketLinkedList.find({ callback: nodeValue => nodeValue.key === key });
|
||||
|
||||
if (!node) {
|
||||
@@ -32,8 +53,14 @@ export default class HashTable {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} key
|
||||
* @return {*}
|
||||
*/
|
||||
delete(key) {
|
||||
const bucketLinkedList = this.buckets[this.hash(key)];
|
||||
const keyHash = this.hash(key);
|
||||
delete this.keys[key];
|
||||
const bucketLinkedList = this.buckets[keyHash];
|
||||
const node = bucketLinkedList.find({ callback: nodeValue => nodeValue.key === key });
|
||||
|
||||
if (node) {
|
||||
@@ -43,10 +70,29 @@ export default class HashTable {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} key
|
||||
* @return {*}
|
||||
*/
|
||||
get(key) {
|
||||
const bucketLinkedList = this.buckets[this.hash(key)];
|
||||
const node = bucketLinkedList.find({ callback: nodeValue => nodeValue.key === key });
|
||||
|
||||
return node ? node.value.value : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} key
|
||||
* @return {boolean}
|
||||
*/
|
||||
has(key) {
|
||||
return Object.hasOwnProperty.call(this.keys, key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {string[]}
|
||||
*/
|
||||
getKeys() {
|
||||
return Object.keys(this.keys);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user