|
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 */ |
|
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 (@c CxList*) the list |
|
186 * @param key (@c CxHashKey, @c char*, @c cxstring, or @c cxmutstr) the key |
|
187 * @param index (@c size_t) the index of the element in the list |
|
188 * @retval zero success |
|
189 * @retval non-zero memory allocation failure or the index is out of bounds |
|
190 */ |
|
191 #define cxKvListSetKey(list, key, index) |
|
192 |
|
193 /** |
|
194 * Removes the key for a list item. |
|
195 * |
|
196 * This can be useful if you want to explicitly remove an item from the lookup map, |
|
197 * for example, when you want to prevent a deletion by cxMapClear(). |
|
198 * |
|
199 * @param list (@c CxList*) the list |
|
200 * @param key (@c CxHashKey, @c char*, @c cxstring, or @c cxmutstr) the key |
|
201 * @param index (@c size_t) the index of the element in the list |
|
202 * @retval zero success |
|
203 * @retval non-zero the index is out of bounds |
|
204 */ |
|
205 #define cxKvListRemoveKey(list, key, index) |
|
206 |
|
207 /** |
|
208 * Inserts an item into the list at the specified index and associates it with the specified key. |
|
209 * |
|
210 * @param list (@c CxList*) the list |
|
211 * @param index (@c size_t) the index the inserted element shall have |
|
212 * @param key (@c CxHashKey, @c char*, @c cxstring, or @c cxmutstr) the key |
|
213 * @param value (@c void*) the value |
|
214 * @retval zero success |
|
215 * @retval non-zero memory allocation failure or the index is out of bounds |
|
216 */ |
|
217 #define cxKvListInsert(list, index, key, value) |
|
218 |
|
219 /** |
|
220 * Adds an item into the list and associates it with the specified key. |
|
221 * |
|
222 * @param list (@c CxList*) the list |
|
223 * @param key (@c CxHashKey, @c char*, @c cxstring, or @c cxmutstr) the key |
|
224 * @param value (@c void*) the value |
|
225 * @retval zero success |
|
226 * @retval non-zero memory allocation failure or the index is out of bounds |
|
227 */ |
|
228 #define cxKvListAdd(list, index, key, value) cxKvListInsert(list, list->collection.size, key, value) |
|
229 |
|
230 #ifdef __cplusplus |
|
231 } // extern "C" |
|
232 #endif |
|
233 |
|
234 #endif // UCX_KV_LIST_H |