Derive-C
Loading...
Searching...
No Matches
hashers.h File Reference
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <derive-c/structures/hashmap/murmurhash.h>
Include dependency graph for hashers.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define ALWAYS_COLLIDE(type)
 The worst possible hash, for testing purposes.
 
#define ID(type)
 
#define MURMURHASH_3_FMIx64(type)
 
#define INT_HASHERS(_apply)
 
#define MURMURHASH_DEFAULT_SEED   0x9747b28c
 
#define STRING_SIZES(_apply)
 
#define MURMURHASH_STRING_FIXED_SIZE(size)
 

Functions

size_t hash_murmurhash_string (const char *str)
 
static size_t hash_combine (size_t seed, size_t h)
 

Macro Definition Documentation

◆ ALWAYS_COLLIDE

#define ALWAYS_COLLIDE ( type)
Value:
static size_t hash_always_collide_##type(type const* key) { return 0; }

The worst possible hash, for testing purposes.

Interesting literature:

Definition at line 12 of file hashers.h.

12#define ALWAYS_COLLIDE(type) \
13 static size_t hash_always_collide_##type(type const* key) { return 0; }

◆ ID

#define ID ( type)
Value:
static size_t hash_id_##type(type const* key) { \
_Static_assert(sizeof(type) <= sizeof(size_t), \
"ID hashing only supports up to size_t integers"); \
return (size_t)(*key); \
}

No hashing, just returns the integer value. For most circumstances with a key as a single integer, this is a good option.

Definition at line 17 of file hashers.h.

17#define ID(type) \
18 static size_t hash_id_##type(type const* key) { \
19 _Static_assert(sizeof(type) <= sizeof(size_t), \
20 "ID hashing only supports up to size_t integers"); \
21 return (size_t)(*key); \
22 }

◆ INT_HASHERS

#define INT_HASHERS ( _apply)
Value:
_apply(int8_t ) \
_apply(int16_t ) \
_apply(int32_t ) \
_apply(int64_t ) \
_apply(uint8_t ) \
_apply(uint16_t) \
_apply(uint32_t) \
_apply(uint64_t)

Definition at line 36 of file hashers.h.

36#define INT_HASHERS(_apply) \
37 _apply(int8_t ) \
38 _apply(int16_t ) \
39 _apply(int32_t ) \
40 _apply(int64_t ) \
41 _apply(uint8_t ) \
42 _apply(uint16_t) \
43 _apply(uint32_t) \
44 _apply(uint64_t)

◆ MURMURHASH_3_FMIx64

#define MURMURHASH_3_FMIx64 ( type)
Value:
static size_t hash_murmurhash3_##type(type const* key) { \
_Static_assert(sizeof(type) <= sizeof(uint64_t), \
"MurmurHash3 only supports up to 64-bit integers"); \
return (size_t)derive_c_fmix64((uint64_t)(*key)); \
}
FORCE_INLINE uint64_t derive_c_fmix64(uint64_t k)
Definition murmurhash.h:25

Using MurmurHash3's finalizer for integer hashing.

Definition at line 28 of file hashers.h.

28#define MURMURHASH_3_FMIx64(type) \
29 static size_t hash_murmurhash3_##type(type const* key) { \
30 _Static_assert(sizeof(type) <= sizeof(uint64_t), \
31 "MurmurHash3 only supports up to 64-bit integers"); \
32 return (size_t)derive_c_fmix64((uint64_t)(*key)); \
33 }

◆ MURMURHASH_DEFAULT_SEED

#define MURMURHASH_DEFAULT_SEED   0x9747b28c

Definition at line 56 of file hashers.h.

◆ MURMURHASH_STRING_FIXED_SIZE

#define MURMURHASH_STRING_FIXED_SIZE ( size)
Value:
static size_t hash_murmurhash_string_##size(const char str[size]) { \
}
static INDEX_TYPE size(SELF const *self)
Definition template.h:194
#define MURMURHASH_DEFAULT_SEED
Definition hashers.h:56
size_t derive_c_murmurhash(const void *key, int len, uint32_t seed)
Definition murmurhash.h:346

Definition at line 73 of file hashers.h.

73#define MURMURHASH_STRING_FIXED_SIZE(size) \
74 static size_t hash_murmurhash_string_##size(const char str[size]) { \
75 return derive_c_murmurhash(str, size, MURMURHASH_DEFAULT_SEED); \
76 }

◆ STRING_SIZES

#define STRING_SIZES ( _apply)
Value:
_apply(1) \
_apply(2) \
_apply(3) \
_apply(4) \
_apply(5) \
_apply(6) \
_apply(7) \
_apply(8)

Definition at line 63 of file hashers.h.

63#define STRING_SIZES(_apply) \
64 _apply(1) \
65 _apply(2) \
66 _apply(3) \
67 _apply(4) \
68 _apply(5) \
69 _apply(6) \
70 _apply(7) \
71 _apply(8) // clang-format on

Function Documentation

◆ hash_combine()

static size_t hash_combine ( size_t seed,
size_t h )
inlinestatic
Examples
structures/hashmap.c.

Definition at line 83 of file hashers.h.

83 {
84 // 0x9e3779b97f4a7c15 is 64-bit fractional part of the golden ratio;
85 // “+ (seed<<6) + (seed>>2)” mixes seed’s bits
86 return seed ^ (h + 0x9e3779b97f4a7c15ULL + (seed << 6) + (seed >> 2));
87}
Here is the caller graph for this function:

◆ hash_murmurhash_string()

size_t hash_murmurhash_string ( const char * str)
Examples
structures/hashmap.c.

Definition at line 58 of file hashers.h.

58 {
59 return derive_c_murmurhash(str, (int)strlen(str), MURMURHASH_DEFAULT_SEED);
60}
Here is the call graph for this function:
Here is the caller graph for this function: