Skip to content

Instantly share code, notes, and snippets.

@Spuffynism
Last active January 23, 2018 17:07
Show Gist options
  • Save Spuffynism/0270125c4e6394d1af21c742fe87b833 to your computer and use it in GitHub Desktop.
Save Spuffynism/0270125c4e6394d1af21c742fe87b833 to your computer and use it in GitHub Desktop.
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