2017-02-28 18:16:38 +01:00
|
|
|
/*
|
2019-08-03 19:43:48 +02:00
|
|
|
* This software is licensed under the terms of the MIT License.
|
2017-02-28 18:16:38 +01:00
|
|
|
* See COPYING for further information.
|
|
|
|
* ---
|
2024-05-16 23:30:41 +02:00
|
|
|
* Copyright (c) 2011-2024, Lukas Weber <laochailan@web.de>.
|
|
|
|
* Copyright (c) 2012-2024, Andrei Alexeyev <akari@taisei-project.org>.
|
2017-02-28 18:16:38 +01:00
|
|
|
*/
|
|
|
|
|
2021-08-12 23:09:01 +02:00
|
|
|
#pragma once
|
2017-11-25 20:45:11 +01:00
|
|
|
#include "taisei.h"
|
2017-02-28 18:16:38 +01:00
|
|
|
|
2018-06-29 23:36:51 +02:00
|
|
|
/*
|
|
|
|
* hash_t
|
|
|
|
*
|
|
|
|
* Type of the hash values.
|
|
|
|
*/
|
2017-03-01 16:42:47 +01:00
|
|
|
typedef uint32_t hash_t;
|
2017-02-28 18:16:38 +01:00
|
|
|
|
2020-03-17 09:09:49 +01:00
|
|
|
#define HT_HASH_LIVE_BIT ((hash_t)(1) << (sizeof(hash_t) * CHAR_BIT - 1))
|
|
|
|
|
|
|
|
typedef hash_t ht_size_t;
|
2017-02-28 18:16:38 +01:00
|
|
|
|
2021-11-29 20:59:14 +01:00
|
|
|
// for hashsets
|
|
|
|
struct ht_empty { };
|
|
|
|
#define HT_EMPTY ((struct ht_empty) {})
|
|
|
|
|
2018-06-29 23:36:51 +02:00
|
|
|
/*
|
|
|
|
* htutil_hashfunc_uint32
|
|
|
|
*
|
|
|
|
* Hash function for 32-bit integers
|
|
|
|
* NOTE: assuming hash_t stays uint32_t, this function has no collisions.
|
|
|
|
*/
|
2019-08-03 18:47:21 +02:00
|
|
|
INLINE hash_t htutil_hashfunc_uint32(uint32_t x) {
|
2018-06-29 23:36:51 +02:00
|
|
|
x = ((x >> 16) ^ x) * 0x45d9f3b;
|
|
|
|
x = ((x >> 16) ^ x) * 0x45d9f3b;
|
|
|
|
x = (x >> 16) ^ x;
|
|
|
|
return (hash_t)x;
|
|
|
|
}
|
2017-02-28 18:16:38 +01:00
|
|
|
|
2018-06-29 23:36:51 +02:00
|
|
|
/*
|
|
|
|
* htutil_hashfunc_uint64
|
|
|
|
*
|
|
|
|
* Hash function for 64-bit integers.
|
|
|
|
*/
|
2019-08-03 18:47:21 +02:00
|
|
|
INLINE hash_t htutil_hashfunc_uint64(uint64_t x) {
|
2018-06-29 23:36:51 +02:00
|
|
|
x = (x ^ (x >> 30)) * UINT64_C(0xbf58476d1ce4e5b9);
|
|
|
|
x = (x ^ (x >> 27)) * UINT64_C(0x94d049bb133111eb);
|
|
|
|
x = x ^ (x >> 31);
|
|
|
|
return (hash_t)x;
|
|
|
|
}
|
2017-02-28 18:16:38 +01:00
|
|
|
|
2018-06-29 23:36:51 +02:00
|
|
|
/*
|
|
|
|
* htutil_hashfunc_string
|
|
|
|
*
|
|
|
|
* Hash function for null-terminated strings.
|
|
|
|
*/
|
2020-03-17 09:09:49 +01:00
|
|
|
INLINE uint32_t htutil_hashfunc_string(const char *str) {
|
|
|
|
// FNV1a hash
|
|
|
|
//
|
|
|
|
// Not looking for '\0' in the for loop because apparently compilers optimize this better.
|
|
|
|
// Both GCC and clang can fold this entire function into a constant with a constant str.
|
|
|
|
// GCC fails that if strlen is not used.
|
|
|
|
|
|
|
|
uint32_t hash = 0x811c9dc5;
|
|
|
|
uint32_t prime = 0x1000193;
|
|
|
|
size_t len = strlen(str);
|
|
|
|
|
|
|
|
for(int i = 0; i < len; ++i) {
|
|
|
|
uint8_t value = str[i];
|
|
|
|
hash = hash ^ value;
|
|
|
|
hash *= prime;
|
|
|
|
}
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
2017-02-28 18:47:47 +01:00
|
|
|
|
2018-06-29 23:36:51 +02:00
|
|
|
// Import public declarations for the predefined hashtable types.
|
|
|
|
#define HT_DECL
|
|
|
|
#include "hashtable_predefs.inc.h"
|
2017-02-28 18:47:47 +01:00
|
|
|
|
2018-06-29 23:36:51 +02:00
|
|
|
// NOTE: For the hashtable API, see hashtable.inc.h
|
|
|
|
// NOTE: For type-generic wrappers around the API, see hashtable_predefs.inc.h
|