Skip to content

Instantly share code, notes, and snippets.

@klandergren
Forked from gvinter/gist:1080478
Created July 13, 2011 16:23
Show Gist options
  • Select an option

  • Save klandergren/1080675 to your computer and use it in GitHub Desktop.

Select an option

Save klandergren/1080675 to your computer and use it in GitHub Desktop.
Configurable hash functions
function max(a, b) {
this.a = a;
this.b = b;
if (a > b) {
return a;
} else {
return b;
}
}
console.log(max(100,1000));
//KL:
//you want to be taking in a hash_function arg so that you can call like:
// function simple_hash(key, table_size, hash_function) {
// //your code
// }
function simple_hash(key,table_size) {
var hash = 0;
var max_var = new Number();
//KL:
//what is the purpose of this operation? 1000 will always be larger than
//100 with your implementation of max()
var max_var = max(100,1000);
//KL:
// This is the functionality you need to pull out. So if you pass in an arg 'hash_function'
// it should look like:
//
// hash = hash_function(key);
//
// and the for loop below is 'pulled out'.
for (var i = 0; i < key.length; i++) {
hash = (hash * 31) + key.charCodeAt(i);
}
return Math.abs(hash) % table_size;
}
// KL:
// max_var is defined within the scope of simple_hash, so why refer to it here?
console.log(simple_hash("test",max_var));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment