docs/Writerside/topics/list.h.md

Thu, 06 Mar 2025 20:28:34 +0100

author
Mike Becker <universe@uap-core.de>
date
Thu, 06 Mar 2025 20:28:34 +0100
changeset 1239
b4b1f15d1866
parent 1238
26299ce9c955
permissions
-rw-r--r--

complete more than 80% of the list.h documentation

relates to #451

1143
0559812df10c assign proper names to the documentation topics
Mike Becker <universe@uap-core.de>
parents: 1142
diff changeset
1 # List Interface
1141
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2
1146
151c057faf7c add marker to every incomplete page
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
3 <warning>
1190
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
4 Outdated Section - will be updated soon!
1146
151c057faf7c add marker to every incomplete page
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
5 </warning>
151c057faf7c add marker to every incomplete page
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
6
1236
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
7 The `list.h` header defines a common interface for all list implementations.
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
8
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
9 UCX already comes with two common list implementations ([linked list](linked_list.h.md) and [array list](array_list.h.md))
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
10 that should cover most use cases.
1237
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
11 But if you feel the need to implement an own list, you will find instructions [below](#implement-own-list-structures).
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
12
1239
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
13 ```C
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
14 #include <cx/linked_list.h>
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
15
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
16 CxList *cxLinkedListCreate(const CxAllocator *allocator,
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
17 cx_compare_func comparator, size_t elem_size);
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
18
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
19 CxList *cxLinkedListCreateSimple(size_t elem_size);
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
20
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
21 #include <cx/array_list.h>
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
22
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
23 CxList *cxArrayListCreate(const CxAllocator *allocator,
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
24 cx_compare_func comparator, size_t elem_size,
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
25 size_t initial_capacity);
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
26
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
27 CxList *cxArrayListCreateSimple(size_t elem_size,
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
28 size_t initial_capacity);
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
29 ```
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
30
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
31 The function `cxLinkedListCreate()` creates a new linked list with the specified `allocator` which stores elements of size `elem_size`.
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
32 The elements are supposed to be compared with the specified `comparator` (cf. [](#access-and-find)).
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
33 The function `cxLinkedListCreateSimple()` will use the stdlib default allocator and does not specify a compare function.
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
34
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
35 Array lists can be created with `cxArrayListCreate()` where the additional argument `initial_capacity` specifies for how many elements the underlying array shall be initially allocated.
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
36
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
37 If `CX_STORE_POINTERS` is used as `elem_size`, the actual element size will be `sizeof(void*)` and the list will behave slightly differently when accessing elements.
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
38 Lists that are storing pointers will always return the stored pointer directly, instead of returning a pointer into the list's memory, thus saving you from unnecessary dereferencing.
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
39 Conversely, when adding elements to the list, the pointers you specify (e.g. in `cxListAdd()` or `cxListInsert()`) are directly added to the list, instead of copying the contents pointed to by the argument.
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
40
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
41 > When you create a list which is storing pointers and do not specify a compare function, `cx_cmp_ptr` will be used by default.
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
42
1237
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
43 ## Example
1236
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
44
1237
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
45 <warning>
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
46 TODO: add example how to work with lists
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
47 </warning>
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
48
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
49 > If you want to lazy-initialize lists, you can use the global `cxEmptyList` symbol as a placeholder instead of using a `NULL`-pointer.
1239
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
50 > While you *must not* insert elements into that list, you can safely access this list or create iterators.
1237
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
51 > This allows you to write clean code without checking for `NULL`-pointer everywhere.
1239
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
52 > You still need to make sure that the placeholder is replaced with an actual list before inserting elements.
1237
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
53
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
54 ## Insert
1236
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
55
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
56 ```C
1239
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
57 #include <cx/list.h>
1236
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
58
1239
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
59 int cxListAdd(CxList *list, const void *elem);
1236
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
60
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
61 int cxListInsert(CxList *list, size_t index, const void *elem);
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
62
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
63 int cxListInsertSorted(CxList *list, const void *elem);
1141
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64
1239
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
65 size_t cxListAddArray(CxList *list, const void *array, size_t n);
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
66
1236
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
67 size_t cxListInsertArray(CxList *list, size_t index,
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
68 const void *array, size_t n);
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
69
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
70 size_t cxListInsertSortedArray(CxList *list,
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
71 const void *array, size_t n);
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
72
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
73 int cxListInsertAfter(CxIterator *iter, const void *elem);
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
74
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
75 int cxListInsertBefore(CxIterator *iter, const void *elem);
1237
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
76 ```
1236
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
77
1239
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
78 The function `cxListAdd()` appends the element `elem` to the list and returns zero on success or non-zero when allocating the memory for the element fails.
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
79 Similarly, `cxListInsert()` adds the element at the specified `index`,
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
80 and `cxListInsertSorted()` adds the element at the correct position so that the list remains sorted according to the list's compare function.
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
81
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
82 When you are currently iterating through a list, you can insert items before or after the current position of the iterator
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
83 with `cxListInsertBefore()` or `cxListInsertAfter()`, respectively.
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
84 This _should_ be done with [mutating iterators](iterator.h.md#mutating-iterators) only, but is also defined for normal iterators.
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
85
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
86 If the list is storing pointers (cf. `cxCollectionStoresPointers()`), the pointer `elem` is directly added to the list.
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
87 Otherwise, the contents at the location pointed to by `elem` are copied to the list's memory with the element size specified during creation of the list.
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
88
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
89 On the other hand the `array` argument for `cxListAddArray()`, `cxListInsertArray()`, and `cxListInsertSortedArray()`
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
90 must always point to an array of elements to be added (i.e. either an array of pointers, or an array of actual elements).
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
91
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
92 A special requirement for `cxListInsertSortedArray()` is, that the `array` must already be sorted.
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
93
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
94 > Implementations are required to optimize the insertion of a sorted array into a sorted list in linear time,
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
95 > while inserting each element separately via `cxListInsertSorted()` may take quadratic time.
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
96
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
97 > The functions `cxListInsertSorted()` and `cxListInsertSortedArray()` do neither check if the list is already sorted, nor do they actively sort the list.
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
98 > {style="note"}
1237
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
99
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
100 ## Access and Find
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
101
1239
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
102 <link-summary>Functions for searching and accessing elements.</link-summary>
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
103
1237
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
104 ```C
1239
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
105 #include <cx/list.h>
1237
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
106
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
107 void *cxListAt(const CxList *list, size_t index);
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
108
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
109 size_t cxListFind(const CxList *list, const void *elem);
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
110
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
111 size_t cxListFindRemove(CxList *list, const void *elem);
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
112
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
113 bool cxListIndexValid(const CxList *list, size_t index);
1239
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
114
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
115 size_t cxListSize(const CxList *list);
1237
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
116 ```
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
117
1239
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
118 The function `cxListAt()` accesses the element at the specified `index`.
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
119 If the list is storing pointers (i.e. `cxCollectionStoresPointers()` returns `true`), the pointer at the specified index is directly returned.
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
120 Otherwise, a pointer to element at the specified index is returned.
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
121 If the index is out-of-bounds, the function returns `NULL`.
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
122
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
123 On the other hand, `cxListFind()` searches for the element pointed to by `elem` by comparing each element in the list with the list`s compare function,
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
124 and returns the first index when the element was found.
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
125 Otherwise, the function returns the list size.
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
126
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
127 The function `cxListFindRemove()` behaves like `cxListFind()`, except that it also removes the first occurrence of the element from the list.
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
128 This will _also_ call destructor functions, if specified, so take special care when you continue to use `elem`, or when the list is storing pointers and the element appears more than once in the list.
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
129
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
130 With `cxListIndexValid()` you can check the index returned by `cxListFind()` or `cxListFindRemove()`,
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
131 which is more convenient than comparing the return value if the return value of `cxListSize()`.
1237
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
132
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
133 ## Remove
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
134
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
135 ```C
1239
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
136 #include <cx/list.h>
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
137
1236
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
138 int cxListRemove(CxList *list, size_t index);
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
139
1239
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
140 size_t cxListRemoveArray(CxList *list, size_t index, size_t num);
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
141
1236
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
142 int cxListRemoveAndGet(CxList *list, size_t index, void *targetbuf);
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
143
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
144 size_t cxListRemoveArrayAndGet(CxList *list, size_t index, size_t num,
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
145 void *targetbuf);
1239
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
146
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
147 size_t cxListFindRemove(CxList *list, const void *elem);
1236
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
148
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
149 void cxListClear(CxList *list);
1237
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
150 ```
1236
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
151
1239
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
152 The function `cxListRemove()` removes the element at the specified index and returns zero,
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
153 or returns non-zero if the index is out-of-bounds.
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
154 The function `cxListRemoveArray()` removes `num` elements starting at `index` and returns the number of elements that have been removed.
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
155 For each removed element, the [destructor functions](collection.h.md#destructor-functions) are called.
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
156
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
157 On the other hand, `cxListRemoveAndGet()` and `cxListRemoveArrayAndGet()` do not invoke the destructor functions
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
158 and instead copy the elements into the `targetbuf`, which must be large enough to hold the removed elements.
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
159
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
160 The function `cxListFindRemove()` finds the first element within the list that is equivalent to the element pointed to by `elem` and removes it from the list.
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
161 This will _also_ call destructor functions, if specified, so take special care when you continue to use `elem`.
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
162
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
163 The function `cxListClear()` simply removes all elements from the list, invoking the destructor functions.
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
164 It behaves equivalently, but is usually more efficient than calling `cxListRemove()` for every index.
1236
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
165
1237
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
166 ## Iterators
1236
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
167
1237
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
168 ```C
1239
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
169 #include <cx/list.h>
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
170
1236
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
171 CxIterator cxListIterator(const CxList *list);
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
172
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
173 CxIterator cxListBackwardsIterator(const CxList *list);
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
174
1237
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
175 CxIterator cxListIteratorAt(const CxList *list, size_t index);
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
176
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
177 CxIterator cxListBackwardsIteratorAt(const CxList *list, size_t index);
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
178
1239
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
179 CxIterator cxListMutIterator(CxList *list);
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
180
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
181 CxIterator cxListMutBackwardsIterator(CxList *list);
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
182
1237
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
183 CxIterator cxListMutIteratorAt(CxList *list, size_t index);
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
184
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
185 CxIterator cxListMutBackwardsIteratorAt(CxList *list, size_t index);
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
186 ```
1236
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
187
1239
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
188 The functions `cxListIterator()` and `cxListBackwardsIterator()` create iterators
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
189 that start and the first or the last element in the list and iterate through the entire list.
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
190
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
191 The functions `cxListIteratorAt()` and `cxListBackwardsIteratorAt()` start with the element at the specified index
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
192 and iterate until the end, or the beginning of the list, respectively.
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
193
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
194 The functions with `Mut` in are equivalently, except that they create a [mutating iterator](iterator.h.md#mutating-iterators).
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
195 Removing elements via a mutating iterator will cause an invocation of the [destructor functions](collection.h.md#destructor-functions) for the removed element.
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
196
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
197 If is safe to specify an out-of-bounds index in which case an iterator is returned for which `cxIteratorValid()` returns `false`, immediately.
1236
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
198
1237
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
199 ## Reorder
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
200
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
201 ```C
1239
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
202 #include <cx/list.h>
1236
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
203
1239
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
204 int cxListSwap(CxList *list, size_t i, size_t j);
1236
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
205
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
206 void cxListReverse(CxList *list);
1239
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
207
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
208 void cxListSort(CxList *list);
1237
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
209 ```
1236
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
210
1239
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
211 The function `cxListSwap()` swaps two elements specified by the indices `i` and `j`.
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
212 The return value is non-zero if one of the indices is out-of-bounds.
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
213
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
214 The function `cxListReverse()` reorders all elements, so that they appear in exactly the opposite order after invoking this function.
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
215
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
216 The function `cxListSort()` sorts all elements with respect to the list's compare function,
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
217 unless the list is already sorted (cf. `cxCollectionSorted()`), in which case the function immediately returns.
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
218
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
219 Default UCX implementations of the list interface make use of [small buffer optimizations](install.md#small-buffer-optimizations) when swapping elements.
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
220
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
221 > An invocation of `cxListSort` sets the `sorted` flag of the [collection](collection.h.md).
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
222 > Implementations usually make use of this flag to optimize search operations, if possible.
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
223 > For example the [array list](array_list.h.md) implementation will use binary search
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
224 > for `cxListFind()` and similar operations, when the list is sorted.
1236
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
225
1237
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
226 ## Compare
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
227
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
228 ```C
1239
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
229 #include <cx/list.h>
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
230
1237
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
231 int cxListCompare(const CxList *list, const CxList *other);
1236
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
232 ```
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
233
1239
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
234 Arbitrary lists can be compared element-wise with `cxListCompare()`, as long as the compare functions of both lists are equivalent.
1238
26299ce9c955 documentation for list compare and dispose
Mike Becker <universe@uap-core.de>
parents: 1237
diff changeset
235
26299ce9c955 documentation for list compare and dispose
Mike Becker <universe@uap-core.de>
parents: 1237
diff changeset
236 That means, you can compare an UCX [array list](array_list.h.md) with a [linked list](linked_list.h.md),
26299ce9c955 documentation for list compare and dispose
Mike Becker <universe@uap-core.de>
parents: 1237
diff changeset
237 and you could even compare lists that are storing pointers with lists that are not storing pointers.
26299ce9c955 documentation for list compare and dispose
Mike Becker <universe@uap-core.de>
parents: 1237
diff changeset
238
26299ce9c955 documentation for list compare and dispose
Mike Becker <universe@uap-core.de>
parents: 1237
diff changeset
239 However, the optimized list-internal compare implementation is only used, when both the compare functions and the list classes are identical.
1239
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
240 Otherwise, `cxListCompare()` will behave as if you were iterating through both lists and manually comparing the elements.
1238
26299ce9c955 documentation for list compare and dispose
Mike Becker <universe@uap-core.de>
parents: 1237
diff changeset
241
1239
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
242 The return value of `cxListCompare()` is zero, if the lists are element-wise equivalent.
1238
26299ce9c955 documentation for list compare and dispose
Mike Becker <universe@uap-core.de>
parents: 1237
diff changeset
243 If they are not, the non-zero return value equals the return value of the used compare function for the first pair of elements that are not equal.
1236
f392f27a1dc6 list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
244
1237
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
245 ## Dispose
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
246
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
247 ```C
1239
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
248 #include <cx/list.h>
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
249
1237
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
250 void cxListFree(CxList *list);
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
251 ```
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
252
1238
26299ce9c955 documentation for list compare and dispose
Mike Becker <universe@uap-core.de>
parents: 1237
diff changeset
253 No matter with which function a `CxList` has been created, you can _always_ deallocate the entire memory with a call to `cxListFree()`.
26299ce9c955 documentation for list compare and dispose
Mike Becker <universe@uap-core.de>
parents: 1237
diff changeset
254
26299ce9c955 documentation for list compare and dispose
Mike Becker <universe@uap-core.de>
parents: 1237
diff changeset
255 The effect is equivalent to invoking `cxListClear()` plus deallocating the memory for the list structure.
1239
b4b1f15d1866 complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1238
diff changeset
256 That means, for each element in the list, the [destructor functions](collection.h.md#destructor-functions) are called before deallocating the list.
1238
26299ce9c955 documentation for list compare and dispose
Mike Becker <universe@uap-core.de>
parents: 1237
diff changeset
257
26299ce9c955 documentation for list compare and dispose
Mike Becker <universe@uap-core.de>
parents: 1237
diff changeset
258 When the list has been storing pointers, make sure that either another reference to the same memory exist in your program,
26299ce9c955 documentation for list compare and dispose
Mike Becker <universe@uap-core.de>
parents: 1237
diff changeset
259 or any of the destructor functions deallocates the memory.
26299ce9c955 documentation for list compare and dispose
Mike Becker <universe@uap-core.de>
parents: 1237
diff changeset
260 Otherwise, there is a risk of a memory leak.
1237
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
261
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
262 ## Implement own List Structures
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
263
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
264 <warning>
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
265 TODO: add documentation
5cba456aff67 add structure to list documentation
Mike Becker <universe@uap-core.de>
parents: 1236
diff changeset
266 </warning>
1142
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
267
1190
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
268 <seealso>
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
269 <category ref="apidoc">
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
270 <a href="https://ucx.sourceforge.io/api/list_8h.html">list.h</a>
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
271 </category>
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
272 </seealso>

mercurial