ucx/cx/kv_list.h

changeset 112
c3f2f16fa4b8
child 113
dde28a806552
equal deleted inserted replaced
111:81c4f73236a4 112:c3f2f16fa4b8
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2025 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 */
28 /**
29 * @file kv_list.h
30 * @brief Linked list implementation with key/value-lookup.
31 * @author Mike Becker
32 * @author Olaf Wintermann
33 * @copyright 2-Clause BSD License
34 */
35
36 #ifndef UCX_KV_LIST_H
37 #define UCX_KV_LIST_H
38
39 #include "common.h"
40 #include "list.h"
41 #include "map.h"
42
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46
47 /**
48 * Allocates a linked list with a lookup-map for storing elements with @p elem_size bytes each.
49 *
50 * If @p elem_size is #CX_STORE_POINTERS, the created list stores pointers instead of
51 * copies of the added elements, and the compare function will be automatically set
52 * to cx_cmp_ptr() if none is given.
53 *
54 * After creating the list, it can also be used as a map after converting the pointer
55 * to a CxMap pointer with cxKvListAsMap().
56 * When you want to use the list interface again, you can also convert the map pointer back
57 * with cxKvListAsList().
58 *
59 * @param allocator the allocator for allocating the list nodes
60 * (if @c NULL, the cxDefaultAllocator will be used)
61 * @param comparator the comparator for the elements
62 * (if @c NULL, and the list is not storing pointers, sort and find
63 * functions will not work)
64 * @param elem_size the size of each element in bytes
65 * @return the created list
66 * @see cxKvListAsMap()
67 * @see cxKvListAsList()
68 */
69 cx_attr_nodiscard
70 cx_attr_malloc
71 cx_attr_dealloc(cxListFree, 1)
72 cx_attr_export
73 CxList *cxKvListCreate(
74 const CxAllocator *allocator,
75 cx_compare_func comparator,
76 size_t elem_size
77 );
78
79 /**
80 * Allocates a linked list with a lookup-map for storing elements with @p elem_size bytes each.
81 *
82 * If @p elem_size is #CX_STORE_POINTERS, the created list stores pointers instead of
83 * copies of the added elements, and the compare function will be automatically set
84 * to cx_cmp_ptr() if none is given.
85 *
86 * This function creates the list with cxKvListCreate() and immediately applies
87 * cxKvListAsMap(). If you want to use the returned object as a list, you can call
88 * cxKvListAsList() later.
89 *
90 * @param allocator the allocator for allocating the list nodes
91 * (if @c NULL, the cxDefaultAllocator will be used)
92 * @param comparator the comparator for the elements
93 * (if @c NULL, and the list is not storing pointers, sort and find
94 * functions will not work)
95 * @param elem_size the size of each element in bytes
96 * @return the created list wrapped into the CxMap interface
97 * @see cxKvListAsMap()
98 * @see cxKvListAsList()
99 */
100 cx_attr_nodiscard
101 cx_attr_malloc
102 cx_attr_dealloc(cxMapFree, 1)
103 cx_attr_export
104 CxMap *cxKvListCreateAsMap(
105 const CxAllocator *allocator,
106 cx_compare_func comparator,
107 size_t elem_size
108 );
109
110 /**
111 * Allocates a linked list with a lookup-map for storing elements with @p elem_size bytes each.
112 *
113 * The list will use cxDefaultAllocator and no comparator function. If you want
114 * to call functions that need a comparator, you must either set one immediately
115 * after list creation or use cxKvListCreate().
116 *
117 * If @p elem_size is #CX_STORE_POINTERS, the created list stores pointers instead of
118 * copies of the added elements, and the compare function will be automatically set
119 * to cx_cmp_ptr().
120 *
121 * After creating the list, it can also be used as a map after converting the pointer
122 * to a CxMap pointer with cxKvListAsMap().
123 * When you want to use the list interface again, you can also convert the map pointer back
124 * with cxKvListAsList().
125 *
126 * @param elem_size (@c size_t) the size of each element in bytes
127 * @return (@c CxList*) the created list
128 * @see cxKvListAsMap()
129 * @see cxKvListAsList()
130 */
131 #define cxKvListCreateSimple(elem_size) cxKvListCreate(NULL, NULL, elem_size)
132
133 /**
134 * Allocates a linked list with a lookup-map for storing elements with @p elem_size bytes each.
135 *
136 * The list will use cxDefaultAllocator and no comparator function. If you want
137 * to call functions that need a comparator, you must either set one immediately
138 * after list creation or use cxKvListCreate().
139 *
140 * If @p elem_size is #CX_STORE_POINTERS, the created list stores pointers instead of
141 * copies of the added elements, and the compare function will be automatically set
142 * to cx_cmp_ptr().
143 *
144 * This macro behaves as if the list was created with cxKvListCreateSimple() and
145 * immediately followed up by cxKvListAsMap().
146 * If you want to use the returned object as a list, you can call cxKvListAsList() later.
147 *
148 * @param elem_size (@c size_t) the size of each element in bytes
149 * @return (@c CxMap*) the created list wrapped into the CxMap interface
150 * @see cxKvListAsMap()
151 * @see cxKvListAsList()
152 */
153 #define cxKvListCreateAsMapSimple(elem_size) cxKvListCreateAsMap(NULL, NULL, elem_size)
154
155 /**
156 * Converts a map pointer belonging to a key-value-List back to the original list pointer.
157 *
158 * @param map a map pointer that was returned by a call to cxKvListAsMap()
159 * @return the original list pointer
160 */
161 cx_attr_nodiscard
162 cx_attr_nonnull
163 cx_attr_returns_nonnull
164 cx_attr_export
165 CxList *cxKvListAsList(CxMap *map);
166
167 /**
168 * Converts a map pointer belonging to a key-value-List back to the original list pointer.
169 *
170 * @param list a list created by cxKvListCreate() or cxKvListCreateSimple()
171 * @return a map pointer that lets you use the list as if it was a map
172 */
173 cx_attr_nodiscard
174 cx_attr_nonnull
175 cx_attr_returns_nonnull
176 cx_attr_export
177 CxMap *cxKvListAsMap(CxList *list);
178
179 /**
180 * Sets or updates the key of a list item.
181 *
182 * This is, for example, useful when you have inserted an element using the CxList interface,
183 * and now you want to associate this element with a key.
184 *
185 * @param list the list
186 * @param index the index of the element in the list
187 * @param key the key
188 * @retval zero success
189 * @retval non-zero memory allocation failure or the index is out of bounds
190 * @see cxKvListSetKey()
191 */
192 cx_attr_nonnull
193 cx_attr_export
194 int cx_kv_list_set_key(CxList *list, size_t index, CxHashKey key);
195
196 /**
197 * Inserts an item into the list at the specified index and associates it with the specified key.
198 *
199 * @param list the list
200 * @param index the index the inserted element shall have
201 * @param key the key
202 * @param value the value
203 * @retval zero success
204 * @retval non-zero memory allocation failure or the index is out of bounds
205 * @see cxKvListInsert()
206 */
207 cx_attr_nonnull
208 cx_attr_export
209 int cx_kv_list_insert(CxList *list, size_t index, CxHashKey key, void *value);
210
211 /**
212 * Sets or updates the key of a list item.
213 *
214 * This is, for example, useful when you have inserted an element using the CxList interface,
215 * and now you want to associate this element with a key.
216 *
217 * @param list (@c CxList*) the list
218 * @param index (@c size_t) the index of the element in the list
219 * @param key (any supported key type) the key
220 * @retval zero success
221 * @retval non-zero memory allocation failure or the index is out of bounds
222 * @see CX_HASH_KEY()
223 */
224 #define cxKvListSetKey(list, index, key) cx_kv_list_set_key(list, index, CX_HASH_KEY(key))
225
226 /**
227 * Inserts an item into the list at the specified index and associates it with the specified key.
228 *
229 * @param list (@c CxList*) the list
230 * @param index (@c size_t) the index the inserted element shall have
231 * @param key (any supported key type) the key
232 * @param value (@c void*) the value
233 * @retval zero success
234 * @retval non-zero memory allocation failure or the index is out of bounds
235 * @see CX_HASH_KEY()
236 */
237 #define cxKvListInsert(list, index, key, value) cx_kv_list_insert(list, index, CX_HASH_KEY(key), value)
238
239
240 /**
241 * Removes the key of a list item.
242 *
243 * This can be useful if you want to explicitly remove an item from the lookup map.
244 *
245 * If no key is associated with the item, nothing happens, and this function returns zero.
246 *
247 * @param list the list
248 * @param index the index of the element in the list
249 * @retval zero success
250 * @retval non-zero the index is out of bounds
251 */
252 cx_attr_nonnull
253 cx_attr_export
254 int cxKvListRemoveKey(CxList *list, size_t index);
255
256 /**
257 * Returns the key of a list item.
258 *
259 * @param list the list
260 * @param index the index of the element in the list
261 * @return a pointer to the key or @c NULL when the index is out of bounds or the item does not have a key
262 */
263 cx_attr_nonnull
264 cx_attr_export
265 const CxHashKey *cxKvListGetKey(CxList *list, size_t index);
266
267 /**
268 * Adds an item into the list and associates it with the specified key.
269 *
270 * @param list (@c CxList*) the list
271 * @param key (@c CxHashKey, @c char*, @c cxstring, or @c cxmutstr) the key
272 * @param value (@c void*) the value
273 * @retval zero success
274 * @retval non-zero memory allocation failure
275 */
276 #define cxKvListAdd(list, key, value) cxKvListInsert(list, (list)->collection.size, key, value)
277
278 #ifdef __cplusplus
279 } // extern "C"
280 #endif
281
282 #endif // UCX_KV_LIST_H

mercurial