LCOV - code coverage report
Current view: top level - include - cbstmap.h (source / functions) Coverage Total Hit
Test: c_collections 2.0.0 Lines: 100.0 % 8 8
Test Date: 2026-11-15 18:57:05 Functions: - 0 0
Legend: Lines: hit not hit

            Line data    Source code
       1              : /*
       2              : MIT License
       3              : 
       4              : Copyright (c) 2026 - A bunch of nerds
       5              : 
       6              : Permission is hereby granted, free of charge, to any person obtaining a copy
       7              : of this software and associated documentation files (the "Software"), to deal
       8              : in the Software without restriction, including without limitation the rights
       9              : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      10              : copies of the Software, and to permit persons to whom the Software is
      11              : furnished to do so, subject to the following conditions:
      12              : 
      13              : The above copyright notice and this permission notice shall be included in all
      14              : copies or substantial portions of the Software.
      15              : 
      16              : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      17              : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      18              : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
      19              : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      20              : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
      21              : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
      22              : SOFTWARE.
      23              : */
      24              : 
      25              : #pragma once
      26              : 
      27              : #include "citerators.h"
      28              : #include "common.h"
      29              : 
      30              : // An ordered map on an AVL tree. Insert, delete and lookup are O(log n), every
      31              : // walk is iterative with a fixed-size stack (no recursion, no heap), and an
      32              : // iterator gives the keys in ascending order. The default order is a signed or
      33              : // unsigned integer comparison, a numeric comparison for floating keys, a byte
      34              : // comparison for strings and memcmp for any other key type; you can pass your
      35              : // own comparator.
      36              : //
      37              : // Everything from here to the matching pop is the public ABI and the shared
      38              : // library exports it. The library builds with -fvisibility=hidden, so anything
      39              : // outside these blocks stays internal.
      40              : #pragma GCC visibility push(default)
      41              : 
      42              : // The map is opaque and cbmap is the handle.
      43              : typedef struct cbstmap cbstmap;
      44              : 
      45              : typedef cbstmap *cbmap;
      46              : 
      47              : // Creates a map for keys of key_type, which picks the default order and the
      48              : // width a fixed-width key must have. mmgmt_procs is your allocator (NULL for
      49              : // malloc and free) and custom_comparison_proc your comparator (NULL for the
      50              : // default); a comparator gets pointers to the key bytes. On failure it gives
      51              : // NULL and, when err isn't NULL, an error string in *err.
      52              : //
      53              : // ccol_char compares as the platform's char; ccol_signed_char and the other
      54              : // signed types compare signed, the unsigned types and ccol_pointer unsigned.
      55              : // Floating keys compare by value, with NaN above every other key and equal to
      56              : // every NaN, so a NaN key can be found and deleted. ccol_string compares bytes.
      57              : // ccol_other_types (a struct, say) compares with memcmp, padding and pointer
      58              : // members included, so give a struct key a comparator.
      59              : cbmap cbstmap_create_full(ccol_data_type key_type,
      60              :                           ccol_memmgmt_procs *mmgmt_procs,
      61              :                           ccol_comparison_proc custom_comparison_proc,
      62              :                           char **err);
      63              : 
      64              : // Creates an ordered map for keys of key_type with the default allocator and
      65              : // the default order for that type (integers and floating keys by value,
      66              : // strings by bytes, any other type with memcmp). key_type also sets the width
      67              : // a fixed-width key must have. Returns the map, or NULL with an error string
      68              : // in *err when err isn't NULL (never free the string). Destroy it with
      69              : // cbstmap_destroy().
      70              : static inline __attribute__((always_inline)) cbmap
      71           49 : cbstmap_create(ccol_data_type key_type, char **err) {
      72           48 :   return cbstmap_create_full(key_type, NULL, NULL, err);
      73              : }
      74              : 
      75              : // Creates an ordered map for keys of key_type that takes all its memory from
      76              : // mmgmt_procs, your allocator table (NULL for malloc and free), and uses the
      77              : // default order for key_type. The table must stay valid until the map is
      78              : // destroyed. Returns the map, or NULL with an error string in *err when err
      79              : // isn't NULL (never free the string). Destroy it with cbstmap_destroy().
      80            6 : static inline __attribute__((always_inline)) cbmap cbstmap_create_mp(
      81              :     ccol_data_type key_type, ccol_memmgmt_procs *mmgmt_procs, char **err) {
      82            6 :   return cbstmap_create_full(key_type, mmgmt_procs, NULL, err);
      83              : }
      84              : 
      85              : // Creates an ordered map whose keys are ordered by custom_comparison_proc,
      86              : // with the default allocator. The comparator gets pointers to the bytes of two
      87              : // keys and returns a negative, zero or positive int; it decides only the
      88              : // order, while key_type decides the key width and alignment. Returns the map,
      89              : // or NULL with an error string in *err when err isn't NULL (never free the
      90              : // string). Destroy it with cbstmap_destroy().
      91              : static inline __attribute__((always_inline)) cbmap
      92              : cbstmap_create_ch(ccol_data_type key_type,
      93              :                   ccol_comparison_proc custom_comparison_proc, char **err) {
      94              :   return cbstmap_create_full(key_type, NULL, custom_comparison_proc, err);
      95              : }
      96              : 
      97              : // Returns the number of key-value pairs in the map. Asserts on a NULL cbm.
      98              : size_t cbstmap_size(cbmap cbm);
      99              : 
     100              : // Removes and frees every key and value and leaves an empty map that you can
     101              : // keep using. It allocates nothing and returns ccol_success. Asserts on a NULL
     102              : // cbm.
     103              : ccol_retval cbstmap_reset(cbmap cbm);
     104              : 
     105              : // Copies the key and the value into the map. When the key is already there only
     106              : // the value is replaced, reallocated if its size changed, and it gives
     107              : // ccol_key_already_present. Other results: ccol_success, ccol_container_full at
     108              : // ccol_max_elem_count, ccol_not_enough_memory, and ccol_invalid_args for a NULL
     109              : // pair, a NULL ptr with a non-zero size, a key whose size doesn't match a
     110              : // fixed-width key type, or a key of size 0 on a ccol_string map (a string key
     111              : // counts its NUL). A size of 0 is fine for any value and for a ccol_other_types
     112              : // key. Asserts on a NULL cbm.
     113              : ccol_retval cbstmap_insert_elem(cbmap cbm, const cmap_pair *key_pair,
     114              :                                 const cmap_pair *val_pair);
     115              : 
     116              : // Copies the value of key_pair into target_buf, whose size must equal the
     117              : // stored value's size exactly. It gives ccol_success, ccol_key_not_found, or
     118              : // ccol_invalid_args for a NULL key_pair, a NULL ptr with a non-zero size, a
     119              : // NULL target_buf with a non-zero size, a size mismatch, a key whose size
     120              : // doesn't match a fixed-width key type, or a size-0 key on a ccol_string map.
     121              : // Asserts on a NULL cbm.
     122              : ccol_retval cbstmap_get_elem_copy(cbmap cbm, const cmap_pair *key_pair,
     123              :                                   void *target_buf, size_t target_buf_size);
     124              : 
     125              : // Finds the value of key_pair without copying it: Fills your val_pair with the
     126              : // address and size of the stored value. You may read and change those bytes in
     127              : // place, but don't free them; to replace a value call cbstmap_insert_elem().
     128              : // The pointer stays valid until the next insert or delete; a lookup never
     129              : // moves anything. Returns ccol_success, ccol_key_not_found, or
     130              : // ccol_invalid_args for a NULL key_pair or val_pair, a NULL ptr with a
     131              : // non-zero size, a key whose size doesn't match a fixed-width key type, or a
     132              : // size-0 key on a ccol_string map. On every failure a non-NULL val_pair gets
     133              : // {NULL, 0}. Asserts on a NULL cbm.
     134              : ccol_retval cbstmap_get_elem_ref(cbmap cbm, const cmap_pair *key_pair,
     135              :                                  cmap_pair *val_pair);
     136              : 
     137              : // Internal: Hands the string arm of cbmap_get_ptr() the accessor the map keeps
     138              : // for a value, whose ptr field is the stored char *. *val_accessor is NULL on
     139              : // failure. Use cbmap_get_ptr() instead.
     140              : ccol_retval cbstmap__get_val_accessor(cbmap cbm, const cmap_pair *key_pair,
     141              :                                       const cmap_pair **val_accessor);
     142              : 
     143              : // Removes the entry of key_pair and frees the map's copies of its key and
     144              : // value. Returns ccol_success, ccol_key_not_found, or ccol_invalid_args for a
     145              : // NULL key_pair, a NULL ptr with a non-zero size, a key whose size doesn't
     146              : // match a fixed-width key type, or a size-0 key on a ccol_string map. Treat
     147              : // pointers into the map as invalid after a delete. Asserts on a NULL cbm.
     148              : ccol_retval cbstmap_remove_elem(cbmap cbm, const cmap_pair *key_pair);
     149              : 
     150              : // Internal: Frees an iterator, at the end of a walk or when a loop leaves early
     151              : // (ccol_iter_destroy()). cbstmap_begin_iter() is declared in citerators.h,
     152              : // because ccol_begin() needs all three containers' functions.
     153              : void cbstmap__iterator_destroy(cmap_iterator *iter);
     154              : 
     155              : // Internal: Frees the map and everything in it. Use cbstmap_destroy() instead.
     156              : void cbstmap__destroy(cbmap cbm);
     157              : 
     158              : // Destroys the map: Frees every node, key and value, and sets cbm to NULL so
     159              : // the handle can't be used after the free. cbm must be a modifiable lvalue and
     160              : // is evaluated once; a NULL handle does nothing. Pointers that the map gave
     161              : // you are invalid afterwards.
     162              : #define cbstmap_destroy(cbm)  \
     163              :   ccol__cbstmap_destroy_impl( \
     164              :       cbm, ccol__uniq(ccol__cbstmap_destroy_slot, __COUNTER__))
     165              : 
     166              : // Internal: The body of cbstmap_destroy(). slot comes from ccol__uniq(), so
     167              : // destroy macros can nest without -Wshadow warnings.
     168              : #define ccol__cbstmap_destroy_impl(cbm, slot) \
     169              :   do {                                        \
     170              :     __typeof__(cbm) *slot = &(cbm);           \
     171              :     cbstmap__destroy(*slot);                  \
     172              :     *slot = NULL;                             \
     173              :   } while (0)
     174              : 
     175              : // Internal: Destroys *cbm and sets it to NULL. It's the cleanup function the
     176              : // _scoped forms attach with ccol__destructor.
     177           29 : static inline void cbstmap__destroy_cleanup(cbmap *cbm) {
     178           29 :   if (cbm && *cbm) {
     179           21 :     cbstmap__destroy(*cbm);
     180           28 :     *cbm = NULL;
     181              :   }
     182              : }
     183              : 
     184              : // Makes the typed cbmap_* macros usable on a map handle that was created in
     185              : // another scope, such as a function parameter or a struct field. It declares
     186              : // only the key and value type information that those macros read; it creates
     187              : // and destroys nothing. KeyT and ValT must be the types the map was created
     188              : // with. An array key or value type is refused at compile time; wrap the array
     189              : // in a struct.
     190              : #define cbmap_redeclare(hm_name, KeyT, ValT)                               \
     191              :   ccol__refuse_array_types("cbmap", KeyT, ValT);                           \
     192              :   __typeof__(KeyT) *hm_name##__ccol_key_type_var __attribute__((unused)) = \
     193              :       NULL;                                                                \
     194              :   __typeof__(ValT) *hm_name##__ccol_val_type_var __attribute__((unused)) = NULL
     195              : 
     196              : // Declares an ordered map handle hm_name with key type KeyT and value type
     197              : // ValT, without creating a map. The handle is left uninitialized: Create the
     198              : // map with cbmap_init(), cbmap_init_mp(), cbmap_init_cc() or cbmap_init_full()
     199              : // before any other cbmap_* macro, and destroy it with cbmap_destroy(). An array
     200              : // key or value type is refused at compile time; wrap it in a struct.
     201              : #define cbmap_declare(hm_name, KeyT, ValT)                                \
     202              :   ccol__refuse_array_types("cbmap", KeyT, ValT);                          \
     203              :   __typeof__(KeyT) *hm_name##__ccol_key_type_var __attribute__((unused)); \
     204              :   __typeof__(ValT) *hm_name##__ccol_val_type_var __attribute__((unused)); \
     205              :   cbmap hm_name
     206              : 
     207              : // Declares an ordered map handle hm_name with key type KeyT and value type
     208              : // ValT, set to NULL, without creating a map. Create it later with cbmap_init()
     209              : // or another cbmap_init_* macro. When the variable goes out of scope the map is
     210              : // destroyed by itself; cbmap_destroy() before that is allowed and leaves NULL
     211              : // behind. An array key or value type is refused at compile time.
     212              : #define cbmap_declare_scoped(hm_name, KeyT, ValT)                          \
     213              :   ccol__refuse_array_types("cbmap", KeyT, ValT);                           \
     214              :   __typeof__(KeyT) *hm_name##__ccol_key_type_var __attribute__((unused)) = \
     215              :       NULL;                                                                \
     216              :   __typeof__(ValT) *hm_name##__ccol_val_type_var __attribute__((unused)) = \
     217              :       NULL;                                                                \
     218              :   cbmap hm_name ccol__destructor(cbstmap__destroy_cleanup) = NULL
     219              : 
     220              : // Creates the map for a handle that cbmap_declare() or cbmap_declare_scoped()
     221              : // declared, with the default allocator and the default order for the declared
     222              : // key type. Ends the program with a fatal error when the map can't be created.
     223              : // A struct key gets a memcmp order, so give it a comparator with
     224              : // cbmap_init_cc().
     225              : #define cbmap_init(hm_name)                                                 \
     226              :   do {                                                                      \
     227              :     char *ccol__cbmap_err = NULL;                                           \
     228              :     hm_name = cbstmap_create_full(                                          \
     229              :         ccol_determine_ccol_data_type(*hm_name##__ccol_key_type_var), NULL, \
     230              :         NULL, &ccol__cbmap_err);                                            \
     231              :     if (!hm_name) {                                                         \
     232              :       ccol__fatal_err("Failed to create BST map '%s': %s", #hm_name,        \
     233              :                       ccol__cbmap_err ? ccol__cbmap_err : "unknown error"); \
     234              :     }                                                                       \
     235              :   } while (0)
     236              : 
     237              : // Creates the map for a handle that cbmap_declare() or cbmap_declare_scoped()
     238              : // declared, taking all its memory from mmgmt_procs, your allocator table (NULL
     239              : // for the default). The table must stay valid until the map is destroyed. Keys
     240              : // get the default order for the declared key type. Ends the program with a
     241              : // fatal error when the map can't be created.
     242              : #define cbmap_init_mp(hm_name, mmgmt_procs)                                 \
     243              :   do {                                                                      \
     244              :     char *ccol__cbmap_err = NULL;                                           \
     245              :     hm_name = cbstmap_create_full(                                          \
     246              :         ccol_determine_ccol_data_type(*hm_name##__ccol_key_type_var),       \
     247              :         (mmgmt_procs), NULL, &ccol__cbmap_err);                             \
     248              :     if (!hm_name) {                                                         \
     249              :       ccol__fatal_err("Failed to create BST map '%s': %s", #hm_name,        \
     250              :                       ccol__cbmap_err ? ccol__cbmap_err : "unknown error"); \
     251              :     }                                                                       \
     252              :   } while (0)
     253              : 
     254              : // Creates the map for a handle that cbmap_declare() or cbmap_declare_scoped()
     255              : // declared, with keys ordered by custom_comparison_proc and the default
     256              : // allocator. The comparator gets pointers to the bytes of two keys and returns
     257              : // a negative, zero or positive int. A struct key needs one. Ends the program
     258              : // with a fatal error when the map can't be created.
     259              : #define cbmap_init_cc(hm_name, custom_comparison_proc)                      \
     260              :   do {                                                                      \
     261              :     char *ccol__cbmap_err = NULL;                                           \
     262              :     hm_name = cbstmap_create_full(                                          \
     263              :         ccol_determine_ccol_data_type(*hm_name##__ccol_key_type_var), NULL, \
     264              :         (custom_comparison_proc), &ccol__cbmap_err);                        \
     265              :     if (!hm_name) {                                                         \
     266              :       ccol__fatal_err("Failed to create BST map '%s': %s", #hm_name,        \
     267              :                       ccol__cbmap_err ? ccol__cbmap_err : "unknown error"); \
     268              :     }                                                                       \
     269              :   } while (0)
     270              : 
     271              : // Creates the map for a handle that cbmap_declare() or cbmap_declare_scoped()
     272              : // declared. mmgmt_procs is your allocator table (NULL for the default; it must
     273              : // outlive the map) and custom_comparison_proc orders the keys (NULL for the
     274              : // default order of the declared key type); it gets pointers to the bytes of
     275              : // two keys and returns a negative, zero or positive int. Ends the program with
     276              : // a fatal error when the map can't be created.
     277              : #define cbmap_init_full(hm_name, mmgmt_procs, custom_comparison_proc)       \
     278              :   do {                                                                      \
     279              :     char *ccol__cbmap_err = NULL;                                           \
     280              :     hm_name = cbstmap_create_full(                                          \
     281              :         ccol_determine_ccol_data_type(*hm_name##__ccol_key_type_var),       \
     282              :         (mmgmt_procs), (custom_comparison_proc), &ccol__cbmap_err);         \
     283              :     if (!hm_name) {                                                         \
     284              :       ccol__fatal_err("Failed to create BST map '%s': %s", #hm_name,        \
     285              :                       ccol__cbmap_err ? ccol__cbmap_err : "unknown error"); \
     286              :     }                                                                       \
     287              :   } while (0)
     288              : 
     289              : // Declares an ordered map handle hm_name for key type KeyT and value type ValT
     290              : // and creates the map with the default allocator and the default order for
     291              : // KeyT. Ends the program with a fatal error when the map can't be created.
     292              : // Destroy it with cbmap_destroy(). A struct key gets a memcmp order, so use
     293              : // cbmap_construct_cc() for it. An array key or value type is refused at
     294              : // compile time.
     295              : #define cbmap_construct(hm_name, KeyT, ValT)                                \
     296              :   ccol__refuse_array_types("cbmap", KeyT, ValT);                            \
     297              :   __typeof__(KeyT) *hm_name##__ccol_key_type_var __attribute__((unused)) =  \
     298              :       NULL;                                                                 \
     299              :   __typeof__(ValT) *hm_name##__ccol_val_type_var __attribute__((unused)) =  \
     300              :       NULL;                                                                 \
     301              :   cbmap hm_name = NULL;                                                     \
     302              :   do {                                                                      \
     303              :     char *ccol__cbmap_err = NULL;                                           \
     304              :     hm_name = cbstmap_create_full(                                          \
     305              :         ccol_determine_ccol_data_type(*hm_name##__ccol_key_type_var), NULL, \
     306              :         NULL, &ccol__cbmap_err);                                            \
     307              :     if (!hm_name) {                                                         \
     308              :       ccol__fatal_err("Failed to create BST map '%s': %s", #hm_name,        \
     309              :                       ccol__cbmap_err ? ccol__cbmap_err : "unknown error"); \
     310              :     }                                                                       \
     311              :   } while (0)
     312              : 
     313              : // Declares an ordered map handle hm_name for key type KeyT and value type ValT
     314              : // and creates the map with the default allocator and the default order for
     315              : // KeyT. The map is destroyed by itself when hm_name goes out of scope. Ends the
     316              : // program with a fatal error when the map can't be created. A struct key gets
     317              : // a memcmp order, so use cbmap_construct_cc_scoped() for it.
     318              : #define cbmap_construct_scoped(hm_name, KeyT, ValT)                         \
     319              :   ccol__refuse_array_types("cbmap", KeyT, ValT);                            \
     320              :   __typeof__(KeyT) *hm_name##__ccol_key_type_var __attribute__((unused)) =  \
     321              :       NULL;                                                                 \
     322              :   __typeof__(ValT) *hm_name##__ccol_val_type_var __attribute__((unused)) =  \
     323              :       NULL;                                                                 \
     324              :   cbmap hm_name ccol__destructor(cbstmap__destroy_cleanup) = NULL;          \
     325              :   do {                                                                      \
     326              :     char *ccol__cbmap_err = NULL;                                           \
     327              :     hm_name = cbstmap_create_full(                                          \
     328              :         ccol_determine_ccol_data_type(*hm_name##__ccol_key_type_var), NULL, \
     329              :         NULL, &ccol__cbmap_err);                                            \
     330              :     if (!hm_name) {                                                         \
     331              :       ccol__fatal_err("Failed to create BST map '%s': %s", #hm_name,        \
     332              :                       ccol__cbmap_err ? ccol__cbmap_err : "unknown error"); \
     333              :     }                                                                       \
     334              :   } while (0)
     335              : 
     336              : // Declares an ordered map handle hm_name for key type KeyT and value type ValT
     337              : // and creates the map, taking all its memory from mmgmt_procs, your allocator
     338              : // table (NULL for the default). The table must stay valid until the map is
     339              : // destroyed. Keys get the default order for KeyT. Ends the program with a
     340              : // fatal error when the map can't be created. Destroy it with cbmap_destroy().
     341              : #define cbmap_construct_mp(hm_name, KeyT, ValT, mmgmt_procs)                \
     342              :   ccol__refuse_array_types("cbmap", KeyT, ValT);                            \
     343              :   __typeof__(KeyT) *hm_name##__ccol_key_type_var __attribute__((unused)) =  \
     344              :       NULL;                                                                 \
     345              :   __typeof__(ValT) *hm_name##__ccol_val_type_var __attribute__((unused)) =  \
     346              :       NULL;                                                                 \
     347              :   cbmap hm_name = NULL;                                                     \
     348              :   do {                                                                      \
     349              :     char *ccol__cbmap_err = NULL;                                           \
     350              :     hm_name = cbstmap_create_full(                                          \
     351              :         ccol_determine_ccol_data_type(*hm_name##__ccol_key_type_var),       \
     352              :         (mmgmt_procs), NULL, &ccol__cbmap_err);                             \
     353              :     if (!hm_name) {                                                         \
     354              :       ccol__fatal_err("Failed to create BST map '%s': %s", #hm_name,        \
     355              :                       ccol__cbmap_err ? ccol__cbmap_err : "unknown error"); \
     356              :     }                                                                       \
     357              :   } while (0)
     358              : 
     359              : // Declares an ordered map handle hm_name for key type KeyT and value type ValT
     360              : // and creates the map, taking all its memory from mmgmt_procs, your allocator
     361              : // table (NULL for the default). The map is destroyed by itself when hm_name
     362              : // goes out of scope, and the table must stay valid until then. Keys get the
     363              : // default order for KeyT. Ends the program with a fatal error when the map
     364              : // can't be created.
     365              : #define cbmap_construct_mp_scoped(hm_name, KeyT, ValT, mmgmt_procs)         \
     366              :   ccol__refuse_array_types("cbmap", KeyT, ValT);                            \
     367              :   __typeof__(KeyT) *hm_name##__ccol_key_type_var __attribute__((unused)) =  \
     368              :       NULL;                                                                 \
     369              :   __typeof__(ValT) *hm_name##__ccol_val_type_var __attribute__((unused)) =  \
     370              :       NULL;                                                                 \
     371              :   cbmap hm_name ccol__destructor(cbstmap__destroy_cleanup) = NULL;          \
     372              :   do {                                                                      \
     373              :     char *ccol__cbmap_err = NULL;                                           \
     374              :     hm_name = cbstmap_create_full(                                          \
     375              :         ccol_determine_ccol_data_type(*hm_name##__ccol_key_type_var),       \
     376              :         (mmgmt_procs), NULL, &ccol__cbmap_err);                             \
     377              :     if (!hm_name) {                                                         \
     378              :       ccol__fatal_err("Failed to create BST map '%s': %s", #hm_name,        \
     379              :                       ccol__cbmap_err ? ccol__cbmap_err : "unknown error"); \
     380              :     }                                                                       \
     381              :   } while (0)
     382              : 
     383              : // Declares an ordered map handle hm_name for key type KeyT and value type ValT
     384              : // and creates the map with keys ordered by custom_comparison_proc. The
     385              : // comparator gets pointers to the bytes of two keys and returns a negative,
     386              : // zero or positive int. Ends the program with a fatal error when the map can't
     387              : // be created. Destroy it with cbmap_destroy().
     388              : #define cbmap_construct_cc(hm_name, KeyT, ValT, custom_comparison_proc)     \
     389              :   ccol__refuse_array_types("cbmap", KeyT, ValT);                            \
     390              :   __typeof__(KeyT) *hm_name##__ccol_key_type_var __attribute__((unused)) =  \
     391              :       NULL;                                                                 \
     392              :   __typeof__(ValT) *hm_name##__ccol_val_type_var __attribute__((unused)) =  \
     393              :       NULL;                                                                 \
     394              :   cbmap hm_name = NULL;                                                     \
     395              :   do {                                                                      \
     396              :     char *ccol__cbmap_err = NULL;                                           \
     397              :     hm_name = cbstmap_create_full(                                          \
     398              :         ccol_determine_ccol_data_type(*hm_name##__ccol_key_type_var), NULL, \
     399              :         (custom_comparison_proc), &ccol__cbmap_err);                        \
     400              :     if (!hm_name) {                                                         \
     401              :       ccol__fatal_err("Failed to create BST map '%s': %s", #hm_name,        \
     402              :                       ccol__cbmap_err ? ccol__cbmap_err : "unknown error"); \
     403              :     }                                                                       \
     404              :   } while (0)
     405              : 
     406              : // Declares an ordered map handle hm_name for key type KeyT and value type ValT
     407              : // and creates the map with keys ordered by custom_comparison_proc, which gets
     408              : // pointers to the bytes of two keys and returns a negative, zero or positive
     409              : // int. The map is destroyed by itself when hm_name goes out of scope. Ends the
     410              : // program with a fatal error when the map can't be created.
     411              : #define cbmap_construct_cc_scoped(hm_name, KeyT, ValT, custom_comparison_proc) \
     412              :   ccol__refuse_array_types("cbmap", KeyT, ValT);                               \
     413              :   __typeof__(KeyT) *hm_name##__ccol_key_type_var __attribute__((unused)) =     \
     414              :       NULL;                                                                    \
     415              :   __typeof__(ValT) *hm_name##__ccol_val_type_var __attribute__((unused)) =     \
     416              :       NULL;                                                                    \
     417              :   cbmap hm_name ccol__destructor(cbstmap__destroy_cleanup) = NULL;             \
     418              :   do {                                                                         \
     419              :     char *ccol__cbmap_err = NULL;                                              \
     420              :     hm_name = cbstmap_create_full(                                             \
     421              :         ccol_determine_ccol_data_type(*hm_name##__ccol_key_type_var), NULL,    \
     422              :         (custom_comparison_proc), &ccol__cbmap_err);                           \
     423              :     if (!hm_name) {                                                            \
     424              :       ccol__fatal_err("Failed to create BST map '%s': %s", #hm_name,           \
     425              :                       ccol__cbmap_err ? ccol__cbmap_err : "unknown error");    \
     426              :     }                                                                          \
     427              :   } while (0)
     428              : 
     429              : // Declares an ordered map handle hm_name for key type KeyT and value type ValT
     430              : // and creates the map. mmgmt_procs is your allocator table (NULL for the
     431              : // default; it must outlive the map) and custom_comparison_proc orders the keys
     432              : // (NULL for the default order of KeyT); it gets pointers to the bytes of two
     433              : // keys and returns a negative, zero or positive int. Ends the program with a
     434              : // fatal error when the map can't be created. Destroy it with cbmap_destroy().
     435              : #define cbmap_construct_full(hm_name, KeyT, ValT, mmgmt_procs,              \
     436              :                              custom_comparison_proc)                        \
     437              :   ccol__refuse_array_types("cbmap", KeyT, ValT);                            \
     438              :   __typeof__(KeyT) *hm_name##__ccol_key_type_var __attribute__((unused)) =  \
     439              :       NULL;                                                                 \
     440              :   __typeof__(ValT) *hm_name##__ccol_val_type_var __attribute__((unused)) =  \
     441              :       NULL;                                                                 \
     442              :   cbmap hm_name = NULL;                                                     \
     443              :   do {                                                                      \
     444              :     char *ccol__cbmap_err = NULL;                                           \
     445              :     hm_name = cbstmap_create_full(                                          \
     446              :         ccol_determine_ccol_data_type(*hm_name##__ccol_key_type_var),       \
     447              :         (mmgmt_procs), (custom_comparison_proc), &ccol__cbmap_err);         \
     448              :     if (!hm_name) {                                                         \
     449              :       ccol__fatal_err("Failed to create BST map '%s': %s", #hm_name,        \
     450              :                       ccol__cbmap_err ? ccol__cbmap_err : "unknown error"); \
     451              :     }                                                                       \
     452              :   } while (0)
     453              : 
     454              : // Declares an ordered map handle hm_name for key type KeyT and value type ValT
     455              : // and creates the map; it is destroyed by itself when hm_name goes out of
     456              : // scope. mmgmt_procs is your allocator table (NULL for the default; it must
     457              : // stay valid until then) and custom_comparison_proc orders the keys (NULL for
     458              : // the default order of KeyT); it gets pointers to the bytes of two keys and
     459              : // returns a negative, zero or positive int. Ends the program with a fatal
     460              : // error when the map can't be created.
     461              : #define cbmap_construct_full_scoped(hm_name, KeyT, ValT, mmgmt_procs,       \
     462              :                                     custom_comparison_proc)                 \
     463              :   ccol__refuse_array_types("cbmap", KeyT, ValT);                            \
     464              :   __typeof__(KeyT) *hm_name##__ccol_key_type_var __attribute__((unused)) =  \
     465              :       NULL;                                                                 \
     466              :   __typeof__(ValT) *hm_name##__ccol_val_type_var __attribute__((unused)) =  \
     467              :       NULL;                                                                 \
     468              :   cbmap hm_name ccol__destructor(cbstmap__destroy_cleanup) = NULL;          \
     469              :   do {                                                                      \
     470              :     char *ccol__cbmap_err = NULL;                                           \
     471              :     hm_name = cbstmap_create_full(                                          \
     472              :         ccol_determine_ccol_data_type(*hm_name##__ccol_key_type_var),       \
     473              :         (mmgmt_procs), (custom_comparison_proc), &ccol__cbmap_err);         \
     474              :     if (!hm_name) {                                                         \
     475              :       ccol__fatal_err("Failed to create BST map '%s': %s", #hm_name,        \
     476              :                       ccol__cbmap_err ? ccol__cbmap_err : "unknown error"); \
     477              :     }                                                                       \
     478              :   } while (0)
     479              : 
     480              : // Destroys the map: Frees every node, key and value, and sets cbm to NULL so
     481              : // the handle can't be used after the free. cbm must be a modifiable lvalue and
     482              : // is evaluated once; a NULL handle does nothing. Pointers that the map gave
     483              : // you (a char * from cbmap_get(), say) are invalid afterwards.
     484              : #define cbmap_destroy(cbm) cbstmap_destroy(cbm)
     485              : 
     486              : // Inserts key with its value, or replaces the value when the key is already
     487              : // there; the map stores its own copies, a string included. It ends the program
     488              : // with a fatal error on any failure, such as lack of memory. key and val are
     489              : // converted to the declared key and value types as a plain assignment would
     490              : // convert them, so a float never lands in an int map as its bit pattern; a
     491              : // char pointer type keeps the type of the expression, so a const char * needs
     492              : // no cast. A NULL char pointer key or value isn't a string and stops the
     493              : // program with ccol_invalid_args. Any expression is fine and each one is
     494              : // evaluated once.
     495              : #define cbmap_insert(hm_name, key, ccol__cbmap_val)                            \
     496              :   ccol__cbmap_insert_impl(hm_name, (key), (ccol__cbmap_val),                   \
     497              :                           ccol__uniq(ccol__cbmap_insert_key_tmp, __COUNTER__), \
     498              :                           ccol__uniq(ccol__cbmap_insert_val_tmp, __COUNTER__), \
     499              :                           ccol__uniq(ccol__cbmap_insert_kp, __COUNTER__),      \
     500              :                           ccol__uniq(ccol__cbmap_insert_vp, __COUNTER__),      \
     501              :                           ccol__uniq(ccol__cbmap_insert_r, __COUNTER__))
     502              : 
     503              : // Internal: The body of cbmap_insert(). Its temporaries come from ccol__uniq()
     504              : // so it can nest in any public macro. They have the declared types (through
     505              : // ccol__declared_or_own_type()), so a narrower or wider expression is converted
     506              : // and the node always holds sizeof(ValT) bytes, and they live for the whole
     507              : // call, which ccol__populate_cmap_pair() needs. The key's padding is cleared
     508              : // because the order is byte-wise for struct keys. A pair with a NULL ptr can
     509              : // only come from a NULL char pointer and is refused before the raw call.
     510              : #define ccol__cbmap_insert_impl(hm_name, key, ccol__cbmap_val,                 \
     511              :                                 ccol__cbmap_key_tmp, ccol__cbmap_val_tmp,      \
     512              :                                 ccol__cbmap_kp, ccol__cbmap_vp, ccol__cbmap_r) \
     513              :   do {                                                                         \
     514              :     ccol__declared_or_own_type(hm_name##__ccol_key_type_var, (key))            \
     515              :         ccol__cbmap_key_tmp = (key);                                           \
     516              :     ccol__clear_padding(&ccol__cbmap_key_tmp);                                 \
     517              :     ccol__declared_or_own_type(hm_name##__ccol_val_type_var,                   \
     518              :                                (ccol__cbmap_val)) ccol__cbmap_val_tmp =        \
     519              :         (ccol__cbmap_val);                                                     \
     520              :     cmap_pair *ccol__cbmap_kp = &(cmap_pair){};                                \
     521              :     cmap_pair *ccol__cbmap_vp = &(cmap_pair){};                                \
     522              :     ccol__populate_cmap_pair(ccol__cbmap_kp, ccol__cbmap_key_tmp);             \
     523              :     ccol__populate_cmap_pair(ccol__cbmap_vp, ccol__cbmap_val_tmp);             \
     524              :     ccol_retval ccol__cbmap_r =                                                \
     525              :         (!ccol__cbmap_kp->ptr || !ccol__cbmap_vp->ptr)                         \
     526              :             ? ccol_invalid_args                                                \
     527              :             : cbstmap_insert_elem(hm_name, ccol__cbmap_kp, ccol__cbmap_vp);    \
     528              :     if (ccol__cbmap_r != ccol_success &&                                       \
     529              :         ccol__cbmap_r != ccol_key_already_present) {                           \
     530              :       ccol__dump_key_to_stderr(ccol__cbmap_kp->ptr, ccol__cbmap_kp->size);     \
     531              :       ccol__fatal_err("cbmap_insert('%s'): r: %d (%s)", #hm_name,              \
     532              :                       ccol__cbmap_r, ccol_retval_to_str(ccol__cbmap_r));       \
     533              :     }                                                                          \
     534              :   } while (0)
     535              : 
     536              : // Removes key and its value from the map and frees the map's copies of both.
     537              : // Evaluates to ccol_success, ccol_key_not_found, or ccol_invalid_args for a
     538              : // NULL char pointer key. It never ends the program, because a missing key is
     539              : // an ordinary outcome. The key is converted to the declared key type, as
     540              : // cbmap_insert() converts it.
     541              : #define cbmap_remove(hm_name, key)                                             \
     542              :   ccol__cbmap_remove_impl(hm_name, (key),                                      \
     543              :                           ccol__uniq(ccol__cbmap_remove_key_tmp, __COUNTER__), \
     544              :                           ccol__uniq(ccol__cbmap_remove_kp, __COUNTER__),      \
     545              :                           ccol__uniq(ccol__cbmap_remove_r, __COUNTER__))
     546              : 
     547              : // Internal: The body of cbmap_remove(). The key goes through the declared key
     548              : // type, as in cbmap_insert().
     549              : #define ccol__cbmap_remove_impl(hm_name, key, ccol__cbmap_key_tmp,           \
     550              :                                 ccol__cbmap_kp, ccol__cbmap_r)               \
     551              :   ({                                                                         \
     552              :     ccol__declared_or_own_type(hm_name##__ccol_key_type_var, (key))          \
     553              :         ccol__cbmap_key_tmp = (key);                                         \
     554              :     ccol__clear_padding(&ccol__cbmap_key_tmp);                               \
     555              :     cmap_pair *ccol__cbmap_kp = &(cmap_pair){};                              \
     556              :     ccol__populate_cmap_pair(ccol__cbmap_kp, ccol__cbmap_key_tmp);           \
     557              :     ccol_retval ccol__cbmap_r =                                              \
     558              :         !ccol__cbmap_kp->ptr ? ccol_invalid_args                             \
     559              :                              : cbstmap_remove_elem(hm_name, ccol__cbmap_kp); \
     560              :     ccol__cbmap_r;                                                           \
     561              :   })
     562              : 
     563              : // Evaluates to a copy of the value stored for key, for a key you know is
     564              : // there. For a string value it gives the map's own char *, which stays valid
     565              : // until that key is replaced or removed or the map is destroyed; don't free
     566              : // it. Ends the program with a fatal error when the key is missing, when the
     567              : // stored size doesn't match the declared value type, and for a NULL char
     568              : // pointer key. Use cbmap_get_ptr() for a lookup that may miss.
     569              : #define cbmap_get(hm_name, key)                                          \
     570              :   ccol__cbmap_get_impl(hm_name, (key),                                   \
     571              :                        ccol__uniq(ccol__cbmap_get_key_tmp, __COUNTER__), \
     572              :                        ccol__uniq(ccol__cbmap_get_val, __COUNTER__),     \
     573              :                        ccol__uniq(ccol__cbmap_get_kp, __COUNTER__),      \
     574              :                        ccol__uniq(ccol__cbmap_get_vp, __COUNTER__),      \
     575              :                        ccol__uniq(ccol__cbmap_get_r, __COUNTER__))
     576              : 
     577              : // Internal: The body of cbmap_get(). A key stored through the declared-type
     578              : // conversion is found only by a lookup that makes the same conversion.
     579              : #define ccol__cbmap_get_impl(hm_name, key, ccol__cbmap_key_tmp,               \
     580              :                              ccol__cbmap_val, ccol__cbmap_kp, ccol__cbmap_vp, \
     581              :                              ccol__cbmap_r)                                   \
     582              :   ({                                                                          \
     583              :     ccol__declared_or_own_type(hm_name##__ccol_key_type_var, (key))           \
     584              :         ccol__cbmap_key_tmp = (key);                                          \
     585              :     ccol__clear_padding(&ccol__cbmap_key_tmp);                                \
     586              :     __typeof__(*hm_name##__ccol_val_type_var) *ccol__cbmap_val = NULL;        \
     587              :     cmap_pair *ccol__cbmap_kp = &(cmap_pair){};                               \
     588              :     cmap_pair ccol__cbmap_vp = {NULL, 0};                                     \
     589              :     ccol__populate_cmap_pair(ccol__cbmap_kp, ccol__cbmap_key_tmp);            \
     590              :     ccol_retval ccol__cbmap_r =                                               \
     591              :         !ccol__cbmap_kp->ptr                                                  \
     592              :             ? ccol_invalid_args                                               \
     593              :             : cbstmap_get_elem_ref(hm_name, ccol__cbmap_kp, &ccol__cbmap_vp); \
     594              :     if (ccol__cbmap_r != ccol_success) {                                      \
     595              :       ccol__dump_key_to_stderr(ccol__cbmap_kp->ptr, ccol__cbmap_kp->size);    \
     596              :       ccol__fatal_err("cbmap_get('%s'): r: %d (%s)", #hm_name, ccol__cbmap_r, \
     597              :                       ccol_retval_to_str(ccol__cbmap_r));                     \
     598              :     }                                                                         \
     599              :     if (ccol__is_char_ptr(*hm_name##__ccol_val_type_var)) {                   \
     600              :       ccol__cbmap_val =                                                       \
     601              :           (__typeof__(*hm_name##__ccol_val_type_var) *)&(ccol__cbmap_vp.ptr); \
     602              :     } else if (ccol__cbmap_vp.size != sizeof(*ccol__cbmap_val)) {             \
     603              :       ccol__fatal_err(                                                        \
     604              :           "cbmap_get('%s'): value size mismatch - stored: %lu bytes, "        \
     605              :           "requested: %lu bytes; wrong type or missing cbmap_redeclare()?",   \
     606              :           #hm_name, (unsigned long)ccol__cbmap_vp.size,                       \
     607              :           (unsigned long)sizeof(*ccol__cbmap_val));                           \
     608              :     } else {                                                                  \
     609              :       ccol__cbmap_val =                                                       \
     610              :           (__typeof__(*hm_name##__ccol_val_type_var) *)(ccol__cbmap_vp.ptr);  \
     611              :     }                                                                         \
     612              :     *ccol__cbmap_val;                                                         \
     613              :   })
     614              : 
     615              : // Internal: Gives p unchanged, except for a map with string values, where the
     616              : // target becomes const. The only char * object the map holds for a value is the
     617              : // ptr field of its accessor, and storing another pointer there would leave the
     618              : // accessor's size describing the old string; later reads would then overrun the
     619              : // new buffer. You can read the char * and edit the string bytes within its
     620              : // length; cbmap_insert() replaces a string.
     621              : #define ccol__cbmap_value_ptr_result(hm_name, p)                               \
     622              :   _Generic(*hm_name##__ccol_val_type_var,                                      \
     623              :       char *: (__typeof__(*hm_name##__ccol_val_type_var) const *)(p),          \
     624              :       const char *: (__typeof__(*hm_name##__ccol_val_type_var) const *)(p),    \
     625              :       signed char *: (__typeof__(*hm_name##__ccol_val_type_var) const *)(p),   \
     626              :       const signed char *: (__typeof__(*hm_name##__ccol_val_type_var)          \
     627              :                                 const *)(p),                                   \
     628              :       unsigned char *: (__typeof__(*hm_name##__ccol_val_type_var) const *)(p), \
     629              :       const unsigned char *: (__typeof__(*hm_name##__ccol_val_type_var)        \
     630              :                                   const *)(p),                                 \
     631              :       default: (p))
     632              : 
     633              : // Evaluates to a pointer to the value stored for key, or NULL when the key is
     634              : // missing or is a NULL char pointer; use it for a lookup that may miss or that
     635              : // changes the value in place. The pointer is valid until the next insert or
     636              : // delete. For a string value the result is char *const *: Read the string and
     637              : // edit its bytes within the stored length, but replace it only with
     638              : // cbmap_insert(). Ends the program with a fatal error when the stored size
     639              : // doesn't match the declared value type.
     640              : #define cbmap_get_ptr(hm_name, key)                                         \
     641              :   ccol__cbmap_get_ptr_impl(                                                 \
     642              :       hm_name, (key), ccol__uniq(ccol__cbmap_get_ptr_key_tmp, __COUNTER__), \
     643              :       ccol__uniq(ccol__cbmap_get_ptr_val, __COUNTER__),                     \
     644              :       ccol__uniq(ccol__cbmap_get_ptr_kp, __COUNTER__),                      \
     645              :       ccol__uniq(ccol__cbmap_get_ptr_vp, __COUNTER__),                      \
     646              :       ccol__uniq(ccol__cbmap_get_ptr_acc, __COUNTER__),                     \
     647              :       ccol__uniq(ccol__cbmap_get_ptr_r, __COUNTER__))
     648              : 
     649              : // Internal: The body of cbmap_get_ptr(). The value pair has no initializer
     650              : // because the lookup fills it on every path and it's read only after a success.
     651              : // For a string value the result points at the accessor's ptr field; the cast
     652              : // through uintptr_t drops the library's const without a -Wcast-qual warning in
     653              : // the caller's build, and the cast to the type of
     654              : // ccol__cbmap_value_ptr_result() adds const back.
     655              : #define ccol__cbmap_get_ptr_impl(                                              \
     656              :     hm_name, key, ccol__cbmap_key_tmp, ccol__cbmap_val, ccol__cbmap_kp,        \
     657              :     ccol__cbmap_vp, ccol__cbmap_acc, ccol__cbmap_r)                            \
     658              :   ({                                                                           \
     659              :     ccol__declared_or_own_type(hm_name##__ccol_key_type_var, (key))            \
     660              :         ccol__cbmap_key_tmp = (key);                                           \
     661              :     ccol__clear_padding(&ccol__cbmap_key_tmp);                                 \
     662              :     __typeof__(*hm_name##__ccol_val_type_var) *ccol__cbmap_val = NULL;         \
     663              :     cmap_pair *ccol__cbmap_kp = &(cmap_pair){};                                \
     664              :     cmap_pair ccol__cbmap_vp;                                                  \
     665              :     const cmap_pair *ccol__cbmap_acc = NULL;                                   \
     666              :     ccol_retval ccol__cbmap_r;                                                 \
     667              :     ccol__populate_cmap_pair(ccol__cbmap_kp, ccol__cbmap_key_tmp);             \
     668              :     if (ccol__cbmap_kp->ptr &&                                                 \
     669              :         ccol__is_char_ptr(*hm_name##__ccol_val_type_var)) {                    \
     670              :       ccol__cbmap_r = cbstmap__get_val_accessor(hm_name, ccol__cbmap_kp,       \
     671              :                                                 &ccol__cbmap_acc);             \
     672              :       if (ccol__cbmap_r == ccol_success) {                                     \
     673              :         ccol__cbmap_val = (__typeof__(*hm_name##__ccol_val_type_var)           \
     674              :                                *)(uintptr_t)&ccol__cbmap_acc->ptr;             \
     675              :       }                                                                        \
     676              :     } else if (ccol__cbmap_kp->ptr) {                                          \
     677              :       ccol__cbmap_r =                                                          \
     678              :           cbstmap_get_elem_ref(hm_name, ccol__cbmap_kp, &ccol__cbmap_vp);      \
     679              :       if (ccol__cbmap_r == ccol_success) {                                     \
     680              :         if (ccol__cbmap_vp.size != sizeof(*ccol__cbmap_val)) {                 \
     681              :           ccol__fatal_err(                                                     \
     682              :               "cbmap_get_ptr('%s'): value size mismatch - stored: %lu "        \
     683              :               "bytes, requested: %lu bytes; wrong type or missing "            \
     684              :               "cbmap_redeclare()?",                                            \
     685              :               #hm_name, (unsigned long)ccol__cbmap_vp.size,                    \
     686              :               (unsigned long)sizeof(*ccol__cbmap_val));                        \
     687              :         }                                                                      \
     688              :         ccol__cbmap_val =                                                      \
     689              :             (__typeof__(*hm_name##__ccol_val_type_var) *)(ccol__cbmap_vp.ptr); \
     690              :       }                                                                        \
     691              :     }                                                                          \
     692              :     (__typeof__(ccol__cbmap_value_ptr_result(                                  \
     693              :         hm_name,                                                               \
     694              :         (__typeof__(*hm_name##__ccol_val_type_var) *)0)))(ccol__cbmap_val);    \
     695              :   })
     696              : 
     697              : // Evaluates to the number of key-value pairs in the map, as a size_t. Asserts
     698              : // on a NULL handle.
     699              : #define cbmap_size(cbm) cbstmap_size((cbm))
     700              : 
     701              : // Removes and frees every key and value and leaves an empty map that you can
     702              : // keep using. Evaluates to ccol_success. Asserts on a NULL handle.
     703              : #define cbmap_reset(cbm) cbstmap_reset((cbm))
     704              : 
     705              : #pragma GCC visibility pop
        

Generated by: LCOV version 2.0-1