LCOV - code coverage report
Current view: top level - lib/generic - map.h Hit Total Coverage
Test: Knot Resolver 3.2.1-POSIX coverage report Lines: 2 2 100.0 %
Date: 2019-03-12 03:31:59
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * critbit89 - A crit-bit map implementation for strings in C89
       3             :  * Written by Jonas Gehring <jonas@jgehring.net>
       4             :  */
       5             : 
       6             : /**
       7             :  * @file map.h
       8             :  * @brief A Crit-bit tree key-value map implementation.
       9             :  *
      10             :  * @warning If the user provides a custom allocator, it must return addresses aligned to 2B boundary.
      11             :  *
      12             :  * # Example usage:
      13             :  *
      14             :  * @code{.c}
      15             :  *      map_t map = map_make(NULL);
      16             :  *
      17             :  *      // Custom allocator (optional)
      18             :  *      map.malloc = &mymalloc;
      19             :  *      map.baton  = &mymalloc_context;
      20             :  *
      21             :  *      // Insert k-v pairs
      22             :  *      int values = { 42, 53, 64 };
      23             :  *      if (map_set(&map, "princess", &values[0]) != 0 ||
      24             :  *          map_set(&map, "prince", &values[1])   != 0 ||
      25             :  *          map_set(&map, "leia", &values[2])     != 0) {
      26             :  *          fail();
      27             :  *      }
      28             :  *
      29             :  *      // Test membership
      30             :  *      if (map_contains(&map, "leia")) {
      31             :  *          success();
      32             :  *      }
      33             :  *
      34             :  *      // Prefix search
      35             :  *      int i = 0;
      36             :  *      int count(const char *k, void *v, void *ext) { (*(int *)ext)++; return 0; }
      37             :  *      if (map_walk_prefixed(map, "princ", count, &i) == 0) {
      38             :  *          printf("%d matches\n", i);
      39             :  *      }
      40             :  *
      41             :  *      // Delete
      42             :  *      if (map_del(&map, "badkey") != 0) {
      43             :  *          fail(); // No such key
      44             :  *      }
      45             :  *
      46             :  *      // Clear the map
      47             :  *      map_clear(&map);
      48             :  * @endcode
      49             :  *
      50             :  * \addtogroup generics
      51             :  * @{
      52             :  */
      53             : 
      54             : #pragma once
      55             : 
      56             : #include <stddef.h>
      57             : 
      58             : #ifdef __cplusplus
      59             : extern "C" {
      60             : #endif
      61             : 
      62             : struct knot_mm; /* avoid the unnecessary include */
      63             : 
      64             : /** Main data structure */
      65             : typedef struct {
      66             :         void *root;
      67             :         struct knot_mm *pool;
      68             : } map_t;
      69             : 
      70             : /** Creates an new empty critbit map.  Pass NULL for malloc+free. */
      71          24 : static inline map_t map_make(struct knot_mm *pool)
      72             : {
      73          24 :         return (map_t){ .root = NULL, .pool = pool };
      74             : }
      75             : 
      76             : /** Returns non-zero if map contains str */
      77             : int map_contains(map_t *map, const char *str);
      78             : 
      79             : /** Returns value if map contains str.  Note: NULL may mean two different things. */
      80             : void *map_get(map_t *map, const char *str);
      81             : 
      82             : /** Inserts str into map.  Returns 0 if new, 1 if replaced, or ENOMEM. */
      83             : int map_set(map_t *map, const char *str, void *val);
      84             : 
      85             : /** Deletes str from the map, returns 0 on suceess */
      86             : int map_del(map_t *map, const char *str);
      87             : 
      88             : /** Clears the given map */
      89             : void map_clear(map_t *map);
      90             : 
      91             : /**
      92             :  * Calls callback for all strings in map
      93             :  * See @fn map_walk_prefixed() for documentation on parameters.
      94             :  */
      95             : #define map_walk(map, callback, baton) \
      96             :         map_walk_prefixed((map), "", (callback), (baton))
      97             : 
      98             : /**
      99             :  * Calls callback for all strings in map with the given prefix.
     100             :  * Returns value immediately if a callback returns nonzero.
     101             :  *
     102             :  * @param map
     103             :  * @param prefix   required string prefix (empty => all strings)
     104             :  * @param callback callback parameters are (key, value, baton)
     105             :  * @param baton    passed uservalue
     106             :  */
     107             : int map_walk_prefixed(map_t *map, const char *prefix,
     108             :         int (*callback)(const char *, void *, void *), void *baton);
     109             : 
     110             : 
     111             : #ifdef __cplusplus
     112             : }
     113             : #endif
     114             : 
     115             : /** @} */

Generated by: LCOV version 1.13