Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix quadractic probing overflowing the array (#21)
function HashTable(size) { this.size = size; this.keys = this.initArray(size); this.values = this.initArray(size); this.limit = 0; } HashTable.prototype.get = function(key) { var hashedIndex = this.hash(key); while (this.keys[hashedIndex] != key) { hashedIndex++; hashedIndex = hashedIndex % this.size; } return this.values[hashedIndex]; } HashTable.prototype.hash = function(key) { // Check if int if (!Number.isInteger(key)) throw 'must be int'; return key % this.size; } HashTable.prototype.initArray = function(size) { var array = []; for (var i = 0; i < size; i++) { array.push(null); } return array; } HashTable.prototype.put = function(key, value) { if (this.limit >= this.size) throw 'hash table is full' var hashedIndex = this.hash(key), squareIndex = 1; // quadratic probing while (this.keys[hashedIndex % this.size] != null) { hashedIndex += Math.pow(squareIndex, 2); squareIndex++; } this.keys[hashedIndex % this.size] = key; this.values[hashedIndex % this.size] = value; this.limit++; } HashTable.prototype.get = function(key) { var hashedIndex = this.hash(key), squareIndex = 1; while (this.keys[hashedIndex % this.size] != key) { hashedIndex += Math.pow(squareIndex, 2); hashedIndex = hashedIndex % this.size; squareIndex++; } return this.values[hashedIndex % this.size]; } var exampletable = new HashTable(13); exampletable.put(7, "hi"); exampletable.put(20, "hello"); exampletable.put(33, "sunny"); exampletable.put(46, "weather"); exampletable.put(59, "wow"); exampletable.put(72, "fourty"); exampletable.put(85, "happy"); exampletable.put(98, "sad");
- Loading branch information