Compare commits

...

2 Commits

Author SHA1 Message Date
Leon Banik
17b7850b6c
Merge ca2cc4c12f into fb54d8b549 2023-05-22 17:38:49 -05:00
Leon Banik
ca2cc4c12f
Update utilities.h
Fix and optimize mixDiscreteIdAndValue() and mixStringIdAndValue() functions
2023-04-01 17:57:31 +02:00

View File

@ -1,18 +1,21 @@
#pragma once
#ifdef __cplusplus
#include <cstdint>
#include <cstring>
namespace twml {
inline int64_t mixDiscreteIdAndValue(int64_t key, int64_t value) {
inline int64_t mixDiscreteIdAndValue(int64_t key, int64_t value) {
key ^= ((17LL + value) * 2654435761LL);
return key;
}
inline int64_t mixStringIdAndValue(int64_t key, int32_t str_len, const uint8_t *str) {
int32_t hash = 0;
for (int32_t i = 0; i < str_len; i++) {
hash = (31 * hash) + (int32_t)str[i];
}
return key ^ hash;
}
}
#endif
inline int64_t mixStringIdAndValue(int64_t key, int32_t str_len, const uint8_t* str) {
const uint8_t* end = str + str_len;
while (str != end) {
key = (key * 31) + *str++;
}
return key;
}
} // namespace twml