docs/Writerside/topics/kv_list.h.md

Thu, 25 Sep 2025 14:30:28 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 25 Sep 2025 14:30:28 +0200
changeset 1394
7b23c6db9500
parent 1390
ff077f793c5d
permissions
-rw-r--r--

add cxKvListGetKey()

resolves #461

1390
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 # Key/Value List
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 The key/value list is a linked list of key/value pairs.
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 It implements both the `CxList` and `CxMap` interfaces.
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 ```C
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 #include <cx/kv_list.h>
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 CxList *cxKvListCreate(const CxAllocator *allocator,
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 cx_compare_func comparator, size_t elem_size);
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 CxList *cxKvListCreateSimple(size_t elem_size);
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 CxMap *cxKvListCreateAsMap(const CxAllocator *allocator,
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 cx_compare_func comparator, size_t elem_size);
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 CxMap *cxKvListCreateAsMapSimple(size_t elem_size);
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 ```
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 The lists are created as usual linked lists with an additional map to look up items by key.
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 The map is created with the same allocator as the list.
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 You can use either interface to access the list.
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 Also, defining destructors on either interface will work as if you defined them on the list.
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 Using the list interface to insert items will insert them without an assigned key,
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 but you can always set a key later using the `cxKvListSetKey()` function (see [](#special-functions) below).
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 > The special thing about key/value lists when used with the map interface is,
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28 > that when you iterate over the map, the elements are returned in the order they appear in the list.
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29 > In contrast, a normal hash map will return the elements in arbitrary order.
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30 > This way you can use a key/value list as an ordered map.
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31 > {style="note"}
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33 ## Converting between the Interfaces
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
34
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35 To easily convert between the `CxList` and `CxMap` interfaces, we provide the following functions.
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37 ```C
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38 #include <cx/kv_list.h>
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40 CxMap *cxKvListAsMap(CxList *list);
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
42 CxList *cxKvListAsList(CxMap *map);
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
43 ```
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
44
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45 They must only be used on lists that have been created as key/value lists.
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
46 Using them with other lists will result in undefined behavior.
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
47
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48 ## Special Functions
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50 The key/value list has some special functions that are not part of the `CxList` or `CxMap` interface.
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51 They also may only be used with lists that have been created as key/value lists.
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52 Using them with other lists will result in undefined behavior.
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 ```C
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55 #include <cx/kv_list.h>
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 int cxKvListSetKey(CxList *list, size_t index, KeyType key);
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 int cxKvListRemoveKey(CxList *list, size_t index);
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60
1394
7b23c6db9500 add cxKvListGetKey()
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
61 const CxHashKey *cxKvListGetKey(CxList *list, size_t index);
7b23c6db9500 add cxKvListGetKey()
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
62
1390
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63 int cxKvListAdd(CxList *list, KeyType key, void *value);
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65 int cxKvListInsert(CxList *list, size_t index,
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
66 KeyType key, void *value);
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67 ```
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69 > All functions working with keys are implemented as generics, so that the following types are supported:
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 > `CxHashKey`, `cxstring`, `cxmutstr`, `const char*`, and `char*`.
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71 > When we write `KeyType`, we mean any of these types.
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72 > {style="note"}
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74 The `cxKvListSetKey()` and `cxKvListRemoveKey()` functions are used to set or remove the key of an element.
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
75 They return zero on success and non-zero on failure.
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
76 A failure can happen when the index is out of bounds or when a memory allocation failed.
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
77 The `cxKvListSetKey()` function also returns non-zero if the key already exists for _another_ element.
1394
7b23c6db9500 add cxKvListGetKey()
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
78 With `cxKvListGetKey()`, you can retrieve the key of an element.
7b23c6db9500 add cxKvListGetKey()
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
79 This function returns `NULL` when an element has no key assigned or the index is out of bounds.
1390
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81 With the `cxKvListAdd()` and `cxKvListInsert()` functions, you can add a new element to the list and immediately assign a key to it.
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82 The `cxKvListAdd()` function will add the element at the end of the list,
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83 while the `cxKvListInsert()` function will insert the element at the specified index.
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
85 > The effect of `cxKvListAdd()` using the list interface and `cxMapPut()` using the map interface is the same.

mercurial