|
| 1 | +const { Node } = require('../LinkedList'); |
| 2 | + |
| 3 | +class HashTable { |
| 4 | + constructor(slots) { |
| 5 | + // init with a default set of slots |
| 6 | + this.slot = slots || 17; |
| 7 | + // size to hold the current size |
| 8 | + // and help to resize when the table is half filled |
| 9 | + this.size = 0; |
| 10 | + // the main bucket |
| 11 | + this.bucket = new Array(this.slot); |
| 12 | + |
| 13 | + // fill the bucket with null |
| 14 | + for (let i = 0; i < this.slot; i += 1) this.bucket[i] = null; |
| 15 | + } |
| 16 | + |
| 17 | + _hash(key) { |
| 18 | + // convert the key to String; |
| 19 | + const stringKey = String(key); |
| 20 | + |
| 21 | + let index = 0; |
| 22 | + const PRIME = 1801; |
| 23 | + |
| 24 | + // loop till the length of the key or mamx 100 |
| 25 | + const loopTill = Math.min(stringKey.length, 100); |
| 26 | + |
| 27 | + for (let i = 0; i < loopTill; i += 1) { |
| 28 | + const char = stringKey[i]; |
| 29 | + const value = char.charCodeAt(0) - 96; |
| 30 | + index = (index + PRIME + value) % this.bucket.length; |
| 31 | + } |
| 32 | + return index; |
| 33 | + } |
| 34 | + |
| 35 | + _push(index, value) { |
| 36 | + /** |
| 37 | + * Util to add a SSL to the index in case of more than once |
| 38 | + * value for the same key exixts |
| 39 | + */ |
| 40 | + const node = new Node(value); |
| 41 | + if (!this.bucket[index]) { |
| 42 | + this.bucket[index] = node; |
| 43 | + this.size += 1; |
| 44 | + return index; |
| 45 | + } |
| 46 | + |
| 47 | + let head = this.bucket[index]; |
| 48 | + |
| 49 | + // traverse to the end |
| 50 | + while (head.next !== null) { |
| 51 | + head = head.next; |
| 52 | + } |
| 53 | + head.next = node; |
| 54 | + this.size += 1; |
| 55 | + return index; |
| 56 | + } |
| 57 | + |
| 58 | + _values(index, key) { |
| 59 | + /** |
| 60 | + * Util to return the values as an array |
| 61 | + */ |
| 62 | + const res = []; |
| 63 | + let head = this.bucket[index]; |
| 64 | + while (head !== null) { |
| 65 | + if (head.data.key === key) { |
| 66 | + res.push(head.data.value); |
| 67 | + } |
| 68 | + head = head.next; |
| 69 | + } |
| 70 | + return res; |
| 71 | + } |
| 72 | + |
| 73 | + set(key, value) { |
| 74 | + // eslint-disable-next-line no-underscore-dangle |
| 75 | + const index = this._hash(key); |
| 76 | + // storing value as an key-value pair |
| 77 | + // eslint-disable-next-line no-underscore-dangle |
| 78 | + this._push(index, { key, value }); |
| 79 | + } |
| 80 | + |
| 81 | + get(key) { |
| 82 | + // get the index |
| 83 | + // eslint-disable-next-line no-underscore-dangle |
| 84 | + const index = this._hash(key); |
| 85 | + if (!this.bucket[index]) return null; |
| 86 | + // eslint-disable-next-line no-underscore-dangle |
| 87 | + return this._values(index, key); |
| 88 | + } |
| 89 | + |
| 90 | + getSize() { |
| 91 | + return this.size; |
| 92 | + } |
| 93 | + |
| 94 | + isEmpty() { |
| 95 | + return this.getSize() === 0; |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +// const ht = new HashTable(); |
| 100 | +// ht.set('hello', 'I am a new value'); |
| 101 | +// ht.set('hello', 'I am a yet another value'); |
| 102 | +// ht.set('maroon', 'I maroon'); |
| 103 | +// ht.set('yellow', 'I am yellow'); |
| 104 | + |
| 105 | +// console.log(ht.get('hello')); |
| 106 | +// console.log(ht.get('maroon')); |
| 107 | +// console.log(ht.bucket); |
| 108 | + |
| 109 | +module.exports = HashTable; |
0 commit comments