src/cx/map.h

changeset 563
69a83fad8a35
parent 558
9b767b07602c
child 564
5d8ad7a0ff71
equal deleted inserted replaced
562:fd3368c20413 563:69a83fad8a35
38 #define UCX_MAP_H 38 #define UCX_MAP_H
39 39
40 #include "common.h" 40 #include "common.h"
41 #include "allocator.h" 41 #include "allocator.h"
42 #include "iterator.h" 42 #include "iterator.h"
43 #include "hash_key.h"
43 44
44 #ifdef __cplusplus 45 #ifdef __cplusplus
45 extern "C" { 46 extern "C" {
46 #endif 47 #endif
47 48
85 * Add or overwrite an element. 86 * Add or overwrite an element.
86 */ 87 */
87 __attribute__((__nonnull__)) 88 __attribute__((__nonnull__))
88 int (*put)( 89 int (*put)(
89 CxMap *map, 90 CxMap *map,
90 CxDataPtr key, 91 CxHashKey key,
91 void *value 92 void *value
92 ); 93 );
93 94
94 /** 95 /**
95 * Returns an element. 96 * Returns an element.
96 */ 97 */
97 __attribute__((__nonnull__, __warn_unused_result__)) 98 __attribute__((__nonnull__, __warn_unused_result__))
98 void *(*get)( 99 void *(*get)(
99 CxMap const *map, 100 CxMap const *map,
100 CxDataPtr key 101 CxHashKey key
101 ); 102 );
102 103
103 /** 104 /**
104 * Removes an element. 105 * Removes an element.
105 */ 106 */
106 __attribute__((__nonnull__, __warn_unused_result__)) 107 __attribute__((__nonnull__, __warn_unused_result__))
107 void *(*remove)( 108 void *(*remove)(
108 CxMap *map, 109 CxMap *map,
109 CxDataPtr key 110 CxHashKey key
110 ); 111 );
111 112
112 /** 113 /**
113 * Iterator over the key/value pairs. 114 * Iterator over the key/value pairs.
114 */ 115 */
133 */ 134 */
134 struct cx_map_entry_s { 135 struct cx_map_entry_s {
135 /** 136 /**
136 * A pointer to the key. 137 * A pointer to the key.
137 */ 138 */
138 CxDataPtr const *key; 139 CxHashKey const *key;
139 /** 140 /**
140 * A pointer to the value. 141 * A pointer to the value.
141 */ 142 */
142 void *value; 143 void *value;
143 }; 144 };
174 * @return 0 on success, non-zero value on failure 175 * @return 0 on success, non-zero value on failure
175 */ 176 */
176 __attribute__((__nonnull__)) 177 __attribute__((__nonnull__))
177 static inline int cxMapPut( 178 static inline int cxMapPut(
178 CxMap *map, 179 CxMap *map,
179 CxDataPtr key, 180 CxHashKey key,
180 void *value 181 void *value
181 ) { 182 ) {
182 return map->cl->put(map, key, value); 183 return map->cl->put(map, key, value);
183 } 184 }
184 185
190 * @return the value 191 * @return the value
191 */ 192 */
192 __attribute__((__nonnull__, __warn_unused_result__)) 193 __attribute__((__nonnull__, __warn_unused_result__))
193 static inline void *cxMapGet( 194 static inline void *cxMapGet(
194 CxMap const *map, 195 CxMap const *map,
195 CxDataPtr key 196 CxHashKey key
196 ) { 197 ) {
197 return map->cl->get(map, key); 198 return map->cl->get(map, key);
198 } 199 }
199 200
200 /** 201 /**
205 * @return the removed value 206 * @return the removed value
206 */ 207 */
207 __attribute__((__nonnull__, __warn_unused_result__)) 208 __attribute__((__nonnull__, __warn_unused_result__))
208 static inline void *cxMapRemove( 209 static inline void *cxMapRemove(
209 CxMap *map, 210 CxMap *map,
210 CxDataPtr key 211 CxHashKey key
211 ) { 212 ) {
212 return map->cl->remove(map, key); 213 return map->cl->remove(map, key);
213 } 214 }
214 215
215 // TODO: set-like map operations (union, intersect, difference) 216 // TODO: set-like map operations (union, intersect, difference)
260 __attribute__((__nonnull__, __warn_unused_result__)) 261 __attribute__((__nonnull__, __warn_unused_result__))
261 static inline CxIterator cxMapIterator(CxMap *map) { 262 static inline CxIterator cxMapIterator(CxMap *map) {
262 return map->cl->iterator(map); 263 return map->cl->iterator(map);
263 } 264 }
264 265
265 /**
266 * Convenience function to make a key from a NULL-terminated string.
267 *
268 * @param str the NULL-terminated string
269 * @return the string wrapped to be used as a map key
270 */
271 __attribute__((__nonnull__, __warn_unused_result__))
272 CxDataPtr cxMapKeyStr(char const *str);
273
274 #ifdef __cplusplus 266 #ifdef __cplusplus
275 } 267 }
276 #endif 268 #endif
277 269
278 #endif // UCX_MAP_H 270 #endif // UCX_MAP_H

mercurial