Astaroth  3.1
Scalable Multi-GPU Stencil Computations
Loading...
Searching...
No Matches
hashtable.h
Go to the documentation of this file.
1/*
2 The latest version of this library is available on GitHub;
3 https://github.com/sheredom/hashmap.h
4*/
5
6/*
7 This is free and unencumbered software released into the public domain.
8
9 Anyone is free to copy, modify, publish, use, compile, sell, or
10 distribute this software, either in source code form or as a compiled
11 binary, for any purpose, commercial or non-commercial, and by any
12 means.
13
14 In jurisdictions that recognize copyright laws, the author or authors
15 of this software dedicate any and all copyright interest in the
16 software to the public domain. We make this dedication for the benefit
17 of the public at large and to the detriment of our heirs and
18 successors. We intend this dedication to be an overt act of
19 relinquishment in perpetuity of all present and future rights to this
20 software under copyright law.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25 IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
26 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 OTHER DEALINGS IN THE SOFTWARE.
29
30 For more information, please refer to <http://unlicense.org/>
31*/
32#ifndef SHEREDOM_HASHMAP_H_INCLUDED
33#define SHEREDOM_HASHMAP_H_INCLUDED
34
35#if defined(_MSC_VER)
36// Workaround a bug in the MSVC runtime where it uses __cplusplus when not
37// defined.
38#pragma warning(push, 0)
39#pragma warning(disable : 4668)
40#endif
41
42#include <stdlib.h>
43#include <string.h>
44
45#include "func_define.h"
46
47#if (defined(_MSC_VER) && defined(__AVX__)) || \
48 (!defined(_MSC_VER) && defined(__SSE4_2__))
49#define HASHMAP_X86_SSE42
50#endif
51
52#if defined(HASHMAP_X86_SSE42)
53#include <nmmintrin.h>
54#endif
55
56#if defined(__aarch64__) && defined(__ARM_FEATURE_CRC32)
57#define HASHMAP_ARM_CRC32
58#endif
59
60#if defined(HASHMAP_ARM_CRC32)
61#include <arm_acle.h>
62#endif
63
64#if defined(_MSC_VER)
65#include <intrin.h>
66#endif
67
68#if defined(_MSC_VER)
69#pragma warning(pop)
70#endif
71
72#if defined(_MSC_VER)
73#pragma warning(push)
74/* Stop MSVC complaining about unreferenced functions */
75#pragma warning(disable : 4505)
76/* Stop MSVC complaining about not inlining functions */
77#pragma warning(disable : 4710)
78/* Stop MSVC complaining about inlining functions! */
79#pragma warning(disable : 4711)
80#endif
81
82#if defined(__clang__)
83#pragma clang diagnostic push
84#pragma clang diagnostic ignored "-Wunused-function"
85#pragma clang diagnostic ignored "-Wstatic-in-inline"
86
87#if __has_warning("-Wunsafe-buffer-usage")
88#pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
89#endif
90#endif
91
92#if defined(__TINYC__)
93#define HASHMAP_ATTRIBUTE(a) __attribute((a))
94#else
95#define HASHMAP_ATTRIBUTE(a) __attribute__((a))
96#endif
97
98#if defined(_MSC_VER)
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)
104#else
105#error Non clang, non gcc, non MSVC, non tcc compiler found!
106#endif
107
108#if defined(_MSC_VER)
109#define HASHMAP_ALWAYS_INLINE __forceinline
110#elif (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \
111 defined(__cplusplus)
112#define HASHMAP_ALWAYS_INLINE __attribute__((always_inline)) inline
113#else
114/* If we cannot use inline, its not safe to use always_inline, so we mark the
115 * function weak. */
116#define HASHMAP_ALWAYS_INLINE HASHMAP_WEAK
117#endif
118
119#if defined(_MSC_VER) && (_MSC_VER < 1920)
120typedef unsigned __int8 hashmap_uint8_t;
121typedef unsigned __int32 hashmap_uint32_t;
122typedef unsigned __int64 hashmap_uint64_t;
123#else
124#include <stdint.h>
125typedef uint8_t hashmap_uint8_t;
126typedef uint32_t hashmap_uint32_t;
127typedef uint64_t hashmap_uint64_t;
128#endif
129
130typedef struct hashmap_element_s {
131 const void *key;
134 void *data;
136
138 const void *key,
139 hashmap_uint32_t key_len);
140typedef int (*hashmap_comparer_t)(const void *a, hashmap_uint32_t a_len,
141 const void *b, hashmap_uint32_t b_len);
142
150
151#define HASHMAP_LINEAR_PROBE_LENGTH (8)
152
159
161
166HASHMAP_WEAK int hashmap_create(const hashmap_uint32_t initial_capacity,
167 struct hashmap_s *const out_hashmap);
168
177// crc32 with Robert Jenkins' mix is used).
178HASHMAP_WEAK int hashmap_create_ex(struct hashmap_create_options_s options,
179 struct hashmap_s *const out_hashmap);
180
191HASHMAP_WEAK int hashmap_put(struct hashmap_s *const hashmap,
192 const void *const key, const hashmap_uint32_t len,
193 void *const value);
194
200HASHMAP_WEAK void *hashmap_get(const struct hashmap_s *const hashmap,
201 const void *const key,
202 const hashmap_uint32_t len);
203
209HASHMAP_WEAK int hashmap_remove(struct hashmap_s *const hashmap,
210 const void *const key,
211 const hashmap_uint32_t len);
212
219HASHMAP_WEAK const void *
220hashmap_remove_and_return_key(struct hashmap_s *const hashmap,
221 const void *const key,
222 const hashmap_uint32_t len);
223
230HASHMAP_WEAK int hashmap_iterate(const struct hashmap_s *const hashmap,
231 int (*iterator)(void *const context,
232 void *const value),
233 void *const context);
234
243HASHMAP_WEAK int hashmap_iterate_pairs(
244 struct hashmap_s *const hashmap,
245 int (*iterator)(void *const, struct hashmap_element_s *const),
246 void *const context);
247
252hashmap_num_entries(const struct hashmap_s *const hashmap);
253
258hashmap_capacity(const struct hashmap_s *const hashmap);
259
262HASHMAP_WEAK void hashmap_destroy(struct hashmap_s *const hashmap);
263
264static hashmap_uint32_t hashmap_crc32_hasher(const hashmap_uint32_t seed,
265 const void *const s,
266 const hashmap_uint32_t len);
267static int hashmap_memcmp_comparer(const void *const a,
268 const hashmap_uint32_t a_len,
269 const void *const b,
270 const hashmap_uint32_t b_len);
272 const struct hashmap_s *const m, const void *const key,
273 const hashmap_uint32_t len);
275hashmap_hash_helper(const struct hashmap_s *const m, const void *const key,
276 const hashmap_uint32_t len,
277 hashmap_uint32_t *const out_index);
278HASHMAP_WEAK int hashmap_rehash_iterator(void *const new_hash,
279 struct hashmap_element_s *const e);
280HASHMAP_ALWAYS_INLINE int hashmap_rehash_helper(struct hashmap_s *const m);
282
284
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
289#else
290#define HASHMAP_CAST(type, x) ((type)(x))
291#define HASHMAP_PTR_CAST(type, x) ((type)(x))
292#define HASHMAP_NULL 0
293#endif
294
295int hashmap_create(const hashmap_uint32_t initial_capacity,
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;
300
301 return hashmap_create_ex(options, out_hashmap);
302}
303
304int hashmap_create_ex(struct hashmap_create_options_s options,
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
310 << (32 - hashmap_clz(options.initial_capacity));
311 }
312
313 if (HASHMAP_NULL == options.hasher) {
314 options.hasher = &hashmap_crc32_hasher;
315 }
316
317 if (HASHMAP_NULL == options.comparer) {
318 options.comparer = &hashmap_memcmp_comparer;
319 }
320
321 out_hashmap->data = HASHMAP_CAST(
322 struct hashmap_element_s *,
323 calloc(options.initial_capacity + HASHMAP_LINEAR_PROBE_LENGTH,
324 sizeof(struct hashmap_element_s)));
325
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;
330
331 return 0;
332}
333
334int hashmap_put(struct hashmap_s *const m, const void *const key,
335 const hashmap_uint32_t len, void *const value) {
337
338 if ((HASHMAP_NULL == key) || (0 == len)) {
339 return 1;
340 }
341
342 /* Find a place to put our value. */
343 while (!hashmap_hash_helper(m, key, len, &index)) {
344 if (hashmap_rehash_helper(m)) {
345 return 1;
346 }
347 }
348
349 /* Set the data. */
350 m->data[index].data = value;
351 m->data[index].key = key;
352 m->data[index].key_len = len;
353
354 /* If the hashmap element was not already in use, set that it is being used
355 * and bump our size. */
356 if (0 == m->data[index].in_use) {
357 m->data[index].in_use = 1;
358 m->size++;
359 }
360
361 return 0;
362}
363
364void *hashmap_get(const struct hashmap_s *const m, const void *const key,
365 const hashmap_uint32_t len) {
366 hashmap_uint32_t i, curr;
367
368 if ((HASHMAP_NULL == key) || (0 == len)) {
369 return HASHMAP_NULL;
370 }
371
372 curr = hashmap_hash_helper_int_helper(m, key, len);
373
374 /* Linear probing, if necessary */
375 for (i = 0; i < HASHMAP_LINEAR_PROBE_LENGTH; i++) {
376 const hashmap_uint32_t index = curr + i;
377
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;
381 }
382 }
383 }
384
385 /* Not found */
386 return HASHMAP_NULL;
387}
388
389int hashmap_remove(struct hashmap_s *const m, const void *const key,
390 const hashmap_uint32_t len) {
391 hashmap_uint32_t i, curr;
392
393 if ((HASHMAP_NULL == key) || (0 == len)) {
394 return 1;
395 }
396
397 curr = hashmap_hash_helper_int_helper(m, key, len);
398
399 /* Linear probing, if necessary */
400 for (i = 0; i < HASHMAP_LINEAR_PROBE_LENGTH; i++) {
401 const hashmap_uint32_t index = curr + i;
402
403 if (m->data[index].in_use) {
404 if (m->comparer(m->data[index].key, m->data[index].key_len, key, len)) {
405 /* Blank out the fields including in_use */
406 memset(&m->data[index], 0, sizeof(struct hashmap_element_s));
407
408 /* Reduce the size */
409 m->size--;
410
411 return 0;
412 }
413 }
414 }
415
416 return 1;
417}
418
419const void *hashmap_remove_and_return_key(struct hashmap_s *const m,
420 const void *const key,
421 const hashmap_uint32_t len) {
422 hashmap_uint32_t i, curr;
423
424 if ((HASHMAP_NULL == key) || (0 == len)) {
425 return HASHMAP_NULL;
426 }
427
428 curr = hashmap_hash_helper_int_helper(m, key, len);
429
430 /* Linear probing, if necessary */
431 for (i = 0; i < HASHMAP_LINEAR_PROBE_LENGTH; i++) {
432 const hashmap_uint32_t index = curr + i;
433
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;
437
438 /* Blank out the fields */
439 memset(&m->data[index], 0, sizeof(struct hashmap_element_s));
440
441 /* Reduce the size */
442 m->size--;
443
444 return stored_key;
445 }
446 }
447 }
448
449 return HASHMAP_NULL;
450}
451
452int hashmap_iterate(const struct hashmap_s *const m,
453 int (*f)(void *const, void *const), void *const context) {
455
456 for (i = 0; i < (hashmap_capacity(m) + HASHMAP_LINEAR_PROBE_LENGTH); i++) {
457 if (m->data[i].in_use) {
458 if (!f(context, m->data[i].data)) {
459 return 1;
460 }
461 }
462 }
463
464 return 0;
465}
466
467int hashmap_iterate_pairs(struct hashmap_s *const m,
468 int (*f)(void *const,
469 struct hashmap_element_s *const),
470 void *const context) {
472 struct hashmap_element_s *p;
473 int r;
474
475 for (i = 0; i < (hashmap_capacity(m) + HASHMAP_LINEAR_PROBE_LENGTH); i++) {
476 p = &m->data[i];
477 if (p->in_use) {
478 r = f(context, p);
479 switch (r) {
480 case -1: /* remove item */
481 memset(p, 0, sizeof(struct hashmap_element_s));
482 m->size--;
483 break;
484 case 0: /* continue iterating */
485 break;
486 default: /* early exit */
487 return 1;
488 }
489 }
490 }
491 return 0;
492}
493
494void hashmap_destroy(struct hashmap_s *const m) {
495 free(m->data);
496 memset(m, 0, sizeof(struct hashmap_s));
497}
498
500hashmap_num_entries(const struct hashmap_s *const m) {
501 return m->size;
502}
503
505hashmap_capacity(const struct hashmap_s *const m) {
506 return 1u << m->log2_capacity;
507}
508
509hashmap_uint32_t hashmap_crc32_hasher(const hashmap_uint32_t seed,
510 const void *const k,
511 const hashmap_uint32_t len) {
512 hashmap_uint32_t i = 0;
513 hashmap_uint32_t crc32val = seed;
514 const hashmap_uint8_t *const s = HASHMAP_PTR_CAST(const hashmap_uint8_t *, k);
515
516#if defined(HASHMAP_X86_SSE42)
517 for (; (i + sizeof(hashmap_uint32_t)) < len; i += sizeof(hashmap_uint32_t)) {
518 hashmap_uint32_t next;
519 memcpy(&next, &s[i], sizeof(next));
520 crc32val = _mm_crc32_u32(crc32val, next);
521 }
522
523 for (; i < len; i++) {
524 crc32val = _mm_crc32_u8(crc32val, s[i]);
525 }
526#elif defined(HASHMAP_ARM_CRC32)
527 for (; (i + sizeof(hashmap_uint64_t)) < len; i += sizeof(hashmap_uint64_t)) {
528 hashmap_uint64_t next;
529 memcpy(&next, &s[i], sizeof(next));
530 crc32val = __crc32d(crc32val, next);
531 }
532
533 for (; i < len; i++) {
534 crc32val = __crc32b(crc32val, s[i]);
535 }
536#else
537 // Using polynomial 0x11EDC6F41 to match SSE 4.2's crc function.
538 static const hashmap_uint32_t crc32_tab[] = {
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,
590 0xAD7D5351U};
591
592 for (; i < len; i++) {
593 crc32val = crc32_tab[(HASHMAP_CAST(hashmap_uint8_t, crc32val) ^ s[i])] ^
594 (crc32val >> 8);
595 }
596#endif
597
598 // Use the mix function from murmur3.
599 crc32val ^= len;
600
601 crc32val ^= crc32val >> 16;
602 crc32val *= 0x85ebca6b;
603 crc32val ^= crc32val >> 13;
604 crc32val *= 0xc2b2ae35;
605 crc32val ^= crc32val >> 16;
606
607 return crc32val;
608}
609
610int hashmap_memcmp_comparer(const void *const a, const hashmap_uint32_t a_len,
611 const void *const b, const hashmap_uint32_t b_len) {
612 return (a_len == b_len) && (0 == memcmp(a, b, a_len));
613}
614
616hashmap_hash_helper_int_helper(const struct hashmap_s *const m,
617 const void *const k, const hashmap_uint32_t l) {
618 return (m->hasher(~0u, k, l) * 2654435769u) >> (32u - m->log2_capacity);
619}
620
622hashmap_hash_helper(const struct hashmap_s *const m, const void *const key,
623 const hashmap_uint32_t len,
624 hashmap_uint32_t *const out_index) {
625 hashmap_uint32_t curr;
627 hashmap_uint32_t first_free;
628
629 /* If full, return immediately */
631 return 0;
632 }
633
634 /* Find the best index */
635 curr = hashmap_hash_helper_int_helper(m, key, len);
636 first_free = ~0u;
637
638 for (i = 0; i < HASHMAP_LINEAR_PROBE_LENGTH; i++) {
639 const hashmap_uint32_t index = curr + i;
640
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,
644 len)) {
645 *out_index = index;
646 return 1;
647 }
648 }
649
650 // Couldn't find a free element in the linear probe.
651 if (~0u == first_free) {
652 return 0;
653 }
654
655 *out_index = first_free;
656 return 1;
657}
658
659int hashmap_rehash_iterator(void *const new_hash,
660 struct hashmap_element_s *const e) {
661 int temp = hashmap_put(HASHMAP_PTR_CAST(struct hashmap_s *, new_hash), e->key,
662 e->key_len, e->data);
663
664 if (0 < temp) {
665 return 1;
666 }
667
668 /* clear old value to avoid stale pointers */
669 return -1;
670}
671
672/*
673 * Doubles the size of the hashmap, and rehashes all the elements
674 */
675HASHMAP_ALWAYS_INLINE int hashmap_rehash_helper(struct hashmap_s *const m) {
676 struct hashmap_create_options_s options;
677 struct hashmap_s new_m;
678 int flag;
679
680 memset(&options, 0, sizeof(options));
681 options.initial_capacity = hashmap_capacity(m) * 2;
682 options.hasher = m->hasher;
683
684 if (0 == options.initial_capacity) {
685 return 1;
686 }
687
688 flag = hashmap_create_ex(options, &new_m);
689
690 if (0 != flag) {
691 return flag;
692 }
693
694 /* copy the old elements to the new table */
696 HASHMAP_PTR_CAST(void *, &new_m));
697
698 if (0 != flag) {
699 return flag;
700 }
701
703
704 /* put new hash into old hash structure by copying */
705 memcpy(m, &new_m, sizeof(struct hashmap_s));
706
707 return 0;
708}
709
711#if defined(_MSC_VER)
712 unsigned long result;
713 _BitScanReverse(&result, x);
714 return 31u - HASHMAP_CAST(hashmap_uint32_t, result);
715#elif defined(__TINYC__)
716 union {
717 double d;
719 } u;
720 u.d = HASHMAP_CAST(double, x) + 0.5;
721 return 1054u - HASHMAP_CAST(hashmap_uint32_t, u.u >> 52);
722#else
723 return HASHMAP_CAST(hashmap_uint32_t, __builtin_clz(x));
724#endif
725}
726
727#if defined(_MSC_VER)
728#pragma warning(pop)
729#endif
730
731#if defined(__clang__)
732#pragma clang diagnostic pop
733#endif
734
735#endif
#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