From e01d3f64d42e2fe88f737d4616424b1c5949ae85 Mon Sep 17 00:00:00 2001 From: steven-na Date: Fri, 2 Oct 2026 00:28:46 -0700 Subject: Improved map api --- src/map.h | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) (limited to 'src/map.h') diff --git a/src/map.h b/src/map.h index 6678ddd..f0d10ca 100644 --- a/src/map.h +++ b/src/map.h @@ -9,7 +9,35 @@ // Uses a5hash to hash keys. typedef struct _map_t* map_t; -map_t map_create(smrt_arena_t *arena, u64 table_size); - i32 map_insert(map_t const map, u8 const *key, u64 keylen, void *value); - i32 map_delete(map_t const map, u8 const *key, u64 keylen); - void *map_lookup(map_t const map, u8 const *key, u64 keylen); +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); -- cgit v1.2.3