Last active
January 23, 2018 17:07
-
-
Save Spuffynism/0270125c4e6394d1af21c742fe87b833 to your computer and use it in GitHub Desktop.
Very basic linear hash table https://twitter.com/SSB4Fairlines/status/942904298534506508
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 ouis = ["oui", "ouip", "ouaip", "yup", "yep", "yes", "ouai", "oua", "ouain"]; | |
function LinearOuiHashTable(size, prime) { | |
this.content = []; | |
this._prime = prime; | |
for (var i = 0; i < size; i++) | |
this.content.push([]); | |
this.insert = (oui) => { | |
this.content[this._hash(oui)].push(oui); | |
}; | |
this._hash = (oui) => { | |
var code = oui.split("") | |
.map(c => c.charCodeAt(0)) | |
.reduce((acc, v) => v + acc, 0); | |
return code % this._prime; | |
}; | |
} | |
// table creation with closest prime smaller than ouis.length for hashing func | |
var table = new LinearOuiHashTable(ouis.length, 7); | |
ouis.forEach(table.insert); | |
console.log(table.content); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment