aboutsummaryrefslogtreecommitdiff
path: root/src/mum.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/mum.c')
-rw-r--r--src/mum.c68
1 files changed, 29 insertions, 39 deletions
diff --git a/src/mum.c b/src/mum.c
index d988418a..146ea3c9 100644
--- a/src/mum.c
+++ b/src/mum.c
@@ -26,67 +26,57 @@
__attribute__((always_inline))
static inline size_t
-_sz(const void *data, ssize_t nbytes)
+_len(const void *key, ssize_t len)
{
- if(nbytes == -1) // is a null-terminated string
- {
- return strlen((const char *)data);
- }
+ return (len == -1) ? strlen((const char *)key) : (size_t)len;
+}
- return nbytes;
+__attribute__((always_inline))
+static inline uint64_t
+_d2tk_hash(uint64_t hash, const void *key, size_t len)
+{
+ return _mum_hash_aligned(hash + len, key, len);
}
D2TK_API uint64_t
-d2tk_hash(const void *data, ssize_t nbytes)
+d2tk_hash(const void *key, ssize_t len)
{
- nbytes = _sz(data, nbytes);
+ len = _len(key, len);
- return mum_hash(data, nbytes, SEED);
+ return mum_hash(key, len, SEED);
}
D2TK_API uint64_t
-d2tk_hash_foreach(const void *data, ssize_t nbytes, ...)
+d2tk_hash_foreach(const void *key, ssize_t len, ...)
{
va_list args;
- const void *src;
-
- nbytes = _sz(data, nbytes);
+ uint64_t hash = mum_hash_init(SEED);
- // derive total temporary buffer size
- size_t sz = nbytes;
+ len = _len(key, len); //FIXME remove
+ hash = _d2tk_hash(hash, key, len);
- va_start(args, nbytes);
+ va_start(args, len);
- while( (src = va_arg(args, const void *)) )
+ while( (key = va_arg(args, const void *)) )
{
- sz += _sz(src, va_arg(args, int));
+ len = _len(key, va_arg(args, int)); //FIXME remove
+ hash = _d2tk_hash(hash, key, len);
}
va_end(args);
- // fill temporary bufffer
- uint8_t *dst = alloca(sz);
- if(dst)
- {
- size_t off = 0;
-
- memcpy(&dst[off], data, nbytes);
- off += nbytes;
-
- va_start(args, nbytes);
-
- while( (src = va_arg(args, const void *)) )
- {
- nbytes = _sz(src, va_arg(args, int));
-
- memcpy(&dst[off], src, nbytes);
- off += nbytes;
- }
+ return mum_hash_finish(hash);
+}
- va_end(args);
+D2TK_API uint64_t
+d2tk_hash_dict(const d2tk_hash_dict_t *dict)
+{
+ uint64_t hash = mum_hash_init(SEED);
- return mum_hash(dst, sz, SEED);
+ for( ; dict->key; dict++)
+ {
+ hash = _d2tk_hash(hash, dict->key, dict->len);
}
- return 0;
+ return mum_hash_finish(hash);
}