summaryrefslogtreecommitdiffstats
path: root/libnetdata/simple_hashtable.h
blob: f6b6db9068e09e62562c18116f22ad8bf058ed35 (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
392
393
394
395
396
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef NETDATA_SIMPLE_HASHTABLE_H
#define NETDATA_SIMPLE_HASHTABLE_H

#ifndef XXH_INLINE_ALL
#define XXH_INLINE_ALL
#endif
#include "xxhash.h"

typedef uint64_t SIMPLE_HASHTABLE_HASH;
#define SIMPLE_HASHTABLE_HASH_SECOND_HASH_SHIFTS 32

#ifndef SIMPLE_HASHTABLE_NAME
#define SIMPLE_HASHTABLE_NAME
#endif

#ifndef SIMPLE_HASHTABLE_VALUE_TYPE
#define SIMPLE_HASHTABLE_VALUE_TYPE void
#endif

// First layer of macro for token concatenation
#define CONCAT_INTERNAL(a, b) a ## b
// Second layer of macro, which ensures proper expansion
#define CONCAT(a, b) CONCAT_INTERNAL(a, b)

// define names for all structures and structures
#define simple_hashtable_init_named CONCAT(simple_hashtable_init, SIMPLE_HASHTABLE_NAME)
#define simple_hashtable_destroy_named CONCAT(simple_hashtable_destroy, SIMPLE_HASHTABLE_NAME)

#define simple_hashtable_slot_named CONCAT(simple_hashtable_slot, SIMPLE_HASHTABLE_NAME)
#define SIMPLE_HASHTABLE_SLOT_NAMED CONCAT(SIMPLE_HASHTABLE_SLOT, SIMPLE_HASHTABLE_NAME)
#define simple_hashtable_named CONCAT(simple_hashtable, SIMPLE_HASHTABLE_NAME)
#define SIMPLE_HASHTABLE_NAMED CONCAT(SIMPLE_HASHTABLE, SIMPLE_HASHTABLE_NAME)
#define simple_hashtable_resize_named CONCAT(simple_hashtable_resize, SIMPLE_HASHTABLE_NAME)
#define simple_hashtable_get_slot_named CONCAT(simple_hashtable_get_slot, SIMPLE_HASHTABLE_NAME)
#define simple_hashtable_del_slot_named CONCAT(simple_hashtable_del_slot, SIMPLE_HASHTABLE_NAME)
#define simple_hashtable_set_slot_named CONCAT(simple_hashtable_set_slot, SIMPLE_HASHTABLE_NAME)
#define simple_hashtable_first_read_only_named CONCAT(simple_hashtable_first_read_only, SIMPLE_HASHTABLE_NAME)
#define simple_hashtable_next_read_only_named CONCAT(simple_hashtable_next_read_only, SIMPLE_HASHTABLE_NAME)

#define simple_hashtable_sorted_binary_search_named CONCAT(simple_hashtable_sorted_binary_search, SIMPLE_HASHTABLE_NAME)
#define simple_hashtable_add_value_sorted_named CONCAT(simple_hashtable_add_value_sorted, SIMPLE_HASHTABLE_NAME)
#define simple_hashtable_del_value_sorted_named CONCAT(simple_hashtable_del_value_sorted, SIMPLE_HASHTABLE_NAME)
#define simple_hashtable_replace_value_sorted_named CONCAT(simple_hashtable_replace_value_sorted, SIMPLE_HASHTABLE_NAME)
#define simple_hashtable_sorted_array_first_read_only_named CONCAT(simple_hashtable_sorted_array_first_read_only, SIMPLE_HASHTABLE_NAME)
#define simple_hashtable_sorted_array_next_read_only_named CONCAT(simple_hashtable_sorted_array_next_read_only, SIMPLE_HASHTABLE_NAME)

typedef struct simple_hashtable_slot_named {
    SIMPLE_HASHTABLE_HASH hash;
    SIMPLE_HASHTABLE_VALUE_TYPE *data;
} SIMPLE_HASHTABLE_SLOT_NAMED;

typedef struct simple_hashtable_named {
    size_t resizes;
    size_t searches;
    size_t collisions;
    size_t deletions;
    size_t deleted;
    size_t used;
    size_t size;
    SIMPLE_HASHTABLE_SLOT_NAMED *hashtable;

#ifdef SIMPLE_HASHTABLE_SORT_FUNCTION
    struct {
        size_t used;
        size_t size;
        SIMPLE_HASHTABLE_VALUE_TYPE **array;
    } sorted;
#endif
} SIMPLE_HASHTABLE_NAMED;

#ifdef SIMPLE_HASHTABLE_SORT_FUNCTION
static inline size_t simple_hashtable_sorted_binary_search_named(SIMPLE_HASHTABLE_NAMED *ht, SIMPLE_HASHTABLE_VALUE_TYPE *value) {
    size_t left = 0, right = ht->sorted.used;

    while (left < right) {
        size_t mid = left + (right - left) / 2;
        if (SIMPLE_HASHTABLE_SORT_FUNCTION(ht->sorted.array[mid], value) < 0)
            left = mid + 1;
        else
            right = mid;
    }

    return left;
}

static inline void simple_hashtable_add_value_sorted_named(SIMPLE_HASHTABLE_NAMED *ht, SIMPLE_HASHTABLE_VALUE_TYPE *value) {
    size_t index = simple_hashtable_sorted_binary_search_named(ht, value);

    // Ensure there's enough space in the sorted array
    if (ht->sorted.used >= ht->sorted.size) {
        size_t size = ht->sorted.size ? ht->sorted.size * 2 : 64;
        SIMPLE_HASHTABLE_VALUE_TYPE **array = mallocz(size * sizeof(SIMPLE_HASHTABLE_VALUE_TYPE *));
        if(ht->sorted.array) {
            memcpy(array, ht->sorted.array, ht->sorted.size * sizeof(SIMPLE_HASHTABLE_VALUE_TYPE *));
            freez(ht->sorted.array);
        }
        ht->sorted.array = array;
        ht->sorted.size = size;
    }

    // Use memmove to shift elements and create space for the new element
    memmove(&ht->sorted.array[index + 1], &ht->sorted.array[index], (ht->sorted.used - index) * sizeof(SIMPLE_HASHTABLE_VALUE_TYPE *));

    ht->sorted.array[index] = value;
    ht->sorted.used++;
}

static inline void simple_hashtable_del_value_sorted_named(SIMPLE_HASHTABLE_NAMED *ht, SIMPLE_HASHTABLE_VALUE_TYPE *value) {
    size_t index = simple_hashtable_sorted_binary_search_named(ht, value);

    // Check if the value exists at the found index
    assert(index < ht->sorted.used && ht->sorted.array[index] == value);

    // Use memmove to shift elements and close the gap
    memmove(&ht->sorted.array[index], &ht->sorted.array[index + 1], (ht->sorted.used - index - 1) * sizeof(SIMPLE_HASHTABLE_VALUE_TYPE *));
    ht->sorted.used--;
}

static inline void simple_hashtable_replace_value_sorted_named(SIMPLE_HASHTABLE_NAMED *ht, SIMPLE_HASHTABLE_VALUE_TYPE *old_value, SIMPLE_HASHTABLE_VALUE_TYPE *new_value) {
    if(new_value == old_value)
        return;

    size_t old_value_index = simple_hashtable_sorted_binary_search_named(ht, old_value);
    assert(old_value_index < ht->sorted.used && ht->sorted.array[old_value_index] == old_value);

    int r = SIMPLE_HASHTABLE_SORT_FUNCTION(old_value, new_value);
    if(r == 0) {
        // Same value, so use the same index
        ht->sorted.array[old_value_index] = new_value;
        return;
    }

    size_t new_value_index = simple_hashtable_sorted_binary_search_named(ht, new_value);
    if(old_value_index == new_value_index) {
        // Not the same value, but still at the same index
        ht->sorted.array[old_value_index] = new_value;
        return;
    }
    else if (old_value_index < new_value_index) {
        // The old value is before the new value
        size_t shift_start = old_value_index + 1;
        size_t shift_end = new_value_index - 1;
        size_t shift_size = shift_end - old_value_index;

        memmove(&ht->sorted.array[old_value_index], &ht->sorted.array[shift_start], shift_size * sizeof(SIMPLE_HASHTABLE_VALUE_TYPE *));
        ht->sorted.array[shift_end] = new_value;
    }
    else {
        // The old value is after the new value
        size_t shift_start = new_value_index;
        size_t shift_end = old_value_index;
        size_t shift_size = shift_end - new_value_index;

        memmove(&ht->sorted.array[new_value_index + 1], &ht->sorted.array[shift_start], shift_size * sizeof(SIMPLE_HASHTABLE_VALUE_TYPE *));
        ht->sorted.array[new_value_index] = new_value;
    }
}

static inline SIMPLE_HASHTABLE_VALUE_TYPE **simple_hashtable_sorted_array_first_read_only_named(SIMPLE_HASHTABLE_NAMED *ht) {
    if (ht->sorted.used > 0) {
        return &ht->sorted.array[0];
    }
    return NULL;
}

static inline SIMPLE_HASHTABLE_VALUE_TYPE **simple_hashtable_sorted_array_next_read_only_named(SIMPLE_HASHTABLE_NAMED *ht, SIMPLE_HASHTABLE_VALUE_TYPE **last) {
    if (!last) return NULL;

    // Calculate the current position in the sorted array
    size_t currentIndex = last - ht->sorted.array;

    // Proceed to the next element if it exists
    if (currentIndex + 1 < ht->sorted.used) {
        return &ht->sorted.array[currentIndex + 1];
    }

    // If no more elements, return NULL
    return NULL;
}

#define SIMPLE_HASHTABLE_SORTED_FOREACH_READ_ONLY(ht, var, type, name) \
    for (type **(var) = simple_hashtable_sorted_array_first_read_only ## name(ht); \
         var; \
         (var) = simple_hashtable_sorted_array_next_read_only ## name(ht, var))

#define SIMPLE_HASHTABLE_SORTED_FOREACH_READ_ONLY_VALUE(var) (*(var))

#else
static inline void simple_hashtable_add_value_sorted_named(SIMPLE_HASHTABLE_NAMED *ht __maybe_unused, SIMPLE_HASHTABLE_VALUE_TYPE *value __maybe_unused) { ; }
static inline void simple_hashtable_del_value_sorted_named(SIMPLE_HASHTABLE_NAMED *ht __maybe_unused, SIMPLE_HASHTABLE_VALUE_TYPE *value __maybe_unused) { ; }
static inline void simple_hashtable_replace_value_sorted_named(SIMPLE_HASHTABLE_NAMED *ht __maybe_unused, SIMPLE_HASHTABLE_VALUE_TYPE *old_value __maybe_unused, SIMPLE_HASHTABLE_VALUE_TYPE *new_value __maybe_unused) { ; }
#endif

static void simple_hashtable_init_named(SIMPLE_HASHTABLE_NAMED *ht, size_t size) {
    memset(ht, 0, sizeof(*ht));
    ht->size = size;
    ht->hashtable = callocz(ht->size, sizeof(*ht->hashtable));
}

static void simple_hashtable_destroy_named(SIMPLE_HASHTABLE_NAMED *ht) {
#ifdef SIMPLE_HASHTABLE_SORT_FUNCTION
    freez(ht->sorted.array);
#endif

    freez(ht->hashtable);
    memset(ht, 0, sizeof(*ht));
}

static inline void simple_hashtable_resize_named(SIMPLE_HASHTABLE_NAMED *ht);

#define SHTS_DATA_UNSET ((void *)NULL)
#define SHTS_DATA_DELETED ((void *)0x01)
#define SHTS_DATA_USERNULL ((void *)0x02)
#define SHTS_IS_UNSET(sl) ((sl)->data == SHTS_DATA_UNSET)
#define SHTS_IS_DELETED(sl) ((sl)->data == SHTS_DATA_DELETED)
#define SHTS_IS_USERNULL(sl) ((sl)->data == SHTS_DATA_USERNULL)
#define SIMPLE_HASHTABLE_SLOT_DATA(sl) ((SHTS_IS_UNSET(sl) || SHTS_IS_DELETED(sl) || SHTS_IS_USERNULL(sl)) ? NULL : (sl)->data)
#define SIMPLE_HASHTABLE_SLOT_UNSET_OR_DELETED(sl) ((SHTS_IS_UNSET(sl) || SHTS_IS_DELETED(sl)) ? NULL : (sl)->data)

// IMPORTANT
// The pointer returned by this call is valid up to the next call of this function (or the resize one)
// If you need to cache something, cache the hash, not the slot pointer.
static inline SIMPLE_HASHTABLE_SLOT_NAMED *simple_hashtable_get_slot_named(SIMPLE_HASHTABLE_NAMED *ht, SIMPLE_HASHTABLE_HASH hash, bool resize) {
    ht->searches++;

    size_t slot;
    SIMPLE_HASHTABLE_SLOT_NAMED *sl;
    SIMPLE_HASHTABLE_SLOT_NAMED *deleted;

    slot = hash % ht->size;
    sl = &ht->hashtable[slot];
    deleted = SHTS_IS_DELETED(sl) ? sl : NULL;
    if(likely(!SIMPLE_HASHTABLE_SLOT_UNSET_OR_DELETED(sl) || sl->hash == hash))
        return (SHTS_IS_UNSET(sl) && deleted) ? deleted : sl;

    ht->collisions++;

    if(unlikely(resize && (ht->size <= (ht->used << 1) || ht->used >= ht->size))) {
        simple_hashtable_resize_named(ht);

        slot = hash % ht->size;
        sl = &ht->hashtable[slot];
        deleted = (!deleted && SHTS_IS_DELETED(sl)) ? sl : deleted;
        if(likely(!SIMPLE_HASHTABLE_SLOT_UNSET_OR_DELETED(sl) || sl->hash == hash))
            return (SHTS_IS_UNSET(sl) && deleted) ? deleted : sl;

        ht->collisions++;
    }

    slot = ((hash >> SIMPLE_HASHTABLE_HASH_SECOND_HASH_SHIFTS) + 1) % ht->size;
    sl = &ht->hashtable[slot];
    deleted = (!deleted && SHTS_IS_DELETED(sl)) ? sl : deleted;

    // Linear probing until we find it
    while (SIMPLE_HASHTABLE_SLOT_UNSET_OR_DELETED(sl) && sl->hash != hash) {
        slot = (slot + 1) % ht->size;  // Wrap around if necessary
        sl = &ht->hashtable[slot];
        deleted = (!deleted && SHTS_IS_DELETED(sl)) ? sl : deleted;
        ht->collisions++;
    }

    return (SHTS_IS_UNSET(sl) && deleted) ? deleted : sl;
}

static inline bool simple_hashtable_del_slot_named(SIMPLE_HASHTABLE_NAMED *ht, SIMPLE_HASHTABLE_SLOT_NAMED *sl) {
    if(SHTS_IS_UNSET(sl) || SHTS_IS_DELETED(sl))
        return false;

    ht->deletions++;
    ht->deleted++;

    simple_hashtable_del_value_sorted_named(ht, SIMPLE_HASHTABLE_SLOT_DATA(sl));

    sl->data = SHTS_DATA_DELETED;
    return true;
}

static inline void simple_hashtable_set_slot_named(SIMPLE_HASHTABLE_NAMED *ht, SIMPLE_HASHTABLE_SLOT_NAMED *sl, SIMPLE_HASHTABLE_HASH hash, SIMPLE_HASHTABLE_VALUE_TYPE *data) {
    if(data == NULL)
        data = SHTS_DATA_USERNULL;

    if(unlikely(data == SHTS_DATA_UNSET || data == SHTS_DATA_DELETED)) {
        simple_hashtable_del_slot_named(ht, sl);
        return;
    }

    if(likely(SHTS_IS_UNSET(sl))) {
        simple_hashtable_add_value_sorted_named(ht, data);
        ht->used++;
    }

    else if(unlikely(SHTS_IS_DELETED(sl))) {
        ht->deleted--;
    }

    else
        simple_hashtable_replace_value_sorted_named(ht, SIMPLE_HASHTABLE_SLOT_DATA(sl), data);

    sl->hash = hash;
    sl->data = data;
}

// IMPORTANT
// this call invalidates all SIMPLE_HASHTABLE_SLOT_NAMED pointers
static inline void simple_hashtable_resize_named(SIMPLE_HASHTABLE_NAMED *ht) {
    SIMPLE_HASHTABLE_SLOT_NAMED *old = ht->hashtable;
    size_t old_size = ht->size;

    ht->resizes++;
    ht->size = (ht->size << 1) - ((ht->size > 16) ? 1 : 0);
    ht->hashtable = callocz(ht->size, sizeof(*ht->hashtable));
    ht->used = ht->deleted = 0;
    for(size_t i = 0 ; i < old_size ; i++) {
        if(!SIMPLE_HASHTABLE_SLOT_UNSET_OR_DELETED(&old[i]))
            continue;

        SIMPLE_HASHTABLE_SLOT_NAMED *slot = simple_hashtable_get_slot_named(ht, old[i].hash, false);
        *slot = old[i];
        ht->used++;
    }

    freez(old);
}

// ----------------------------------------------------------------------------
// hashtable traversal, in read-only mode
// the hashtable should not be modified while the traversal is taking place

static inline SIMPLE_HASHTABLE_SLOT_NAMED *simple_hashtable_first_read_only_named(SIMPLE_HASHTABLE_NAMED *ht) {
    for(size_t i = 0; i < ht->used ;i++) {
        SIMPLE_HASHTABLE_SLOT_NAMED *sl = &ht->hashtable[i];
        if(!SIMPLE_HASHTABLE_SLOT_UNSET_OR_DELETED(sl))
            return sl;
    }

    return NULL;
}

static inline SIMPLE_HASHTABLE_SLOT_NAMED *simple_hashtable_next_read_only_named(SIMPLE_HASHTABLE_NAMED *ht, SIMPLE_HASHTABLE_SLOT_NAMED *last) {
    if (!last) return NULL;

    // Calculate the current position in the array
    size_t currentIndex = last - ht->hashtable;

    // Iterate over the hashtable starting from the next element
    for (size_t i = currentIndex + 1; i < ht->size; i++) {
        SIMPLE_HASHTABLE_SLOT_NAMED *sl = &ht->hashtable[i];
        if (!SIMPLE_HASHTABLE_SLOT_UNSET_OR_DELETED(sl)) {
            return sl;
        }
    }

    // If no more data slots are found, return NULL
    return NULL;
}

#define SIMPLE_HASHTABLE_FOREACH_READ_ONLY(ht, var, name) \
    for(struct simple_hashtable_slot ## name *(var) = simple_hashtable_first_read_only ## name(ht); \
    var;                                                                                                             \
    (var) = simple_hashtable_next_read_only ## name(ht, var))

#define SIMPLE_HASHTABLE_FOREACH_READ_ONLY_VALUE(var) SIMPLE_HASHTABLE_SLOT_DATA(var)

// ----------------------------------------------------------------------------
// high level implementation

#ifdef SIMPLE_HASHTABLE_SAMPLE_IMPLEMENTATION

#define simple_hashtable_set_named CONCAT(simple_hashtable_set, SIMPLE_HASHTABLE_NAME)
#define simple_hashtable_get_named CONCAT(simple_hashtable_get, SIMPLE_HASHTABLE_NAME)
#define simple_hashtable_del_named CONCAT(simple_hashtable_del, SIMPLE_HASHTABLE_NAME)

static inline SIMPLE_HASHTABLE_VALUE_TYPE *simple_hashtable_set_named(SIMPLE_HASHTABLE_NAMED *ht, void *key, size_t key_len, SIMPLE_HASHTABLE_VALUE_TYPE *data) {
    XXH64_hash_t hash = XXH3_64bits(key, key_len);
    SIMPLE_HASHTABLE_SLOT_NAMED *sl = simple_hashtable_get_slot_named(ht, hash, true);
    simple_hashtable_set_slot_named(ht, sl, hash, data);
    return SIMPLE_HASHTABLE_SLOT_DATA(sl);
}

static inline SIMPLE_HASHTABLE_VALUE_TYPE *simple_hashtable_get_named(SIMPLE_HASHTABLE_NAMED *ht, void *key, size_t key_len, SIMPLE_HASHTABLE_VALUE_TYPE *data) {
    XXH64_hash_t hash = XXH3_64bits(key, key_len);
    SIMPLE_HASHTABLE_SLOT_NAMED *sl = simple_hashtable_get_slot_named(ht, hash, true);
    return SIMPLE_HASHTABLE_SLOT_DATA(sl);
}

static inline bool simple_hashtable_del_named(SIMPLE_HASHTABLE_NAMED *ht, void *key, size_t key_len, SIMPLE_HASHTABLE_VALUE_TYPE *data) {
    XXH64_hash_t hash = XXH3_64bits(key, key_len);
    SIMPLE_HASHTABLE_SLOT_NAMED *sl = simple_hashtable_get_slot_named(ht, hash, true);
    return simple_hashtable_del_slot_named(ht, sl);
}

#endif // SIMPLE_HASHTABLE_SAMPLE_IMPLEMENTATION

#endif //NETDATA_SIMPLE_HASHTABLE_H