src/cx/kv_list.h

changeset 1348
a1da355ed3b8
parent 1347
d02b1fde73ee
equal deleted inserted replaced
1347:d02b1fde73ee 1348:a1da355ed3b8
1 /* 1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 * 3 *
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. 4 * Copyright 2025 Mike Becker, Olaf Wintermann All rights reserved.
5 * 5 *
6 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met: 7 * modification, are permitted provided that the following conditions are met:
8 * 8 *
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
174 cx_attr_nonnull 174 cx_attr_nonnull
175 cx_attr_returns_nonnull 175 cx_attr_returns_nonnull
176 cx_attr_export 176 cx_attr_export
177 CxMap *cxKvListAsMap(CxList *list); 177 CxMap *cxKvListAsMap(CxList *list);
178 178
179 cx_attr_nonnull
180 cx_attr_export
181 int cx_kv_list_set_key(CxList *list, size_t index, CxHashKey key);
182
183 cx_attr_nonnull
184 cx_attr_export
185 int cx_kv_list_remove_key(CxList *list, size_t index, CxHashKey key);
186
187 cx_attr_nonnull
188 cx_attr_export
189 int cx_kv_list_insert(CxList *list, size_t index, CxHashKey key, void *value);
190
191 // Generic Functions
192 #ifdef __cplusplus
193
194 cx_attr_nonnull
195 static inline int cxKvListSetKey(CxList *list, size_t index, CxHashKey key) {
196 return cx_kv_list_set_key(list, index, key);
197 }
198
199 cx_attr_nonnull
200 static inline int cxKvListSetKey(CxList *list, size_t index, cxstring key) {
201 return cx_kv_list_set_key(list, index, cx_hash_key_cxstr(key));
202 }
203
204 cx_attr_nonnull
205 static inline int cxKvListSetKey(CxList *list, size_t index, cxmutstr key) {
206 return cx_kv_list_set_key(list, index, cx_hash_key_cxstr(key));
207 }
208
209 cx_attr_nonnull
210 cx_attr_cstr_arg(2)
211 static inline int cxKvListSetKey(CxList *list, size_t index, const char *key) {
212 return cx_kv_list_set_key(list, index, cx_hash_key_str(key));
213 }
214
215 cx_attr_nonnull
216 static inline int cxKvListRemoveKey(CxList *list, size_t index, CxHashKey key) {
217 return cx_kv_list_remove_key(list, index, key);
218 }
219
220 cx_attr_nonnull
221 static inline int cxKvListRemoveKey(CxList *list, size_t index, cxstring key) {
222 return cx_kv_list_remove_key(list, index, cx_hash_key_cxstr(key));
223 }
224
225 cx_attr_nonnull
226 static inline int cxKvListRemoveKey(CxList *list, size_t index, cxmutstr key) {
227 return cx_kv_list_remove_key(list, index, cx_hash_key_cxstr(key));
228 }
229
230 cx_attr_nonnull
231 cx_attr_cstr_arg(2)
232 static inline int cxKvListRemoveKey(CxList *list, size_t index, const char *key) {
233 return cx_kv_list_remove_key(list, index, cx_hash_key_str(key));
234 }
235
236 cx_attr_nonnull
237 static inline int cxKvListInsert(CxList *list, size_t index, CxHashKey key, void *value) {
238 return cx_kv_list_insert(list, index, key, value, value);
239 }
240
241 cx_attr_nonnull
242 static inline int cxKvListInsert(CxList *list, size_t index, cxstring key, void *value) {
243 return cx_kv_list_insert(list, index, cx_hash_key_cxstr(key), value);
244 }
245
246 cx_attr_nonnull
247 static inline int cxKvListInsert(CxList *list, size_t index, cxmutstr key, void *value) {
248 return cx_kv_list_insert(list, index, cx_hash_key_cxstr(key), value);
249 }
250
251 cx_attr_nonnull
252 cx_attr_cstr_arg(2)
253 static inline int cxKvListInsert(CxList *list, size_t index, const char *key, void *value) {
254 return cx_kv_list_insert(list, index, cx_hash_key_str(key), value);
255 }
256
257 #else /* ! __cplusplus */
179 /** 258 /**
180 * Sets or updates the key of a list item. 259 * Sets or updates the key of a list item.
181 * 260 *
182 * This is, for example, useful when you have inserted an element using the CxList interface, 261 * 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. 262 * and now you want to associate this element with a key.
184 * 263 *
185 * @param list (@c CxList*) the list 264 * @param list (@c CxList*) the list
265 * @param index (@c size_t) the index of the element in the list
186 * @param key (@c CxHashKey, @c char*, @c cxstring, or @c cxmutstr) the key 266 * @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 267 * @retval zero success
189 * @retval non-zero memory allocation failure or the index is out of bounds 268 * @retval non-zero memory allocation failure or the index is out of bounds
190 */ 269 */
191 #define cxKvListSetKey(list, key, index) 270 #define cxKvListSetKey(list, index, key) _Generic((key), \
271 CxHashKey: cx_kv_list_set_key, \
272 cxstring: cx_kv_list_set_key_cxstr, \
273 cxmutstr: cx_kv_list_set_key_mustr, \
274 char*: cx_kv_list_set_key_str, \
275 const char*: cx_kv_list_set_key_str) \
276 (list, index, key)
277
278 static inline int cx_kv_list_set_key_cxstr(CxList *list, size_t index, cxstring key) {
279 return cx_kv_list_set_key(list, index, cx_hash_key_cxstr(key));
280 }
281
282 static inline int cx_kv_list_set_key_mustr(CxList *list, size_t index, cxmutstr key) {
283 return cx_kv_list_set_key(list, index, cx_hash_key_cxstr(key));
284 }
285
286 static inline int cx_kv_list_set_key_str(CxList *list, size_t index, const char *key) {
287 return cx_kv_list_set_key(list, index, cx_hash_key_str(key));
288 }
192 289
193 /** 290 /**
194 * Removes the key for a list item. 291 * Removes the key for a list item.
195 * 292 *
196 * This can be useful if you want to explicitly remove an item from the lookup map, 293 * 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(). 294 * for example, when you want to prevent a deletion by cxMapClear().
198 * 295 *
199 * @param list (@c CxList*) the list 296 * @param list (@c CxList*) the list
297 * @param index (@c size_t) the index of the element in the list
200 * @param key (@c CxHashKey, @c char*, @c cxstring, or @c cxmutstr) the key 298 * @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 299 * @retval zero success
203 * @retval non-zero the index is out of bounds 300 * @retval non-zero the index is out of bounds
204 */ 301 */
205 #define cxKvListRemoveKey(list, key, index) 302 #define cxKvListRemoveKey(list, index, key) _Generic((key), \
303 CxHashKey: cx_kv_list_remove_key, \
304 cxstring: cx_kv_list_remove_key_cxstr, \
305 cxmutstr: cx_kv_list_remove_key_mustr, \
306 char*: cx_kv_list_remove_key_str, \
307 const char*: cx_kv_list_remove_key_str) \
308 (list, index, key)
309
310 static inline int cx_kv_list_remove_key_cxstr(CxList *list, size_t index, cxstring key) {
311 return cx_kv_list_remove_key(list, index, cx_hash_key_cxstr(key));
312 }
313
314 static inline int cx_kv_list_remove_key_mustr(CxList *list, size_t index, cxmutstr key) {
315 return cx_kv_list_remove_key(list, index, cx_hash_key_cxstr(key));
316 }
317
318 static inline int cx_kv_list_remove_key_str(CxList *list, size_t index, const char *key) {
319 return cx_kv_list_remove_key(list, index, cx_hash_key_str(key));
320 }
206 321
207 /** 322 /**
208 * Inserts an item into the list at the specified index and associates it with the specified key. 323 * Inserts an item into the list at the specified index and associates it with the specified key.
209 * 324 *
210 * @param list (@c CxList*) the list 325 * @param list (@c CxList*) the list
212 * @param key (@c CxHashKey, @c char*, @c cxstring, or @c cxmutstr) the key 327 * @param key (@c CxHashKey, @c char*, @c cxstring, or @c cxmutstr) the key
213 * @param value (@c void*) the value 328 * @param value (@c void*) the value
214 * @retval zero success 329 * @retval zero success
215 * @retval non-zero memory allocation failure or the index is out of bounds 330 * @retval non-zero memory allocation failure or the index is out of bounds
216 */ 331 */
217 #define cxKvListInsert(list, index, key, value) 332 #define cxKvListInsert(list, index, key, value) _Generic((key), \
333 CxHashKey: cx_kv_list_insert, \
334 cxstring: cx_kv_list_insert_cxstr, \
335 cxmutstr: cx_kv_list_insert_mustr, \
336 char*: cx_kv_list_insert_str, \
337 const char*: cx_kv_list_insert_str) \
338 (list, index, key, value)
339
340 static inline int cx_kv_list_insert_cxstr(CxList *list, size_t index, cxstring key, void *value) {
341 return cx_kv_list_insert(list, index, cx_hash_key_cxstr(key), value);
342 }
343
344 static inline int cx_kv_list_insert_mustr(CxList *list, size_t index, cxmutstr key, void *value) {
345 return cx_kv_list_insert(list, index, cx_hash_key_cxstr(key), value);
346 }
347
348 static inline int cx_kv_list_insert_str(CxList *list, size_t index, const char *key, void *value) {
349 return cx_kv_list_insert(list, index, cx_hash_key_str(key), value);
350 }
351
352 #endif
218 353
219 /** 354 /**
220 * Adds an item into the list and associates it with the specified key. 355 * Adds an item into the list and associates it with the specified key.
221 * 356 *
222 * @param list (@c CxList*) the list 357 * @param list (@c CxList*) the list

mercurial