2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
29 #include "cx/array_list.h"
33 // LOW LEVEL ARRAY LIST FUNCTIONS
35 enum cx_array_copy_result cx_array_copy(
43 struct cx_array_reallocator_s *reallocator
46 assert(target != NULL);
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
60 // a reallocator and a capacity variable must be available
61 if (reallocator == NULL || capacity == NULL) {
62 return CX_ARRAY_COPY_REALLOC_NOT_SUPPORTED;
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
80 return CX_ARRAY_COPY_REALLOC_FAILED;
83 // repair src pointer, if necessary
85 src = ((char *) newmem) + (srcaddr - targetaddr);
88 // store new pointer and capacity
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);
101 // return successfully
102 return CX_ARRAY_COPY_SUCCESS;
105 #ifndef CX_ARRAY_SWAP_SBO_SIZE
106 #define CX_ARRAY_SWAP_SBO_SIZE 512
118 if (idx1 == idx2) return;
120 char sbo_mem[CX_ARRAY_SWAP_SBO_SIZE];
123 // decide if we can use the local buffer
124 if (elem_size > CX_ARRAY_SWAP_SBO_SIZE) {
125 tmp = malloc(elem_size);
126 // we don't want to enforce error handling
127 if (tmp == NULL) abort();
132 // calculate memory locations
133 char *left = arr, *right = arr;
134 left += idx1 * elem_size;
135 right += idx2 * elem_size;
138 memcpy(tmp, left, elem_size);
139 memcpy(left, right, elem_size);
140 memcpy(right, tmp, elem_size);
142 // free dynamic memory, if it was needed
143 if (tmp != sbo_mem) {
148 // HIGH LEVEL ARRAY LIST FUNCTIONS
151 struct cx_list_s base;
153 struct cx_array_reallocator_s reallocator;
156 static void *cx_arl_realloc(
160 struct cx_array_reallocator_s *alloc
162 // retrieve the pointer to the list allocator
163 CxAllocator const *al = alloc->ptr1;
165 // use the list allocator to reallocate the memory
166 return cxRealloc(al, array, capacity * elem_size);
169 static void cx_arl_destructor(struct cx_list_s *list) {
170 cx_array_list *arl = (cx_array_list *) list;
171 cxFree(list->allocator, arl->data);
174 static size_t cx_arl_insert_array(
175 struct cx_list_s *list,
180 // out of bounds and special case check
181 if (index > list->size || n == 0) return 0;
183 // get a correctly typed pointer to the list
184 cx_array_list *arl = (cx_array_list *) list;
186 // do we need to move some elements?
187 if (index < list->size) {
188 char const *first_to_move = (char const *) arl->data;
189 first_to_move += index * list->itemsize;
190 size_t elems_to_move = list->size - index;
191 size_t start_of_moved = index + n;
193 if (CX_ARRAY_COPY_SUCCESS != cx_array_copy(
203 // if moving existing elems is unsuccessful, abort
208 // note that if we had to move the elements, the following operation
209 // is guaranteed to succeed, because we have the memory already allocated
210 // therefore, it is impossible to leave this function with an invalid array
212 // place the new elements
213 if (CX_ARRAY_COPY_SUCCESS == cx_array_copy(
225 // array list implementation is "all or nothing"
230 static int cx_arl_insert_element(
231 struct cx_list_s *list,
235 return 1 != cx_arl_insert_array(list, index, element, 1);
238 static int cx_arl_insert_iter(
239 struct cx_mut_iterator_s *iter,
243 struct cx_list_s *list = iter->src_handle;
244 if (iter->index < list->size) {
245 int result = cx_arl_insert_element(
247 iter->index + 1 - prepend,
250 if (result == 0 && prepend != 0) {
252 iter->elem_handle = ((char *) iter->elem_handle) + list->itemsize;
256 int result = cx_arl_insert_element(list, list->size, elem);
257 iter->index = list->size;
262 static int cx_arl_remove(
263 struct cx_list_s *list,
266 cx_array_list *arl = (cx_array_list *) list;
268 // out-of-bounds check
269 if (index >= list->size) {
273 // content destruction
274 if (list->content_destructor_type != CX_DESTRUCTOR_NONE) {
275 char *ptr = arl->data;
276 ptr += index * list->itemsize;
277 cx_list_invoke_destructor(list, ptr);
280 // short-circuit removal of last element
281 if (index == list->size - 1) {
286 // just move the elements starting at index to the left
287 int result = cx_array_copy(
292 ((char *) arl->data) + (index + 1) * list->itemsize,
294 list->size - index - 1,
304 static void cx_arl_clear(struct cx_list_s *list) {
305 if (list->size == 0) return;
307 cx_array_list *arl = (cx_array_list *) list;
308 char *ptr = arl->data;
310 switch (list->content_destructor_type) {
311 case CX_DESTRUCTOR_SIMPLE: {
312 for (size_t i = 0; i < list->size; i++) {
313 cx_list_invoke_simple_destructor(list, ptr);
314 ptr += list->itemsize;
318 case CX_DESTRUCTOR_ADVANCED: {
319 for (size_t i = 0; i < list->size; i++) {
320 cx_list_invoke_advanced_destructor(list, ptr);
321 ptr += list->itemsize;
325 case CX_DESTRUCTOR_NONE:
329 memset(arl->data, 0, list->size * list->itemsize);
333 static int cx_arl_swap(
334 struct cx_list_s *list,
338 if (i >= list->size || j >= list->size) return 1;
339 cx_array_list *arl = (cx_array_list *) list;
340 cx_array_swap(arl->data, list->itemsize, i, j);
344 static void *cx_arl_at(
345 struct cx_list_s const *list,
348 if (index < list->size) {
349 cx_array_list const *arl = (cx_array_list const *) list;
350 char *space = arl->data;
351 return space + index * list->itemsize;
357 static size_t cx_arl_find(
358 struct cx_list_s const *list,
361 assert(list->cmpfunc != NULL);
362 char *cur = ((cx_array_list const *) list)->data;
364 for (size_t i = 0; i < list->size; i++) {
365 if (0 == list->cmpfunc(elem, cur)) {
368 cur += list->itemsize;
374 static void cx_arl_sort(struct cx_list_s *list) {
375 assert(list->cmpfunc != NULL);
376 qsort(((cx_array_list *) list)->data,
383 static int cx_arl_compare(
384 struct cx_list_s const *list,
385 struct cx_list_s const *other
387 assert(list->cmpfunc != NULL);
388 if (list->size == other->size) {
389 char const *left = ((cx_array_list const *) list)->data;
390 char const *right = ((cx_array_list const *) other)->data;
391 for (size_t i = 0; i < list->size; i++) {
392 int d = list->cmpfunc(left, right);
396 left += list->itemsize;
397 right += other->itemsize;
401 return list->size < other->size ? -1 : 1;
405 static void cx_arl_reverse(struct cx_list_s *list) {
406 if (list->size < 2) return;
407 void *data = ((cx_array_list const *) list)->data;
408 size_t half = list->size / 2;
409 for (size_t i = 0; i < half; i++) {
410 cx_array_swap(data, list->itemsize, i, list->size - 1 - i);
414 static bool cx_arl_iter_valid(void const *it) {
415 struct cx_iterator_s const *iter = it;
416 struct cx_list_s const *list = iter->src_handle;
417 return iter->index < list->size;
420 static void *cx_arl_iter_current(void const *it) {
421 struct cx_iterator_s const *iter = it;
422 return iter->elem_handle;
425 static void cx_arl_iter_next(void *it) {
426 struct cx_iterator_base_s *itbase = it;
427 if (itbase->remove) {
428 struct cx_mut_iterator_s *iter = it;
429 itbase->remove = false;
430 cx_arl_remove(iter->src_handle, iter->index);
432 struct cx_iterator_s *iter = it;
435 ((char *) iter->elem_handle)
436 + ((struct cx_list_s const *) iter->src_handle)->itemsize;
440 static void cx_arl_iter_prev(void *it) {
441 struct cx_iterator_base_s *itbase = it;
442 struct cx_mut_iterator_s *iter = it;
443 cx_array_list *const list = iter->src_handle;
444 if (itbase->remove) {
445 itbase->remove = false;
446 cx_arl_remove(iter->src_handle, iter->index);
449 if (iter->index < list->base.size) {
450 iter->elem_handle = ((char *) list->data)
451 + iter->index * list->base.itemsize;
455 static bool cx_arl_iter_flag_rm(void *it) {
456 struct cx_iterator_base_s *iter = it;
457 if (iter->mutating) {
465 static struct cx_iterator_s cx_arl_iterator(
466 struct cx_list_s const *list,
470 struct cx_iterator_s iter;
473 iter.src_handle = list;
474 iter.elem_handle = cx_arl_at(list, index);
475 iter.base.valid = cx_arl_iter_valid;
476 iter.base.current = cx_arl_iter_current;
477 iter.base.next = backwards ? cx_arl_iter_prev : cx_arl_iter_next;
478 iter.base.flag_removal = cx_arl_iter_flag_rm;
479 iter.base.remove = false;
480 iter.base.mutating = false;
485 static cx_list_class cx_array_list_class = {
487 cx_arl_insert_element,
501 CxList *cxArrayListCreate(
502 CxAllocator const *allocator,
503 CxListComparator comparator,
505 size_t initial_capacity
507 if (allocator == NULL) {
508 allocator = cxDefaultAllocator;
511 cx_array_list *list = cxCalloc(allocator, 1, sizeof(cx_array_list));
512 if (list == NULL) return NULL;
514 list->data = cxCalloc(allocator, initial_capacity, item_size);
515 if (list->data == NULL) {
516 cxFree(allocator, list);
520 list->base.cl = &cx_array_list_class;
521 list->base.allocator = allocator;
522 list->base.cmpfunc = comparator;
523 list->base.capacity = initial_capacity;
526 list->base.itemsize = item_size;
528 cxListStorePointers((CxList *) list);
531 // configure the reallocator
532 list->reallocator.realloc = cx_arl_realloc;
533 list->reallocator.ptr1 = (void *) allocator;
535 return (CxList *) list;