diff options
Diffstat (limited to 'libmisc/include/libmisc/hash.h')
-rw-r--r-- | libmisc/include/libmisc/hash.h | 24 |
1 files changed, 7 insertions, 17 deletions
diff --git a/libmisc/include/libmisc/hash.h b/libmisc/include/libmisc/hash.h index 91e6b10..029bd3b 100644 --- a/libmisc/include/libmisc/hash.h +++ b/libmisc/include/libmisc/hash.h @@ -1,31 +1,21 @@ /* libmisc/hash.h - General-purpose hash utilities * - * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com> + * Copyright (C) 2024-2025 Luke T. Shumaker <lukeshu@lukeshu.com> * SPDX-License-Identifier: AGPL-3.0-or-later */ #ifndef _LIBMISC_HASH_H_ #define _LIBMISC_HASH_H_ -#include <stdint.h> /* for uint{n}_t */ #include <stddef.h> /* for size_t */ +#include <stdint.h> /* for uint{n}_t */ -/* djb2 hash */ -typedef uint32_t hash_t; -static inline void hash_init(hash_t *hash) { - *hash = 5381; -} -static inline void hash_write(hash_t *hash, void *dat, size_t len) { - for (size_t i = 0; i < len; i++) - *hash = (*hash * 33) + (hash_t)(((unsigned char *)dat)[i]); -} +/* base */ +typedef uint32_t hash_t; /* size subject to change */ +void hash_init(hash_t *hash); +void hash_write(hash_t *hash, void *dat, size_t len); /* utilities */ -static inline hash_t hash(void *dat, size_t len) { - hash_t h; - hash_init(&h); - hash_write(&h, dat, len); - return h; -} +hash_t hash(void *dat, size_t len); #endif /* _LIBMISC_HASH_H_ */ |