Skip to content

Commit

Permalink
Added binding for MurmurHash2 (explosion#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanMabille authored Dec 12, 2021
1 parent e9fb863 commit d086eff
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
2 changes: 1 addition & 1 deletion murmurhash/mrmr.pxd
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from libc.stdint cimport uint64_t, int64_t, uint32_t


cdef uint32_t hash32(void* key, int length, uint32_t seed) nogil
cdef uint32_t hash32(void* key, int length, uint32_t seed, int version) nogil
cdef uint64_t hash64(void* key, int length, uint64_t seed) nogil
cdef uint64_t real_hash64(void* key, int length, uint64_t seed) nogil
cdef void hash128_x86(const void* key, int len, uint32_t seed, void* out) nogil
Expand Down
24 changes: 14 additions & 10 deletions murmurhash/mrmr.pyx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from libc.stdint cimport uint64_t, int64_t, int32_t
from libc.stdint cimport uint64_t, int64_t, uint32_t, int32_t


cdef extern from "murmurhash/MurmurHash3.h":
Expand All @@ -9,11 +9,15 @@ cdef extern from "murmurhash/MurmurHash3.h":
cdef extern from "murmurhash/MurmurHash2.h":
uint64_t MurmurHash64A(void * key, int length, uint32_t seed) nogil
uint64_t MurmurHash64B(void * key, int length, uint32_t seed) nogil
uint32_t MurmurHash2(void * key, int length, uint32_t seed) nogil


cdef uint32_t hash32(void* key, int length, uint32_t seed) nogil:
cdef uint32_t hash32(void* key, int length, uint32_t seed, int version) nogil:
cdef int32_t out
MurmurHash3_x86_32(key, length, seed, &out)
if version==3:
MurmurHash3_x86_32(key, length, seed, &out)
else:
out = MurmurHash2(key, length, seed)
return out


Expand All @@ -34,17 +38,17 @@ cdef void hash128_x64(const void* key, int length, uint32_t seed, void* out) nog
MurmurHash3_x64_128(key, length, seed, out)


cpdef int32_t hash(value, uint32_t seed=0):
cpdef int32_t hash(value, uint32_t seed=0, murmur_version=3):
if isinstance(value, unicode):
return hash_unicode(value, seed=seed)
return hash_unicode(value, seed=seed, murmur_version=murmur_version)
else:
return hash_bytes(value, seed=seed)
return hash_bytes(value, seed=seed, murmur_version=murmur_version)


cpdef int32_t hash_unicode(unicode value, uint32_t seed=0):
return hash_bytes(value.encode('utf8'), seed=seed)
cpdef int32_t hash_unicode(unicode value, uint32_t seed=0, murmur_version=3):
return hash_bytes(value.encode('utf8'), seed=seed, murmur_version=murmur_version)


cpdef int32_t hash_bytes(bytes value, uint32_t seed=0):
cpdef int32_t hash_bytes(bytes value, uint32_t seed=0, murmur_version=3):
cdef char* chars = <char*>value
return hash32(chars, len(value), seed)
return hash32(chars, len(value), seed, murmur_version)

0 comments on commit d086eff

Please sign in to comment.