diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2024-08-18 14:26:45 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2024-08-18 14:26:45 +0200 |
| commit | 18c9a8b8905304cf5f8fc15825769046a3144866 (patch) | |
| tree | a7807bb32b0a5d9ded7d3cedccc598f64a9b00fe /src/solvers/h48/map.h | |
| parent | f25a10e19eca294c4e6a99e4f80ce5cfd11a0e5f (diff) | |
| download | nissy-core-18c9a8b8905304cf5f8fc15825769046a3144866.tar.gz nissy-core-18c9a8b8905304cf5f8fc15825769046a3144866.zip | |
Reorganized folder structure
Diffstat (limited to 'src/solvers/h48/map.h')
| -rw-r--r-- | src/solvers/h48/map.h | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/src/solvers/h48/map.h b/src/solvers/h48/map.h new file mode 100644 index 0000000..82e5a2c --- /dev/null +++ b/src/solvers/h48/map.h | |||
| @@ -0,0 +1,104 @@ | |||
| 1 | #define MAP_UNSET UINT64_C(0xFFFFFFFFFFFFFFFF) | ||
| 2 | #define MAP_KEYMASK UINT64_C(0xFFFFFFFFFF) | ||
| 3 | #define MAP_KEYSHIFT UINT64_C(40) | ||
| 4 | |||
| 5 | typedef struct { | ||
| 6 | uint64_t n; | ||
| 7 | uint64_t capacity; | ||
| 8 | uint64_t randomizer; | ||
| 9 | uint64_t *table; | ||
| 10 | } h48map_t; | ||
| 11 | |||
| 12 | typedef struct { | ||
| 13 | uint64_t key; | ||
| 14 | uint64_t val; | ||
| 15 | } kvpair_t; | ||
| 16 | |||
| 17 | _static void h48map_create(h48map_t *, uint64_t, uint64_t); | ||
| 18 | _static void h48map_clear(h48map_t *); | ||
| 19 | _static void h48map_destroy(h48map_t *); | ||
| 20 | _static uint64_t h48map_lookup(h48map_t *, uint64_t); | ||
| 21 | _static void h48map_insertmin(h48map_t *, uint64_t, uint64_t); | ||
| 22 | _static uint64_t h48map_value(h48map_t *, uint64_t); | ||
| 23 | _static kvpair_t h48map_nextkvpair(h48map_t *, uint64_t *); | ||
| 24 | |||
| 25 | _static void | ||
| 26 | h48map_create(h48map_t *map, uint64_t capacity, uint64_t randomizer) | ||
| 27 | { | ||
| 28 | map->capacity = capacity; | ||
| 29 | map->randomizer = randomizer; | ||
| 30 | |||
| 31 | map->table = malloc(map->capacity * sizeof(int64_t)); | ||
| 32 | h48map_clear(map); | ||
| 33 | } | ||
| 34 | |||
| 35 | _static void | ||
| 36 | h48map_clear(h48map_t *map) | ||
| 37 | { | ||
| 38 | memset(map->table, 0xFF, map->capacity * sizeof(uint64_t)); | ||
| 39 | map->n = 0; | ||
| 40 | } | ||
| 41 | |||
| 42 | _static void | ||
| 43 | h48map_destroy(h48map_t *map) | ||
| 44 | { | ||
| 45 | free(map->table); | ||
| 46 | } | ||
| 47 | |||
| 48 | _static_inline uint64_t | ||
| 49 | h48map_lookup(h48map_t *map, uint64_t x) | ||
| 50 | { | ||
| 51 | uint64_t hash, i; | ||
| 52 | |||
| 53 | hash = ((x % map->capacity) * map->randomizer) % map->capacity; | ||
| 54 | for (i = hash; | ||
| 55 | map->table[i] != MAP_UNSET && (map->table[i] & MAP_KEYMASK) != x; | ||
| 56 | i = (i+1) % map->capacity | ||
| 57 | ) ; | ||
| 58 | |||
| 59 | return i; | ||
| 60 | } | ||
| 61 | |||
| 62 | _static_inline void | ||
| 63 | h48map_insertmin(h48map_t *map, uint64_t key, uint64_t val) | ||
| 64 | { | ||
| 65 | uint64_t i, oldval, min; | ||
| 66 | |||
| 67 | i = h48map_lookup(map, key); | ||
| 68 | oldval = map->table[i] >> MAP_KEYSHIFT; | ||
| 69 | min = _min(val, oldval); | ||
| 70 | |||
| 71 | map->n += map->table[i] == MAP_UNSET; | ||
| 72 | map->table[i] = (key & MAP_KEYMASK) | (min << MAP_KEYSHIFT); | ||
| 73 | } | ||
| 74 | |||
| 75 | _static_inline uint64_t | ||
| 76 | h48map_value(h48map_t *map, uint64_t key) | ||
| 77 | { | ||
| 78 | return map->table[h48map_lookup(map, key)] >> MAP_KEYSHIFT; | ||
| 79 | } | ||
| 80 | |||
| 81 | _static kvpair_t | ||
| 82 | h48map_nextkvpair(h48map_t *map, uint64_t *p) | ||
| 83 | { | ||
| 84 | kvpair_t kv; | ||
| 85 | uint64_t pair; | ||
| 86 | |||
| 87 | kv.key = MAP_UNSET; | ||
| 88 | kv.val = MAP_UNSET; | ||
| 89 | |||
| 90 | DBG_ASSERT(*p < map->capacity, kv, | ||
| 91 | "Error looping over map: given index %" PRIu64 " is out of " | ||
| 92 | "range [0,%" PRIu64 "]", *p, map->capacity); | ||
| 93 | |||
| 94 | for ( ; *p < map->capacity; (*p)++) { | ||
| 95 | if (map->table[*p] != MAP_UNSET) { | ||
| 96 | pair = map->table[(*p)++]; | ||
| 97 | kv.key = pair & MAP_KEYMASK; | ||
| 98 | kv.val = pair >> MAP_KEYSHIFT; | ||
| 99 | return kv; | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | return kv; | ||
| 104 | } | ||
