summaryrefslogtreecommitdiff
path: root/src/hashm.h
blob: 67bb3e04a08b06cb1b56c19ecf5632c5baca2193 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#pragma once

#include "hashmap.h"
#include "utils.h"
#include <cstring>
#include <mimalloc.h>

#define WAYC_HASHMAP_SEED 0
#define WAYC_HASHMAP_CAP 16

template <typename T> struct hashmap_s {
  struct hashmap *inner;
};

template <typename T>
static inline i32 wayc_hashmap_hash(const void *item, uint64_t seed0,
                                    uint64_t seed1) {
  wayc_notnull(item);
  return hashmap_xxhash3(item, sizeof(T), seed0, seed1);
}

template <typename T>
static inline bool wayc_hashmap_compare(const void *a, const void *b,
                                        void *udata) {
  (void)udata;

  wayc_notnull(a);
  wayc_notnull(b);
  return memcmp(a, b, sizeof(T)) == 0;
}

template <typename T> inline hashmap_s<T> wayc_hashmap_init(void) {
  struct hashmap *map = hashmap_new_with_allocator(
      mi_malloc, mi_realloc, mi_free, sizeof(T), WAYC_HASHMAP_CAP,
      WAYC_HASHMAP_SEED, WAYC_HASHMAP_SEED, wayc_hashmap_hash<T>,
      wayc_hashmap_compare<T>, nullptr, nullptr);

  hashmap_s<T> result;
  result.inner = map;

  return result;
}

template <typename T> inline void wayc_hashmap_deinit(hashmap_s<T> *map) {
  hashmap_free(map->inner);
  map->inner = nullptr;
}