Wed, 08 Feb 2023 20:26:26 +0100
Automated merge
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
29 #include "cx/array_list.h"
30 #include <assert.h>
31 #include <string.h>
33 // LOW LEVEL ARRAY LIST FUNCTIONS
35 enum cx_array_copy_result cx_array_copy(
36 void **target,
37 size_t *size,
38 size_t *capacity,
39 size_t index,
40 void const *src,
41 size_t elem_size,
42 size_t elem_count,
43 struct cx_array_reallocator_s *reallocator
44 ) {
45 // assert pointers
46 assert(target != NULL);
47 assert(size != NULL);
48 assert(src != NULL);
50 // determine capacity
51 size_t cap = capacity == NULL ? *size : *capacity;
53 // check if resize is required
54 size_t minsize = index + elem_count;
55 size_t newsize = *size < minsize ? minsize : *size;
56 bool needrealloc = newsize > cap;
58 // reallocate if possible
59 if (needrealloc) {
60 // a reallocator and a capacity variable must be available
61 if (reallocator == NULL || capacity == NULL) {
62 return CX_ARRAY_COPY_REALLOC_NOT_SUPPORTED;
63 }
65 // check, if we need to repair the src pointer
66 uintptr_t targetaddr = (uintptr_t) *target;
67 uintptr_t srcaddr = (uintptr_t) src;
68 bool repairsrc = targetaddr <= srcaddr
69 && srcaddr < targetaddr + cap * elem_size;
71 // calculate new capacity (next number divisible by 16)
72 cap = newsize - (newsize % 16) + 16;
73 assert(cap > newsize);
75 // perform reallocation
76 void *newmem = reallocator->realloc(
77 *target, cap, elem_size, reallocator
78 );
79 if (newmem == NULL) {
80 return CX_ARRAY_COPY_REALLOC_FAILED;
81 }
83 // repair src pointer, if necessary
84 if (repairsrc) {
85 src = ((char *) newmem) + (srcaddr - targetaddr);
86 }
88 // store new pointer and capacity
89 *target = newmem;
90 *capacity = cap;
91 }
93 // determine target pointer
94 char *start = *target;
95 start += index * elem_size;
97 // copy elements and set new size
98 memmove(start, src, elem_count * elem_size);
99 *size = newsize;
101 // return successfully
102 return CX_ARRAY_COPY_SUCCESS;
103 }
105 #ifndef CX_ARRAY_SWAP_SBO_SIZE
106 #define CX_ARRAY_SWAP_SBO_SIZE 512
107 #endif
109 void cx_array_swap(
110 void *arr,
111 size_t elem_size,
112 size_t idx1,
113 size_t idx2
114 ) {
115 // short circuit
116 if (idx1 == idx2) return;
118 char sbo_mem[CX_ARRAY_SWAP_SBO_SIZE];
119 void *tmp;
121 // decide if we can use the local buffer
122 if (elem_size > CX_ARRAY_SWAP_SBO_SIZE) {
123 tmp = malloc(elem_size);
124 // we don't want to enforce error handling
125 if (tmp == NULL) abort();
126 } else {
127 tmp = sbo_mem;
128 }
130 // calculate memory locations
131 char *left = arr, *right = arr;
132 left += idx1 * elem_size;
133 right += idx2 * elem_size;
135 // three-way swap
136 memcpy(tmp, left, elem_size);
137 memcpy(left, right, elem_size);
138 memcpy(right, tmp, elem_size);
140 // free dynamic memory, if it was needed
141 if (tmp != sbo_mem) {
142 free(tmp);
143 }
144 }
146 // HIGH LEVEL ARRAY LIST FUNCTIONS
148 typedef struct {
149 struct cx_list_s base;
150 void *data;
151 struct cx_array_reallocator_s reallocator;
152 } cx_array_list;
154 static void *cx_arl_realloc(
155 void *array,
156 size_t capacity,
157 size_t elem_size,
158 struct cx_array_reallocator_s *alloc
159 ) {
160 // retrieve the pointer to the list allocator
161 CxAllocator const *al = alloc->ptr1;
163 // use the list allocator to reallocate the memory
164 return cxRealloc(al, array, capacity * elem_size);
165 }
167 static void cx_arl_destructor(struct cx_list_s *list) {
168 cx_array_list *arl = (cx_array_list *) list;
169 cxFree(list->allocator, arl->data);
170 }
172 static size_t cx_arl_insert_array(
173 struct cx_list_s *list,
174 size_t index,
175 void const *array,
176 size_t n
177 ) {
178 // out of bounds and special case check
179 if (index > list->size || n == 0) return 0;
181 // get a correctly typed pointer to the list
182 cx_array_list *arl = (cx_array_list *) list;
184 // do we need to move some elements?
185 if (index < list->size) {
186 char const *first_to_move = (char const *) arl->data;
187 first_to_move += index * list->itemsize;
188 size_t elems_to_move = list->size - index;
189 size_t start_of_moved = index + n;
191 if (CX_ARRAY_COPY_SUCCESS != cx_array_copy(
192 &arl->data,
193 &list->size,
194 &list->capacity,
195 start_of_moved,
196 first_to_move,
197 list->itemsize,
198 elems_to_move,
199 &arl->reallocator
200 )) {
201 // if moving existing elems is unsuccessful, abort
202 return 0;
203 }
204 }
206 // note that if we had to move the elements, the following operation
207 // is guaranteed to succeed, because we have the memory already allocated
208 // therefore, it is impossible to leave this function with an invalid array
210 // place the new elements
211 if (CX_ARRAY_COPY_SUCCESS == cx_array_copy(
212 &arl->data,
213 &list->size,
214 &list->capacity,
215 index,
216 array,
217 list->itemsize,
218 n,
219 &arl->reallocator
220 )) {
221 return n;
222 } else {
223 // array list implementation is "all or nothing"
224 return 0;
225 }
226 }
228 static int cx_arl_insert_element(
229 struct cx_list_s *list,
230 size_t index,
231 void const *element
232 ) {
233 return 1 != cx_arl_insert_array(list, index, element, 1);
234 }
236 static int cx_arl_insert_iter(
237 struct cx_mut_iterator_s *iter,
238 void const *elem,
239 int prepend
240 ) {
241 struct cx_list_s *list = iter->src_handle;
242 if (iter->index < list->size) {
243 int result = cx_arl_insert_element(
244 list,
245 iter->index + 1 - prepend,
246 elem
247 );
248 if (result == 0 && prepend != 0) {
249 iter->index++;
250 iter->elem_handle = ((char *) iter->elem_handle) + list->itemsize;
251 }
252 return result;
253 } else {
254 int result = cx_arl_insert_element(list, list->size, elem);
255 iter->index = list->size;
256 return result;
257 }
258 }
260 static int cx_arl_remove(
261 struct cx_list_s *list,
262 size_t index
263 ) {
264 // out-of-bounds check
265 if (index >= list->size) {
266 return 1;
267 }
269 // short-circuit removal of last element
270 if (index == list->size - 1) {
271 list->size--;
272 return 0;
273 }
275 // just move the elements starting at index to the left
276 cx_array_list *arl = (cx_array_list *) list;
277 int result = cx_array_copy(
278 &arl->data,
279 &list->size,
280 &list->capacity,
281 index,
282 ((char *) arl->data) + (index + 1) * list->itemsize,
283 list->itemsize,
284 list->size - index - 1,
285 &arl->reallocator
286 );
287 if (result == 0) {
288 // decrease the size
289 list->size--;
290 }
291 return result;
292 }
294 static int cx_arl_swap(
295 struct cx_list_s *list,
296 size_t i,
297 size_t j
298 ) {
299 if (i >= list->size || j >= list->size) return 1;
300 cx_array_list *arl = (cx_array_list *) list;
301 cx_array_swap(arl->data, list->itemsize, i, j);
302 return 0;
303 }
305 static void *cx_arl_at(
306 struct cx_list_s const *list,
307 size_t index
308 ) {
309 if (index < list->size) {
310 cx_array_list const *arl = (cx_array_list const *) list;
311 char *space = arl->data;
312 return space + index * list->itemsize;
313 } else {
314 return NULL;
315 }
316 }
318 static size_t cx_arl_find(
319 struct cx_list_s const *list,
320 void const *elem
321 ) {
322 char *cur = ((cx_array_list const *) list)->data;
324 for (size_t i = 0; i < list->size; i++) {
325 if (0 == list->cmpfunc(elem, cur)) {
326 return i;
327 }
328 cur += list->itemsize;
329 }
331 return list->size;
332 }
334 static void cx_arl_sort(struct cx_list_s *list) {
335 qsort(((cx_array_list *) list)->data,
336 list->size,
337 list->itemsize,
338 list->cmpfunc
339 );
340 }
342 static int cx_arl_compare(
343 struct cx_list_s const *list,
344 struct cx_list_s const *other
345 ) {
346 if (list->size == other->size) {
347 char const *left = ((cx_array_list const *) list)->data;
348 char const *right = ((cx_array_list const *) other)->data;
349 for (size_t i = 0; i < list->size; i++) {
350 int d = list->cmpfunc(left, right);
351 if (d != 0) {
352 return d;
353 }
354 left += list->itemsize;
355 right += other->itemsize;
356 }
357 return 0;
358 } else {
359 return list->size < other->size ? -1 : 1;
360 }
361 }
363 static void cx_arl_reverse(struct cx_list_s *list) {
364 if (list->size < 2) return;
365 void *data = ((cx_array_list const *) list)->data;
366 size_t half = list->size / 2;
367 for (size_t i = 0; i < half; i++) {
368 cx_array_swap(data, list->itemsize, i, list->size - 1 - i);
369 }
370 }
372 static bool cx_arl_iter_valid(void const *it) {
373 struct cx_iterator_s const *iter = it;
374 struct cx_list_s const *list = iter->src_handle;
375 return iter->index < list->size;
376 }
378 static void *cx_arl_iter_current(void const *it) {
379 struct cx_iterator_s const *iter = it;
380 return iter->elem_handle;
381 }
383 static void cx_arl_iter_next(void *it) {
384 struct cx_iterator_base_s *itbase = it;
385 if (itbase->remove) {
386 struct cx_mut_iterator_s *iter = it;
387 itbase->remove = false;
388 cx_arl_remove(iter->src_handle, iter->index);
389 } else {
390 struct cx_iterator_s *iter = it;
391 iter->index++;
392 iter->elem_handle =
393 ((char *) iter->elem_handle)
394 + ((struct cx_list_s const *) iter->src_handle)->itemsize;
395 }
396 }
398 static bool cx_arl_iter_flag_rm(void *it) {
399 struct cx_iterator_base_s *iter = it;
400 if (iter->mutating) {
401 iter->remove = true;
402 return true;
403 } else {
404 return false;
405 }
406 }
408 static struct cx_iterator_s cx_arl_iterator(
409 struct cx_list_s const *list,
410 size_t index
411 ) {
412 struct cx_iterator_s iter;
414 iter.index = index;
415 iter.src_handle = list;
416 iter.elem_handle = cx_arl_at(list, index);
417 iter.base.valid = cx_arl_iter_valid;
418 iter.base.current = cx_arl_iter_current;
419 iter.base.next = cx_arl_iter_next;
420 iter.base.flag_removal = cx_arl_iter_flag_rm;
421 iter.base.remove = false;
422 iter.base.mutating = false;
424 return iter;
425 }
427 static cx_list_class cx_array_list_class = {
428 cx_arl_destructor,
429 cx_arl_insert_element,
430 cx_arl_insert_array,
431 cx_arl_insert_iter,
432 cx_arl_remove,
433 cx_arl_swap,
434 cx_arl_at,
435 cx_arl_find,
436 cx_arl_sort,
437 cx_arl_compare,
438 cx_arl_reverse,
439 cx_arl_iterator,
440 };
442 CxList *cxArrayListCreate(
443 CxAllocator const *allocator,
444 CxListComparator comparator,
445 size_t item_size,
446 size_t initial_capacity
447 ) {
448 cx_array_list *list = cxCalloc(allocator, 1, sizeof(cx_array_list));
449 if (list == NULL) return NULL;
451 list->data = cxCalloc(allocator, initial_capacity, item_size);
452 if (list->data == NULL) {
453 cxFree(allocator, list);
454 return NULL;
455 }
457 list->base.cl = &cx_array_list_class;
458 list->base.allocator = allocator;
459 list->base.cmpfunc = comparator;
460 list->base.itemsize = item_size;
461 list->base.capacity = initial_capacity;
463 // configure the reallocator
464 list->reallocator.realloc = cx_arl_realloc;
465 list->reallocator.ptr1 = (void *) allocator;
467 return (CxList *) list;
468 }