summaryrefslogtreecommitdiff
path: root/src/map.h
blob: f0d10caf74613991e50347a77643f5ee0531a6b7 (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
#pragma once

#include "common.h"
#include "smrt_arena.h"

// Hashmap structure that stores u8* keys and void* values.
// API: map_create, map_insert, map_delete, map_lookup
// There is no map_destroy. It is allocated on the arena
// Uses a5hash to hash keys.
typedef struct _map_t* map_t;

typedef enum {
    MapInsertResultSuccessNew = 0,
    MapInsertResultSuccessPreexisted = 1,
    MapInsertResultFailedFull = -1,
} MapInsertResultK;

typedef enum {
    MapDeleteResultSuccess = 0,
    MapDeleteResultNotFound = -1,
} MapDeleteResultK;

typedef enum {
    MapChangeKeyPtrResultSuccess = 0,
    MapChangeKeyPtrResultKeyNotPresent = -1,
} MapChangeKeyPtrResultK;

map_t map_create(smrt_arena_t *arena, u64 table_size);

MapInsertResultK map_insert(map_t map, u8 const *key, u64 keylen, void *value);

MapDeleteResultK map_delete(map_t map, u8 const *key, u64 keylen);
void map_clear(map_t map);

void *map_lookup(map_t const map, u8 const *key, u64 keylen);

#define map_lookup_or(map, key, keylen, default_val)   \
    ({                                                 \
        void *val = map_lookup((map), (key), (keylen); \
        (val != NULL) ? val : (default_val);           \
    })

MapChangeKeyPtrResultK map_change_key_ptr(map_t map, u8 const *new_but_identical_key, u64 keylen);