|
| 1 | +// src/08-dictionary-hash/hash-table-linear-probing.js |
| 2 | + |
| 3 | +class HashTableLinearProbing { |
| 4 | + #table = []; |
| 5 | + |
| 6 | + #loseLoseHashCode(key) { |
| 7 | + if (typeof key !== 'string') { |
| 8 | + key = this.#elementToString(key); |
| 9 | + } |
| 10 | + const calcASCIIValue = (acc, char) => acc + char.charCodeAt(0); |
| 11 | + const hash = key.split('').reduce((acc, char) => calcASCIIValue, 0); |
| 12 | + return hash % 37; // mod to reduce the hash code |
| 13 | + } |
| 14 | + |
| 15 | + #djb2HashCode(key) { |
| 16 | + if (typeof key !== 'string') { |
| 17 | + key = this.#elementToString(key); |
| 18 | + } |
| 19 | + const calcASCIIValue = (acc, char) => (acc * 33) + char.charCodeAt(0); |
| 20 | + const hash = key.split('').reduce((acc, char) => calcASCIIValue, 5381); |
| 21 | + return hash % 1013; |
| 22 | + } |
| 23 | + |
| 24 | + hash(key) { |
| 25 | + return this.#loseLoseHashCode(key); |
| 26 | + } |
| 27 | + |
| 28 | + #elementToString(data) { |
| 29 | + if (typeof data === 'object' && data !== null) { |
| 30 | + return JSON.stringify(data); |
| 31 | + } else { |
| 32 | + return data.toString(); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + put(key, value) { |
| 37 | + if (key != null && value != null) { |
| 38 | + let index = this.hash(key); |
| 39 | + |
| 40 | + // linear probing to find an empty slot |
| 41 | + while (this.#table[index] != null) { |
| 42 | + if (this.#table[index].key === key) { |
| 43 | + this.#table[index].value = value; // update existing key |
| 44 | + return true; |
| 45 | + } |
| 46 | + index++; |
| 47 | + index %= this.#table.length; // wrap around if end is reached |
| 48 | + } |
| 49 | + |
| 50 | + this.#table[index] = {key, value}; |
| 51 | + return true; |
| 52 | + } |
| 53 | + return false; |
| 54 | + } |
| 55 | + |
| 56 | + get(key) { |
| 57 | + let index = this.hash(key); |
| 58 | + |
| 59 | + // Linear probing to search for the key |
| 60 | + while (this.#table[index] != null) { |
| 61 | + if (this.#table[index].key === key) { |
| 62 | + return this.#table[index].value; |
| 63 | + } |
| 64 | + index++; |
| 65 | + index %= this.#table.length; |
| 66 | + } |
| 67 | + |
| 68 | + return undefined; // key not found |
| 69 | + } |
| 70 | + |
| 71 | + remove(key) { |
| 72 | + let index = this.hash(key); |
| 73 | + while (this.#table[index] != null) { |
| 74 | + if (this.#table[index].key === key) { |
| 75 | + delete this.#table[index]; |
| 76 | + this.#verifyRemoveSideEffect(key, index); |
| 77 | + return true; |
| 78 | + } |
| 79 | + index++; |
| 80 | + index %= this.#table.length; |
| 81 | + } |
| 82 | + return false; // key not found |
| 83 | + } |
| 84 | + |
| 85 | + #verifyRemoveSideEffect(key, removedPosition) { |
| 86 | + const size = this.#table.length; |
| 87 | + let index = removedPosition + 1; |
| 88 | + while (this.#table[index] != null) { |
| 89 | + const currentKey = this.#table[index].key; |
| 90 | + const currentHash = this.hash(currentKey); |
| 91 | + // check if the element should be repositioned |
| 92 | + if (currentHash <= removedPosition) { |
| 93 | + this.#table[removedPosition] = this.#table[index]; |
| 94 | + delete this.#table[index]; |
| 95 | + removedPosition = index; |
| 96 | + } |
| 97 | + index++; |
| 98 | + index %= size; // Wrap around if end is reached |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments