X-Git-Url: https://pd.if.org/git/?p=nbds;a=blobdiff_plain;f=map%2Fmap.c;h=6305c4502e20c13d6ff921e818d397271df175c7;hp=cb761ef3cddfb7b28f2dd80c67157e8bdcfb1033;hb=HEAD;hpb=8143ca0acc36e19d004431952e3b6f9b3d337f49 diff --git a/map/map.c b/map/map.c index cb761ef..6305c45 100644 --- a/map/map.c +++ b/map/map.c @@ -1,4 +1,4 @@ -/* +/* * Written by Josh Dybnis and released to the public domain, as explained at * http://creativecommons.org/licenses/publicdomain * @@ -6,20 +6,23 @@ */ #include "common.h" -#include "mlocal.h" -#include "mem.h" #include "map.h" +#include "mem.h" struct map { const map_impl_t *impl; void *data; }; -map_t *map_alloc (map_type_t map_type) { - const map_impl_t *map_impl = map_type; - map_t *map = nbd_malloc(sizeof(map_t)); +struct map_iter { + const map_impl_t *impl; + void *state; +}; + +map_t *map_alloc (const map_impl_t *map_impl, const datatype_t *key_type) { + map_t *map = nbd_malloc(sizeof(map_t)); map->impl = map_impl; - map->data = map->impl->alloc(); + map->data = map->impl->alloc(key_type); return map; } @@ -27,34 +30,50 @@ void map_free (map_t *map) { map->impl->free_(map->data); } -void map_print (map_t *map) { - map->impl->print(map->data); +void map_print (map_t *map, int verbose) { + map->impl->print(map->data, verbose); } -uint64_t map_count (map_t *map) { +map_val_t map_count (map_t *map) { return map->impl->count(map->data); } -uint64_t map_get (map_t *map, const void *key_data, uint32_t key_len) { - return map->impl->get(map->data, key_data, key_len); +map_val_t map_get (map_t *map, map_key_t key) { + return map->impl->get(map->data, key); +} + +map_val_t map_set (map_t *map, map_key_t key, map_val_t new_val) { + return map->impl->cas(map->data, key, CAS_EXPECT_WHATEVER, new_val); +} + +map_val_t map_add (map_t *map, map_key_t key, map_val_t new_val) { + return map->impl->cas(map->data, key, CAS_EXPECT_DOES_NOT_EXIST, new_val); +} + +map_val_t map_cas (map_t *map, map_key_t key, map_val_t expected_val, map_val_t new_val) { + return map->impl->cas(map->data, key, expected_val, new_val); } -uint64_t map_set (map_t *map, const void *key_data, uint32_t key_len, uint64_t new_val) { - return map->impl->cas(map->data, key_data, key_len, CAS_EXPECT_WHATEVER, new_val); +map_val_t map_replace(map_t *map, map_key_t key, map_val_t new_val) { + return map->impl->cas(map->data, key, CAS_EXPECT_EXISTS, new_val); } -uint64_t map_add (map_t *map, const void *key_data, uint32_t key_len, uint64_t new_val) { - return map->impl->cas(map->data, key_data, key_len, CAS_EXPECT_DOES_NOT_EXIST, new_val); +map_val_t map_remove (map_t *map, map_key_t key) { + return map->impl->remove(map->data, key); } -uint64_t map_cas (map_t *map, const void *key_data, uint32_t key_len, uint64_t expected_val, uint64_t new_val) { - return map->impl->cas(map->data, key_data, key_len, expected_val, new_val); +map_iter_t * map_iter_begin (map_t *map, map_key_t key) { + map_iter_t *iter = nbd_malloc(sizeof(map_iter_t)); + iter->impl = map->impl; + iter->state = map->impl->iter_begin(map->data, key); + return iter; } -uint64_t map_replace(map_t *map, const void *key_data, uint32_t key_len, uint64_t new_val) { - return map->impl->cas(map->data, key_data, key_len, CAS_EXPECT_EXISTS, new_val); +map_val_t map_iter_next (map_iter_t *iter, map_key_t *key_ptr) { + return iter->impl->iter_next(iter->state, key_ptr); } -uint64_t map_remove (map_t *map, const void *key_data, uint32_t key_len) { - return map->impl->remove(map->data, key_data, key_len); +void map_iter_free (map_iter_t *iter) { + iter->impl->iter_free(iter->state); + nbd_free(iter); }