2 * Written by Josh Dybnis and released to the public domain, as explained at
3 * http://creativecommons.org/licenses/publicdomain
5 * generic interface for map-like data structures
14 const map_impl_t *impl;
18 map_t *map_alloc (map_type_t map_type) {
19 const map_impl_t *map_impl = map_type;
20 map_t *map = nbd_malloc(sizeof(map_t));
22 map->data = map->impl->alloc();
26 void map_free (map_t *map) {
27 map->impl->free_(map->data);
30 void map_print (map_t *map) {
31 map->impl->print(map->data);
34 uint64_t map_count (map_t *map) {
35 return map->impl->count(map->data);
38 uint64_t map_get (map_t *map, const void *key_data, uint32_t key_len) {
39 return map->impl->get(map->data, key_data, key_len);
42 uint64_t map_set (map_t *map, const void *key_data, uint32_t key_len, uint64_t new_val) {
43 return map->impl->cas(map->data, key_data, key_len, CAS_EXPECT_WHATEVER, new_val);
46 uint64_t map_add (map_t *map, const void *key_data, uint32_t key_len, uint64_t new_val) {
47 return map->impl->cas(map->data, key_data, key_len, CAS_EXPECT_DOES_NOT_EXIST, new_val);
50 uint64_t map_cas (map_t *map, const void *key_data, uint32_t key_len, uint64_t expected_val, uint64_t new_val) {
51 return map->impl->cas(map->data, key_data, key_len, expected_val, new_val);
54 uint64_t map_replace(map_t *map, const void *key_data, uint32_t key_len, uint64_t new_val) {
55 return map->impl->cas(map->data, key_data, key_len, CAS_EXPECT_EXISTS, new_val);
58 uint64_t map_remove (map_t *map, const void *key_data, uint32_t key_len) {
59 return map->impl->remove(map->data, key_data, key_len);