aboutsummaryrefslogtreecommitdiff
path: root/src/hm.c
blob: 3421564731baa19c64bf7b3dab60e9b0e788c704 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// hm.c --- Hash Map                                                   -*- C -*-
//
// Copyright (c) 2026 Martin Michalec <martin@michalec.dev>
//
// SPDX-License-Identifier: MIT
//
// This file is part of COSMOS Codebase.
//
// Commentary:
//
// Code:

#define HM_INCLUDE_INTERNAL
#include "hm.h"
#include "dbg.h"

#include <string.h>             // TODO(cmmm): drop libc dep (memset)

void
hm_init (struct hm *hm, struct gpa gpa)
{
  DBG_ASSERT_NN (hm);
  hm->_gpa = gpa;
}

const struct mv **
hm_upsert (struct hm *hm, struct mv key)
{
  DBG_ASSERT_NN (hm);
  DBG_ASSERT_MV (key);

  u64 hash = _hm_hash (hm, key);
  // DBG_TRACE ("info", "hash: %lu", hash);
  struct _hm_slot *slot =
    _hm_slot_ensure (hm, NULL, &hash, key, _HM_KEY_KIND_MV);
  DBG_ASSERT_NN (slot);

  if (slot->hash <= _HM_SLOT_TOMB) {
    hm->_live_cnt += 1;
    if (slot->hash == _HM_SLOT_TOMB)
      hm->_tomb_cnt -= 1;
    slot->hash = hash;
    slot->key.ptr = NULL;
  }

  return (void *) &slot->key.ptr;
}

int
hm_insert (struct hm *hm, const struct mv *key)
{
  DBG_ASSERT_NN ( hm);
  DBG_ASSERT_NN ( key);
  DBG_ASSERT_MV (*key);
  DBG_ASSERT (!mv_eq (*key, MV_NULL));

  u64 hash = _hm_hash (hm, *key);
  struct _hm_slot *slot =
    _hm_slot_ensure (hm, NULL, &hash, *key, _HM_KEY_KIND_MV);
  DBG_ASSERT_NN (slot);

  if (slot->hash >  _HM_SLOT_TOMB) return 0;
  if (slot->hash == _HM_SLOT_TOMB) hm->_tomb_cnt -= 1;

  hm->_live_cnt += 1;
  slot->hash = hash;
  slot->key.ptr = key;

  return 1;
}

const struct mv *
hm_lookup (const struct hm *hm, struct mv key)
{
  DBG_ASSERT_NN (hm);
  DBG_ASSERT_MV (key);

  const struct _hm_slot *slot =
    _hm_slot (hm, NULL, _hm_hash (hm, key), key, _HM_KEY_KIND_MV);

  if (slot       ==  NULL)         return NULL;
  if (slot->hash <= _HM_SLOT_TOMB) return NULL;

  return slot->key.ptr;
}

int
hm_delete (struct hm *hm, struct mv key)
{
  DBG_ASSERT_NN (hm);
  DBG_ASSERT_MV (key);

  struct _hm_slot *slot =
    _hm_slot (hm, NULL, _hm_hash (hm, key), key, _HM_KEY_KIND_MV);

  if (slot       ==  NULL)         return 0;
  if (slot->hash <= _HM_SLOT_TOMB) return 0;

  hm->_tomb_cnt += 1;
  hm->_live_cnt -= 1;
  slot->hash = _HM_SLOT_TOMB;

  return 1;
}

hm_iter
hm_iter_init (const struct hm *hm)
{
  DBG_ASSERT_NN (hm);
  struct _hm_slot *slot      = hm->_slots;
  struct _hm_slot *slots_end = hm->_slots + hm->_slot_cnt;
  while (slot < slots_end) {
    if (slot->hash > _HM_SLOT_TOMB)
      return (hm_iter) &slot->key.ptr;
    slot += 1;
  } return NULL;
}

hm_iter
hm_iter_next (const struct hm *hm, hm_iter iter)
{
  DBG_ASSERT_NN (hm);
  DBG_ASSERT_NN (iter);
  struct _hm_slot *slot      = CONTAINER_OF (iter, *slot, key);
  struct _hm_slot *slots_end = hm->_slots + hm->_slot_cnt;
  while (++slot < slots_end) {
    if (slot->hash > _HM_SLOT_TOMB)
      return (hm_iter) &slot->key.ptr;
  } return NULL;
}

ptrdiff *
hm_rel_upsert (struct hm *hm, struct mv key, const struct mv *base)
{
  DBG_ASSERT_NN (hm);
  DBG_ASSERT_MV (key);
  DBG_ASSERT_NN (base);

  u64 hash = _hm_hash (hm, key);
  // DBG_TRACE ("info", "hash: %lu", hash);
  struct _hm_slot *slot =
    _hm_slot_ensure (hm, base, &hash, key, _HM_KEY_KIND_MV);
  DBG_ASSERT_NN (slot);

  if (slot->hash <= _HM_SLOT_TOMB) {
    hm->_live_cnt += 1;
    if (slot->hash == _HM_SLOT_TOMB)
      hm->_tomb_cnt -= 1;
    slot->hash = hash;
    slot->key.ofs = HM_REL_NEW;
  }

  return &slot->key.ofs;
}

int
hm_rel_insert (struct hm *hm, const struct mv *key, const struct mv *base)
{
  DBG_ASSERT_NN ( hm);
  DBG_ASSERT_NN ( key);
  DBG_ASSERT_MV (*key);
  DBG_ASSERT_NN ( base);

  u64 hash = _hm_hash (hm, *key);
  struct _hm_slot *slot =
    _hm_slot_ensure (hm, base, &hash, *key, _HM_KEY_KIND_MV);
  DBG_ASSERT_NN (slot);

  if (slot->hash >  _HM_SLOT_TOMB) return 0;
  if (slot->hash == _HM_SLOT_TOMB) hm->_tomb_cnt -= 1;
  hm->_live_cnt += 1;
  slot->hash = hash;
  slot->key.ofs = (const u8 *) key - (const u8 *) base;

  return 1;
}

const struct mv *
hm_rel_lookup (const struct hm *hm, struct mv key, const struct mv *base)
{
  DBG_ASSERT_NN (hm);
  DBG_ASSERT_MV (key);
  DBG_ASSERT_NN (base);

  const struct _hm_slot *slot =
    _hm_slot (hm, base, _hm_hash (hm, key), key, _HM_KEY_KIND_MV);

  if (slot == NULL)                return NULL;
  if (slot->hash <= _HM_SLOT_TOMB) return NULL;

  return (void *) ((const u8 *) base + slot->key.ofs);
}

int
hm_rel_delete (struct hm *hm, struct mv key, const struct mv *base)
{
  DBG_ASSERT_NN (hm);
  DBG_ASSERT_MV (key);
  DBG_ASSERT_NN (base);

  struct _hm_slot *slot =
    _hm_slot (hm, base, _hm_hash (hm, key), key, _HM_KEY_KIND_MV);

  if (slot       ==  NULL)         return 0;
  if (slot->hash <= _HM_SLOT_TOMB) return 0;

  hm->_tomb_cnt += 1;
  hm->_live_cnt -= 1;
  slot->hash = _HM_SLOT_TOMB;

  return 1;
}

const struct mv *const*hm_rel_iter_init (const struct hm *);
const struct mv *const*hm_rel_iter_next (const struct hm *, const struct mv *const*);

void
hm_drop (struct hm *hm)
{
  DBG_ASSERT_NN (hm);
  GPA_DROP_N (hm->_gpa, hm->_slots, hm->_slot_cnt);
  hm->_slot_cnt = 0;
  hm->_tomb_cnt = 0;
}

void
hm_clear (struct hm *hm)
{
  DBG_ASSERT_NN (hm);
  hm->_tomb_cnt = 0;
  memset (hm->_slots, 0, hm->_slot_cnt*sizeof (*hm->_slots));
}

/* ================================ INTERNAL ================================ */

void
_hm_resize (struct hm *hm, u32 new_slot_cnt)
{
  DBG_ASSERT_NN (hm);
  DBG_ASSERT_EQ (POPCOUNT (new_slot_cnt), 1, "%u", "%u");

  u32 new_mask = new_slot_cnt - 1;
  struct _hm_slot *new_slots = GPA_GRAB_N0 (hm->_gpa, new_slot_cnt, *new_slots);
  struct _hm_slot *slots_end = &hm->_slots[hm->_slot_cnt];
  u32 live_left = hm->_live_cnt;
  for (struct _hm_slot *slot = hm->_slots; slot < slots_end; ++slot) {
    if (slot->hash <= _HM_SLOT_TOMB)
      continue;

    u32 new_slot_idx = slot->hash & new_mask;
    while (new_slots[new_slot_idx].hash != _HM_SLOT_EMPTY)
      new_slot_idx = (new_slot_idx + 1) & new_mask;

    new_slots[new_slot_idx] = *slot;

    live_left -= 1;
    if (live_left == 0)
      break;
  }

  if (hm->_slots)
    GPA_DROP_N (hm->_gpa, hm->_slots, hm->_slot_cnt);

  hm->_tomb_cnt  = 0;
  hm->_slot_cnt  =   new_slot_cnt;
  hm->_slot_msk  =   new_slot_cnt - 1;
  hm->_resize_at = 4*new_slot_cnt/3;
  hm->_slots     =   new_slots;
}

struct _hm_slot *
_hm_slot (const struct hm *hm, const void *base, u64 hash,
          struct mv key, enum _hm_key_kind key_kind)
{
  DBG_ASSERT_NN (hm);
  DBG_ASSERT_NN (key.beg);
  DBG_ASSERT_LT (key.beg, key.end, "%p", "%p");

  if (hm->_slots == NULL)
    return NULL;

  u32 mask     = hm->_slot_msk;
  u32 slot_idx = hash & mask;
  struct _hm_slot *slots = hm->_slots;
  for (u32 i = 0; i < hm->_max_probe; ++i) {
    u64 slot_hash = slots[slot_idx].hash;

    if (slot_hash == _HM_SLOT_EMPTY)
      return NULL;

    if (slot_hash == hash) {
      struct mv slot_key = _hm_key_ptr_to_mv (
        base + slots[slot_idx].key.ofs, key_kind, _hm_key_sz (key, key_kind));
      if (mv_equal (slot_key, key))
        return &slots[slot_idx];
    }

    slot_idx = (slot_idx + 1) & mask;
  }

  return NULL;
}

struct _hm_slot *
_hm_slot_ensure (struct hm *hm, const void *base, u64 *hash,
                 struct mv key, enum _hm_key_kind key_kind)
{
  DBG_ASSERT_NN (hm);
  DBG_ASSERT_NN (hash);
  DBG_ASSERT_IN (0, key_kind, _HM_KEY_KIND_CNT, "%d", "%d", "%d");

  if (!hm->_slots) {
    hm->_slot_cnt  =   HM_INIT_SLOTS;
    hm->_slot_msk  =   HM_INIT_SLOTS - 1;
    hm->_resize_at = 4*HM_INIT_SLOTS/3;
    hm->_tomb_cnt  = 0;
    hm->_live_cnt  = 0;
    hm->_max_probe = 0;
    hm->_slots = GPA_GRAB_N0 (hm->_gpa, hm->_slot_cnt, *hm->_slots);
    if (!hm->_hash_init) {
      hm->_hash_init = HM_INIT_HASH ((uptr) hm->_slots ^ (uptr) &hm);
      *hash = _hm_hash (hm, key);
    }
  } else if (hm->_tomb_cnt + hm->_live_cnt + 1 >= hm->_resize_at) {
    _hm_resize (hm, 2*hm->_slot_cnt);
  }

  u32 mask     = hm->_slot_msk;
  u32 slot_idx = *hash & mask;
  struct _hm_slot *first_tomb = NULL;
  struct _hm_slot *slots = hm->_slots;
  for (u32 i = 0; i < hm->_slot_cnt; ++i) {
    u64 slot_hash = slots[slot_idx].hash;

    if (slot_hash == _HM_SLOT_EMPTY) {
      if (i + 1 > hm->_max_probe)
        hm->_max_probe = i + 1;
      return first_tomb ?: &slots[slot_idx];
    }

    if (slot_hash == _HM_SLOT_TOMB) {
      if (!first_tomb)
        first_tomb = &slots[slot_idx];
    } else if (slot_hash == *hash) {
      struct mv slot_key = _hm_key_ptr_to_mv (
        base + slots[slot_idx].key.ofs, key_kind, _hm_key_sz (key, key_kind));
      if (mv_equal (slot_key, key)) {
        if (i + 1 > hm->_max_probe)
          hm->_max_probe = i + 1;
        return &slots[slot_idx];
      }
    }

    slot_idx = (slot_idx + 1) & mask;
  }

  DBG_UNREACHABLE ();
  return NULL;
}

usz
_hm_key_sz (struct mv key, enum _hm_key_kind key_kind)
{
  switch (key_kind) {
    case _HM_KEY_KIND_MV:   return sizeof (struct mv);
    case _HM_KEY_KIND_CSTR: return sizeof (char *);
    case _HM_KEY_KIND_EMB:  return mv_sz (key);
  } DBG_UNREACHABLE ();
  return 0;
}

struct mv
_hm_key_ptr_to_mv (const void *ptr, enum _hm_key_kind key_kind, usz key_sz)
{
  DBG_ASSERT_NN (ptr);
  switch (key_kind) {
    case _HM_KEY_KIND_MV:   return *(const struct mv *) ptr;
    case _HM_KEY_KIND_CSTR: return mv_from_cstr (*(const char **) ptr);
    case _HM_KEY_KIND_EMB:  return mv_from (ptr, (u8 *) ptr + key_sz);
  } DBG_UNREACHABLE ();
  return MV_NULL;
}

u64
_hm_hash (const struct hm *hm, struct mv mv)
{
  u64 h = HM_HASH (hm->_hash_init, mv);
  return h < 2 ? h + 2 : h;
}

// hm.c ends here