32#ifndef SHEREDOM_HASHMAP_H_INCLUDED
33#define SHEREDOM_HASHMAP_H_INCLUDED
38#pragma warning(push, 0)
39#pragma warning(disable : 4668)
47#if (defined(_MSC_VER) && defined(__AVX__)) || \
48 (!defined(_MSC_VER) && defined(__SSE4_2__))
49#define HASHMAP_X86_SSE42
52#if defined(HASHMAP_X86_SSE42)
56#if defined(__aarch64__) && defined(__ARM_FEATURE_CRC32)
57#define HASHMAP_ARM_CRC32
60#if defined(HASHMAP_ARM_CRC32)
75#pragma warning(disable : 4505)
77#pragma warning(disable : 4710)
79#pragma warning(disable : 4711)
83#pragma clang diagnostic push
84#pragma clang diagnostic ignored "-Wunused-function"
85#pragma clang diagnostic ignored "-Wstatic-in-inline"
87#if __has_warning("-Wunsafe-buffer-usage")
88#pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
93#define HASHMAP_ATTRIBUTE(a) __attribute((a))
95#define HASHMAP_ATTRIBUTE(a) __attribute__((a))
99#define HASHMAP_WEAK __inline
100#elif defined(__MINGW32__) || defined(__MINGW64__)
101#define HASHMAP_WEAK static HASHMAP_ATTRIBUTE(used)
102#elif defined(__clang__) || defined(__GNUC__) || defined(__TINYC__)
103#define HASHMAP_WEAK HASHMAP_ATTRIBUTE(weak)
105#error Non clang, non gcc, non MSVC, non tcc compiler found!
109#define HASHMAP_ALWAYS_INLINE __forceinline
110#elif (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \
112#define HASHMAP_ALWAYS_INLINE __attribute__((always_inline)) inline
116#define HASHMAP_ALWAYS_INLINE HASHMAP_WEAK
119#if defined(_MSC_VER) && (_MSC_VER < 1920)
130typedef struct hashmap_element_s {
143typedef struct hashmap_s {
148 struct hashmap_element_s *
data;
151#define HASHMAP_LINEAR_PROBE_LENGTH (8)
153typedef struct hashmap_create_options_s {
167 struct hashmap_s *
const out_hashmap);
179 struct hashmap_s *
const out_hashmap);
191HASHMAP_WEAK
int hashmap_put(
struct hashmap_s *
const hashmap,
200HASHMAP_WEAK
void *
hashmap_get(
const struct hashmap_s *
const hashmap,
201 const void *
const key,
210 const void *
const key,
219HASHMAP_WEAK
const void *
221 const void *
const key,
231 int (*iterator)(
void *
const context,
233 void *
const context);
244 struct hashmap_s *
const hashmap,
245 int (*iterator)(
void *
const,
struct hashmap_element_s *
const),
246 void *
const context);
267static int hashmap_memcmp_comparer(
const void *
const a,
272 const struct hashmap_s *
const m,
const void *
const key,
279 struct hashmap_element_s *
const e);
285#if defined(__cplusplus)
286#define HASHMAP_CAST(type, x) static_cast<type>(x)
287#define HASHMAP_PTR_CAST(type, x) reinterpret_cast<type>(x)
288#define HASHMAP_NULL NULL
290#define HASHMAP_CAST(type, x) ((type)(x))
291#define HASHMAP_PTR_CAST(type, x) ((type)(x))
292#define HASHMAP_NULL 0
296 struct hashmap_s *
const out_hashmap) {
297 struct hashmap_create_options_s options;
298 memset(&options, 0,
sizeof(options));
299 options.initial_capacity = initial_capacity;
305 struct hashmap_s *
const out_hashmap) {
306 if (2 > options.initial_capacity) {
307 options.initial_capacity = 2;
308 }
else if (0 != (options.initial_capacity & (options.initial_capacity - 1))) {
309 options.initial_capacity = 1u
314 options.hasher = &hashmap_crc32_hasher;
318 options.comparer = &hashmap_memcmp_comparer;
322 struct hashmap_element_s *,
324 sizeof(
struct hashmap_element_s)));
326 out_hashmap->log2_capacity = 31 -
hashmap_clz(options.initial_capacity);
327 out_hashmap->size = 0;
328 out_hashmap->hasher = options.hasher;
329 out_hashmap->comparer = options.comparer;
350 m->data[
index].data = value;
351 m->data[
index].key = key;
352 m->data[
index].key_len = len;
356 if (0 == m->data[
index].in_use) {
357 m->data[
index].in_use = 1;
364void *
hashmap_get(
const struct hashmap_s *
const m,
const void *
const key,
378 if (m->data[
index].in_use) {
379 if (m->comparer(m->data[
index].key, m->data[
index].key_len, key, len)) {
380 return m->data[
index].data;
403 if (m->data[
index].in_use) {
404 if (m->comparer(m->data[
index].key, m->data[
index].key_len, key, len)) {
406 memset(&m->data[
index], 0,
sizeof(
struct hashmap_element_s));
420 const void *
const key,
434 if (m->data[
index].in_use) {
435 if (m->comparer(m->data[
index].key, m->data[
index].key_len, key, len)) {
436 const void *
const stored_key = m->data[
index].key;
439 memset(&m->data[
index], 0,
sizeof(
struct hashmap_element_s));
453 int (*f)(
void *
const,
void *
const),
void *
const context) {
457 if (m->data[i].in_use) {
458 if (!f(context, m->data[i].data)) {
468 int (*f)(
void *
const,
469 struct hashmap_element_s *
const),
470 void *
const context) {
472 struct hashmap_element_s *p;
481 memset(p, 0,
sizeof(
struct hashmap_element_s));
496 memset(m, 0,
sizeof(
struct hashmap_s));
506 return 1u << m->log2_capacity;
516#if defined(HASHMAP_X86_SSE42)
519 memcpy(&next, &s[i],
sizeof(next));
520 crc32val = _mm_crc32_u32(crc32val, next);
523 for (; i < len; i++) {
524 crc32val = _mm_crc32_u8(crc32val, s[i]);
526#elif defined(HASHMAP_ARM_CRC32)
529 memcpy(&next, &s[i],
sizeof(next));
530 crc32val = __crc32d(crc32val, next);
533 for (; i < len; i++) {
534 crc32val = __crc32b(crc32val, s[i]);
539 0x00000000U, 0xF26B8303U, 0xE13B70F7U, 0x1350F3F4U, 0xC79A971FU,
540 0x35F1141CU, 0x26A1E7E8U, 0xD4CA64EBU, 0x8AD958CFU, 0x78B2DBCCU,
541 0x6BE22838U, 0x9989AB3BU, 0x4D43CFD0U, 0xBF284CD3U, 0xAC78BF27U,
542 0x5E133C24U, 0x105EC76FU, 0xE235446CU, 0xF165B798U, 0x030E349BU,
543 0xD7C45070U, 0x25AFD373U, 0x36FF2087U, 0xC494A384U, 0x9A879FA0U,
544 0x68EC1CA3U, 0x7BBCEF57U, 0x89D76C54U, 0x5D1D08BFU, 0xAF768BBCU,
545 0xBC267848U, 0x4E4DFB4BU, 0x20BD8EDEU, 0xD2D60DDDU, 0xC186FE29U,
546 0x33ED7D2AU, 0xE72719C1U, 0x154C9AC2U, 0x061C6936U, 0xF477EA35U,
547 0xAA64D611U, 0x580F5512U, 0x4B5FA6E6U, 0xB93425E5U, 0x6DFE410EU,
548 0x9F95C20DU, 0x8CC531F9U, 0x7EAEB2FAU, 0x30E349B1U, 0xC288CAB2U,
549 0xD1D83946U, 0x23B3BA45U, 0xF779DEAEU, 0x05125DADU, 0x1642AE59U,
550 0xE4292D5AU, 0xBA3A117EU, 0x4851927DU, 0x5B016189U, 0xA96AE28AU,
551 0x7DA08661U, 0x8FCB0562U, 0x9C9BF696U, 0x6EF07595U, 0x417B1DBCU,
552 0xB3109EBFU, 0xA0406D4BU, 0x522BEE48U, 0x86E18AA3U, 0x748A09A0U,
553 0x67DAFA54U, 0x95B17957U, 0xCBA24573U, 0x39C9C670U, 0x2A993584U,
554 0xD8F2B687U, 0x0C38D26CU, 0xFE53516FU, 0xED03A29BU, 0x1F682198U,
555 0x5125DAD3U, 0xA34E59D0U, 0xB01EAA24U, 0x42752927U, 0x96BF4DCCU,
556 0x64D4CECFU, 0x77843D3BU, 0x85EFBE38U, 0xDBFC821CU, 0x2997011FU,
557 0x3AC7F2EBU, 0xC8AC71E8U, 0x1C661503U, 0xEE0D9600U, 0xFD5D65F4U,
558 0x0F36E6F7U, 0x61C69362U, 0x93AD1061U, 0x80FDE395U, 0x72966096U,
559 0xA65C047DU, 0x5437877EU, 0x4767748AU, 0xB50CF789U, 0xEB1FCBADU,
560 0x197448AEU, 0x0A24BB5AU, 0xF84F3859U, 0x2C855CB2U, 0xDEEEDFB1U,
561 0xCDBE2C45U, 0x3FD5AF46U, 0x7198540DU, 0x83F3D70EU, 0x90A324FAU,
562 0x62C8A7F9U, 0xB602C312U, 0x44694011U, 0x5739B3E5U, 0xA55230E6U,
563 0xFB410CC2U, 0x092A8FC1U, 0x1A7A7C35U, 0xE811FF36U, 0x3CDB9BDDU,
564 0xCEB018DEU, 0xDDE0EB2AU, 0x2F8B6829U, 0x82F63B78U, 0x709DB87BU,
565 0x63CD4B8FU, 0x91A6C88CU, 0x456CAC67U, 0xB7072F64U, 0xA457DC90U,
566 0x563C5F93U, 0x082F63B7U, 0xFA44E0B4U, 0xE9141340U, 0x1B7F9043U,
567 0xCFB5F4A8U, 0x3DDE77ABU, 0x2E8E845FU, 0xDCE5075CU, 0x92A8FC17U,
568 0x60C37F14U, 0x73938CE0U, 0x81F80FE3U, 0x55326B08U, 0xA759E80BU,
569 0xB4091BFFU, 0x466298FCU, 0x1871A4D8U, 0xEA1A27DBU, 0xF94AD42FU,
570 0x0B21572CU, 0xDFEB33C7U, 0x2D80B0C4U, 0x3ED04330U, 0xCCBBC033U,
571 0xA24BB5A6U, 0x502036A5U, 0x4370C551U, 0xB11B4652U, 0x65D122B9U,
572 0x97BAA1BAU, 0x84EA524EU, 0x7681D14DU, 0x2892ED69U, 0xDAF96E6AU,
573 0xC9A99D9EU, 0x3BC21E9DU, 0xEF087A76U, 0x1D63F975U, 0x0E330A81U,
574 0xFC588982U, 0xB21572C9U, 0x407EF1CAU, 0x532E023EU, 0xA145813DU,
575 0x758FE5D6U, 0x87E466D5U, 0x94B49521U, 0x66DF1622U, 0x38CC2A06U,
576 0xCAA7A905U, 0xD9F75AF1U, 0x2B9CD9F2U, 0xFF56BD19U, 0x0D3D3E1AU,
577 0x1E6DCDEEU, 0xEC064EEDU, 0xC38D26C4U, 0x31E6A5C7U, 0x22B65633U,
578 0xD0DDD530U, 0x0417B1DBU, 0xF67C32D8U, 0xE52CC12CU, 0x1747422FU,
579 0x49547E0BU, 0xBB3FFD08U, 0xA86F0EFCU, 0x5A048DFFU, 0x8ECEE914U,
580 0x7CA56A17U, 0x6FF599E3U, 0x9D9E1AE0U, 0xD3D3E1ABU, 0x21B862A8U,
581 0x32E8915CU, 0xC083125FU, 0x144976B4U, 0xE622F5B7U, 0xF5720643U,
582 0x07198540U, 0x590AB964U, 0xAB613A67U, 0xB831C993U, 0x4A5A4A90U,
583 0x9E902E7BU, 0x6CFBAD78U, 0x7FAB5E8CU, 0x8DC0DD8FU, 0xE330A81AU,
584 0x115B2B19U, 0x020BD8EDU, 0xF0605BEEU, 0x24AA3F05U, 0xD6C1BC06U,
585 0xC5914FF2U, 0x37FACCF1U, 0x69E9F0D5U, 0x9B8273D6U, 0x88D28022U,
586 0x7AB90321U, 0xAE7367CAU, 0x5C18E4C9U, 0x4F48173DU, 0xBD23943EU,
587 0xF36E6F75U, 0x0105EC76U, 0x12551F82U, 0xE03E9C81U, 0x34F4F86AU,
588 0xC69F7B69U, 0xD5CF889DU, 0x27A40B9EU, 0x79B737BAU, 0x8BDCB4B9U,
589 0x988C474DU, 0x6AE7C44EU, 0xBE2DA0A5U, 0x4C4623A6U, 0x5F16D052U,
592 for (; i < len; i++) {
601 crc32val ^= crc32val >> 16;
602 crc32val *= 0x85ebca6b;
603 crc32val ^= crc32val >> 13;
604 crc32val *= 0xc2b2ae35;
605 crc32val ^= crc32val >> 16;
610int hashmap_memcmp_comparer(
const void *
const a,
const hashmap_uint32_t a_len,
612 return (a_len == b_len) && (0 == memcmp(a, b, a_len));
618 return (m->hasher(~0u, k, l) * 2654435769u) >> (32u - m->log2_capacity);
641 if (!m->data[
index].in_use) {
642 first_free = (first_free <
index) ? first_free :
index;
643 }
else if (m->comparer(m->data[
index].key, m->data[
index].key_len, key,
651 if (~0u == first_free) {
655 *out_index = first_free;
660 struct hashmap_element_s *
const e) {
662 e->key_len, e->data);
676 struct hashmap_create_options_s options;
677 struct hashmap_s new_m;
680 memset(&options, 0,
sizeof(options));
682 options.hasher = m->hasher;
684 if (0 == options.initial_capacity) {
705 memcpy(m, &new_m,
sizeof(
struct hashmap_s));
712 unsigned long result;
713 _BitScanReverse(&result, x);
715#elif defined(__TINYC__)
731#if defined(__clang__)
732#pragma clang diagnostic pop
#define AC_BEGIN_C_DECLARATIONS
Definition func_define.h:14
#define AC_END_C_DECLARATIONS
Definition func_define.h:15
int(* hashmap_comparer_t)(const void *a, hashmap_uint32_t a_len, const void *b, hashmap_uint32_t b_len)
Definition hashtable.h:140
HASHMAP_WEAK int hashmap_iterate(const struct hashmap_s *const hashmap, int(*iterator)(void *const context, void *const value), void *const context)
Iterate over all the elements in a hashmap.
Definition hashtable.h:452
HASHMAP_WEAK int hashmap_create(const hashmap_uint32_t initial_capacity, struct hashmap_s *const out_hashmap)
Create a hashmap.
Definition hashtable.h:295
HASHMAP_WEAK hashmap_uint32_t hashmap_capacity(const struct hashmap_s *const hashmap)
Get the capacity of the hashmap.
Definition hashtable.h:505
HASHMAP_WEAK hashmap_uint32_t hashmap_hash_helper_int_helper(const struct hashmap_s *const m, const void *const key, const hashmap_uint32_t len)
Definition hashtable.h:616
#define HASHMAP_CAST(type, x)
Definition hashtable.h:290
HASHMAP_WEAK const void * hashmap_remove_and_return_key(struct hashmap_s *const hashmap, const void *const key, const hashmap_uint32_t len)
Remove an element from the hashmap.
Definition hashtable.h:419
hashmap_uint32_t(* hashmap_hasher_t)(hashmap_uint32_t seed, const void *key, hashmap_uint32_t key_len)
Definition hashtable.h:137
HASHMAP_WEAK void hashmap_destroy(struct hashmap_s *const hashmap)
Destroy the hashmap.
Definition hashtable.h:494
#define HASHMAP_NULL
Definition hashtable.h:292
HASHMAP_WEAK void * hashmap_get(const struct hashmap_s *const hashmap, const void *const key, const hashmap_uint32_t len)
Get an element from the hashmap.
Definition hashtable.h:364
#define HASHMAP_PTR_CAST(type, x)
Definition hashtable.h:291
HASHMAP_WEAK int hashmap_remove(struct hashmap_s *const hashmap, const void *const key, const hashmap_uint32_t len)
Remove an element from the hashmap.
Definition hashtable.h:389
HASHMAP_WEAK int hashmap_create_ex(struct hashmap_create_options_s options, struct hashmap_s *const out_hashmap)
Create a hashmap.
Definition hashtable.h:304
HASHMAP_WEAK int hashmap_put(struct hashmap_s *const hashmap, const void *const key, const hashmap_uint32_t len, void *const value)
Put an element into the hashmap.
Definition hashtable.h:334
HASHMAP_WEAK hashmap_uint32_t hashmap_clz(const hashmap_uint32_t x)
Definition hashtable.h:710
HASHMAP_WEAK int hashmap_rehash_helper(struct hashmap_s *const m)
Definition hashtable.h:675
#define HASHMAP_ALWAYS_INLINE
Definition hashtable.h:116
#define HASHMAP_LINEAR_PROBE_LENGTH
Definition hashtable.h:151
HASHMAP_WEAK int hashmap_rehash_iterator(void *const new_hash, struct hashmap_element_s *const e)
Definition hashtable.h:659
uint32_t hashmap_uint32_t
Definition hashtable.h:126
HASHMAP_WEAK int hashmap_hash_helper(const struct hashmap_s *const m, const void *const key, const hashmap_uint32_t len, hashmap_uint32_t *const out_index)
Definition hashtable.h:622
HASHMAP_WEAK int hashmap_iterate_pairs(struct hashmap_s *const hashmap, int(*iterator)(void *const, struct hashmap_element_s *const), void *const context)
Iterate over all the elements in a hashmap.
Definition hashtable.h:467
HASHMAP_WEAK hashmap_uint32_t hashmap_num_entries(const struct hashmap_s *const hashmap)
Get the size of the hashmap.
Definition hashtable.h:500
uint8_t hashmap_uint8_t
Definition hashtable.h:125
uint64_t hashmap_uint64_t
Definition hashtable.h:127
Definition hashtable.h:153
hashmap_comparer_t comparer
Definition hashtable.h:155
hashmap_hasher_t hasher
Definition hashtable.h:154
hashmap_uint32_t initial_capacity
Definition hashtable.h:156
hashmap_uint32_t _
Definition hashtable.h:157
Definition hashtable.h:130
int in_use
Definition hashtable.h:133
const void * key
Definition hashtable.h:131
void * data
Definition hashtable.h:134
hashmap_uint32_t key_len
Definition hashtable.h:132
Definition hashtable.h:143
hashmap_hasher_t hasher
Definition hashtable.h:146
hashmap_uint32_t log2_capacity
Definition hashtable.h:144
hashmap_uint32_t size
Definition hashtable.h:145
hashmap_comparer_t comparer
Definition hashtable.h:147
struct hashmap_element_s * data
Definition hashtable.h:148
static __device__ const int const int index
Definition user_kernel.h:128