ucx
UAP Common Extensions
Loading...
Searching...
No Matches
map.h
Go to the documentation of this file.
1/*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2021 Mike Becker, 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.
27 */
36#ifndef UCX_MAP_H
37#define UCX_MAP_H
38
39#include "common.h"
40#include "collection.h"
41#include "string.h"
42#include "hash_key.h"
43
44#ifdef __cplusplus
45extern "C" {
46#endif
47
49typedef struct cx_map_s CxMap;
50
53
56
59
69
77 const CxHashKey *key;
81 void *value;
82};
83
101
110
114 union {
122 const CxMap *c;
124
130 void *elem;
131
139
145 size_t slot;
146
151 size_t index;
152
156 size_t elem_size;
157
165
170};
171
179 void (*deallocate)(struct cx_map_s *map);
180
184 void (*clear)(struct cx_map_s *map);
185
189 int (*put)(
190 CxMap *map,
191 CxHashKey key,
192 void *value
193 );
194
198 void *(*get)(
199 const CxMap *map,
200 CxHashKey key
201 );
202
213 int (*remove)(
214 CxMap *map,
215 CxHashKey key,
216 void *targetbuf
217 );
218
223};
224
234extern CxMap *const cxEmptyMap;
235
244void cxMapFree(CxMap *map);
245
246
255static inline void cxMapClear(CxMap *map) {
256 map->cl->clear(map);
257}
258
266static inline size_t cxMapSize(const CxMap *map) {
267 return map->collection.size;
268}
269
285static inline CxMapIterator cxMapIteratorValues(const CxMap *map) {
286 return map->cl->iterator(map, CX_MAP_ITERATOR_VALUES);
287}
288
303static inline CxMapIterator cxMapIteratorKeys(const CxMap *map) {
304 return map->cl->iterator(map, CX_MAP_ITERATOR_KEYS);
305}
306
323static inline CxMapIterator cxMapIterator(const CxMap *map) {
324 return map->cl->iterator(map, CX_MAP_ITERATOR_PAIRS);
325}
326
327
345
362
381
382#ifdef __cplusplus
383} // end the extern "C" block here, because we want to start overloading
385static inline int cxMapPut(
386 CxMap *map,
387 CxHashKey const &key,
388 void *value
389) {
390 return map->cl->put(map, key, value);
391}
392
394static inline int cxMapPut(
395 CxMap *map,
396 cxstring const &key,
397 void *value
398) {
399 return map->cl->put(map, cx_hash_key_cxstr(key), value);
400}
401
403static inline int cxMapPut(
404 CxMap *map,
405 cxmutstr const &key,
406 void *value
407) {
408 return map->cl->put(map, cx_hash_key_cxstr(key), value);
409}
410
413static inline int cxMapPut(
414 CxMap *map,
415 const char *key,
416 void *value
417) {
418 return map->cl->put(map, cx_hash_key_str(key), value);
419}
420
423static inline void *cxMapGet(
424 const CxMap *map,
425 CxHashKey const &key
426) {
427 return map->cl->get(map, key);
428}
429
432static inline void *cxMapGet(
433 const CxMap *map,
434 cxstring const &key
435) {
436 return map->cl->get(map, cx_hash_key_cxstr(key));
437}
438
441static inline void *cxMapGet(
442 const CxMap *map,
443 cxmutstr const &key
444) {
445 return map->cl->get(map, cx_hash_key_cxstr(key));
446}
447
451static inline void *cxMapGet(
452 const CxMap *map,
453 const char *key
454) {
455 return map->cl->get(map, cx_hash_key_str(key));
456}
457
459static inline int cxMapRemove(
460 CxMap *map,
461 CxHashKey const &key
462) {
463 return map->cl->remove(map, key, nullptr);
464}
465
467static inline int cxMapRemove(
468 CxMap *map,
469 cxstring const &key
470) {
471 return map->cl->remove(map, cx_hash_key_cxstr(key), nullptr);
472}
473
475static inline int cxMapRemove(
476 CxMap *map,
477 cxmutstr const &key
478) {
479 return map->cl->remove(map, cx_hash_key_cxstr(key), nullptr);
480}
481
484static inline int cxMapRemove(
485 CxMap *map,
486 const char *key
487) {
488 return map->cl->remove(map, cx_hash_key_str(key), nullptr);
489}
490
493static inline int cxMapRemoveAndGet(
494 CxMap *map,
495 CxHashKey key,
496 void *targetbuf
497) {
498 return map->cl->remove(map, key, targetbuf);
499}
500
503static inline int cxMapRemoveAndGet(
504 CxMap *map,
505 cxstring key,
506 void *targetbuf
507) {
508 return map->cl->remove(map, cx_hash_key_cxstr(key), targetbuf);
509}
510
513static inline int cxMapRemoveAndGet(
514 CxMap *map,
515 cxmutstr key,
516 void *targetbuf
517) {
518 return map->cl->remove(map, cx_hash_key_cxstr(key), targetbuf);
519}
520
524static inline int cxMapRemoveAndGet(
525 CxMap *map,
526 const char *key,
527 void *targetbuf
528) {
529 return map->cl->remove(map, cx_hash_key_str(key), targetbuf);
530}
531
532#else // __cplusplus
533
538static inline int cx_map_put(
539 CxMap *map,
540 CxHashKey key,
541 void *value
542) {
543 return map->cl->put(map, key, value);
544}
545
550static inline int cx_map_put_cxstr(
551 CxMap *map,
552 cxstring key,
553 void *value
554) {
555 return map->cl->put(map, cx_hash_key_cxstr(key), value);
556}
557
562static inline int cx_map_put_mustr(
563 CxMap *map,
564 cxmutstr key,
565 void *value
566) {
567 return map->cl->put(map, cx_hash_key_cxstr(key), value);
568}
569
575static inline int cx_map_put_str(
576 CxMap *map,
577 const char *key,
578 void *value
579) {
580 return map->cl->put(map, cx_hash_key_str(key), value);
581}
582
602#define cxMapPut(map, key, value) _Generic((key), \
603 CxHashKey: cx_map_put, \
604 cxstring: cx_map_put_cxstr, \
605 cxmutstr: cx_map_put_mustr, \
606 char*: cx_map_put_str, \
607 const char*: cx_map_put_str) \
608 (map, key, value)
609
615static inline void *cx_map_get(
616 const CxMap *map,
617 CxHashKey key
618) {
619 return map->cl->get(map, key);
620}
621
627static inline void *cx_map_get_cxstr(
628 const CxMap *map,
629 cxstring key
630) {
631 return map->cl->get(map, cx_hash_key_cxstr(key));
632}
633
639static inline void *cx_map_get_mustr(
640 const CxMap *map,
641 cxmutstr key
642) {
643 return map->cl->get(map, cx_hash_key_cxstr(key));
644}
645
652static inline void *cx_map_get_str(
653 const CxMap *map,
654 const char *key
655) {
656 return map->cl->get(map, cx_hash_key_str(key));
657}
658
670#define cxMapGet(map, key) _Generic((key), \
671 CxHashKey: cx_map_get, \
672 cxstring: cx_map_get_cxstr, \
673 cxmutstr: cx_map_get_mustr, \
674 char*: cx_map_get_str, \
675 const char*: cx_map_get_str) \
676 (map, key)
677
682static inline int cx_map_remove(
683 CxMap *map,
684 CxHashKey key
685) {
686 return map->cl->remove(map, key, NULL);
687}
688
693static inline int cx_map_remove_cxstr(
694 CxMap *map,
695 cxstring key
696) {
697 return map->cl->remove(map, cx_hash_key_cxstr(key), NULL);
698}
699
704static inline int cx_map_remove_mustr(
705 CxMap *map,
706 cxmutstr key
707) {
708 return map->cl->remove(map, cx_hash_key_cxstr(key), NULL);
709}
710
716static inline int cx_map_remove_str(
717 CxMap *map,
718 const char *key
719) {
720 return map->cl->remove(map, cx_hash_key_str(key), NULL);
721}
722
735#define cxMapRemove(map, key) _Generic((key), \
736 CxHashKey: cx_map_remove, \
737 cxstring: cx_map_remove_cxstr, \
738 cxmutstr: cx_map_remove_mustr, \
739 char*: cx_map_remove_str, \
740 const char*: cx_map_remove_str) \
741 (map, key)
742
748static inline int cx_map_remove_and_get(
749 CxMap *map,
750 CxHashKey key,
751 void *targetbuf
752) {
753 return map->cl->remove(map, key, targetbuf);
754}
755
762 CxMap *map,
763 cxstring key,
764 void *targetbuf
765) {
766 return map->cl->remove(map, cx_hash_key_cxstr(key), targetbuf);
767}
768
775 CxMap *map,
776 cxmutstr key,
777 void *targetbuf
778) {
779 return map->cl->remove(map, cx_hash_key_cxstr(key), targetbuf);
780}
781
789 CxMap *map,
790 const char *key,
791 void *targetbuf
792) {
793 return map->cl->remove(map, cx_hash_key_str(key), targetbuf);
794}
795
815#define cxMapRemoveAndGet(map, key, targetbuf) _Generic((key), \
816 CxHashKey: cx_map_remove_and_get, \
817 cxstring: cx_map_remove_and_get_cxstr, \
818 cxmutstr: cx_map_remove_and_get_mustr, \
819 char*: cx_map_remove_and_get_str, \
820 const char*: cx_map_remove_and_get_str) \
821 (map, key, targetbuf)
822
823#endif // __cplusplus
824
825#endif // UCX_MAP_H
Common definitions for various collection implementations.
#define CX_COLLECTION_BASE
Use this macro to declare common members for a collection structure.
Definition collection.h:113
Common definitions and feature checks.
#define cx_attr_export
Only used for building Windows DLLs.
Definition common.h:285
#define cx_attr_nonnull
All pointer arguments must be non-NULL.
Definition common.h:136
#define cx_attr_nodiscard
Warn about discarded return value.
Definition common.h:265
#define cx_attr_access_w(...)
Specifies that the function will only write through the given pointer.
Definition common.h:241
#define cx_attr_cstr_arg(idx)
No support for null_terminated_string_arg in clang or GCC below 14.
Definition common.h:206
Interface for map implementations.
CxHashKey cx_hash_key_str(const char *str)
Computes a hash key from a string.
#define cx_hash_key_cxstr(str)
Computes a hash key from a UCX string.
Definition hash_key.h:146
#define CX_ITERATOR_BASE
Declares base attributes for an iterator.
Definition iterator.h:92
CxMapIterator cxMapMutIterator(CxMap *map)
Creates a mutating iterator for a map.
static void * cx_map_get(const CxMap *map, CxHashKey key)
Retrieves a value by using a key.
Definition map.h:615
#define cxMapRemoveAndGet(map, key, targetbuf)
Removes a key/value-pair from the map by using the key.
Definition map.h:815
static CxMapIterator cxMapIteratorValues(const CxMap *map)
Creates a value iterator for a map.
Definition map.h:285
static size_t cxMapSize(const CxMap *map)
Returns the number of elements in this map.
Definition map.h:266
static int cx_map_put(CxMap *map, CxHashKey key, void *value)
Puts a key/value-pair into the map.
Definition map.h:538
static int cx_map_remove_and_get_mustr(CxMap *map, cxmutstr key, void *targetbuf)
Removes a key/value-pair from the map by using the key.
Definition map.h:774
static int cx_map_put_cxstr(CxMap *map, cxstring key, void *value)
Puts a key/value-pair into the map.
Definition map.h:550
CxMapIterator cxMapMutIteratorKeys(CxMap *map)
Creates a mutating iterator over the keys of a map.
static int cx_map_remove_and_get(CxMap *map, CxHashKey key, void *targetbuf)
Removes a key/value-pair from the map by using the key.
Definition map.h:748
cx_map_iterator_type
The type of iterator for a map.
Definition map.h:87
@ CX_MAP_ITERATOR_KEYS
Iterates over keys only.
Definition map.h:95
@ CX_MAP_ITERATOR_PAIRS
Iterates over key/value pairs.
Definition map.h:91
@ CX_MAP_ITERATOR_VALUES
Iterates over values only.
Definition map.h:99
CxMapIterator cxMapMutIteratorValues(CxMap *map)
Creates a mutating iterator over the values of a map.
#define cxMapRemove(map, key)
Removes a key/value-pair from the map by using the key.
Definition map.h:735
static int cx_map_remove_str(CxMap *map, const char *key)
Removes a key/value-pair from the map by using the key.
Definition map.h:716
static int cx_map_remove(CxMap *map, CxHashKey key)
Removes a key/value-pair from the map by using the key.
Definition map.h:682
static void cxMapClear(CxMap *map)
Clears a map by removing all elements.
Definition map.h:255
struct cx_map_iterator_s CxMapIterator
Type for a map iterator.
Definition map.h:55
static CxMapIterator cxMapIteratorKeys(const CxMap *map)
Creates a key iterator for a map.
Definition map.h:303
static int cx_map_remove_and_get_cxstr(CxMap *map, cxstring key, void *targetbuf)
Removes a key/value-pair from the map by using the key.
Definition map.h:761
static int cx_map_remove_cxstr(CxMap *map, cxstring key)
Removes a key/value-pair from the map by using the key.
Definition map.h:693
static int cx_map_remove_and_get_str(CxMap *map, const char *key, void *targetbuf)
Removes a key/value-pair from the map by using the key.
Definition map.h:788
void cxMapFree(CxMap *map)
Deallocates the memory of the specified map.
static void * cx_map_get_cxstr(const CxMap *map, cxstring key)
Retrieves a value by using a key.
Definition map.h:627
static void * cx_map_get_str(const CxMap *map, const char *key)
Retrieves a value by using a key.
Definition map.h:652
static int cx_map_put_str(CxMap *map, const char *key, void *value)
Puts a key/value-pair into the map.
Definition map.h:575
#define cxMapPut(map, key, value)
Puts a key/value-pair into the map.
Definition map.h:602
static int cx_map_remove_mustr(CxMap *map, cxmutstr key)
Removes a key/value-pair from the map by using the key.
Definition map.h:704
CxMap *const cxEmptyMap
A shared instance of an empty map.
static CxMapIterator cxMapIterator(const CxMap *map)
Creates an iterator for a map.
Definition map.h:323
static void * cx_map_get_mustr(const CxMap *map, cxmutstr key)
Retrieves a value by using a key.
Definition map.h:639
#define cxMapGet(map, key)
Retrieves a value by using a key.
Definition map.h:670
static int cx_map_put_mustr(CxMap *map, cxmutstr key, void *value)
Puts a key/value-pair into the map.
Definition map.h:562
Strings that know their length.
size_t size
The number of currently stored elements.
Definition collection.h:71
Internal structure for a key within a hash map.
Definition hash_key.h:48
The class definition for arbitrary maps.
Definition map.h:175
int(* put)(CxMap *map, CxHashKey key, void *value)
Add or overwrite an element.
Definition map.h:189
void(* deallocate)(struct cx_map_s *map)
Deallocates the entire memory.
Definition map.h:179
int(* remove)(CxMap *map, CxHashKey key, void *targetbuf)
Removes an element.
Definition map.h:213
void(* clear)(struct cx_map_s *map)
Removes all elements.
Definition map.h:184
CxMapIterator(* iterator)(const CxMap *map, enum cx_map_iterator_type type)
Creates an iterator for this map.
Definition map.h:222
void *(* get)(const CxMap *map, CxHashKey key)
Returns an element.
Definition map.h:198
A map entry.
Definition map.h:73
const CxHashKey * key
A pointer to the key.
Definition map.h:77
void * value
A pointer to the value.
Definition map.h:81
Internal iterator struct - use CxMapIterator.
Definition map.h:105
size_t slot
Field for storing the current slot number.
Definition map.h:145
CxMapEntry entry
Reserved memory for a map entry.
Definition map.h:138
size_t index
Counts the elements successfully.
Definition map.h:151
size_t elem_count
May contain the total number of elements, if known.
Definition map.h:164
union cx_map_iterator_s::@4 map
Handle for the source map.
CxMap * m
Access for mutating iterators.
Definition map.h:118
void * elem
Handle for the current element.
Definition map.h:130
size_t elem_size
The size of a value stored in this map.
Definition map.h:156
enum cx_map_iterator_type type
The type of this iterator.
Definition map.h:169
const CxMap * c
Access for normal iterators.
Definition map.h:122
Structure for the UCX map.
Definition map.h:61
struct cx_collection_s collection
Base attributes.
Definition map.h:65
cx_map_class * cl
The map class definition.
Definition map.h:67
The UCX string structure.
Definition string.h:51
The UCX string structure for immutable (constant) strings.
Definition string.h:69