Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 45 additions & 3 deletions src/data-structures/hash-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,42 @@
(function (exports) {
'use strict';

/**
* Constructs a Node to store data and next/prev nodes in Hash table.
*
* @public
* @constructor
* @param {Number|String} key Key of the node.
* @param {Number|String} data Data to be stored in hash table.
*/
exports.Node = function (key, data) {
this.key = key;
this.data = data;
this.next = undefined;
this.prev = undefined;
};

/**
* Construct a Hash table..
*
* @public
* @constructor
*/
exports.Hashtable = function () {
this.buckets = [];
// The higher the bucket count; less likely for collisions.
this.maxBucketCount = 100;
};

/*
Using simple non-crypto x->integer based hash.
*/
/**
* Simple non-crypto hash used to hash keys, which determines
* while bucket the value will be placed in.
* A javascript implementation of Java's 32bitint hash.
*
* @public
* @method
* @param {Number|String} val Key to be hashed.
*/
exports.Hashtable.prototype.hashCode = function (val) {
var i;
var hashCode = 0;
Expand All @@ -63,6 +83,14 @@
return hashCode;
};

/**
* Puts data into the table based on hashed key value.
*
* @public
* @method
* @param {Number|String} key Key for data.
* @param {Number|String} data Data to be stored in table.
*/
exports.Hashtable.prototype.put = function (key, data, hashCode) {
/*
Make collision testing easy with optional hashCode parameter.
Expand Down Expand Up @@ -104,6 +132,13 @@
newNode.prev = first;
};

/**
* Get's data from the table based on key.
*
* @public
* @method
* @param {Number|String} key Key for data to be retrieved.
*/
exports.Hashtable.prototype.get = function (key, hashCode) {
/*
Make collision testing easy with optional hashCode parameter.
Expand Down Expand Up @@ -143,6 +178,13 @@
}
};

/**
* Removes data from the table based on key.
*
* @public
* @method
* @param {Number|String} key Key of the data to be removed.
*/
exports.Hashtable.prototype.remove = function (key, hashCode) {
/*
Make collision testing easy with optional hashCode parameter.
Expand Down