ucx/cx/map.h

branch
dav-2
changeset 886
da79af4baec8
parent 854
1c8401ece69e
child 889
42cdbf9bbd49
equal deleted inserted replaced
885:591377a27fa3 886:da79af4baec8
183 */ 183 */
184 void (*clear)(struct cx_map_s *map); 184 void (*clear)(struct cx_map_s *map);
185 185
186 /** 186 /**
187 * Add or overwrite an element. 187 * Add or overwrite an element.
188 */ 188 * If the @p value is @c NULL, the implementation
189 int (*put)( 189 * 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.
191 */
192 void *(*put)(
190 CxMap *map, 193 CxMap *map,
191 CxHashKey key, 194 CxHashKey key,
192 void *value 195 void *value
193 ); 196 );
194 197
275 * respective elements are stored. 278 * respective elements are stored.
276 * 279 *
277 * @note An iterator iterates over all elements successively. Therefore, the order 280 * @note An iterator iterates over all elements successively. Therefore, the order
278 * highly depends on the map implementation and may change arbitrarily when the contents change. 281 * highly depends on the map implementation and may change arbitrarily when the contents change.
279 * 282 *
280 * @param map the map to create the iterator for 283 * @param map the map to create the iterator for (can be @c NULL)
281 * @return an iterator for the currently stored values 284 * @return an iterator for the currently stored values
282 */ 285 */
283 cx_attr_nonnull
284 cx_attr_nodiscard 286 cx_attr_nodiscard
285 static inline CxMapIterator cxMapIteratorValues(const CxMap *map) { 287 static inline CxMapIterator cxMapIteratorValues(const CxMap *map) {
288 if (map == NULL) map = cxEmptyMap;
286 return map->cl->iterator(map, CX_MAP_ITERATOR_VALUES); 289 return map->cl->iterator(map, CX_MAP_ITERATOR_VALUES);
287 } 290 }
288 291
289 /** 292 /**
290 * Creates a key iterator for a map. 293 * Creates a key iterator for a map.
293 * during iterator shall be treated as @c const @c CxHashKey* . 296 * during iterator shall be treated as @c const @c CxHashKey* .
294 * 297 *
295 * @note An iterator iterates over all elements successively. Therefore, the order 298 * @note An iterator iterates over all elements successively. Therefore, the order
296 * highly depends on the map implementation and may change arbitrarily when the contents change. 299 * highly depends on the map implementation and may change arbitrarily when the contents change.
297 * 300 *
298 * @param map the map to create the iterator for 301 * @param map the map to create the iterator for (can be @c NULL)
299 * @return an iterator for the currently stored keys 302 * @return an iterator for the currently stored keys
300 */ 303 */
301 cx_attr_nonnull
302 cx_attr_nodiscard 304 cx_attr_nodiscard
303 static inline CxMapIterator cxMapIteratorKeys(const CxMap *map) { 305 static inline CxMapIterator cxMapIteratorKeys(const CxMap *map) {
306 if (map == NULL) map = cxEmptyMap;
304 return map->cl->iterator(map, CX_MAP_ITERATOR_KEYS); 307 return map->cl->iterator(map, CX_MAP_ITERATOR_KEYS);
305 } 308 }
306 309
307 /** 310 /**
308 * Creates an iterator for a map. 311 * Creates an iterator for a map.
311 * during iterator shall be treated as @c const @c CxMapEntry* . 314 * during iterator shall be treated as @c const @c CxMapEntry* .
312 * 315 *
313 * @note An iterator iterates over all elements successively. Therefore, the order 316 * @note An iterator iterates over all elements successively. Therefore, the order
314 * highly depends on the map implementation and may change arbitrarily when the contents change. 317 * highly depends on the map implementation and may change arbitrarily when the contents change.
315 * 318 *
316 * @param map the map to create the iterator for 319 * @param map the map to create the iterator for (can be @c NULL)
317 * @return an iterator for the currently stored entries 320 * @return an iterator for the currently stored entries
318 * @see cxMapIteratorKeys() 321 * @see cxMapIteratorKeys()
319 * @see cxMapIteratorValues() 322 * @see cxMapIteratorValues()
320 */ 323 */
321 cx_attr_nonnull
322 cx_attr_nodiscard 324 cx_attr_nodiscard
323 static inline CxMapIterator cxMapIterator(const CxMap *map) { 325 static inline CxMapIterator cxMapIterator(const CxMap *map) {
326 if (map == NULL) map = cxEmptyMap;
324 return map->cl->iterator(map, CX_MAP_ITERATOR_PAIRS); 327 return map->cl->iterator(map, CX_MAP_ITERATOR_PAIRS);
325 } 328 }
326 329
327 330
328 /** 331 /**
333 * respective elements are stored. 336 * respective elements are stored.
334 * 337 *
335 * @note An iterator iterates over all elements successively. Therefore, the order 338 * @note An iterator iterates over all elements successively. Therefore, the order
336 * highly depends on the map implementation and may change arbitrarily when the contents change. 339 * highly depends on the map implementation and may change arbitrarily when the contents change.
337 * 340 *
338 * @param map the map to create the iterator for 341 * @param map the map to create the iterator for (can be @c NULL)
339 * @return an iterator for the currently stored values 342 * @return an iterator for the currently stored values
340 */ 343 */
341 cx_attr_nonnull
342 cx_attr_nodiscard 344 cx_attr_nodiscard
343 cx_attr_export 345 cx_attr_export
344 CxMapIterator cxMapMutIteratorValues(CxMap *map); 346 CxMapIterator cxMapMutIteratorValues(CxMap *map);
345 347
346 /** 348 /**
350 * during iterator shall be treated as @c const @c CxHashKey* . 352 * during iterator shall be treated as @c const @c CxHashKey* .
351 * 353 *
352 * @note An iterator iterates over all elements successively. Therefore, the order 354 * @note An iterator iterates over all elements successively. Therefore, the order
353 * highly depends on the map implementation and may change arbitrarily when the contents change. 355 * highly depends on the map implementation and may change arbitrarily when the contents change.
354 * 356 *
355 * @param map the map to create the iterator for 357 * @param map the map to create the iterator for (can be @c NULL)
356 * @return an iterator for the currently stored keys 358 * @return an iterator for the currently stored keys
357 */ 359 */
358 cx_attr_nonnull
359 cx_attr_nodiscard 360 cx_attr_nodiscard
360 cx_attr_export 361 cx_attr_export
361 CxMapIterator cxMapMutIteratorKeys(CxMap *map); 362 CxMapIterator cxMapMutIteratorKeys(CxMap *map);
362 363
363 /** 364 /**
367 * during iterator shall be treated as @c const @c CxMapEntry* . 368 * during iterator shall be treated as @c const @c CxMapEntry* .
368 * 369 *
369 * @note An iterator iterates over all elements successively. Therefore, the order 370 * @note An iterator iterates over all elements successively. Therefore, the order
370 * highly depends on the map implementation and may change arbitrarily when the contents change. 371 * highly depends on the map implementation and may change arbitrarily when the contents change.
371 * 372 *
372 * @param map the map to create the iterator for 373 * @param map the map to create the iterator for (can be @c NULL)
373 * @return an iterator for the currently stored entries 374 * @return an iterator for the currently stored entries
374 * @see cxMapMutIteratorKeys() 375 * @see cxMapMutIteratorKeys()
375 * @see cxMapMutIteratorValues() 376 * @see cxMapMutIteratorValues()
376 */ 377 */
377 cx_attr_nonnull
378 cx_attr_nodiscard 378 cx_attr_nodiscard
379 cx_attr_export 379 cx_attr_export
380 CxMapIterator cxMapMutIterator(CxMap *map); 380 CxMapIterator cxMapMutIterator(CxMap *map);
381 381
382 #ifdef __cplusplus 382 #ifdef __cplusplus
385 static inline int cxMapPut( 385 static inline int cxMapPut(
386 CxMap *map, 386 CxMap *map,
387 CxHashKey const &key, 387 CxHashKey const &key,
388 void *value 388 void *value
389 ) { 389 ) {
390 return map->cl->put(map, key, value); 390 return map->cl->put(map, key, value) == NULL;
391 } 391 }
392 392
393 cx_attr_nonnull 393 cx_attr_nonnull
394 static inline int cxMapPut( 394 static inline int cxMapPut(
395 CxMap *map, 395 CxMap *map,
396 cxstring const &key, 396 cxstring const &key,
397 void *value 397 void *value
398 ) { 398 ) {
399 return map->cl->put(map, cx_hash_key_cxstr(key), value); 399 return map->cl->put(map, cx_hash_key_cxstr(key), value) == NULL;
400 } 400 }
401 401
402 cx_attr_nonnull 402 cx_attr_nonnull
403 static inline int cxMapPut( 403 static inline int cxMapPut(
404 CxMap *map, 404 CxMap *map,
405 cxmutstr const &key, 405 cxmutstr const &key,
406 void *value 406 void *value
407 ) { 407 ) {
408 return map->cl->put(map, cx_hash_key_cxstr(key), value); 408 return map->cl->put(map, cx_hash_key_cxstr(key), value) == NULL;
409 } 409 }
410 410
411 cx_attr_nonnull 411 cx_attr_nonnull
412 cx_attr_cstr_arg(2) 412 cx_attr_cstr_arg(2)
413 static inline int cxMapPut( 413 static inline int cxMapPut(
414 CxMap *map, 414 CxMap *map,
415 const char *key, 415 const char *key,
416 void *value 416 void *value
417 ) { 417 ) {
418 return map->cl->put(map, cx_hash_key_str(key), value); 418 return map->cl->put(map, cx_hash_key_str(key), value) == NULL;
419 }
420
421 cx_attr_nonnull
422 static inline void *cxMapEmplace(
423 CxMap *map,
424 CxHashKey const &key
425 ) {
426 return map->cl->put(map, key, NULL);
427 }
428
429 cx_attr_nonnull
430 static inline void *cxMapEmplace(
431 CxMap *map,
432 cxstring const &key
433 ) {
434 return map->cl->put(map, cx_hash_key_cxstr(key), NULL);
435 }
436
437 cx_attr_nonnull
438 static inline void *cxMapEmplace(
439 CxMap *map,
440 cxmutstr const &key
441 ) {
442 return map->cl->put(map, cx_hash_key_cxstr(key), NULL);
443 }
444
445 cx_attr_nonnull
446 cx_attr_cstr_arg(2)
447 static inline void *cxMapEmplace(
448 CxMap *map,
449 const char *key
450 ) {
451 return map->cl->put(map, cx_hash_key_str(key), NULL);
419 } 452 }
420 453
421 cx_attr_nonnull 454 cx_attr_nonnull
422 cx_attr_nodiscard 455 cx_attr_nodiscard
423 static inline void *cxMapGet( 456 static inline void *cxMapGet(
538 static inline int cx_map_put( 571 static inline int cx_map_put(
539 CxMap *map, 572 CxMap *map,
540 CxHashKey key, 573 CxHashKey key,
541 void *value 574 void *value
542 ) { 575 ) {
543 return map->cl->put(map, key, value); 576 return map->cl->put(map, key, value) == NULL;
544 } 577 }
545 578
546 /** 579 /**
547 * @copydoc cxMapPut() 580 * @copydoc cxMapPut()
548 */ 581 */
550 static inline int cx_map_put_cxstr( 583 static inline int cx_map_put_cxstr(
551 CxMap *map, 584 CxMap *map,
552 cxstring key, 585 cxstring key,
553 void *value 586 void *value
554 ) { 587 ) {
555 return map->cl->put(map, cx_hash_key_cxstr(key), value); 588 return map->cl->put(map, cx_hash_key_cxstr(key), value) == NULL;
556 } 589 }
557 590
558 /** 591 /**
559 * @copydoc cxMapPut() 592 * @copydoc cxMapPut()
560 */ 593 */
562 static inline int cx_map_put_mustr( 595 static inline int cx_map_put_mustr(
563 CxMap *map, 596 CxMap *map,
564 cxmutstr key, 597 cxmutstr key,
565 void *value 598 void *value
566 ) { 599 ) {
567 return map->cl->put(map, cx_hash_key_cxstr(key), value); 600 return map->cl->put(map, cx_hash_key_cxstr(key), value) == NULL;
568 } 601 }
569 602
570 /** 603 /**
571 * @copydoc cxMapPut() 604 * @copydoc cxMapPut()
572 */ 605 */
575 static inline int cx_map_put_str( 608 static inline int cx_map_put_str(
576 CxMap *map, 609 CxMap *map,
577 const char *key, 610 const char *key,
578 void *value 611 void *value
579 ) { 612 ) {
580 return map->cl->put(map, cx_hash_key_str(key), value); 613 return map->cl->put(map, cx_hash_key_str(key), value) == NULL;
581 } 614 }
582 615
583 /** 616 /**
584 * Puts a key/value-pair into the map. 617 * Puts a key/value-pair into the map.
585 * 618 *
606 char*: cx_map_put_str, \ 639 char*: cx_map_put_str, \
607 const char*: cx_map_put_str) \ 640 const char*: cx_map_put_str) \
608 (map, key, value) 641 (map, key, value)
609 642
610 /** 643 /**
644 * @copydoc cxMapEmplace()
645 */
646 cx_attr_nonnull
647 static inline void *cx_map_emplace(
648 CxMap *map,
649 CxHashKey key
650 ) {
651 return map->cl->put(map, key, NULL);
652 }
653
654 /**
655 * @copydoc cxMapEmplace()
656 */
657 cx_attr_nonnull
658 static inline void *cx_map_emplace_cxstr(
659 CxMap *map,
660 cxstring key
661 ) {
662 return map->cl->put(map, cx_hash_key_cxstr(key), NULL);
663 }
664
665 /**
666 * @copydoc cxMapEmplace()
667 */
668 cx_attr_nonnull
669 static inline void *cx_map_emplace_mustr(
670 CxMap *map,
671 cxmutstr key
672 ) {
673 return map->cl->put(map, cx_hash_key_cxstr(key), NULL);
674 }
675
676 /**
677 * @copydoc cxMapEmplace()
678 */
679 cx_attr_nonnull
680 cx_attr_cstr_arg(2)
681 static inline void *cx_map_emplace_str(
682 CxMap *map,
683 const char *key
684 ) {
685 return map->cl->put(map, cx_hash_key_str(key), NULL);
686 }
687
688 /**
689 * Allocates memory for a value in the map associated with the specified key.
690 *
691 * A possible existing value will be overwritten.
692 * If destructor functions are specified, they are called for
693 * the overwritten element.
694 *
695 * If the map is storing pointers, this function returns a @c void** pointer,
696 * meaning a pointer to that pointer.
697 *
698 * The @p key is always copied.
699 *
700 * @param map (@c CxMap*) the map
701 * @param key (@c CxHashKey, @c char*, @c cxstring, or @c cxmutstr) the key
702 * @return the pointer to the allocated memory or @c NULL if allocation fails
703 * @retval zero success
704 * @retval non-zero value on memory allocation failure
705 */
706 #define cxMapEmplace(map, key) _Generic((key), \
707 CxHashKey: cx_map_emplace, \
708 cxstring: cx_map_emplace_cxstr, \
709 cxmutstr: cx_map_emplace_mustr, \
710 char*: cx_map_emplace_str, \
711 const char*: cx_map_emplace_str) \
712 (map, key)
713
714 /**
611 * @copydoc cxMapGet() 715 * @copydoc cxMapGet()
612 */ 716 */
613 cx_attr_nonnull 717 cx_attr_nonnull
614 cx_attr_nodiscard 718 cx_attr_nodiscard
615 static inline void *cx_map_get( 719 static inline void *cx_map_get(

mercurial