libcstl
Loading...
Searching...
No Matches
map.h
Go to the documentation of this file.
1/*!
2 * @file
3 */
4
5#ifndef CSTL_MAP_H
6#define CSTL_MAP_H
7
8/*!
9 * @defgroup map Map
10 * @ingroup highlevel
11 * @brief A container of key/value pairs with unique keys
12 */
13/*!
14 * @addtogroup map
15 * @{
16 */
17
18#include "cstl/rbtree.h"
19
20#include <stdbool.h>
21
22/*!
23 * @brief The map object
24 *
25 * The map may be declared on the stack or allocated. In either
26 * case, it must be initialized via cstl_map_init(). The map
27 * must be cleared with cstl_map_clear() to free any memory alloced
28 * by the map itself.
29 */
30typedef struct
31{
32 /*! @privatesection */
33 struct cstl_rbtree t;
34 struct
35 {
36 /*! @privatesection */
38 void * p;
39 } cmp;
41
42/*!
43 * @brief A pointer to an element within the map
44 */
45typedef struct
46{
47 /*! @brief Pointer to the key associated with the element */
48 const void * key;
49 /*! @brief Pointer to the value contained by the element */
50 void * val;
51
52 /*! @private */
53 void * _;
55
56/*!
57 * @name Iterators
58 * @{
59 */
60
61/*!
62 * @brief Return an iterator that refers to the end of the map
63 *
64 * Iterators for functions like cstl_map_find() and cstl_map_erase() that
65 * do not find a corresponding entry in the map will return an iterator
66 * that will compare equal with this value.
67 *
68 * @param[in] map A pointer to the map associated with the iterator
69 *
70 * @return A pointer to an iterator referring to the end of the map
71 */
73/*!
74 * @brief Compare two iterators for equality
75 *
76 * @param[in] a A pointer to a map iterator
77 * @param[in] b A pointer to a map iterator
78 *
79 * @retval true The iterators are equal
80 * @retval false The iterators are not equal
81 */
82static inline bool cstl_map_iterator_eq(
83 const cstl_map_iterator_t * const a, const cstl_map_iterator_t * const b)
84{
85 return a->_ == b->_;
86}
87
88/*!
89 * @}
90 */
91
92/*!
93 * @brief Initialize a map
94 *
95 * Initializing a previously initialized map that has not been cleared
96 * may cause the loss/leaking of memory.
97 *
98 * @param[out] map The map to be initialized
99 * @param[in] cmp A function to be used to compare keys
100 * @param[in] priv A pointer to be passed to each invocation of @p cmp
101 */
102void cstl_map_init(cstl_map_t * map, cstl_compare_func_t * cmp, void * priv);
103
104/*!
105 * @brief Return the number of elements in the map
106 *
107 * @param[in] map A pointer to the map
108 *
109 * @return The number of elements in the map
110 */
111static inline size_t cstl_map_size(const cstl_map_t * const map)
112{
113 return cstl_rbtree_size(&map->t);
114}
115
116/*!
117 * @brief Insert a key/value pair into the map
118 *
119 * @param[in] map The map into which to insert the pair
120 * @param[in] key A pointer to the key
121 * @param[in] val A pointer to the value
122 * @param[out] i A pointer to an iterator in which to return a pointer to
123 * the new or existing element in the map. This parameter
124 * may be NULL
125 *
126 * @retval -1 The function failed to allocate memory
127 * @retval 0 The function successfully inserted the pair, and @p i
128 * points to the inserted element
129 * @retval 1 An element with the same key already exists in the map. The
130 * new pair was not inserted, @p i points to the existing element
131 * in the map
132 */
134 const void * key, void * val,
136
137/*!
138 * @brief Find an element in the map with a matching key
139 *
140 * @param[in] map A pointer to the map to be searched
141 * @param[in] key A pointer to the key that is sought
142 * @param[out] i A pointer to an iterator in which to return a pointer to the
143 * sought element. This parameter may not be NULL
144 *
145 * The @p i parameter will be "end" if the element is not found
146 */
147void cstl_map_find(const cstl_map_t * map, const void * key,
149
150/*!
151 * @brief Erase the element with the supplied key from the map
152 *
153 * @param[in] map A pointer to the map in which to search for the key
154 * @param[in] key A pointer to the key to be removed
155 * @param[out] i A pointer to an iterator in which to return a pointer to the
156 * sought element. This parameter may be NULL
157 *
158 * The @p i parameter will compare as equal with "end" upon return; however,
159 * if an element with a matching key existed, the @p key and @p val members
160 * will point to the key and value, respectively that we contained in the
161 * element.
162 *
163 * @retval 0 The element was found and removed
164 * @retval -1 No matching element was found
165 */
166int cstl_map_erase(cstl_map_t * map, const void * key,
168
169/*!
170 * @brief Erase the element pointed to by the iterator
171 *
172 * @param[in] map The map into which the iterator points
173 * @param[in] i A pointer to the iterator indicating which element to removed
174 */
176
177/*!
178 * @brief Remove all elements from the map
179 *
180 * @param[in] map A pointer to the map to be cleared
181 * @param[in] clr A function to call for each element as it is removed
182 * @param[in] priv A pointer to be passed to each invocation of @p clr
183 *
184 * The first argument to the @p clr function will be an iterator containing
185 * pointers to the key and value associated with the removed element. It is
186 * undefined whether the element is still in the tree at the time that the
187 * @p clr function is called, and the callee must not do anything with the
188 * iterator except retrieve the key and value pointers.
189 */
190void cstl_map_clear(cstl_map_t * map, cstl_xtor_func_t * clr, void * priv);
191
192/*!
193 * @}
194 */
195
196#endif
void cstl_xtor_func_t(void *obj, void *priv)
Type for functions called to construct, clear, or destroy an object.
Definition common.h:97
int cstl_compare_func_t(const void *obj1, const void *obj2, void *priv)
Function type for comparing (in)equality of two objects.
Definition common.h:49
int cstl_map_erase(cstl_map_t *map, const void *key, cstl_map_iterator_t *i)
Erase the element with the supplied key from the map.
Definition map.c:144
void cstl_map_clear(cstl_map_t *map, cstl_xtor_func_t *clr, void *priv)
Remove all elements from the map.
Definition map.c:102
static size_t cstl_map_size(const cstl_map_t *const map)
Return the number of elements in the map.
Definition map.h:111
void cstl_map_find(const cstl_map_t *map, const void *key, cstl_map_iterator_t *i)
Find an element in the map with a matching key.
Definition map.c:136
void cstl_map_init(cstl_map_t *map, cstl_compare_func_t *cmp, void *priv)
Initialize a map.
Definition map.c:114
static bool cstl_map_iterator_eq(const cstl_map_iterator_t *const a, const cstl_map_iterator_t *const b)
Compare two iterators for equality.
Definition map.h:82
void cstl_map_erase_iterator(cstl_map_t *map, cstl_map_iterator_t *i)
Erase the element pointed to by the iterator.
Definition map.c:165
int cstl_map_insert(cstl_map_t *map, const void *key, void *val, cstl_map_iterator_t *i)
Insert a key/value pair into the map.
Definition map.c:173
const cstl_map_iterator_t * cstl_map_iterator_end(const cstl_map_t *map)
Return an iterator that refers to the end of the map.
Definition map.c:17
static size_t cstl_rbtree_size(const struct cstl_rbtree *const t)
Get the number of objects in the tree.
Definition rbtree.h:145
A pointer to an element within the map.
Definition map.h:46
void * val
Pointer to the value contained by the element.
Definition map.h:50
const void * key
Pointer to the key associated with the element.
Definition map.h:48
The map object.
Definition map.h:31
Red-black tree object.
Definition rbtree.h:75