]> pd.if.org Git - nbds/blob - map/map.c
cb761ef3cddfb7b28f2dd80c67157e8bdcfb1033
[nbds] / map / map.c
1 /* 
2  * Written by Josh Dybnis and released to the public domain, as explained at
3  * http://creativecommons.org/licenses/publicdomain
4  *
5  * generic interface for map-like data structures
6  */
7
8 #include "common.h"
9 #include "mlocal.h"
10 #include "mem.h"
11 #include "map.h"
12
13 struct map {
14     const map_impl_t *impl;
15     void *data;
16 };
17
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)); 
21     map->impl  = map_impl;
22     map->data  = map->impl->alloc();
23     return map;
24 }
25
26 void map_free (map_t *map) {
27     map->impl->free_(map->data);
28 }
29
30 void map_print (map_t *map) {
31     map->impl->print(map->data);
32 }
33
34 uint64_t map_count (map_t *map) {
35     return map->impl->count(map->data);
36 }
37
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);
40 }
41
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);
44 }
45
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);
48 }
49
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);
52 }
53
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);
56 }
57
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);
60 }