Skip to content

Instantly share code, notes, and snippets.

@co-dan
Created September 10, 2013 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save co-dan/6511275 to your computer and use it in GitHub Desktop.
Save co-dan/6511275 to your computer and use it in GitHub Desktop.
/* FNV-1 hash
*
* The FNV-1 hash description: http://isthe.com/chongo/tech/comp/fnv/
* The FNV-1 hash is public domain: http://isthe.com/chongo/tech/comp/fnv/#public_domain
long hashable_fnv_hash(const unsigned char* str, long len, long hash) {
while (len--) {
hash = (hash * 16777619) ^ *str++;
}
return hash;
}
*/
function h$hashable_fnv_hash(str, len, hashInit) {
var hash = hashInit;
var i;
for (i = 0; i < len; i++) {
hash = (hash * 16777619) ^ str[i];
}
return hash;
}
/* Used for ByteArray#s. We can't treat them like pointers in
native Haskell, but we can in unsafe FFI calls.
long hashable_fnv_hash_offset(const unsigned char* str, long offset, long len, long hash) {
return hashable_fnv_hash(str + offset, len, hash);
}
*/
function h$hashable_fnv_hash_offset(str, offset, len, hash) {
return h$hashable_fnv_hash(str.substring(offset), len, hash);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment