src/server/ucx/map.h

changeset 66
74babc0082b7
parent 51
b28cf69f42e8
child 67
50505dc3f8a6
equal deleted inserted replaced
65:14722c5f8856 66:74babc0082b7
1 /* 1 /*
2 * 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2013 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
3 */ 27 */
4 28
5 #ifndef MAP_H 29 #ifndef MAP_H
6 #define MAP_H 30 #define MAP_H
7 31
13 #ifdef __cplusplus 37 #ifdef __cplusplus
14 extern "C" { 38 extern "C" {
15 #endif 39 #endif
16 40
17 #define UCX_MAP_FOREACH(elm,iter) \ 41 #define UCX_MAP_FOREACH(elm,iter) \
18 for(;ucx_map_iter_next(&iter,(void*)&elm)==0;) 42 for(;ucx_map_iter_next(&iter,(void**)&elm)==0;)
19 43
20 typedef struct UcxMap UcxMap; 44 typedef struct UcxMap UcxMap;
21 typedef struct UcxKey UcxKey; 45 typedef struct UcxKey UcxKey;
22 typedef struct UcxMapElement UcxMapElement; 46 typedef struct UcxMapElement UcxMapElement;
23 typedef struct UcxMapIterator UcxMapIterator; 47 typedef struct UcxMapIterator UcxMapIterator;
35 size_t size; 59 size_t size;
36 size_t count; 60 size_t count;
37 }; 61 };
38 62
39 struct UcxKey { 63 struct UcxKey {
40 void *data; 64 char *data;
41 size_t len; 65 size_t len;
42 int hash; 66 int hash;
43 }; 67 };
44 68
45 struct UcxMapElement { 69 struct UcxMapElement {
49 }; 73 };
50 74
51 struct UcxMapIterator { 75 struct UcxMapIterator {
52 UcxMap *map; 76 UcxMap *map;
53 UcxMapElement *cur; 77 UcxMapElement *cur;
54 int index; 78 size_t index;
55 }; 79 };
56 80
57 81
58 UcxMap *ucx_map_new(size_t size); 82 UcxMap *ucx_map_new(size_t size);
59 void ucx_map_free(UcxMap *map); 83 void ucx_map_free(UcxMap *map);
60 /* you cannot clone maps with more than 390 mio entries */ 84 /* you cannot clone maps with more than 390 mio entries */
61 int ucx_map_copy(UcxMap *from, UcxMap *to, copy_func fnc, void *data); 85 int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to,
86 copy_func fnc, void *data);
62 UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data); 87 UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data);
63 int ucx_map_rehash(UcxMap *map); 88 int ucx_map_rehash(UcxMap *map);
64 89
65 int ucx_map_put(UcxMap *map, UcxKey key, void *data); 90 int ucx_map_put(UcxMap *map, UcxKey key, void *data);
66 void* ucx_map_get(UcxMap *map, UcxKey key); 91 void* ucx_map_get(UcxMap *map, UcxKey key);
92 void* ucx_map_remove(UcxMap *map, UcxKey key);
67 93
68 #define ucx_map_sstr_put(m, s, d) ucx_map_put(m, ucx_key(s.ptr, s.length), d) 94 #define ucx_map_sstr_put(m, s, d) \
69 #define ucx_map_cstr_put(m, s, d) ucx_map_put(m, ucx_key(s, strlen(s)), d) 95 ucx_map_put(m, ucx_key(s.ptr, s.length), d)
70 #define ucx_map_sstr_get(m, s) ucx_map_get(m, ucx_key(s.ptr, s.length)) 96 #define ucx_map_cstr_put(m, s, d) \
71 #define ucx_map_cstr_get(m, s) ucx_map_get(m, ucx_key(s, strlen(s))) 97 ucx_map_put(m, ucx_key((void*)s, strlen(s)), d)
98 #define ucx_map_int_put(m, i, d) \
99 ucx_map_put(m, ucx_key((void*)&i, sizeof(i)), d)
100
101 #define ucx_map_sstr_get(m, s) \
102 ucx_map_get(m, ucx_key(s.ptr, s.length))
103 #define ucx_map_cstr_get(m, s) \
104 ucx_map_get(m, ucx_key((void*)s, strlen(s)))
105 #define ucx_map_int_get(m, i) \
106 ucx_map_get(m, ucx_key((void*)&i, sizeof(int)))
107
108 #define ucx_map_sstr_remove(m, s) \
109 ucx_map_remove(m, ucx_key(s.ptr, s.length))
110 #define ucx_map_cstr_remove(m, s) \
111 ucx_map_remove(m, ucx_key((void*)s, strlen(s)))
112 #define ucx_map_int_remove(m, i) \
113 ucx_map_remove(m, ucx_key((void*)&i, sizeof(i)))
72 114
73 UcxKey ucx_key(void *data, size_t len); 115 UcxKey ucx_key(void *data, size_t len);
74 116
75 int ucx_hash(char *data, size_t len); 117 int ucx_hash(const char *data, size_t len);
76 118
77 UcxMapIterator ucx_map_iterator(UcxMap *map); 119 UcxMapIterator ucx_map_iterator(UcxMap *map);
78 120
79 int ucx_map_iter_next(UcxMapIterator *i, void **elm); 121 int ucx_map_iter_next(UcxMapIterator *i, void **elm);
80 122

mercurial