| 79 ## Add or Insert Elements |
79 ## Add or Insert Elements |
| 80 |
80 |
| 81 ```C |
81 ```C |
| 82 #include <cx/array_list.h> |
82 #include <cx/array_list.h> |
| 83 |
83 |
| 84 int cx_array_add(CxArray array, const void *element); |
84 int cx_array_add(CxArray array, Any element); |
| 85 |
85 |
| 86 int cx_array_add_array(CxArray array, const void *other, size_t n); |
86 int cx_array_add_array(CxArray array, const void *other, size_t n); |
| 87 |
87 |
| 88 int cx_array_insert(CxArray array, size_t index, const void *element); |
88 int cx_array_insert(CxArray array, size_t index, Any element); |
| 89 |
89 |
| 90 int cx_array_insert_array(CxArray array, |
90 int cx_array_insert_array(CxArray array, |
| 91 size_t index, const void *other, size_t n); |
91 size_t index, const void *other, size_t n); |
| 92 |
92 |
| 93 int cx_array_add_a(const CxAllocator* allocator, |
93 int cx_array_add_a(const CxAllocator* allocator, |
| 94 CxArray array, const void *element); |
94 CxArray array, Any element); |
| 95 |
95 |
| 96 int cx_array_add_array(const CxAllocator* allocator, |
96 int cx_array_add_array(const CxAllocator* allocator, |
| 97 CxArray array, const void *other, size_t n); |
97 CxArray array, const void *other, size_t n); |
| 98 |
98 |
| 99 int cx_array_insert_a(const CxAllocator* allocator, |
99 int cx_array_insert_a(const CxAllocator* allocator, |
| 100 CxArray array, size_t index, const void *element); |
100 CxArray array, size_t index, Any element); |
| 101 |
101 |
| 102 int cx_array_insert_array_a(const CxAllocator* allocator, |
102 int cx_array_insert_array_a(const CxAllocator* allocator, |
| 103 CxArray array, size_t index, const void *other, size_t n); |
103 CxArray array, size_t index, const void *other, size_t n); |
| 104 ``` |
104 ``` |
| 105 |
105 |
| 106 The above functions insert one or more elements into the array at the specified `index` |
106 The above functions insert one or more elements into the array at the specified `index` |
| 107 or add them to the end of the array. |
107 or add them to the end of the array. |
| 108 |
108 |
| 109 When the array capacity is not sufficient, a re-allocation is attempted. |
109 When the array capacity is not sufficient, a re-allocation is attempted. |
| 110 If the allocation fails, the function returns non-zero. |
110 If the allocation fails, the function returns non-zero. |
| |
111 |
| |
112 > Since the "functions" are actually macros, the variants which add or insert one single element |
| |
113 > also conveniently take the address of the passed argument, so you don't have to do it. |
| |
114 > This is why the type is specified as `Any` instead of `const void*` here. |
| 111 |
115 |
| 112 > Be careful when using these functions on an array that was initialized with fixed-sized memory. |
116 > Be careful when using these functions on an array that was initialized with fixed-sized memory. |
| 113 > In this case, you MUST make sure that the capacity is sufficient or reallocate the array |
117 > In this case, you MUST make sure that the capacity is sufficient or reallocate the array |
| 114 > with `cx_array_copy_to_new()` or `cx_array_copy_to_new_a()` before adding or inserting more elements. |
118 > with `cx_array_copy_to_new()` or `cx_array_copy_to_new_a()` before adding or inserting more elements. |
| 115 >{style="note"} |
119 >{style="note"} |
| 141 ## Insertion Sort |
145 ## Insertion Sort |
| 142 |
146 |
| 143 ```C |
147 ```C |
| 144 #include <cx/array_list.h> |
148 #include <cx/array_list.h> |
| 145 |
149 |
| 146 int cx_array_insert_sorted(CxArray array, |
150 int cx_array_insert_sorted(CxArray array, Any element, |
| 147 const void *element, |
|
| 148 cx_compare_func cmp_func); |
151 cx_compare_func cmp_func); |
| 149 |
152 |
| 150 int cx_array_insert_sorted_array(CxArray array, |
153 int cx_array_insert_sorted_array(CxArray array, |
| 151 const void *sorted_data, size_t n, |
154 const void *sorted_data, size_t n, |
| 152 cx_compare_func cmp_func); |
155 cx_compare_func cmp_func); |
| 153 |
156 |
| 154 int cx_array_insert_sorted_a(const CxAllocator *allocator, |
157 int cx_array_insert_sorted_a(const CxAllocator *allocator, |
| 155 CxArray array, const void *element, |
158 CxArray array, Any element, |
| 156 cx_compare_func cmp_func); |
159 cx_compare_func cmp_func); |
| 157 |
160 |
| 158 int cx_array_insert_sorted_array_a(const CxAllocator *allocator, |
161 int cx_array_insert_sorted_array_a(const CxAllocator *allocator, |
| 159 CxArray array, const void *sorted_data, size_t n, |
162 CxArray array, const void *sorted_data, size_t n, |
| 160 cx_compare_func cmp_func); |
163 cx_compare_func cmp_func); |
| 161 |
164 |
| 162 int cx_array_insert_sorted_c(CxArray array, |
165 int cx_array_insert_sorted_c(CxArray array, Any element, |
| 163 const void *element, |
|
| 164 cx_compare_func2 cmp_func, void *context); |
166 cx_compare_func2 cmp_func, void *context); |
| 165 |
167 |
| 166 int cx_array_insert_sorted_array_c(CxArray array, |
168 int cx_array_insert_sorted_array_c(CxArray array, |
| 167 const void *sorted_data, size_t n, |
169 const void *sorted_data, size_t n, |
| 168 cx_compare_func2 cmp_func, void *context); |
170 cx_compare_func2 cmp_func, void *context); |
| 169 |
171 |
| 170 int cx_array_insert_sorted_ca(const CxAllocator *allocator, |
172 int cx_array_insert_sorted_ca(const CxAllocator *allocator, |
| 171 CxArray array, const void *element, |
173 CxArray array, Any element, |
| 172 cx_compare_func2 cmp_func, void *context); |
174 cx_compare_func2 cmp_func, void *context); |
| 173 |
175 |
| 174 int cx_array_insert_sorted_array_ca(const CxAllocator *allocator, |
176 int cx_array_insert_sorted_array_ca(const CxAllocator *allocator, |
| 175 CxArray array, const void *sorted_data, size_t n, |
177 CxArray array, const void *sorted_data, size_t n, |
| 176 cx_compare_func2 cmp_func, void *context); |
178 cx_compare_func2 cmp_func, void *context); |
| 183 ## Insert Unique Elements |
185 ## Insert Unique Elements |
| 184 |
186 |
| 185 ```C |
187 ```C |
| 186 #include <cx/array_list.h> |
188 #include <cx/array_list.h> |
| 187 |
189 |
| 188 int cx_array_insert_unique(CxArray array, |
190 int cx_array_insert_unique(CxArray array, Any element, |
| 189 const void *element, |
|
| 190 cx_compare_func cmp_func); |
191 cx_compare_func cmp_func); |
| 191 |
192 |
| 192 int cx_array_insert_unique_array(CxArray array, |
193 int cx_array_insert_unique_array(CxArray array, |
| 193 const void *sorted_data, size_t n, |
194 const void *sorted_data, size_t n, |
| 194 cx_compare_func cmp_func); |
195 cx_compare_func cmp_func); |
| 195 |
196 |
| 196 int cx_array_insert_unique_a(const CxAllocator *allocator, |
197 int cx_array_insert_unique_a(const CxAllocator *allocator, |
| 197 CxArray array, const void *element, |
198 CxArray array, Any element, |
| 198 cx_compare_func cmp_func); |
199 cx_compare_func cmp_func); |
| 199 |
200 |
| 200 int cx_array_insert_unique_array_a(const CxAllocator *allocator, |
201 int cx_array_insert_unique_array_a(const CxAllocator *allocator, |
| 201 CxArray array, const void *sorted_data, size_t n, |
202 CxArray array, const void *sorted_data, size_t n, |
| 202 cx_compare_func cmp_func); |
203 cx_compare_func cmp_func); |
| 203 |
204 |
| 204 int cx_array_insert_unique_c(CxArray array, |
205 int cx_array_insert_unique_c(CxArray array, Any element, |
| 205 const void *element, |
|
| 206 cx_compare_func2 cmp_func, void *context); |
206 cx_compare_func2 cmp_func, void *context); |
| 207 |
207 |
| 208 int cx_array_insert_unique_array_c(CxArray array, |
208 int cx_array_insert_unique_array_c(CxArray array, |
| 209 const void *sorted_data, size_t n, |
209 const void *sorted_data, size_t n, |
| 210 cx_compare_func2 cmp_func, void *context); |
210 cx_compare_func2 cmp_func, void *context); |
| 211 |
211 |
| 212 int cx_array_insert_unique_ca(const CxAllocator *allocator, |
212 int cx_array_insert_unique_ca(const CxAllocator *allocator, |
| 213 CxArray array, const void *element, |
213 CxArray array, Any element, |
| 214 cx_compare_func2 cmp_func, void *context); |
214 cx_compare_func2 cmp_func, void *context); |
| 215 |
215 |
| 216 int cx_array_insert_unique_array_ca(const CxAllocator *allocator, |
216 int cx_array_insert_unique_array_ca(const CxAllocator *allocator, |
| 217 CxArray array, const void *sorted_data, size_t n, |
217 CxArray array, const void *sorted_data, size_t n, |
| 218 cx_compare_func2 cmp_func, void *context); |
218 cx_compare_func2 cmp_func, void *context); |