docs/Writerside/topics/array_list.h.md

Tue, 16 Dec 2025 18:30:08 +0100

author
Mike Becker <universe@uap-core.de>
date
Tue, 16 Dec 2025 18:30:08 +0100
changeset 1614
1e0e7f08ccd6
parent 1605
55b13f583356
permissions
-rw-r--r--

update the online docs with the new array API - resolves #619

1143
0559812df10c assign proper names to the documentation topics
Mike Becker <universe@uap-core.de>
parents: 1142
diff changeset
1 # Array List
1141
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
3 Next to an array list implementation of the list interface,
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
4 UCX offers several functions to work with plain C arrays equipped with a size and a capacity.
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
5
1390
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents: 1322
diff changeset
6 The high-level [list interface](list.h.md) is documented on a separate page and explains how lists are used
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
7 that are created by one of the following functions.
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
8
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
9 ```C
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
10 #include <cx/array_list.h>
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
11
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
12 CxList *cxArrayListCreate(const CxAllocator *allocator,
1605
55b13f583356 refactor the list and map construction functions and remove the simple macros
Mike Becker <universe@uap-core.de>
parents: 1507
diff changeset
13 size_t elem_size, size_t initial_capacity);
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
14 ```
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
15
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
16 The remaining documentation on this page concentrates on dealing with plain C arrays.
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
17
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
18 ## Declaring and Initializing
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
19
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
20 ```C
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
21 #include <cx/array_list.h>
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
22
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
23 #define CX_ARRAY(type, name)
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
24
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
25 int cx_array_init(CxArray array, size_t capacity);
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
26
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
27 int cx_array_init_a(const CxAllocator *allocator,
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
28 CxArray array, size_t capacity);
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
29
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
30 void cx_array_init_fixed(CxArray array,
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
31 void *fixed_size_array, size_t num_initialized);
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
32 ```
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
33
1246
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
34 The purpose of the UCX array functions is to keep track of the size and capacity of a plain C array.
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
35
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
36 The `CX_ARRAY()` macro declares a variable over an anonymous struct which is compatible to `CxArray`.
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
37 The thus created struct contains the three members `data`, `size`, and `capacity`.
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
38 You can then pass a pseudo-reference to this variable to any of the UCX array functions.
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
39 This is realized by macros which will automatically take the address of your variable and cast it to a pointer of `CxArray`.
1246
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
40
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
41 Once the array is declared, you can use one of `cx_array_init()`, `cx_array_init_a()`, or `cx_array_init_fixed()` to initialize it.
1246
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
42
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
43 The former two functions allocate memory for the array using the [default allocator](allocator.h.md#default-allocator) or the allocator passed to the function.
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
44 They return zero on success and non-zero on allocation failure.
1246
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
45
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
46 The latter function initializes the array with a fixed-sized array.
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
47 The `num_initialized` argument specifies the number of elements that are already initialized in the array.
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
48 This is useful, for example, if you want to initialize the array with stack memory and only want to
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
49 allocate memory if the capacity you reserved on the stack is not sufficient.
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
50 More on this in the following section, when discussing `cx_array_copy_to_new()`.
1246
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
51
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
52 ## Reallocation
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
53
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
54 ```C
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
55 #include <cx/array_list.h>
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
56
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
57 int cx_array_reserve(CxArray array, size_t capacity);
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
58
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
59 int cx_array_copy_to_new(CxArray array, size_t capacity);
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
60
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
61 int cx_array_reserve_a(const CxAllocator *allocator,
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
62 CxArray array, size_t capacity);
1248
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
63
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
64 int cx_array_copy_to_new_a(const CxAllocator *allocator,
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
65 CxArray array, size_t capacity);
1141
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
66 ```
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
67
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
68 The functions `cx_array_reserve()` and `cx_array_reserve_a()` change the capacity of the array to exactly `capacity` elements.
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
69 If the current `size` is larger than `capacity`, the array is truncated to `capacity`.
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
70
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
71 The functions `cx_array_copy_to_new()` and `cx_array_copy_to_new_a()` allocate a new memory block with the specified capacity
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
72 and copy the elements of the old memory to the new memory.
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
73
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
74 > The functions `cx_array_copy_to_new()` and `cx_array_copy_to_new_a()` do **not** free the old memory.
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
75 > If the old memory is not located on the stack, or you don't have another reference to it, it will be leaked.
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
76 > The functions are particularly useful when the array was initialized with a fixed-sized array.
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
77 >{style="note"}
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
78
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
79 ## Add or Insert Elements
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
80
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
81 ```C
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
82 #include <cx/array_list.h>
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
83
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
84 int cx_array_add(CxArray array, const void *element);
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
85
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
86 int cx_array_add_array(CxArray array, const void *other, size_t n);
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
87
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
88 int cx_array_insert(CxArray array, size_t index, const void *element);
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
89
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
90 int cx_array_insert_array(CxArray array,
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
91 size_t index, const void *other, size_t n);
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
92
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
93 int cx_array_add_a(const CxAllocator* allocator,
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
94 CxArray array, const void *element);
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
95
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
96 int cx_array_add_array(const CxAllocator* allocator,
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
97 CxArray array, const void *other, size_t n);
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
98
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
99 int cx_array_insert_a(const CxAllocator* allocator,
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
100 CxArray array, size_t index, const void *element);
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
101
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
102 int cx_array_insert_array_a(const CxAllocator* allocator,
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
103 CxArray array, size_t index, const void *other, size_t n);
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
104 ```
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
105
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
106 The above functions insert one or more elements into the array at the specified `index`
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
107 or add them to the end of the array.
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
108
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
109 When the array capacity is not sufficient, a re-allocation is attempted.
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
110 If the allocation fails, the function returns non-zero.
1248
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
111
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
112 > Be careful when using these functions on an array that was initialized with fixed-sized memory.
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
113 > In this case, you MUST make sure that the capacity is sufficient or reallocate the array
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
114 > with `cx_array_copy_to_new()` or `cx_array_copy_to_new_a()` before adding or inserting more elements.
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
115 >{style="note"}
1141
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
116
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
117 ## Insertion Sort
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
118
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
119 ```C
1419
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
120 #include <cx/array_list.h>
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
121
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
122 int cx_array_insert_sorted(CxArray array,
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
123 cx_compare_func cmp_func, const void *element);
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
124
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
125 int cx_array_insert_sorted_array(CxArray array,
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
126 cx_compare_func cmp_func, const void *sorted_data, size_t n);
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
127
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
128 int cx_array_insert_sorted_a(const CxAllocator *allocator,
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
129 CxArray array, cx_compare_func cmp_func, const void *element);
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
130
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
131 int cx_array_insert_sorted_array_a(const CxAllocator *allocator,
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
132 CxArray array, cx_compare_func cmp_func,
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
133 const void *sorted_data, size_t n);
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
134 ```
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
135
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
136 The above functions are equivalent to `cx_array_insert()` and `cx_array_insert_array()`,
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
137 except that they only work on sorted arrays and insert the element at the correct position with respect to the sort order.
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
138 If either the array or the `sorted_data` is not sorted according to the given `cmp_func`, the behavior is undefined.
1248
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
139
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
140 ## Insert Unique Elements
1419
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
141
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
142 ```C
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
143 #include <cx/array_list.h>
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
144
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
145 int cx_array_insert_unique(CxArray array,
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
146 cx_compare_func cmp_func, const void *element);
1419
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
147
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
148 int cx_array_insert_unique_array(CxArray array,
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
149 cx_compare_func cmp_func, const void *sorted_data, size_t n);
1419
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
150
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
151 int cx_array_insert_unique_a(const CxAllocator *allocator,
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
152 CxArray array, cx_compare_func cmp_func, const void *element);
1419
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
153
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
154 int cx_array_insert_unique_array_a(const CxAllocator *allocator,
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
155 CxArray array, cx_compare_func cmp_func,
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
156 const void *sorted_data, size_t n);
1419
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
157 ```
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
158
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
159 The above functions are similar to the functions for insertion sort,
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
160 except that they only insert elements that are not already present in the array.
1419
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
161
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
162 ## Binary Search
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
163
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
164 ```C
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
165 #include <cx/array_list.h>
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
166
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
167 size_t cx_array_binary_search(
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
168 const void *arr, size_t size, size_t elem_size,
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
169 const void *elem, cx_compare_func cmp_func);
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
170
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
171 size_t cx_array_binary_search_inf(
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
172 const void *arr, size_t size, size_t elem_size,
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
173 const void *elem, cx_compare_func cmp_func);
1142
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
174
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
175 size_t cx_array_binary_search_sup(
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
176 const void *arr, size_t size, size_t elem_size,
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
177 const void *elem, cx_compare_func cmp_func);
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
178 ```
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
179
1248
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
180 The function `cx_array_binary_search()` searches the array `arr` for the data pointed to by `elem` using the compare function `cmp_func`.
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
181
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
182 Note that these functions do not operate on `CxArray` and are instead more general and also useful on plain C arrays.
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
183
1248
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
184 If the element is found (i.e. `cmp_func` returns zero), the function returns the index of the element.
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
185 Otherwise, the function returns `size`.
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
186
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
187 The functions `cx_array_binary_search_inf()` and `cx_array_binary_search_sup()` are equivalent,
1424
563033aa998c fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents: 1419
diff changeset
188 except that they return the index of the closest element if the searched element is not found.
563033aa998c fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents: 1419
diff changeset
189 The former function returns the closest element that is less or equal (the greatest lower bound / infimum),
563033aa998c fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents: 1419
diff changeset
190 and the latter function returns the closest element that is larger or equal (the least upper bound / supremum)
1248
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
191 than the searched element.
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
192
1507
f4010cda9a2a stable return value for binary search when there are duplicates in the array
Mike Becker <universe@uap-core.de>
parents: 1429
diff changeset
193 When the found element appears more than once in the array,
f4010cda9a2a stable return value for binary search when there are duplicates in the array
Mike Becker <universe@uap-core.de>
parents: 1429
diff changeset
194 the binary search and the infimum report the largest index
f4010cda9a2a stable return value for binary search when there are duplicates in the array
Mike Becker <universe@uap-core.de>
parents: 1429
diff changeset
195 and the supremum reports the smallest index of the identical items, respectively.
f4010cda9a2a stable return value for binary search when there are duplicates in the array
Mike Becker <universe@uap-core.de>
parents: 1429
diff changeset
196
1425
83284b289430 remove unnecessary members from the array reallocator struct - fixes #621
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
197 > Note that all the above functions are only well-defined on arrays which are sorted with respect to the given compare function.
1248
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
198 >
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
199 > This can, for example, easily be achieved by calling the standard library's `qsort()` function.
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
200 >{style="note"}
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
201
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
202 > Despite the name, neither C nor POSIX standards require the standard library's `bsearch()` function to be implemented using binary search.
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
203 > But observations show that it usually is.
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
204 > This makes `cx_array_binary_search()` likely to be equivalent to `bsearch()`, except that it returns an index rather than a pointer.
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
205
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
206 ## Iterators
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
207
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
208 ```C
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
209 #include <cx/iterator.h>
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
210
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
211 CxIterator cxIterator(const void *array,
1429
6e0c3a8a914a remove the concept of "mutating iterators" - resolves #579
Mike Becker <universe@uap-core.de>
parents: 1425
diff changeset
212 size_t elem_size, size_t elem_count,
6e0c3a8a914a remove the concept of "mutating iterators" - resolves #579
Mike Becker <universe@uap-core.de>
parents: 1425
diff changeset
213 bool remove_keeps_order);
6e0c3a8a914a remove the concept of "mutating iterators" - resolves #579
Mike Becker <universe@uap-core.de>
parents: 1425
diff changeset
214
6e0c3a8a914a remove the concept of "mutating iterators" - resolves #579
Mike Becker <universe@uap-core.de>
parents: 1425
diff changeset
215 CxIterator cxIteratorPtr(const void *array,
6e0c3a8a914a remove the concept of "mutating iterators" - resolves #579
Mike Becker <universe@uap-core.de>
parents: 1425
diff changeset
216 size_t elem_count,
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
217 bool remove_keeps_order);
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
218 ```
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
219
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
220 Iterators over plain C arrays are defined in [iterator.h](iterator.h.md#creating-an-iterator).
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
221
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
222 ## Other
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
223
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
224 ```C
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
225 #include <cx/array_list.h>
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
226
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
227 void cx_array_swap(
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
228 void *arr,
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
229 size_t elem_size,
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
230 size_t idx1,
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
231 size_t idx2
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
232 );
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
233 ```
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
234
1248
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
235 The function `cx_array_swap()` exchanges two items with a three-way swap.
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
236
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
237 No memory is allocated if the element size `elem_size` is not larger than `CX_ARRAY_SWAP_SBO_SIZE` (cf. [](install.md#small-buffer-optimizations)).
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
238
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
239 You have to make sure that both indices are within bounds of the array `arr`.
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
240
1190
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
241 <seealso>
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
242 <category ref="apidoc">
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
243 <a href="https://ucx.sourceforge.io/api/array__list_8h.html">array_list.h</a>
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
244 </category>
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
245 </seealso>

mercurial