ucx/cx/map.h

changeset 870
e167cf006213
parent 845
f3ab28ed22e5
child 943
9b5948aa5b90
equal deleted inserted replaced
869:6b7a178cff7c 870:e167cf006213
109 CX_ITERATOR_BASE; 109 CX_ITERATOR_BASE;
110 110
111 /** 111 /**
112 * Handle for the source map. 112 * Handle for the source map.
113 */ 113 */
114 union { 114 CxMap *map;
115 /**
116 * Access for mutating iterators.
117 */
118 CxMap *m;
119 /**
120 * Access for normal iterators.
121 */
122 const CxMap *c;
123 } map;
124 115
125 /** 116 /**
126 * Handle for the current element. 117 * Handle for the current element.
127 * 118 *
128 * @attention Depends on the map implementation, do not assume a type (better: do not use!). 119 * @attention Depends on the map implementation, do not assume a type (better: do not use!).
187 * Add or overwrite an element. 178 * Add or overwrite an element.
188 * If the @p value is @c NULL, the implementation 179 * If the @p value is @c NULL, the implementation
189 * shall only allocate memory instead of adding an existing value to the map. 180 * shall only allocate memory instead of adding an existing value to the map.
190 * Returns a pointer to the allocated memory or @c NULL if allocation fails. 181 * Returns a pointer to the allocated memory or @c NULL if allocation fails.
191 */ 182 */
192 void *(*put)( 183 void *(*put)(CxMap *map, CxHashKey key, void *value);
193 CxMap *map,
194 CxHashKey key,
195 void *value
196 );
197 184
198 /** 185 /**
199 * Returns an element. 186 * Returns an element.
200 */ 187 */
201 void *(*get)( 188 void *(*get)(const CxMap *map, CxHashKey key);
202 const CxMap *map,
203 CxHashKey key
204 );
205 189
206 /** 190 /**
207 * Removes an element. 191 * Removes an element.
208 * 192 *
209 * Implementations SHALL check if @p targetbuf is set and copy the elements 193 * Implementations SHALL check if @p targetbuf is set and copy the elements
211 * When @p targetbuf is not set, the destructors SHALL be invoked. 195 * When @p targetbuf is not set, the destructors SHALL be invoked.
212 * 196 *
213 * The function SHALL return zero when the @p key was found and 197 * The function SHALL return zero when the @p key was found and
214 * non-zero, otherwise. 198 * non-zero, otherwise.
215 */ 199 */
216 int (*remove)( 200 int (*remove)(CxMap *map, CxHashKey key, void *targetbuf);
217 CxMap *map,
218 CxHashKey key,
219 void *targetbuf
220 );
221 201
222 /** 202 /**
223 * Creates an iterator for this map. 203 * Creates an iterator for this map.
224 */ 204 */
225 CxMapIterator (*iterator)(const CxMap *map, enum cx_map_iterator_type type); 205 CxMapIterator (*iterator)(const CxMap *map, enum cx_map_iterator_type type);
231 * Writing to that map is not allowed. 211 * Writing to that map is not allowed.
232 * 212 *
233 * You can use this as a placeholder for initializing CxMap pointers 213 * You can use this as a placeholder for initializing CxMap pointers
234 * for which you do not want to reserve memory right from the beginning. 214 * for which you do not want to reserve memory right from the beginning.
235 */ 215 */
236 cx_attr_export 216 CX_EXPORT extern CxMap *const cxEmptyMap;
237 extern CxMap *const cxEmptyMap;
238 217
239 /** 218 /**
240 * Deallocates the memory of the specified map. 219 * Deallocates the memory of the specified map.
241 * 220 *
242 * Also calls the content destructor functions for each element, if specified. 221 * Also calls the content destructor functions for each element, if specified.
243 * 222 *
244 * @param map the map to be freed 223 * @param map the map to be freed
245 */ 224 */
246 cx_attr_export 225 CX_EXPORT void cxMapFree(CxMap *map);
247 void cxMapFree(CxMap *map);
248 226
249 227
250 /** 228 /**
251 * Clears a map by removing all elements. 229 * Clears a map by removing all elements.
252 * 230 *
253 * Also calls the content destructor functions for each element, if specified. 231 * Also calls the content destructor functions for each element, if specified.
254 * 232 *
255 * @param map the map to be cleared 233 * @param map the map to be cleared
256 */ 234 */
257 cx_attr_nonnull 235 cx_attr_nonnull
258 static inline void cxMapClear(CxMap *map) { 236 CX_EXPORT void cxMapClear(CxMap *map);
259 map->cl->clear(map);
260 }
261 237
262 /** 238 /**
263 * Returns the number of elements in this map. 239 * Returns the number of elements in this map.
264 * 240 *
265 * @param map the map 241 * @param map the map
266 * @return the number of stored elements 242 * @return the number of stored elements
267 */ 243 */
268 cx_attr_nonnull 244 cx_attr_nonnull
269 static inline size_t cxMapSize(const CxMap *map) { 245 CX_EXPORT size_t cxMapSize(const CxMap *map);
270 return map->collection.size;
271 }
272 246
273 /** 247 /**
274 * Creates a value iterator for a map. 248 * Creates a value iterator for a map.
275 * 249 *
276 * When the map is storing pointers, those pointers are returned. 250 * When the map is storing pointers, those pointers are returned.
282 * 256 *
283 * @param map the map to create the iterator for (can be @c NULL) 257 * @param map the map to create the iterator for (can be @c NULL)
284 * @return an iterator for the currently stored values 258 * @return an iterator for the currently stored values
285 */ 259 */
286 cx_attr_nodiscard 260 cx_attr_nodiscard
287 static inline CxMapIterator cxMapIteratorValues(const CxMap *map) { 261 CX_EXPORT CxMapIterator cxMapIteratorValues(const CxMap *map);
288 if (map == NULL) map = cxEmptyMap;
289 return map->cl->iterator(map, CX_MAP_ITERATOR_VALUES);
290 }
291 262
292 /** 263 /**
293 * Creates a key iterator for a map. 264 * Creates a key iterator for a map.
294 * 265 *
295 * The elements of the iterator are keys of type CxHashKey, and the pointer returned 266 * The elements of the iterator are keys of type CxHashKey, and the pointer returned
300 * 271 *
301 * @param map the map to create the iterator for (can be @c NULL) 272 * @param map the map to create the iterator for (can be @c NULL)
302 * @return an iterator for the currently stored keys 273 * @return an iterator for the currently stored keys
303 */ 274 */
304 cx_attr_nodiscard 275 cx_attr_nodiscard
305 static inline CxMapIterator cxMapIteratorKeys(const CxMap *map) { 276 CX_EXPORT CxMapIterator cxMapIteratorKeys(const CxMap *map);
306 if (map == NULL) map = cxEmptyMap;
307 return map->cl->iterator(map, CX_MAP_ITERATOR_KEYS);
308 }
309 277
310 /** 278 /**
311 * Creates an iterator for a map. 279 * Creates an iterator for a map.
312 * 280 *
313 * The elements of the iterator are key/value pairs of type CxMapEntry, and the pointer returned 281 * The elements of the iterator are key/value pairs of type CxMapEntry, and the pointer returned
320 * @return an iterator for the currently stored entries 288 * @return an iterator for the currently stored entries
321 * @see cxMapIteratorKeys() 289 * @see cxMapIteratorKeys()
322 * @see cxMapIteratorValues() 290 * @see cxMapIteratorValues()
323 */ 291 */
324 cx_attr_nodiscard 292 cx_attr_nodiscard
325 static inline CxMapIterator cxMapIterator(const CxMap *map) { 293 CX_EXPORT CxMapIterator cxMapIterator(const CxMap *map);
326 if (map == NULL) map = cxEmptyMap;
327 return map->cl->iterator(map, CX_MAP_ITERATOR_PAIRS);
328 }
329
330
331 /**
332 * Creates a mutating iterator over the values of a map.
333 *
334 * When the map is storing pointers, those pointers are returned.
335 * Otherwise, the iterator iterates over pointers to the memory within the map where the
336 * respective elements are stored.
337 *
338 * @note An iterator iterates over all elements successively. Therefore, the order
339 * highly depends on the map implementation and may change arbitrarily when the contents change.
340 *
341 * @param map the map to create the iterator for (can be @c NULL)
342 * @return an iterator for the currently stored values
343 */
344 cx_attr_nodiscard
345 cx_attr_export
346 CxMapIterator cxMapMutIteratorValues(CxMap *map);
347
348 /**
349 * Creates a mutating iterator over the keys of a map.
350 *
351 * The elements of the iterator are keys of type CxHashKey, and the pointer returned
352 * during iterator shall be treated as @c const @c CxHashKey* .
353 *
354 * @note An iterator iterates over all elements successively. Therefore, the order
355 * highly depends on the map implementation and may change arbitrarily when the contents change.
356 *
357 * @param map the map to create the iterator for (can be @c NULL)
358 * @return an iterator for the currently stored keys
359 */
360 cx_attr_nodiscard
361 cx_attr_export
362 CxMapIterator cxMapMutIteratorKeys(CxMap *map);
363
364 /**
365 * Creates a mutating iterator for a map.
366 *
367 * The elements of the iterator are key/value pairs of type CxMapEntry, and the pointer returned
368 * during iterator shall be treated as @c const @c CxMapEntry* .
369 *
370 * @note An iterator iterates over all elements successively. Therefore, the order
371 * highly depends on the map implementation and may change arbitrarily when the contents change.
372 *
373 * @param map the map to create the iterator for (can be @c NULL)
374 * @return an iterator for the currently stored entries
375 * @see cxMapMutIteratorKeys()
376 * @see cxMapMutIteratorValues()
377 */
378 cx_attr_nodiscard
379 cx_attr_export
380 CxMapIterator cxMapMutIterator(CxMap *map);
381 294
382 /** 295 /**
383 * Puts a key/value-pair into the map. 296 * Puts a key/value-pair into the map.
384 * 297 *
385 * A possible existing value will be overwritten. 298 * A possible existing value will be overwritten.
398 * @retval zero success 311 * @retval zero success
399 * @retval non-zero value on memory allocation failure 312 * @retval non-zero value on memory allocation failure
400 * @see cxMapPut() 313 * @see cxMapPut()
401 */ 314 */
402 cx_attr_nonnull 315 cx_attr_nonnull
403 static inline int cx_map_put( 316 CX_EXPORT int cx_map_put(CxMap *map, CxHashKey key, void *value);
404 CxMap *map,
405 CxHashKey key,
406 void *value
407 ) {
408 return map->cl->put(map, key, value) == NULL;
409 }
410 317
411 /** 318 /**
412 * Puts a key/value-pair into the map. 319 * Puts a key/value-pair into the map.
413 * 320 *
414 * A possible existing value will be overwritten. 321 * A possible existing value will be overwritten.
448 * @retval zero success 355 * @retval zero success
449 * @retval non-zero value on memory allocation failure 356 * @retval non-zero value on memory allocation failure
450 * @see cxMapEmplace() 357 * @see cxMapEmplace()
451 */ 358 */
452 cx_attr_nonnull 359 cx_attr_nonnull
453 static inline void *cx_map_emplace( 360 CX_EXPORT void *cx_map_emplace(CxMap *map, CxHashKey key);
454 CxMap *map,
455 CxHashKey key
456 ) {
457 return map->cl->put(map, key, NULL);
458 }
459 361
460 /** 362 /**
461 * Allocates memory for a value in the map associated with the specified key. 363 * Allocates memory for a value in the map associated with the specified key.
462 * 364 *
463 * A possible existing value will be overwritten. 365 * A possible existing value will be overwritten.
488 * @param map the map 390 * @param map the map
489 * @param key the key 391 * @param key the key
490 * @return the value 392 * @return the value
491 * @see cxMapGet() 393 * @see cxMapGet()
492 */ 394 */
493 cx_attr_nonnull 395 cx_attr_nonnull cx_attr_nodiscard
494 cx_attr_nodiscard 396 CX_EXPORT void *cx_map_get(const CxMap *map, CxHashKey key);
495 static inline void *cx_map_get(
496 const CxMap *map,
497 CxHashKey key
498 ) {
499 return map->cl->get(map, key);
500 }
501 397
502 /** 398 /**
503 * Retrieves a value by using a key. 399 * Retrieves a value by using a key.
504 * 400 *
505 * If this map is storing pointers, the stored pointer is returned. 401 * If this map is storing pointers, the stored pointer is returned.
527 * 423 *
528 * @see cxMapRemove() 424 * @see cxMapRemove()
529 * @see cxMapRemoveAndGet() 425 * @see cxMapRemoveAndGet()
530 */ 426 */
531 cx_attr_nonnull_arg(1) 427 cx_attr_nonnull_arg(1)
532 static inline int cx_map_remove( 428 CX_EXPORT int cx_map_remove(CxMap *map, CxHashKey key, void *targetbuf);
533 CxMap *map,
534 CxHashKey key,
535 void *targetbuf
536 ) {
537 return map->cl->remove(map, key, targetbuf);
538 }
539 429
540 /** 430 /**
541 * Removes a key/value-pair from the map by using the key. 431 * Removes a key/value-pair from the map by using the key.
542 * 432 *
543 * Always invokes the destructor functions, if any, on the removed element. 433 * Always invokes the destructor functions, if any, on the removed element.

mercurial