Skip to content

Commit 998ca00

Browse files
committed
fix: cast using const void *
1 parent 9fc9ce3 commit 998ca00

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

numpy/_core/src/multiarray/unique.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ FinalAction<F> finally(F f) {
2929
}
3030

3131
// 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) {
3433
// http://www.isthe.com/chongo/tech/comp/fnv/#FNV-1a
3534
#if NPY_SIZEOF_INTP == 4
3635
static const size_t FNV_OFFSET_BASIS = 2166136261U;
@@ -39,12 +38,16 @@ size_t str_hash(const T *str, npy_intp num_chars) {
3938
static const size_t FNV_OFFSET_BASIS = 14695981039346656037ULL;
4039
static const size_t FNV_PRIME = 1099511628211ULL;
4140
#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+
4345
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++;
4648
hash *= FNV_PRIME;
4749
}
50+
4851
return hash;
4952
}
5053

@@ -144,7 +147,7 @@ unique_string(PyArrayObject *self, npy_bool equal_nan)
144147
npy_intp itemsize = descr->elsize;
145148
npy_intp num_chars = itemsize / sizeof(T);
146149
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));
148151
};
149152
auto equal = [itemsize](const T *lhs, const T *rhs) -> bool {
150153
return std::memcmp(lhs, rhs, itemsize) == 0;
@@ -232,7 +235,7 @@ unique_vstring(PyArrayObject *self, npy_bool equal_nan)
232235
return std::hash<const npy_static_string *>{}(value);
233236
}
234237
}
235-
return str_hash(value->buf, value->size);
238+
return str_hash(value->buf, value->size * sizeof(char));
236239
};
237240
auto equal = [equal_nan](const npy_static_string *lhs, const npy_static_string *rhs) -> bool {
238241
if (lhs->buf == NULL && rhs->buf == NULL) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy