-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Handle collision in the HashMap - Fixes #66 #68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,14 +18,21 @@ | |
* HashTable.remove(10); | ||
* HashTable.remove('key'); | ||
* | ||
* console.log(HashTable.get(10)); // 'undefined' | ||
* console.log(HashTable.get('key')); // 'undefined' | ||
* console.log(HashTable.get(10)); // null | ||
* console.log(HashTable.get('key')); // null | ||
* | ||
* @module data-structures/hash-table | ||
*/ | ||
(function (exports) { | ||
'use strict'; | ||
|
||
exports.Node = function (key, data) { | ||
this.key = key; | ||
this.data = data; | ||
this.next = null; | ||
this.prev = null; | ||
}; | ||
|
||
exports.HashTable = function () { | ||
this.elements = []; | ||
}; | ||
|
@@ -41,25 +48,71 @@ | |
|
||
for (i = 0; i < str.length; i += 1) { | ||
character = str.charCodeAt(i); | ||
/*jshint -W016 */ | ||
hashCode = ((hashCode << 5) - hashCode) + character; | ||
hashCode = hashCode & hashCode; | ||
/*jshint -W016 */ | ||
} | ||
|
||
return hashCode; | ||
}; | ||
|
||
exports.HashTable.prototype.put = function (key, value) { | ||
exports.HashTable.prototype.put = function (key, data) { | ||
var hashCode = this.hashCode(key); | ||
this.elements[hashCode] = value; | ||
var newNode = new Node(key, data); | ||
|
||
if (this.elements[hashCode] === null) { | ||
this.elements[hashCode] = newNode; | ||
} else { | ||
var first = this.elements[hashCode]; | ||
|
||
while (first.next !== null) { | ||
first = first.next; | ||
} | ||
|
||
first.next = newNode; | ||
newNode.prev = first; | ||
} | ||
}; | ||
|
||
exports.HashTable.prototype.get = function (key) { | ||
var hashCode = this.hashCode(key); | ||
return this.elements[hashCode]; | ||
|
||
if (this.elements[hashCode] === null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
return null; | ||
} else if (this.elements[hashCode].next === null) { | ||
return this.elements[hashCode]; | ||
} else { | ||
var first = this.elements[hashCode]; | ||
|
||
while (first.key !== key) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the code enters the body of this if statement and the key is not found (i.e. while (first && first.key !== key) {
first = first.next;
} |
||
first = first.next; | ||
} | ||
|
||
return first; | ||
} | ||
}; | ||
|
||
exports.HashTable.prototype.remove = function (key) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
var hashCode = this.hashCode(key); | ||
this.elements.splice(hashCode, 1); | ||
|
||
if (this.elements[hashCode] === null) { | ||
return false; | ||
} else if (this.elements[hashCode].next === null) { | ||
this.elements.splice(hashCode, 1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using You can use |
||
return true; | ||
} else { | ||
var first = this.elements[hashCode]; | ||
|
||
while (first.key !== key) { | ||
first = first.next; | ||
} | ||
|
||
if (first.next !== null) { | ||
first.next.prev = first.prev; | ||
} | ||
|
||
first.prev.next = first.next; | ||
} | ||
}; | ||
})(typeof window === 'undefined' ? module.exports : window); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since
this.elements[hashCode]
might not be defined, I guess the check should be:this.elements[hashCode] === undefined