@@ -29,8 +29,7 @@ FinalAction<F> finally(F f) {
29
29
}
30
30
31
31
// function to caluculate the hash of a string
32
- template <typename T>
33
- size_t str_hash (const T *str, npy_intp num_chars) {
32
+ size_t str_hash (const void *buf, size_t len) {
34
33
// http://www.isthe.com/chongo/tech/comp/fnv/#FNV-1a
35
34
#if NPY_SIZEOF_INTP == 4
36
35
static const size_t FNV_OFFSET_BASIS = 2166136261U ;
@@ -39,12 +38,16 @@ size_t str_hash(const T *str, npy_intp num_chars) {
39
38
static const size_t FNV_OFFSET_BASIS = 14695981039346656037ULL ;
40
39
static const size_t FNV_PRIME = 1099511628211ULL ;
41
40
#endif
42
- const unsigned char * bytes = reinterpret_cast <const unsigned char *>(str);
41
+
42
+ unsigned char *bp = (unsigned char *)buf; /* start of buffer */
43
+ unsigned char *be = bp + len; /* beyond end of buffer */
44
+
43
45
size_t hash = FNV_OFFSET_BASIS;
44
- for (npy_intp i = 0 ; i < num_chars * (npy_intp) sizeof (T); ++i ) {
45
- hash ^= bytes[i] ;
46
+ while (bp < be ) {
47
+ hash ^= *bp++ ;
46
48
hash *= FNV_PRIME;
47
49
}
50
+
48
51
return hash;
49
52
}
50
53
@@ -144,7 +147,7 @@ unique_string(PyArrayObject *self, npy_bool equal_nan)
144
147
npy_intp itemsize = descr->elsize ;
145
148
npy_intp num_chars = itemsize / sizeof (T);
146
149
auto hash = [num_chars](const T *value) -> size_t {
147
- return str_hash (value, num_chars);
150
+ return str_hash (value, num_chars * sizeof (T) );
148
151
};
149
152
auto equal = [itemsize](const T *lhs, const T *rhs) -> bool {
150
153
return std::memcmp (lhs, rhs, itemsize) == 0 ;
@@ -232,7 +235,7 @@ unique_vstring(PyArrayObject *self, npy_bool equal_nan)
232
235
return std::hash<const npy_static_string *>{}(value);
233
236
}
234
237
}
235
- return str_hash (value->buf , value->size );
238
+ return str_hash (value->buf , value->size * sizeof ( char ) );
236
239
};
237
240
auto equal = [equal_nan](const npy_static_string *lhs, const npy_static_string *rhs) -> bool {
238
241
if (lhs->buf == NULL && rhs->buf == NULL ) {
0 commit comments