Created
March 25, 2014 18:44
-
-
Save bjkeller/9768443 to your computer and use it in GitHub Desktop.
Beginning of simple hash table in JS for workshopping. Uses higher-order function that creates a hash code function on Juliene Walker's jsw_hash.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var SimpleHashTable = function(hashCode) { | |
var table = []; | |
var Constructor = function() { }; | |
Constructor.prototype.add = function(opts) { | |
}; | |
return new Constructor(); | |
} | |
module.exports.SimpleHashTable = SimpleHashTable; | |
/* | |
Based on jswhash given on http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx | |
*/ | |
var createJSWHash = function(len) { | |
var tab = []; | |
for (var i = 0; i < 256; i++) { | |
tab[i] = Math.floor(Math.random()*Math.pow(2,32)); | |
} | |
var hash = function(strkey) { | |
var h = (16777551 >>> 0); | |
for (var i = 0; i < len; i++) { | |
h = (h << 1 | h >>> 31) ^ tab[strkey.charCodeAt(i)]; | |
} | |
return h; | |
} | |
return hash; | |
} | |
module.exports.createJSWHash = createJSWHash; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment