Sun, 23 Nov 2025 13:15:19 +0100
optimize sorted insertion by using the infimum instead of the supremum
The reason is that the supremum returns the equal element with the smallest index, and we want the largest.
Therefore, we use the infimum, which already gives us the largest index when there are equal elements, and increase the index by one. The infimum is also guaranteed to exist in that case.
|
1143
0559812df10c
assign proper names to the documentation topics
Mike Becker <universe@uap-core.de>
parents:
1142
diff
changeset
|
1 | # List Interface |
| 1141 | 2 | |
|
1236
f392f27a1dc6
list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
3 | 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
|
4 | |
|
f392f27a1dc6
list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
5 | 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
|
6 | that should cover most use cases. |
|
1351
4407229bd944
fix some grammar and wording issues
Mike Becker <universe@uap-core.de>
parents:
1344
diff
changeset
|
7 | But if you feel the need to implement your own list, you will find the instructions [below](#implement-own-list-structures). |
|
1237
5cba456aff67
add structure to list documentation
Mike Becker <universe@uap-core.de>
parents:
1236
diff
changeset
|
8 | |
|
1239
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
9 | ```C |
|
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
10 | #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
|
11 | |
|
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
12 | 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
|
13 | 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
|
14 | |
|
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
15 | 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
|
16 | |
|
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
17 | #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
|
18 | |
|
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
19 | 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
|
20 | 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
|
21 | size_t initial_capacity); |
|
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 *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
|
24 | size_t initial_capacity); |
|
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
25 | ``` |
|
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 | 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
|
28 | The elements are supposed to be compared with the specified `comparator` (cf. [](#access-and-find)). |
|
1318
12fa1d37fe48
allow changing the cxDefaultAllocator - resolves #669
Mike Becker <universe@uap-core.de>
parents:
1316
diff
changeset
|
29 | The function `cxLinkedListCreateSimple()` will use the [default allocator](allocator.h.md#default-allocator) and does not specify a compare function. |
|
1239
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 | 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
|
32 | |
|
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
33 | 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
|
34 | 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. |
|
1351
4407229bd944
fix some grammar and wording issues
Mike Becker <universe@uap-core.de>
parents:
1344
diff
changeset
|
35 | 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. |
|
1239
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 | > 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
|
38 | |
|
1243
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1240
diff
changeset
|
39 | > If you want to lazy-initialize lists, you can use the global `cxEmptyList` symbol as a placeholder instead of using a `NULL`-pointer. |
|
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1240
diff
changeset
|
40 | > While you *must not* insert elements into that list, you can safely access this list or create iterators. |
|
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1240
diff
changeset
|
41 | > This allows you to write clean code without checking for `NULL`-pointer everywhere. |
|
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1240
diff
changeset
|
42 | > You still need to make sure that the placeholder is replaced with an actual list before inserting elements. |
|
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1240
diff
changeset
|
43 | |
|
1237
5cba456aff67
add structure to list documentation
Mike Becker <universe@uap-core.de>
parents:
1236
diff
changeset
|
44 | ## Example |
|
1236
f392f27a1dc6
list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
45 | |
|
1240
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
46 | In the following example we create a linked-list of regular expressions for filtering data. |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
47 | |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
48 | ```C |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
49 | #include <cx/linked_list.h> |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
50 | #include <regex.h> |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
51 | |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
52 | CxList *create_pattern_list(void) { |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
53 | // create a linked list to store patterns |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
54 | CxList *list = cxLinkedListCreateSimple(sizeof(regex_t)); |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
55 | |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
56 | // automatically free the pattern when list gets destroyed |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
57 | cxDefineDestructor(list, regfree); |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
58 | |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
59 | return list; |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
60 | } |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
61 | |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
62 | int add_pattern(CxList *list, const char *value) { |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
63 | // compile pattern and add it to the list, if successful |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
64 | regex_t regex; |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
65 | if (regcomp(®ex, value, REG_EXTENDED|REG_NOSUB)) { |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
66 | return 1; |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
67 | } else { |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
68 | // take address of local variable |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
69 | // since we are not storing pointers in this list, |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
70 | // we get a copy of the data directly in the list |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
71 | cxListAdd(list, ®ex); |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
72 | return 0; |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
73 | } |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
74 | } |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
75 | |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
76 | bool matches_any(CxList *list, const char *text) { |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
77 | CxIterator iter = cxListIterator(list); |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
78 | cx_foreach(regex_t*, pat, iter) { |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
79 | if (regexec(pat, text, 0, NULL, 0) == 0) { |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
80 | return true; |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
81 | } |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
82 | } |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
83 | return false; |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
84 | } |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
85 | ``` |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
86 | |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
87 | If in the above example the list was created with `CX_STORE_POINTERS` instead of `sizeof(regex_t)`, |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
88 | the `add_pattern()` function would need to be changed as follows: |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
89 | |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
90 | ```C |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
91 | int add_pattern(CxList *list, const char *value) { |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
92 | // allocate memory here, because now we are storing pointers |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
93 | regex_t *regex = malloc(sizeof(regex_t)); |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
94 | if (!regex || regcomp(regex, value, REG_EXTENDED|REG_NOSUB)) { |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
95 | return 1; |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
96 | } else { |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
97 | cxListAdd(list, regex); |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
98 | return 0; |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
99 | } |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
100 | } |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
101 | ``` |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
102 | |
|
1420
c6f55a2b3495
fix various typos in the web documentation
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
103 | Also, just registering `regfree()` as a destructor is not enough anymore because the `regex_t` structure also needs to be freed. |
|
1240
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
104 | Therefore, we would need to wrap the calls to `regfree()` and `free()` into an own destructor, which we then register with the list. |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
105 | However, it should be clear by now that using `CX_STORE_POINTERS` is a bad choice for this use case to begin with. |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
106 | |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
107 | As a rule of thumb: if you allocate memory for an element that you immediately put into the list, consider storing the element directly. |
|
1243
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1240
diff
changeset
|
108 | And if you are getting pointers to already allocated memory from somewhere else, and you just want to organize those elements in a list, then consider using `CX_STORE_POINTERS`. |
|
1237
5cba456aff67
add structure to list documentation
Mike Becker <universe@uap-core.de>
parents:
1236
diff
changeset
|
109 | |
|
5cba456aff67
add structure to list documentation
Mike Becker <universe@uap-core.de>
parents:
1236
diff
changeset
|
110 | ## Insert |
|
1236
f392f27a1dc6
list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
111 | |
|
f392f27a1dc6
list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
112 | ```C |
|
1239
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
113 | #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
|
114 | |
|
1239
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
115 | 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
|
116 | |
|
f392f27a1dc6
list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
117 | 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
|
118 | |
|
1316
c41538edfcef
add cxListEmplace() and cxListEmplaceAt() plus some improvements to the array list implementation
Mike Becker <universe@uap-core.de>
parents:
1315
diff
changeset
|
119 | void *cxListEmplace(CxList *list); |
|
c41538edfcef
add cxListEmplace() and cxListEmplaceAt() plus some improvements to the array list implementation
Mike Becker <universe@uap-core.de>
parents:
1315
diff
changeset
|
120 | |
|
c41538edfcef
add cxListEmplace() and cxListEmplaceAt() plus some improvements to the array list implementation
Mike Becker <universe@uap-core.de>
parents:
1315
diff
changeset
|
121 | void *cxListEmplaceAt(CxList *list, size_t index); |
|
c41538edfcef
add cxListEmplace() and cxListEmplaceAt() plus some improvements to the array list implementation
Mike Becker <universe@uap-core.de>
parents:
1315
diff
changeset
|
122 | |
|
1433
81c301a59b7c
add cxListEmplaceArray() and cxListEmplaceArrayAt() as preparation for the clone implementation
Mike Becker <universe@uap-core.de>
parents:
1429
diff
changeset
|
123 | CxIterator cxListEmplaceArray(CxList *list, size_t n); |
|
81c301a59b7c
add cxListEmplaceArray() and cxListEmplaceArrayAt() as preparation for the clone implementation
Mike Becker <universe@uap-core.de>
parents:
1429
diff
changeset
|
124 | |
|
81c301a59b7c
add cxListEmplaceArray() and cxListEmplaceArrayAt() as preparation for the clone implementation
Mike Becker <universe@uap-core.de>
parents:
1429
diff
changeset
|
125 | CxIterator cxListEmplaceArrayAt(CxList *list, size_t index, size_t n); |
|
81c301a59b7c
add cxListEmplaceArray() and cxListEmplaceArrayAt() as preparation for the clone implementation
Mike Becker <universe@uap-core.de>
parents:
1429
diff
changeset
|
126 | |
|
1236
f392f27a1dc6
list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
127 | int cxListInsertSorted(CxList *list, const void *elem); |
| 1141 | 128 | |
|
1419
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1351
diff
changeset
|
129 | int cxListInsertUnique(CxList *list, const void *elem); |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1351
diff
changeset
|
130 | |
|
1239
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
131 | 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
|
132 | |
|
1236
f392f27a1dc6
list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
133 | 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
|
134 | 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
|
135 | |
|
f392f27a1dc6
list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
136 | 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
|
137 | 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
|
138 | |
|
1419
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1351
diff
changeset
|
139 | size_t cxListInsertUniqueArray(CxList *list, |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1351
diff
changeset
|
140 | const void *array, size_t n); |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1351
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 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
|
143 | |
|
f392f27a1dc6
list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
144 | int cxListInsertBefore(CxIterator *iter, const void *elem); |
|
1237
5cba456aff67
add structure to list documentation
Mike Becker <universe@uap-core.de>
parents:
1236
diff
changeset
|
145 | ``` |
|
1236
f392f27a1dc6
list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
146 | |
|
1239
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
147 | 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. |
|
1316
c41538edfcef
add cxListEmplace() and cxListEmplaceAt() plus some improvements to the array list implementation
Mike Becker <universe@uap-core.de>
parents:
1315
diff
changeset
|
148 | Similarly, `cxListInsert()` adds the element at the specified `index`. |
|
c41538edfcef
add cxListEmplace() and cxListEmplaceAt() plus some improvements to the array list implementation
Mike Becker <universe@uap-core.de>
parents:
1315
diff
changeset
|
149 | |
|
c41538edfcef
add cxListEmplace() and cxListEmplaceAt() plus some improvements to the array list implementation
Mike Becker <universe@uap-core.de>
parents:
1315
diff
changeset
|
150 | The functions `cxListEmplace()` and `cxListEmplaceAt()` behave like `cxListAdd()` and `cxListInsert()`, |
|
c41538edfcef
add cxListEmplace() and cxListEmplaceAt() plus some improvements to the array list implementation
Mike Becker <universe@uap-core.de>
parents:
1315
diff
changeset
|
151 | except that they only allocate the memory and return a pointer to it, leaving it to the callee to copy the element data into it. |
|
1433
81c301a59b7c
add cxListEmplaceArray() and cxListEmplaceArrayAt() as preparation for the clone implementation
Mike Becker <universe@uap-core.de>
parents:
1429
diff
changeset
|
152 | The same is true for `cxListEmplaceArray()` and `cxListEmplaceArrayAt()`, which allocate memory for `n` elements and return an iterator to the first element. |
|
1316
c41538edfcef
add cxListEmplace() and cxListEmplaceAt() plus some improvements to the array list implementation
Mike Becker <universe@uap-core.de>
parents:
1315
diff
changeset
|
153 | Be aware that when the list is storing pointers, the allocated memory will be for the pointer, not the actual element's data. |
|
c41538edfcef
add cxListEmplace() and cxListEmplaceAt() plus some improvements to the array list implementation
Mike Becker <universe@uap-core.de>
parents:
1315
diff
changeset
|
154 | |
|
1433
81c301a59b7c
add cxListEmplaceArray() and cxListEmplaceArrayAt() as preparation for the clone implementation
Mike Becker <universe@uap-core.de>
parents:
1429
diff
changeset
|
155 | > If `cxListEmplaceArray()` or `cxListEmplaceArrayAt()` is unable to allocate memory for `n` elements, |
|
81c301a59b7c
add cxListEmplaceArray() and cxListEmplaceArrayAt() as preparation for the clone implementation
Mike Becker <universe@uap-core.de>
parents:
1429
diff
changeset
|
156 | > the iterator will iterate only over the elements that have been successfully allocated. |
|
81c301a59b7c
add cxListEmplaceArray() and cxListEmplaceArrayAt() as preparation for the clone implementation
Mike Becker <universe@uap-core.de>
parents:
1429
diff
changeset
|
157 | > The `elem_count` attribute of the iterator will be set to the number of successfully allocated elements. |
|
81c301a59b7c
add cxListEmplaceArray() and cxListEmplaceArrayAt() as preparation for the clone implementation
Mike Becker <universe@uap-core.de>
parents:
1429
diff
changeset
|
158 | > And the `index` attribute will count from zero to `elem_count - 1`. |
|
81c301a59b7c
add cxListEmplaceArray() and cxListEmplaceArrayAt() as preparation for the clone implementation
Mike Becker <universe@uap-core.de>
parents:
1429
diff
changeset
|
159 | > {style="note"} |
|
81c301a59b7c
add cxListEmplaceArray() and cxListEmplaceArrayAt() as preparation for the clone implementation
Mike Becker <universe@uap-core.de>
parents:
1429
diff
changeset
|
160 | |
|
1316
c41538edfcef
add cxListEmplace() and cxListEmplaceAt() plus some improvements to the array list implementation
Mike Becker <universe@uap-core.de>
parents:
1315
diff
changeset
|
161 | The function `cxListInsertSorted()` inserts the element at the correct position so that the list remains sorted according to the list's compare function. |
|
1428
0ac4aa1737fd
add support for non-sorted lists in cxListInsertUnique() and cxListInsertUniqueArray()
Mike Becker <universe@uap-core.de>
parents:
1421
diff
changeset
|
162 | It is important that the list is already sorted before calling this function. |
|
0ac4aa1737fd
add support for non-sorted lists in cxListInsertUnique() and cxListInsertUniqueArray()
Mike Becker <universe@uap-core.de>
parents:
1421
diff
changeset
|
163 | On the other hand, `cxListInsertUnique()` inserts the element only if it is not already in the list. |
|
0ac4aa1737fd
add support for non-sorted lists in cxListInsertUnique() and cxListInsertUniqueArray()
Mike Becker <universe@uap-core.de>
parents:
1421
diff
changeset
|
164 | In this case it is strongly recommended that the list is already sorted but not required; the function will fall back to an inefficient algorithm when the list is not sorted. |
|
1239
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
165 | |
|
1240
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
166 | When you are currently iterating through a list, you can insert elements before or after the current position of the iterator |
|
1239
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
167 | 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
|
168 | |
|
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
169 | 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
|
170 | 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
|
171 | |
|
1419
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1351
diff
changeset
|
172 | On the other hand, the `array` argument for `cxListAddArray()`, `cxListInsertArray()`, `cxListInsertSortedArray()`, and `cxListInsertUniqueArray()` |
|
1351
4407229bd944
fix some grammar and wording issues
Mike Becker <universe@uap-core.de>
parents:
1344
diff
changeset
|
173 | must always point to an array of elements to be added (i.e., either an array of pointers or an array of actual elements). |
|
1239
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
174 | |
|
1419
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1351
diff
changeset
|
175 | The return values of the array-inserting functions are the number of elements that have been successfully _processed_. |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1351
diff
changeset
|
176 | Usually this is equivalent to the number of inserted elements, except for `cxListInsertUniqueArray()`, |
|
1421
809eb30cd621
fix missing documentation if insert_unique member - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1420
diff
changeset
|
177 | where the actual number of inserted elements may be lower than the number of successfully processed elements. |
|
1239
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
178 | |
|
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
179 | > 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
|
180 | > while inserting each element separately via `cxListInsertSorted()` may take quadratic time. |
|
1428
0ac4aa1737fd
add support for non-sorted lists in cxListInsertUnique() and cxListInsertUniqueArray()
Mike Becker <universe@uap-core.de>
parents:
1421
diff
changeset
|
181 | > |
|
0ac4aa1737fd
add support for non-sorted lists in cxListInsertUnique() and cxListInsertUniqueArray()
Mike Becker <universe@uap-core.de>
parents:
1421
diff
changeset
|
182 | > When used with sorted lists, the arrays passed to the functions must also be sorted according to the list's compare function. |
|
0ac4aa1737fd
add support for non-sorted lists in cxListInsertUnique() and cxListInsertUniqueArray()
Mike Becker <universe@uap-core.de>
parents:
1421
diff
changeset
|
183 | > Only when `cxListInsertUniqueArray()` is used with a list that is not sorted, the array does not need to be sorted. |
|
0ac4aa1737fd
add support for non-sorted lists in cxListInsertUnique() and cxListInsertUniqueArray()
Mike Becker <universe@uap-core.de>
parents:
1421
diff
changeset
|
184 | > |
|
0ac4aa1737fd
add support for non-sorted lists in cxListInsertUnique() and cxListInsertUniqueArray()
Mike Becker <universe@uap-core.de>
parents:
1421
diff
changeset
|
185 | > The functions do not check if the passed arrays are sorted. |
|
1239
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
186 | > {style="note"} |
|
1237
5cba456aff67
add structure to list documentation
Mike Becker <universe@uap-core.de>
parents:
1236
diff
changeset
|
187 | |
|
5cba456aff67
add structure to list documentation
Mike Becker <universe@uap-core.de>
parents:
1236
diff
changeset
|
188 | ## Access and Find |
|
5cba456aff67
add structure to list documentation
Mike Becker <universe@uap-core.de>
parents:
1236
diff
changeset
|
189 | |
|
1239
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
190 | <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
|
191 | |
|
1237
5cba456aff67
add structure to list documentation
Mike Becker <universe@uap-core.de>
parents:
1236
diff
changeset
|
192 | ```C |
|
1239
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
193 | #include <cx/list.h> |
|
1237
5cba456aff67
add structure to list documentation
Mike Becker <universe@uap-core.de>
parents:
1236
diff
changeset
|
194 | |
|
5cba456aff67
add structure to list documentation
Mike Becker <universe@uap-core.de>
parents:
1236
diff
changeset
|
195 | void *cxListAt(const CxList *list, size_t index); |
|
5cba456aff67
add structure to list documentation
Mike Becker <universe@uap-core.de>
parents:
1236
diff
changeset
|
196 | |
|
1315
b4c3e0b4c3d5
add convenience functions for easy access to first/last element of a list
Mike Becker <universe@uap-core.de>
parents:
1294
diff
changeset
|
197 | void *cxListFirst(const CxList *list); |
|
b4c3e0b4c3d5
add convenience functions for easy access to first/last element of a list
Mike Becker <universe@uap-core.de>
parents:
1294
diff
changeset
|
198 | |
|
b4c3e0b4c3d5
add convenience functions for easy access to first/last element of a list
Mike Becker <universe@uap-core.de>
parents:
1294
diff
changeset
|
199 | void *cxListLast(const CxList *list); |
|
b4c3e0b4c3d5
add convenience functions for easy access to first/last element of a list
Mike Becker <universe@uap-core.de>
parents:
1294
diff
changeset
|
200 | |
|
1287
3a3ffc27813f
adds cxListSet() - resolves #642
Mike Becker <universe@uap-core.de>
parents:
1244
diff
changeset
|
201 | int cxListSet(CxList *list, size_t index, const void *elem); |
|
3a3ffc27813f
adds cxListSet() - resolves #642
Mike Becker <universe@uap-core.de>
parents:
1244
diff
changeset
|
202 | |
|
1237
5cba456aff67
add structure to list documentation
Mike Becker <universe@uap-core.de>
parents:
1236
diff
changeset
|
203 | 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
|
204 | |
|
5cba456aff67
add structure to list documentation
Mike Becker <universe@uap-core.de>
parents:
1236
diff
changeset
|
205 | size_t cxListFindRemove(CxList *list, const void *elem); |
|
5cba456aff67
add structure to list documentation
Mike Becker <universe@uap-core.de>
parents:
1236
diff
changeset
|
206 | |
|
1294
30d7ae76c76a
add test and documentation for cxListContains() - fixes #643
Mike Becker <universe@uap-core.de>
parents:
1287
diff
changeset
|
207 | bool cxListContains(const CxList *list, const void *elem); |
|
30d7ae76c76a
add test and documentation for cxListContains() - fixes #643
Mike Becker <universe@uap-core.de>
parents:
1287
diff
changeset
|
208 | |
|
1237
5cba456aff67
add structure to list documentation
Mike Becker <universe@uap-core.de>
parents:
1236
diff
changeset
|
209 | 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
|
210 | |
|
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
211 | size_t cxListSize(const CxList *list); |
|
1237
5cba456aff67
add structure to list documentation
Mike Becker <universe@uap-core.de>
parents:
1236
diff
changeset
|
212 | ``` |
|
5cba456aff67
add structure to list documentation
Mike Becker <universe@uap-core.de>
parents:
1236
diff
changeset
|
213 | |
|
1239
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
214 | 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
|
215 | If the list is storing pointers (i.e. `cxCollectionStoresPointers()` returns `true`), the pointer at the specified index is directly returned. |
|
1351
4407229bd944
fix some grammar and wording issues
Mike Becker <universe@uap-core.de>
parents:
1344
diff
changeset
|
216 | Otherwise, a pointer to the element at the specified index is returned. |
|
1239
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
217 | If the index is out-of-bounds, the function returns `NULL`. |
|
1315
b4c3e0b4c3d5
add convenience functions for easy access to first/last element of a list
Mike Becker <universe@uap-core.de>
parents:
1294
diff
changeset
|
218 | The functions `cxListFirst()` and `cxListLast()` behave like `cxListAt()`, |
|
b4c3e0b4c3d5
add convenience functions for easy access to first/last element of a list
Mike Becker <universe@uap-core.de>
parents:
1294
diff
changeset
|
219 | accessing the first or the last valid index, respectively, and returning `NULL` when the list is empty. |
|
1239
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
220 | |
|
1287
3a3ffc27813f
adds cxListSet() - resolves #642
Mike Becker <universe@uap-core.de>
parents:
1244
diff
changeset
|
221 | The function `cxListSet()` allows you to modify elements at a specific index in the list. |
|
3a3ffc27813f
adds cxListSet() - resolves #642
Mike Becker <universe@uap-core.de>
parents:
1244
diff
changeset
|
222 | This function replaces the element at the specified `index` with the value pointed to by `elem`. |
|
3a3ffc27813f
adds cxListSet() - resolves #642
Mike Becker <universe@uap-core.de>
parents:
1244
diff
changeset
|
223 | If the list is storing values directly (not pointers), the contents at the memory location `elem` will be copied into the list. |
|
3a3ffc27813f
adds cxListSet() - resolves #642
Mike Becker <universe@uap-core.de>
parents:
1244
diff
changeset
|
224 | If the list is storing pointers, the pointer value itself will be stored. |
|
3a3ffc27813f
adds cxListSet() - resolves #642
Mike Becker <universe@uap-core.de>
parents:
1244
diff
changeset
|
225 | The function returns `0` on success and `1` if the index is out of bounds. |
|
3a3ffc27813f
adds cxListSet() - resolves #642
Mike Becker <universe@uap-core.de>
parents:
1244
diff
changeset
|
226 | |
|
1420
c6f55a2b3495
fix various typos in the web documentation
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
227 | 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 |
|
1239
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
228 | 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
|
229 | 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
|
230 | |
|
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
231 | 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
|
232 | 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
|
233 | |
|
1420
c6f55a2b3495
fix various typos in the web documentation
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
234 | The function `cxListContains()` returns `true` if and only if `cxListFind()` returns a valid index. |
|
1294
30d7ae76c76a
add test and documentation for cxListContains() - fixes #643
Mike Becker <universe@uap-core.de>
parents:
1287
diff
changeset
|
235 | |
|
1239
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
236 | 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
|
237 | 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
|
238 | |
|
5cba456aff67
add structure to list documentation
Mike Becker <universe@uap-core.de>
parents:
1236
diff
changeset
|
239 | ## Remove |
|
5cba456aff67
add structure to list documentation
Mike Becker <universe@uap-core.de>
parents:
1236
diff
changeset
|
240 | |
|
5cba456aff67
add structure to list documentation
Mike Becker <universe@uap-core.de>
parents:
1236
diff
changeset
|
241 | ```C |
|
1239
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
242 | #include <cx/list.h> |
|
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
243 | |
|
1236
f392f27a1dc6
list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
244 | 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
|
245 | |
|
1239
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
246 | 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
|
247 | |
|
1315
b4c3e0b4c3d5
add convenience functions for easy access to first/last element of a list
Mike Becker <universe@uap-core.de>
parents:
1294
diff
changeset
|
248 | size_t cxListFindRemove(CxList *list, const void *elem); |
|
b4c3e0b4c3d5
add convenience functions for easy access to first/last element of a list
Mike Becker <universe@uap-core.de>
parents:
1294
diff
changeset
|
249 | |
|
1236
f392f27a1dc6
list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
250 | 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
|
251 | |
|
1315
b4c3e0b4c3d5
add convenience functions for easy access to first/last element of a list
Mike Becker <universe@uap-core.de>
parents:
1294
diff
changeset
|
252 | int cxListRemoveAndGetFirst(CxList *list, void *targetbuf); |
|
b4c3e0b4c3d5
add convenience functions for easy access to first/last element of a list
Mike Becker <universe@uap-core.de>
parents:
1294
diff
changeset
|
253 | |
|
b4c3e0b4c3d5
add convenience functions for easy access to first/last element of a list
Mike Becker <universe@uap-core.de>
parents:
1294
diff
changeset
|
254 | int cxListPopFront(CxList *list, void *targetbuf); |
|
b4c3e0b4c3d5
add convenience functions for easy access to first/last element of a list
Mike Becker <universe@uap-core.de>
parents:
1294
diff
changeset
|
255 | |
|
b4c3e0b4c3d5
add convenience functions for easy access to first/last element of a list
Mike Becker <universe@uap-core.de>
parents:
1294
diff
changeset
|
256 | int cxListRemoveAndLast(CxList *list, void *targetbuf); |
|
b4c3e0b4c3d5
add convenience functions for easy access to first/last element of a list
Mike Becker <universe@uap-core.de>
parents:
1294
diff
changeset
|
257 | |
|
b4c3e0b4c3d5
add convenience functions for easy access to first/last element of a list
Mike Becker <universe@uap-core.de>
parents:
1294
diff
changeset
|
258 | int cxListPop(CxList *list, void *targetbuf); |
|
b4c3e0b4c3d5
add convenience functions for easy access to first/last element of a list
Mike Becker <universe@uap-core.de>
parents:
1294
diff
changeset
|
259 | |
|
1236
f392f27a1dc6
list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
260 | 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
|
261 | void *targetbuf); |
|
f392f27a1dc6
list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
262 | |
|
f392f27a1dc6
list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
263 | void cxListClear(CxList *list); |
|
1237
5cba456aff67
add structure to list documentation
Mike Becker <universe@uap-core.de>
parents:
1236
diff
changeset
|
264 | ``` |
|
1236
f392f27a1dc6
list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
265 | |
|
1239
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
266 | 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
|
267 | 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
|
268 | 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
|
269 | 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
|
270 | |
|
1315
b4c3e0b4c3d5
add convenience functions for easy access to first/last element of a list
Mike Becker <universe@uap-core.de>
parents:
1294
diff
changeset
|
271 | 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. |
|
b4c3e0b4c3d5
add convenience functions for easy access to first/last element of a list
Mike Becker <universe@uap-core.de>
parents:
1294
diff
changeset
|
272 | This will _also_ call destructor functions, if specified, so take special care when you continue to use `elem`. |
|
b4c3e0b4c3d5
add convenience functions for easy access to first/last element of a list
Mike Becker <universe@uap-core.de>
parents:
1294
diff
changeset
|
273 | |
|
b4c3e0b4c3d5
add convenience functions for easy access to first/last element of a list
Mike Becker <universe@uap-core.de>
parents:
1294
diff
changeset
|
274 | On the other hand, `cxListRemoveAndGet()` family of functions do not invoke the destructor functions |
|
1239
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
275 | 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
|
276 | |
|
1420
c6f55a2b3495
fix various typos in the web documentation
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
277 | > Note that when the list was initialized with `CX_STORE_POINTERS`, |
|
1315
b4c3e0b4c3d5
add convenience functions for easy access to first/last element of a list
Mike Becker <universe@uap-core.de>
parents:
1294
diff
changeset
|
278 | > the elements that will be copied to the `targetbuf` are the _pointers_. |
|
b4c3e0b4c3d5
add convenience functions for easy access to first/last element of a list
Mike Becker <universe@uap-core.de>
parents:
1294
diff
changeset
|
279 | > In contrast to other list functions, like `cxListAt()`, an automatic dereferencing does not happen here. |
|
b4c3e0b4c3d5
add convenience functions for easy access to first/last element of a list
Mike Becker <universe@uap-core.de>
parents:
1294
diff
changeset
|
280 | >{style="note"} |
|
1239
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
281 | |
|
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
282 | The function `cxListClear()` simply removes all elements from the list, invoking the destructor functions. |
|
1420
c6f55a2b3495
fix various typos in the web documentation
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
283 | 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
|
284 | |
|
1237
5cba456aff67
add structure to list documentation
Mike Becker <universe@uap-core.de>
parents:
1236
diff
changeset
|
285 | ## Iterators |
|
1236
f392f27a1dc6
list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
286 | |
|
1237
5cba456aff67
add structure to list documentation
Mike Becker <universe@uap-core.de>
parents:
1236
diff
changeset
|
287 | ```C |
|
1239
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
288 | #include <cx/list.h> |
|
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
289 | |
|
1236
f392f27a1dc6
list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
290 | 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
|
291 | |
|
f392f27a1dc6
list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
292 | 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
|
293 | |
|
1237
5cba456aff67
add structure to list documentation
Mike Becker <universe@uap-core.de>
parents:
1236
diff
changeset
|
294 | CxIterator cxListIteratorAt(const CxList *list, size_t index); |
|
5cba456aff67
add structure to list documentation
Mike Becker <universe@uap-core.de>
parents:
1236
diff
changeset
|
295 | |
|
5cba456aff67
add structure to list documentation
Mike Becker <universe@uap-core.de>
parents:
1236
diff
changeset
|
296 | CxIterator cxListBackwardsIteratorAt(const CxList *list, size_t index); |
|
5cba456aff67
add structure to list documentation
Mike Becker <universe@uap-core.de>
parents:
1236
diff
changeset
|
297 | ``` |
|
1236
f392f27a1dc6
list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
298 | |
|
1239
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
299 | 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
|
300 | 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
|
301 | |
|
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
302 | 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
|
303 | 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
|
304 | |
|
1429
6e0c3a8a914a
remove the concept of "mutating iterators" - resolves #579
Mike Becker <universe@uap-core.de>
parents:
1428
diff
changeset
|
305 | Removing elements via an iterator will cause an invocation of the [destructor functions](collection.h.md#destructor-functions) for the removed element. |
|
1239
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
306 | |
|
1420
c6f55a2b3495
fix various typos in the web documentation
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
307 | It is safe to specify an out-of-bounds index, or a `NULL` pointer, in which cases the returned iterator will behave like an iterator over an empty list. |
|
1236
f392f27a1dc6
list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
308 | |
|
1237
5cba456aff67
add structure to list documentation
Mike Becker <universe@uap-core.de>
parents:
1236
diff
changeset
|
309 | ## Reorder |
|
5cba456aff67
add structure to list documentation
Mike Becker <universe@uap-core.de>
parents:
1236
diff
changeset
|
310 | |
|
5cba456aff67
add structure to list documentation
Mike Becker <universe@uap-core.de>
parents:
1236
diff
changeset
|
311 | ```C |
|
1239
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
312 | #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
|
313 | |
|
1239
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
314 | 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
|
315 | |
|
f392f27a1dc6
list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
316 | 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
|
317 | |
|
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
318 | void cxListSort(CxList *list); |
|
1237
5cba456aff67
add structure to list documentation
Mike Becker <universe@uap-core.de>
parents:
1236
diff
changeset
|
319 | ``` |
|
1236
f392f27a1dc6
list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
320 | |
|
1239
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
321 | 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
|
322 | 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
|
323 | |
|
1420
c6f55a2b3495
fix various typos in the web documentation
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
324 | The function `cxListReverse()` reorders all elements so that they appear in exactly the opposite order after invoking this function. |
|
1239
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
325 | |
|
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
326 | 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
|
327 | 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
|
328 | |
|
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
329 | 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
|
330 | |
|
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
331 | > 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
|
332 | > Implementations usually make use of this flag to optimize search operations, if possible. |
|
1351
4407229bd944
fix some grammar and wording issues
Mike Becker <universe@uap-core.de>
parents:
1344
diff
changeset
|
333 | > For example, the [array list](array_list.h.md) implementation will use binary search |
|
1420
c6f55a2b3495
fix various typos in the web documentation
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
334 | > 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
|
335 | |
|
1237
5cba456aff67
add structure to list documentation
Mike Becker <universe@uap-core.de>
parents:
1236
diff
changeset
|
336 | ## Compare |
|
5cba456aff67
add structure to list documentation
Mike Becker <universe@uap-core.de>
parents:
1236
diff
changeset
|
337 | |
|
5cba456aff67
add structure to list documentation
Mike Becker <universe@uap-core.de>
parents:
1236
diff
changeset
|
338 | ```C |
|
1239
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
339 | #include <cx/list.h> |
|
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
340 | |
|
1237
5cba456aff67
add structure to list documentation
Mike Becker <universe@uap-core.de>
parents:
1236
diff
changeset
|
341 | 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
|
342 | ``` |
|
f392f27a1dc6
list all function from list.h that need to be documented
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
343 | |
|
1239
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
344 | 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
|
345 | |
|
1351
4407229bd944
fix some grammar and wording issues
Mike Becker <universe@uap-core.de>
parents:
1344
diff
changeset
|
346 | That means you can compare an UCX [array list](array_list.h.md) with a [linked list](linked_list.h.md), |
|
1238
26299ce9c955
documentation for list compare and dispose
Mike Becker <universe@uap-core.de>
parents:
1237
diff
changeset
|
347 | 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
|
348 | |
|
1351
4407229bd944
fix some grammar and wording issues
Mike Becker <universe@uap-core.de>
parents:
1344
diff
changeset
|
349 | 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
|
350 | 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
|
351 | |
|
1420
c6f55a2b3495
fix various typos in the web documentation
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
352 | 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
|
353 | 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
|
354 | |
|
1436
c331add0d9f8
add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents:
1433
diff
changeset
|
355 | ## Clone |
|
c331add0d9f8
add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents:
1433
diff
changeset
|
356 | |
|
c331add0d9f8
add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents:
1433
diff
changeset
|
357 | ```C |
|
c331add0d9f8
add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents:
1433
diff
changeset
|
358 | #include <cx/allocator.h> |
|
c331add0d9f8
add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents:
1433
diff
changeset
|
359 | |
|
c331add0d9f8
add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents:
1433
diff
changeset
|
360 | typedef void*(cx_clone_func)( |
|
c331add0d9f8
add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents:
1433
diff
changeset
|
361 | void *target, const void *source, |
|
c331add0d9f8
add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents:
1433
diff
changeset
|
362 | const CxAllocator *allocator, |
|
c331add0d9f8
add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents:
1433
diff
changeset
|
363 | void *data); |
|
c331add0d9f8
add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents:
1433
diff
changeset
|
364 | |
|
c331add0d9f8
add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents:
1433
diff
changeset
|
365 | #include <cx/list.h> |
|
c331add0d9f8
add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents:
1433
diff
changeset
|
366 | |
|
1444
dd9dcbb39c2f
make clone functions return int instead of size_t
Mike Becker <universe@uap-core.de>
parents:
1441
diff
changeset
|
367 | int cxListClone(CxList *dst, const CxList *src, |
|
1436
c331add0d9f8
add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents:
1433
diff
changeset
|
368 | cx_clone_func clone_func, |
|
c331add0d9f8
add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents:
1433
diff
changeset
|
369 | const CxAllocator *clone_allocator, |
|
c331add0d9f8
add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents:
1433
diff
changeset
|
370 | void *data); |
|
1453
b6fc5b1d5c5d
add implementation for cxListDifference() - issue #745
Mike Becker <universe@uap-core.de>
parents:
1444
diff
changeset
|
371 | |
|
1479
ac1baaed2fd7
implement simple versions of the clone functions
Mike Becker <universe@uap-core.de>
parents:
1478
diff
changeset
|
372 | int cxListCloneSimple(CxList *dst, const CxList *src); |
|
ac1baaed2fd7
implement simple versions of the clone functions
Mike Becker <universe@uap-core.de>
parents:
1478
diff
changeset
|
373 | |
|
1453
b6fc5b1d5c5d
add implementation for cxListDifference() - issue #745
Mike Becker <universe@uap-core.de>
parents:
1444
diff
changeset
|
374 | int cxListDifference(CxList *dst, |
|
b6fc5b1d5c5d
add implementation for cxListDifference() - issue #745
Mike Becker <universe@uap-core.de>
parents:
1444
diff
changeset
|
375 | const CxList *minuend, const CxList *subtrahend, |
|
b6fc5b1d5c5d
add implementation for cxListDifference() - issue #745
Mike Becker <universe@uap-core.de>
parents:
1444
diff
changeset
|
376 | cx_clone_func clone_func, |
|
b6fc5b1d5c5d
add implementation for cxListDifference() - issue #745
Mike Becker <universe@uap-core.de>
parents:
1444
diff
changeset
|
377 | const CxAllocator *clone_allocator, |
|
b6fc5b1d5c5d
add implementation for cxListDifference() - issue #745
Mike Becker <universe@uap-core.de>
parents:
1444
diff
changeset
|
378 | void *data); |
|
1479
ac1baaed2fd7
implement simple versions of the clone functions
Mike Becker <universe@uap-core.de>
parents:
1478
diff
changeset
|
379 | |
|
ac1baaed2fd7
implement simple versions of the clone functions
Mike Becker <universe@uap-core.de>
parents:
1478
diff
changeset
|
380 | int cxListDifferenceSimple(CxList *dst, |
|
ac1baaed2fd7
implement simple versions of the clone functions
Mike Becker <universe@uap-core.de>
parents:
1478
diff
changeset
|
381 | const CxList *minuend, const CxList *subtrahend); |
|
ac1baaed2fd7
implement simple versions of the clone functions
Mike Becker <universe@uap-core.de>
parents:
1478
diff
changeset
|
382 | |
|
1465
dc886f1a6155
specify the intersection functions
Mike Becker <universe@uap-core.de>
parents:
1453
diff
changeset
|
383 | int cxListIntersection(CxList *dst, |
|
dc886f1a6155
specify the intersection functions
Mike Becker <universe@uap-core.de>
parents:
1453
diff
changeset
|
384 | const CxList *src, const CxList *other, |
|
dc886f1a6155
specify the intersection functions
Mike Becker <universe@uap-core.de>
parents:
1453
diff
changeset
|
385 | cx_clone_func clone_func, |
|
dc886f1a6155
specify the intersection functions
Mike Becker <universe@uap-core.de>
parents:
1453
diff
changeset
|
386 | const CxAllocator *clone_allocator, |
|
dc886f1a6155
specify the intersection functions
Mike Becker <universe@uap-core.de>
parents:
1453
diff
changeset
|
387 | void *data); |
|
1479
ac1baaed2fd7
implement simple versions of the clone functions
Mike Becker <universe@uap-core.de>
parents:
1478
diff
changeset
|
388 | |
|
ac1baaed2fd7
implement simple versions of the clone functions
Mike Becker <universe@uap-core.de>
parents:
1478
diff
changeset
|
389 | int cxListIntersectionSimple(CxList *dst, |
|
ac1baaed2fd7
implement simple versions of the clone functions
Mike Becker <universe@uap-core.de>
parents:
1478
diff
changeset
|
390 | const CxList *src, const CxList *other); |
|
ac1baaed2fd7
implement simple versions of the clone functions
Mike Becker <universe@uap-core.de>
parents:
1478
diff
changeset
|
391 | |
|
1477
9170a7dff573
implement cxListUnion() - resolves #755
Mike Becker <universe@uap-core.de>
parents:
1465
diff
changeset
|
392 | int cxListUnion(CxList *dst, |
|
9170a7dff573
implement cxListUnion() - resolves #755
Mike Becker <universe@uap-core.de>
parents:
1465
diff
changeset
|
393 | const CxList *src, const CxList *other, |
|
9170a7dff573
implement cxListUnion() - resolves #755
Mike Becker <universe@uap-core.de>
parents:
1465
diff
changeset
|
394 | cx_clone_func clone_func, |
|
9170a7dff573
implement cxListUnion() - resolves #755
Mike Becker <universe@uap-core.de>
parents:
1465
diff
changeset
|
395 | const CxAllocator *clone_allocator, |
|
9170a7dff573
implement cxListUnion() - resolves #755
Mike Becker <universe@uap-core.de>
parents:
1465
diff
changeset
|
396 | void *data); |
|
1479
ac1baaed2fd7
implement simple versions of the clone functions
Mike Becker <universe@uap-core.de>
parents:
1478
diff
changeset
|
397 | |
|
ac1baaed2fd7
implement simple versions of the clone functions
Mike Becker <universe@uap-core.de>
parents:
1478
diff
changeset
|
398 | int cxListUnionSimple(CxList *dst, |
|
ac1baaed2fd7
implement simple versions of the clone functions
Mike Becker <universe@uap-core.de>
parents:
1478
diff
changeset
|
399 | const CxList *src, const CxList *other); |
|
1436
c331add0d9f8
add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents:
1433
diff
changeset
|
400 | ``` |
|
c331add0d9f8
add cxListClone() - resolves #744 except for test coverage
Mike Becker <universe@uap-core.de>
parents:
1433
diff
changeset
|
401 | |
|
1441
78ec3e2243e4
add documentation for cxListClone() - relates to #744
Mike Becker <universe@uap-core.de>
parents:
1436
diff
changeset
|
402 | With `cxListClone()` you can create deep copies of the elements in a list and insert them into another list. |
|
78ec3e2243e4
add documentation for cxListClone() - relates to #744
Mike Becker <universe@uap-core.de>
parents:
1436
diff
changeset
|
403 | The destination list does not need to be empty, in which case the elements will be appended. |
|
1489
185dc2a4b45c
fix various typos in the docs
Mike Becker <universe@uap-core.de>
parents:
1482
diff
changeset
|
404 | Depending on the concrete list implementation, `cxListClone()` tries to allocate enough memory up-front before trying |
|
1441
78ec3e2243e4
add documentation for cxListClone() - relates to #744
Mike Becker <universe@uap-core.de>
parents:
1436
diff
changeset
|
405 | to insert anything. |
|
78ec3e2243e4
add documentation for cxListClone() - relates to #744
Mike Becker <universe@uap-core.de>
parents:
1436
diff
changeset
|
406 | |
|
1477
9170a7dff573
implement cxListUnion() - resolves #755
Mike Becker <universe@uap-core.de>
parents:
1465
diff
changeset
|
407 | The function `cxListDifference()` clones elements from the `minuend` that are _not_ contained in the `subtrahend`, |
|
9170a7dff573
implement cxListUnion() - resolves #755
Mike Becker <universe@uap-core.de>
parents:
1465
diff
changeset
|
408 | while `cxListIntersection()` only clones elements from the `src` that are _also_ contained in the `other` list. |
|
9170a7dff573
implement cxListUnion() - resolves #755
Mike Becker <universe@uap-core.de>
parents:
1465
diff
changeset
|
409 | And `cxListUnion()` clones elements from `src` first, and from `other` only when they are _not_ contained in `src`. |
|
9170a7dff573
implement cxListUnion() - resolves #755
Mike Becker <universe@uap-core.de>
parents:
1465
diff
changeset
|
410 | |
|
9170a7dff573
implement cxListUnion() - resolves #755
Mike Becker <universe@uap-core.de>
parents:
1465
diff
changeset
|
411 | All three functions, for union, difference, and intersection, are optimized for sorted lists. |
|
9170a7dff573
implement cxListUnion() - resolves #755
Mike Becker <universe@uap-core.de>
parents:
1465
diff
changeset
|
412 | In that case they will take linear time instead of quadratic time for the operation. |
|
9170a7dff573
implement cxListUnion() - resolves #755
Mike Becker <universe@uap-core.de>
parents:
1465
diff
changeset
|
413 | Also, `cxListUnion()` makes sure that the elements from `src` and `other` are cloned interleaving, |
|
9170a7dff573
implement cxListUnion() - resolves #755
Mike Becker <universe@uap-core.de>
parents:
1465
diff
changeset
|
414 | so that the result of the union is still sorted. |
|
9170a7dff573
implement cxListUnion() - resolves #755
Mike Becker <universe@uap-core.de>
parents:
1465
diff
changeset
|
415 | |
|
9170a7dff573
implement cxListUnion() - resolves #755
Mike Becker <universe@uap-core.de>
parents:
1465
diff
changeset
|
416 | However, when the `dst` list already contains elements, all functions will only append the result of the operation to that list. |
|
1489
185dc2a4b45c
fix various typos in the docs
Mike Becker <universe@uap-core.de>
parents:
1482
diff
changeset
|
417 | That means the `dst` list is only guaranteed to be sorted when it was empty and the input lists are all sorted. |
|
1453
b6fc5b1d5c5d
add implementation for cxListDifference() - issue #745
Mike Becker <universe@uap-core.de>
parents:
1444
diff
changeset
|
418 | |
|
1441
78ec3e2243e4
add documentation for cxListClone() - relates to #744
Mike Becker <universe@uap-core.de>
parents:
1436
diff
changeset
|
419 | Refer to the documentation of the [clone-function callback](allocator.h.md#clone-function) to learn how to implement it. |
|
78ec3e2243e4
add documentation for cxListClone() - relates to #744
Mike Becker <universe@uap-core.de>
parents:
1436
diff
changeset
|
420 | |
|
1479
ac1baaed2fd7
implement simple versions of the clone functions
Mike Becker <universe@uap-core.de>
parents:
1478
diff
changeset
|
421 | The _simple_ versions of the above functions use an internal shallow clone function |
|
ac1baaed2fd7
implement simple versions of the clone functions
Mike Becker <universe@uap-core.de>
parents:
1478
diff
changeset
|
422 | which uses `memcpy()` to copy the elements. |
|
ac1baaed2fd7
implement simple versions of the clone functions
Mike Becker <universe@uap-core.de>
parents:
1478
diff
changeset
|
423 | If the destination map is storing pointers, this internal clone function uses the current default allocator to allocate the memory. |
|
ac1baaed2fd7
implement simple versions of the clone functions
Mike Becker <universe@uap-core.de>
parents:
1478
diff
changeset
|
424 | |
|
1453
b6fc5b1d5c5d
add implementation for cxListDifference() - issue #745
Mike Becker <universe@uap-core.de>
parents:
1444
diff
changeset
|
425 | The functions return zero if and only if all clone operations were successful. |
|
1441
78ec3e2243e4
add documentation for cxListClone() - relates to #744
Mike Becker <universe@uap-core.de>
parents:
1436
diff
changeset
|
426 | |
|
78ec3e2243e4
add documentation for cxListClone() - relates to #744
Mike Becker <universe@uap-core.de>
parents:
1436
diff
changeset
|
427 | > It is perfectly possible to clone items into a list of a different type. |
|
78ec3e2243e4
add documentation for cxListClone() - relates to #744
Mike Becker <universe@uap-core.de>
parents:
1436
diff
changeset
|
428 | > For example, you can clone elements from a list that is just storing pointers (`CX_STORE_POINTERS`) to a list that |
|
78ec3e2243e4
add documentation for cxListClone() - relates to #744
Mike Becker <universe@uap-core.de>
parents:
1436
diff
changeset
|
429 | > allocates the memory for the objects (and vice versa). |
|
78ec3e2243e4
add documentation for cxListClone() - relates to #744
Mike Becker <universe@uap-core.de>
parents:
1436
diff
changeset
|
430 | |
|
1478
bba2e5efdca0
add warning, not to pass the same pointer multiple times to the clone functions
Mike Becker <universe@uap-core.de>
parents:
1477
diff
changeset
|
431 | > The lists passed to the above functions must all be different. |
|
bba2e5efdca0
add warning, not to pass the same pointer multiple times to the clone functions
Mike Becker <universe@uap-core.de>
parents:
1477
diff
changeset
|
432 | > Passing the same pointer for different list arguments may result in erroneous behavior. |
|
bba2e5efdca0
add warning, not to pass the same pointer multiple times to the clone functions
Mike Becker <universe@uap-core.de>
parents:
1477
diff
changeset
|
433 | >{style="warning"} |
|
bba2e5efdca0
add warning, not to pass the same pointer multiple times to the clone functions
Mike Becker <universe@uap-core.de>
parents:
1477
diff
changeset
|
434 | |
|
1482
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1479
diff
changeset
|
435 | ## Reserve and Shrink |
|
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1479
diff
changeset
|
436 | |
|
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1479
diff
changeset
|
437 | ```C |
|
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1479
diff
changeset
|
438 | #include <cx/list.h> |
|
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1479
diff
changeset
|
439 | |
|
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1479
diff
changeset
|
440 | int cxListReserve(CxList *list, size_t count); |
|
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1479
diff
changeset
|
441 | |
|
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1479
diff
changeset
|
442 | int cxListShrink(CxList *list); |
|
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1479
diff
changeset
|
443 | ``` |
|
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1479
diff
changeset
|
444 | |
|
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1479
diff
changeset
|
445 | If a list implementation allows overallocation, the function `cxListReserve()` can be used to reserve memory for a total of `count` elements. |
|
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1479
diff
changeset
|
446 | On the other hand, you can use `cxListShrink()` to dispose of any overallocated memory and reduce the capacity to the actual number of currently stored elements. |
|
1489
185dc2a4b45c
fix various typos in the docs
Mike Becker <universe@uap-core.de>
parents:
1482
diff
changeset
|
447 | Calling `cxListReserve()` with a `count` argument that is less than the current size of the list has no effect, and the function returns zero. |
|
1482
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1479
diff
changeset
|
448 | |
|
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1479
diff
changeset
|
449 | If allocating memory fails, `cxListReserve()` returns a non-zero value. |
|
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1479
diff
changeset
|
450 | Since shrinking should never fail, `cxListShrink()` will usually always return zero, |
|
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1479
diff
changeset
|
451 | but depending on the list (or allocator) implementation, the possibility for a non-zero return value is there. |
|
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1479
diff
changeset
|
452 | |
|
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1479
diff
changeset
|
453 | List implementations that do not overallocate are advised to simply return zero. |
|
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1479
diff
changeset
|
454 | That means, using those functions never does any harm and calls to them can be added whenever it seems appropriate. |
|
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1479
diff
changeset
|
455 | Even if the currently used list implementation does not support overallocation, it may become extremely useful when the concrete implementation is exchanged later. |
|
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1479
diff
changeset
|
456 | |
|
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1479
diff
changeset
|
457 | > The function `cxListReserve()` should not be confused with `cxListEmplaceArray()`. |
|
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1479
diff
changeset
|
458 | > The former will only increase the capacity of a list which supports overallocation without changing the size. |
|
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1479
diff
changeset
|
459 | > The latter will actually add real, uninitialized elements. |
|
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1479
diff
changeset
|
460 | >{style="note"} |
|
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1479
diff
changeset
|
461 | |
|
1237
5cba456aff67
add structure to list documentation
Mike Becker <universe@uap-core.de>
parents:
1236
diff
changeset
|
462 | ## Dispose |
|
5cba456aff67
add structure to list documentation
Mike Becker <universe@uap-core.de>
parents:
1236
diff
changeset
|
463 | |
|
5cba456aff67
add structure to list documentation
Mike Becker <universe@uap-core.de>
parents:
1236
diff
changeset
|
464 | ```C |
|
1239
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
465 | #include <cx/list.h> |
|
b4b1f15d1866
complete more than 80% of the list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1238
diff
changeset
|
466 | |
|
1237
5cba456aff67
add structure to list documentation
Mike Becker <universe@uap-core.de>
parents:
1236
diff
changeset
|
467 | void cxListFree(CxList *list); |
|
5cba456aff67
add structure to list documentation
Mike Becker <universe@uap-core.de>
parents:
1236
diff
changeset
|
468 | ``` |
|
5cba456aff67
add structure to list documentation
Mike Becker <universe@uap-core.de>
parents:
1236
diff
changeset
|
469 | |
|
1238
26299ce9c955
documentation for list compare and dispose
Mike Becker <universe@uap-core.de>
parents:
1237
diff
changeset
|
470 | 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
|
471 | |
|
26299ce9c955
documentation for list compare and dispose
Mike Becker <universe@uap-core.de>
parents:
1237
diff
changeset
|
472 | 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
|
473 | 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
|
474 | |
|
1351
4407229bd944
fix some grammar and wording issues
Mike Becker <universe@uap-core.de>
parents:
1344
diff
changeset
|
475 | When the list has been storing pointers, make sure that either another reference to the same memory exists in your program, |
|
1238
26299ce9c955
documentation for list compare and dispose
Mike Becker <universe@uap-core.de>
parents:
1237
diff
changeset
|
476 | 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
|
477 | 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
|
478 | |
|
5cba456aff67
add structure to list documentation
Mike Becker <universe@uap-core.de>
parents:
1236
diff
changeset
|
479 | ## Implement own List Structures |
|
5cba456aff67
add structure to list documentation
Mike Becker <universe@uap-core.de>
parents:
1236
diff
changeset
|
480 | |
|
1240
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
481 | If you want to create your own list implementation, this is extremely easy. |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
482 | |
|
1351
4407229bd944
fix some grammar and wording issues
Mike Becker <universe@uap-core.de>
parents:
1344
diff
changeset
|
483 | You need to define a function for creating your list and assign a `cx_list_class` structure with the pointers to your implementation. |
|
1240
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
484 | Then you call the `cx_list_init()` helper function to initialize the collection sture. |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
485 | This also automatically adds support for `CX_STORE_POINTERS` to your list. |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
486 | |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
487 | ```C |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
488 | // define the class with pointers to your functions |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
489 | static cx_list_class my_list_class = { |
|
1244
9a8e781258ac
complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1243
diff
changeset
|
490 | my_list_deallocate, |
|
1240
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
491 | my_list_insert_element, |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
492 | my_list_insert_array, |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
493 | my_list_insert_sorted, |
|
1421
809eb30cd621
fix missing documentation if insert_unique member - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1420
diff
changeset
|
494 | my_list_insert_unique, |
|
1240
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
495 | my_list_insert_iter, |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
496 | my_list_remove, |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
497 | my_list_clear, |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
498 | my_list_swap, |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
499 | my_list_at, |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
500 | my_list_find_remove, |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
501 | my_list_sort, |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
502 | my_list_compare, |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
503 | my_list_reverse, |
|
1482
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1479
diff
changeset
|
504 | my_list_change_capacity, |
|
1240
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
505 | my_list_iterator, |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
506 | }; |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
507 | |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
508 | typedef struct { |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
509 | struct cx_list_s base; |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
510 | // your custom list data goes here |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
511 | } my_list; |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
512 | |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
513 | CxList *my_list_create( |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
514 | const CxAllocator *allocator, |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
515 | cx_compare_func cmpfun, |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
516 | size_t elem_size |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
517 | ) { |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
518 | if (allocator == NULL) { |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
519 | allocator = cxDefaultAllocator; |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
520 | } |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
521 | |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
522 | my_list *list = cxCalloc(allocator, 1, sizeof(my_list)); |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
523 | if (list == NULL) return NULL; |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
524 | cx_list_init((CxList*)list, &my_list_class, |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
525 | allocator, cmpfun, elem_size); |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
526 | |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
527 | // initialize your custom list data here |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
528 | |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
529 | return (CxList *) list; |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
530 | } |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
531 | ``` |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
532 | |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
533 | The required behavior for the implementations is described in the following table. |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
534 | You can always look at the source code of the UCX implementations to get inspiration. |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
535 | |
|
1482
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1479
diff
changeset
|
536 | | Function | Description | |
|
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1479
diff
changeset
|
537 | |-------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |
|
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1479
diff
changeset
|
538 | | `clear` | Invoke destructor functions on all elements and remove them from the list. | |
|
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1479
diff
changeset
|
539 | | `deallocate` | Invoke destructor functions on all elements and deallocate the entire list memory. | |
|
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1479
diff
changeset
|
540 | | `insert_element` | Insert or allocate a single element at the specified index. Return a pointer to the allocated element or `NULL` on failure. | |
|
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1479
diff
changeset
|
541 | | `insert_array` | Insert or allocate an array of elements starting at the specified index. Return the number of successfully inserted or allocated elements. | |
|
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1479
diff
changeset
|
542 | | `insert_sorted` | Insert an array of sorted elements into a sorted list. Return the number of elements processed (equals the number of elements inserted in this case). | |
|
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1479
diff
changeset
|
543 | | `insert_unique` | Insert an array of sorted unique elements into a sorted list. Return the number of elements processed (not the number of elements inserted, which might be lower). | |
|
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1479
diff
changeset
|
544 | | `insert_iter` | Insert a single element depending on the iterator position. The third argument to this function is zero when the element shall be inserted after the iterator position and non-zero if it shall be inserted before the iterator position. The implementation is also responsible for adjusting the iterator, respectively. | |
|
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1479
diff
changeset
|
545 | | `remove` | Remove multiple elements starting at the specified index. If a target buffer is specified, copy the elements to that buffer. Otherwise, invoke the destructor functions for the elements. Return the number of elements actually removed. | |
|
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1479
diff
changeset
|
546 | | `swap` | Swap two elements by index. Return zero on success or non-zero when any index was out-of-bounds. | |
|
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1479
diff
changeset
|
547 | | `at` | Return a pointer to the element at the specified index or `NULL` when the index is out-of-bounds. | |
|
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1479
diff
changeset
|
548 | | `find_remove` | Search for the specified element with the list's compare function and return the index if found. If the `remove` argument is true, invoke the destructor functions and remove the element. Return the list size if the element is not found. | |
|
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1479
diff
changeset
|
549 | | `sort` | Sort all elements in the list with respect to the list's compare function. | |
|
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1479
diff
changeset
|
550 | | `compare` | This function pointer can be `NULL`, in which case an unoptimized fallback is used. You can implement an optimized compare function that compares the list of another list of the same class. | |
|
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1479
diff
changeset
|
551 | | `reverse` | Reorder all elements in the list so that they appear in exactly the opposite order. | |
|
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1479
diff
changeset
|
552 | | `change_capacity` | When your list supports overallocation, the capacity shall be changed to the specified value. Return non-zero on error and zero on success. When the list does not support overallocation, this function pointer can be `NULL`. | |
|
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1479
diff
changeset
|
553 | | `iterator` | Create an iterator starting at the specified index. The Boolean argument indicates whether iteration is supposed to traverse backwards. | |
|
1240
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
554 | |
|
1420
c6f55a2b3495
fix various typos in the web documentation
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
555 | > If you initialize your list with `cx_list_init()`, you do not have to worry about making a |
|
1240
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
556 | > difference between storing pointers and storing elements, because your implementation will |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
557 | > be automatically wrapped. |
|
1351
4407229bd944
fix some grammar and wording issues
Mike Becker <universe@uap-core.de>
parents:
1344
diff
changeset
|
558 | > This means you only have to handle the one single case described above. |
|
1240
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
559 | |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
560 | ### Default Class Function Implementations |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
561 | |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
562 | If you are satisfied with some of the default implementations, you can use some pre-defined functions. |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
563 | Note, however, that those are not optimized for any specific list structure |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
564 | and just serve as a quick and convenient solution if performance does not matter for your use case. |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
565 | |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
566 | |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
567 | | Default Function | Description | |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
568 | |---------------------------------|-----------------------------------------------------------------------------------| |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
569 | | `cx_list_default_insert_array` | Falls back to multiple calls of `insert_element`. | |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
570 | | `cx_list_default_insert_sorted` | Uses linear search to find insertion points. | |
|
1421
809eb30cd621
fix missing documentation if insert_unique member - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1420
diff
changeset
|
571 | | `cx_list_default_insert_unique` | Like `cx_default_insert_sorted` but skips elements that are already contained. | |
|
1240
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
572 | | `cx_list_default_sort` | Copies all elements to an array, applies `qsort()`, and copies the elements back. | |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
573 | | `cx_list_default_swap` | Uses a temporarily allocated buffer to perform a three-way-swap. | |
|
c5924c781372
complete list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1239
diff
changeset
|
574 | |
|
1142
9437530176bc
add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents:
1141
diff
changeset
|
575 | |
|
1190
a7b913d5d589
bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
576 | <seealso> |
|
a7b913d5d589
bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
577 | <category ref="apidoc"> |
|
a7b913d5d589
bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
578 | <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
|
579 | </category> |
|
a7b913d5d589
bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
580 | </seealso> |