aboutsummaryrefslogtreecommitdiff
path: root/src/solvers/h48/map.h
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2024-08-18 14:26:45 +0200
committerSebastiano Tronto <sebastiano@tronto.net>2024-08-18 14:26:45 +0200
commit18c9a8b8905304cf5f8fc15825769046a3144866 (patch)
treea7807bb32b0a5d9ded7d3cedccc598f64a9b00fe /src/solvers/h48/map.h
parentf25a10e19eca294c4e6a99e4f80ce5cfd11a0e5f (diff)
downloadnissy-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.h104
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
5typedef struct {
6 uint64_t n;
7 uint64_t capacity;
8 uint64_t randomizer;
9 uint64_t *table;
10} h48map_t;
11
12typedef 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
26h48map_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
36h48map_clear(h48map_t *map)
37{
38 memset(map->table, 0xFF, map->capacity * sizeof(uint64_t));
39 map->n = 0;
40}
41
42_static void
43h48map_destroy(h48map_t *map)
44{
45 free(map->table);
46}
47
48_static_inline uint64_t
49h48map_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
63h48map_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
76h48map_value(h48map_t *map, uint64_t key)
77{
78 return map->table[h48map_lookup(map, key)] >> MAP_KEYSHIFT;
79}
80
81_static kvpair_t
82h48map_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}

Generated with cgit - Back to sebastiano.tronto.net