docs/Writerside/topics/array_list.h.md

Sun, 14 Dec 2025 17:30:17 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 14 Dec 2025 17:30:17 +0100
changeset 1605
55b13f583356
parent 1507
f4010cda9a2a
permissions
-rw-r--r--

refactor the list and map construction functions and remove the simple macros

relates to #780
relates to #622

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
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
18 ## Declare Array with Size and Capacity
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
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
23 #define CX_ARRAY_DECLARE_SIZED(type, name, size_type)
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
24
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
25 #define CX_ARRAY_DECLARE(type, name)
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
26
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
27 #define cx_array_initialize(ARRAY, capacity)
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
28
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
29 #define cx_array_initialize_a(allocator, ARRAY, capacity)
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
30 ```
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
31
1246
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
32 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
33
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
34 The raw functions do not need this information packed into a structure, but working with independent variables is quite cumbersome.
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
35 Therefore, UCX defines several macros that call the raw functions assuming certain variable names.
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
36
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
37 This is what the `CX_ARRAY_DECLARE_SIZED()` and `CX_ARRAY_DECLARE()` macros are for.
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
38 They take a `type` for the array members, a `name` for the array variable, and a `size_type` for the type of the size and capacity variables (`size_t` by default when you use `CX_ARRAY_DECLARE()`),
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
39 and generate three variables named `name`, `name_size`, and `name_capacity`.
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
40
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
41 For example, you can abbreviate the following code
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
42 ```C
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
43 struct my_data {
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
44 int other_int;
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
45 float other_float;
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
46 int *my_array;
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
47 size_t my_array_size;
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
48 size_t my_array_capacity;
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
49 }
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
50 ```
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
51 by substituting the three members for the array with `CX_ARRAY_DECLARE()`.
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
52 ```C
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
53 struct my_data {
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
54 int other_int;
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
55 float other_float;
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
56 CX_ARRAY_DECLARE(int, my_array);
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
57 }
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
58 ```
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
59
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
60 > Using `CX_ARRAY_DECLARE_SIZED()` can save some space when you are using a size type that is _half_ as wide as `sizeof(void*)`.
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
61 > On 64-bit architectures, having the possibility to store more than four billion items is rarely necessary, hence using 32-bit integers for size and capacity
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
62 > saves eight bytes (assuming proper alignment in your struct).
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
63
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
64 Once the array is declared, you can use one of the macros `cx_array_initialize()` or `cx_array_initialize_a()` to allocate memory.
1318
12fa1d37fe48 allow changing the cxDefaultAllocator - resolves #669
Mike Becker <universe@uap-core.de>
parents: 1248
diff changeset
65 The former uses the [default allocator](allocator.h.md#default-allocator) and the latter allows you to use a specific allocator.
1246
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
66 Important to note is, that the `ARRAY` argument expects the variable's _name_.
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
67 The macros set `ARRAY_size` to zero, `ARRAY_capacity` to the specified initial capacity, and invoke the allocator's `malloc()` function to allocate the memory.
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
68
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
69 Using the example from above, and the UCX [memory pool](mempool.h.md), this could look like this:
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
70
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
71 ```C
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
72 CxMempool *mpool = cxMempoolCreateSimple(128);
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
73
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
74 struct my_data data;
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
75 cx_array_initialize_a(mpool->allocator, data.my_array, 16);
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
76 ```
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
77
1424
563033aa998c fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents: 1419
diff changeset
78 > The aforementioned macros simplify your life, but keep in mind that using them is not mandatory.
563033aa998c fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents: 1419
diff changeset
79 > All other macros continue to work perfectly if you declare and initialize your array manually, as long as you use
1246
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
80 > the naming convention to suffix the size variable with `_size` and the capacity variable with `_capacity`.
1424
563033aa998c fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents: 1419
diff changeset
81 > Also, you can freely decide in which order you want to declare the variables.
1246
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
82 >
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
83 > For example, when you have multiple arrays in your struct with 8-bit `size_type` (because in your use case you don't expect more than 255 elements),
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
84 > it is favorable to group all pointers and then declare the size and capacity variables to avoid padding between the array declarations.
1146
151c057faf7c add marker to every incomplete page
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
85
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
86 ## Array Reallocator
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
87
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
88 ```C
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
89 #include <cx/array_list.h>
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
90
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
91 typedef struct {
1322
7be10b57f658 fix critical memory overflow in the stack-based array reallocator
Mike Becker <universe@uap-core.de>
parents: 1318
diff changeset
92 void *(*realloc)(void *array,
7be10b57f658 fix critical memory overflow in the stack-based array reallocator
Mike Becker <universe@uap-core.de>
parents: 1318
diff changeset
93 size_t old_capacity, size_t new_capacity,
7be10b57f658 fix critical memory overflow in the stack-based array reallocator
Mike Becker <universe@uap-core.de>
parents: 1318
diff changeset
94 size_t elem_size, CxArrayReallocator *alloc);
1425
83284b289430 remove unnecessary members from the array reallocator struct - fixes #621
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
95 const CxAllocator *allocator;
83284b289430 remove unnecessary members from the array reallocator struct - fixes #621
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
96 const void *stack_ptr;
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
97 } CxArrayReallocator;
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
98
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
99 CxArrayReallocator cx_array_reallocator(
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
100 const struct cx_allocator_s *allocator,
1425
83284b289430 remove unnecessary members from the array reallocator struct - fixes #621
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
101 const void *stack_ptr
1141
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
102 );
1248
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
103
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
104 extern CxArrayReallocator* cx_array_default_reallocator;
1141
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
105 ```
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
106
1424
563033aa998c fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents: 1419
diff changeset
107 The main purpose of the functions defined in the array list header
563033aa998c fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents: 1419
diff changeset
108 is to make it easier to write to an array without caring too much about a possibly necessary reallocation.
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
109
1424
563033aa998c fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents: 1419
diff changeset
110 This is realized by passing a reallocator to the various functions that specify how an array shall be reallocated when needed.
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
111
1318
12fa1d37fe48 allow changing the cxDefaultAllocator - resolves #669
Mike Becker <universe@uap-core.de>
parents: 1248
diff changeset
112 The default `cx_array_default_reallocator` simply defers to the [default allocator](allocator.h.md#default-allocator).
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
113
1248
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
114 A reallocator created with the `cx_array_reallocator()` function uses a more sophisticated approach.
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
115 On the one hand, it can use an arbitrary UCX [allocator](allocator.h.md) for the reallocation,
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
116 and on the other hand, it can optionally keep track of a stack memory pointer.
1425
83284b289430 remove unnecessary members from the array reallocator struct - fixes #621
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
117 If you pass a non-`NULL` pointer to `stack_ptr`, the reallocator will _always_ allocate _new_ memory for the array,
83284b289430 remove unnecessary members from the array reallocator struct - fixes #621
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
118 if the current location equals the `stack_ptr` address.
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
119
1248
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
120 This allows you to allocate an array on the stack and instruct UCX to automatically move it to heap memory when the capacity is exceeded.
1425
83284b289430 remove unnecessary members from the array reallocator struct - fixes #621
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
121 Combined with a UCX [memory pool](mempool.h.md) this can be a powerful tool.
83284b289430 remove unnecessary members from the array reallocator struct - fixes #621
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
122 For example, you can add small arrays to your structs plus a memory pool and then use that memory pool to reallocate the arrays on the heap when needed.
83284b289430 remove unnecessary members from the array reallocator struct - fixes #621
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
123 When you are done with the arrays, you can then use the memory pool to free the memory for those arrays that needed to be moved to the heap.
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
124
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
125 ## Reserve
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
126
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
127 ```C
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
128 #include <cx/array_list.h>
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
129
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
130 int cx_array_reserve(
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
131 void **array, void *size, void *capacity, unsigned width,
1248
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
132 size_t elem_size, size_t count,
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
133 CxArrayReallocator *reallocator);
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 #define cx_array_simple_reserve(ARRAY, count)
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
136 #define cx_array_simple_reserve_a(reallocator, ARRAY, count)
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
137 ```
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
138
1248
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
139 The function `cx_array_reserve()` guarantees that at least `count` _additional_ elements
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
140 can be stored in the array pointed to by `array`.
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
141
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
142 The `array` argument is a pointer to the location of the target array pointer.
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
143 The reason for this additional indirection is that this function writes
1424
563033aa998c fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents: 1419
diff changeset
144 a new pointer back to that variable if the array needed to be reallocated.
1248
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
145
1424
563033aa998c fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents: 1419
diff changeset
146 If reallocation fails, this function returns non-zero, leaving the array as it was.
1248
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
147 Otherwise, the function returns zero.
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
148
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
149 If `*size + elem_count` does not exceed the current `*capacity`, this function does nothing and simply returns zero.
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
150 Otherwise, the specified `reallocator` is used to reserve the necessary space.
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
151 If reallocation was necessary but failed, this function returns non-zero.
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
152
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
153 The actual data type of `size` and `capacity` can be a pointer to an arbitrary integer whose byte-width is determined by the `width` argument.
1424
563033aa998c fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents: 1419
diff changeset
154 On 32-bit platforms the widths 1, 2, and 4 are supported, and on 64-bit platforms a width of 8 is also supported.
1248
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
155 Passing zero as argument makes the function assume the native width for size arguments `sizeof(size_t)`.
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
156
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
157 The convenience macros take the _name_ of an array variable and invoke the function by deducing the other arguments
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
158 (including the width of the size and capacity).
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
159 The reallocator used by the `cx_array_simple_reserve()` macro is the `cx_array_default_reallocator`.
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
160
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
161 > While the actual integer type for `size` and `capacity` can be chosen freely, it must be _the same_ for both variables.
1424
563033aa998c fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents: 1419
diff changeset
162 > For example, it is not allowed, and it does not make any sense either, to use a 16-bit integer for the size, but a 32-bit integer for the capacity.
1245
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 ## Copy
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
165
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
166 ```C
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
167 #include <cx/array_list.h>
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
168
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
169 int cx_array_copy(
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
170 void **target, void *size, void *capacity, unsigned width,
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
171 size_t index, const void *src,
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
172 size_t elem_size, size_t elem_count,
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
173 CxArrayReallocator *reallocator);
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
174
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
175 #define cx_array_simple_copy(ARRAY, index, src, count)
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
176
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
177 #define cx_array_simple_copy_a(reallocator, ARRAY, index, src, count)
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_copy()` first [reserves](#reserve) enough memory to copy `elem_count` number of elements from the `src` array
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
181 to the target array at the specified `index`, and then copies the data with one call to `memmove()`.
1141
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
182
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
183 A few things to note:
1248
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
184 * `*target` and `src` can point to the same memory region, since the underlying copy operation is a `memmove()`
1141
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
185 * `*target` does not need to point to the start of the array, but `size` and `capacity` always start counting from the
1248
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
186 position `*target` points to - in this scenario, the need for reallocation must be avoided for obvious reasons
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
187 * `index` does not need to be within size and not even within the capacity of the current array
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
188 * if `index` equals `*size`, this function effectively appends the data to the target array
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
189 * the data in the target array is overwritten - if you want to insert data, you first need to copy the existing data to the end of the array and then copy the new data in a separate call
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
190
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
191 > If you are using `cx_array_reserve()` already in your program, there is no need to call `cx_array_copy()` or any of the convenience macros anymore.
1424
563033aa998c fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents: 1419
diff changeset
192 > In other words: if you already guarantee, by any means that no reallocation is necessary when writing to your array,
1248
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
193 > it is strongly recommended to use standard library functions or direct index-based access instead of the UCX functions,
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
194 > which only purpose is to make it easier for you to guarantee that your array's capacity is large enough to hold new elements.
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
195
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
196 ## Add Elements
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
197
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
198 ```C
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
199 #include <cx/array_list.h>
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
200
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
201 #define cx_array_add(target, size, capacity, elem_size, elem,
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
202 reallocator);
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
203
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
204 #define cx_array_simple_add(ARRAY, elem)
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
205
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
206 #define cx_array_simple_add_a(reallocator, ARRAY, elem)
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
207 ```
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
208
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
209 The macros for adding an element are all convenience macros that invoke `cx_array_copy()`
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
210 interpreting the `elem` as an array of size one, copied to the past-by-one index of the target array.
1141
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
211
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
212 ## Insertion Sort
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
213
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
214 ```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
215 #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
216
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
217 int cx_array_insert_sorted(
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
218 void **target, size_t *size, size_t *capacity,
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
219 cx_compare_func cmp_func,
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
220 const void *src, size_t elem_size, size_t elem_count,
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
221 CxArrayReallocator *reallocator);
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
222
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
223 #define cx_array_simple_insert_sorted(ARRAY,
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
224 src, elem_count, cmp_func)
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
225
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
226 #define cx_array_simple_insert_sorted_a(reallocator, ARRAY,
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
227 src, elem_count, cmp_func)
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
228
1248
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
229 #define cx_array_add_sorted(target, size, capacity,
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
230 elem_size, elem, cx_compare_func cmp_func, reallocator);
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
231
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
232 #define cx_array_simple_add_sorted(ARRAY,
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
233 elem, cmp_func)
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
234
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
235 #define cx_array_simple_add_sorted_a(reallocator, ARRAY,
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
236 elem, cmp_func)
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
237 ```
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
238
1248
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
239 The function `cx_array_insert_sorted()` inserts the `elem_count` number of already sorted elements from the `src` array
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
240 into the target array, comparing the elements with the given `cmp_func`.
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
241
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
242 The arguments of this function are used similar to [`cx_array_copy()`](#copy) with two notable exceptions:
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
243 first, this function actually inserts the items, moving existing items when necessary, and second,
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
244 no particular index is required because this function determines the insertion points automatically using [binary search](#binary-search).
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
245
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
246 If either the target or the source array is not already sorted with respect to the given compare function, the behavior is undefined.
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
247
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
248 The convenience macros are all calling `cx_array_insert_sorted()` by deducing the missing arguments.
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
249 The `cx_array_add_sorted()` family of macros are interpreting the `elem` as a `src` array with an `elem_count` of one.
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
250
1419
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
251 ## Sets of Unique Elements
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
252
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
253 ```C
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
254 #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
255
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
256 int cx_array_insert_unique(
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
257 void **target, size_t *size, size_t *capacity,
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
258 cx_compare_func cmp_func,
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
259 const void *src, size_t elem_size, size_t elem_count,
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
260 CxArrayReallocator *reallocator);
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
261
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
262 #define cx_array_simple_insert_unique(ARRAY,
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
263 src, elem_count, cmp_func)
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
264
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
265 #define cx_array_simple_insert_unique_a(reallocator, ARRAY,
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
266 src, elem_count, cmp_func)
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
267
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
268 #define cx_array_add_unique(target, size, capacity,
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
269 elem_size, elem, cx_compare_func cmp_func, reallocator);
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
270
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
271 #define cx_array_simple_add_unique(ARRAY,
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
272 elem, cmp_func)
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
273
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
274 #define cx_array_simple_add_unique_a(reallocator, ARRAY,
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
275 elem, cmp_func)
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
276 ```
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
277
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
278 The above functions are similar to `cx_array_insert_sorted()` and its [relatives](#insertion-sort),
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
279 except that they skip insertion of elements that are already present in the target array.
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
280
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
281 ## Binary Search
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
282
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
283 ```C
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
284 #include <cx/array_list.h>
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
285
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
286 size_t cx_array_binary_search(
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
287 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
288 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
289
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
290 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
291 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
292 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
293
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
294 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
295 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
296 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
297 ```
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
298
1248
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
299 The function `cx_array_binary_search()` searches the array `arr` for the data pointed to by `elem` using the compare function `cmp_func`.
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
300
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
301 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
302 Otherwise, the function returns `size`.
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
303
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
304 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
305 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
306 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
307 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
308 than the searched element.
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
309
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
310 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
311 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
312 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
313
1425
83284b289430 remove unnecessary members from the array reallocator struct - fixes #621
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
314 > 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
315 >
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
316 > 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
317 >{style="note"}
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
318
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
319 > 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
320 > But observations show that it usually is.
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
321 > 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
322
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
323 ## Iterators
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
324
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
325 ```C
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
326 #include <cx/iterator.h>
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
327
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
328 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
329 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
330 bool remove_keeps_order);
6e0c3a8a914a remove the concept of "mutating iterators" - resolves #579
Mike Becker <universe@uap-core.de>
parents: 1425
diff changeset
331
6e0c3a8a914a remove the concept of "mutating iterators" - resolves #579
Mike Becker <universe@uap-core.de>
parents: 1425
diff changeset
332 CxIterator cxIteratorPtr(const void *array,
6e0c3a8a914a remove the concept of "mutating iterators" - resolves #579
Mike Becker <universe@uap-core.de>
parents: 1425
diff changeset
333 size_t elem_count,
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
334 bool remove_keeps_order);
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
335 ```
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
336
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
337 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
338
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
339 ## Other
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
340
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
341 ```C
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
342 #include <cx/array_list.h>
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
343
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
344 void cx_array_swap(
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
345 void *arr,
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
346 size_t elem_size,
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
347 size_t idx1,
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
348 size_t idx2
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
349 );
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
350 ```
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
351
1248
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
352 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
353
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
354 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
355
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
356 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
357
1190
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
358 <seealso>
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
359 <category ref="apidoc">
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
360 <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
361 </category>
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
362 </seealso>

mercurial