src/array_list.c

Sat, 13 Jan 2024 17:51:42 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 13 Jan 2024 17:51:42 +0100
changeset 804
5136f2fc32ec
parent 764
ccbdbd088455
child 807
c8d692131b1e
permissions
-rw-r--r--

add CX_DISABLE_ARRAY_LIST_SWAP_SBO flag

     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 "cx/compare.h"
    31 #include <assert.h>
    32 #include <string.h>
    34 // LOW LEVEL ARRAY LIST FUNCTIONS
    36 enum cx_array_copy_result cx_array_copy(
    37         void **target,
    38         size_t *size,
    39         size_t *capacity,
    40         size_t index,
    41         void const *src,
    42         size_t elem_size,
    43         size_t elem_count,
    44         struct cx_array_reallocator_s *reallocator
    45 ) {
    46     // assert pointers
    47     assert(target != NULL);
    48     assert(size != NULL);
    49     assert(src != NULL);
    51     // determine capacity
    52     size_t cap = capacity == NULL ? *size : *capacity;
    54     // check if resize is required
    55     size_t minsize = index + elem_count;
    56     size_t newsize = *size < minsize ? minsize : *size;
    57     bool needrealloc = newsize > cap;
    59     // reallocate if possible
    60     if (needrealloc) {
    61         // a reallocator and a capacity variable must be available
    62         if (reallocator == NULL || capacity == NULL) {
    63             return CX_ARRAY_COPY_REALLOC_NOT_SUPPORTED;
    64         }
    66         // check, if we need to repair the src pointer
    67         uintptr_t targetaddr = (uintptr_t) *target;
    68         uintptr_t srcaddr = (uintptr_t) src;
    69         bool repairsrc = targetaddr <= srcaddr
    70                          && srcaddr < targetaddr + cap * elem_size;
    72         // calculate new capacity (next number divisible by 16)
    73         cap = newsize - (newsize % 16) + 16;
    74         assert(cap > newsize);
    76         // perform reallocation
    77         void *newmem = reallocator->realloc(
    78                 *target, cap, elem_size, reallocator
    79         );
    80         if (newmem == NULL) {
    81             return CX_ARRAY_COPY_REALLOC_FAILED;
    82         }
    84         // repair src pointer, if necessary
    85         if (repairsrc) {
    86             src = ((char *) newmem) + (srcaddr - targetaddr);
    87         }
    89         // store new pointer and capacity
    90         *target = newmem;
    91         *capacity = cap;
    92     }
    94     // determine target pointer
    95     char *start = *target;
    96     start += index * elem_size;
    98     // copy elements and set new size
    99     memmove(start, src, elem_count * elem_size);
   100     *size = newsize;
   102     // return successfully
   103     return CX_ARRAY_COPY_SUCCESS;
   104 }
   106 #ifndef CX_ARRAY_SWAP_SBO_SIZE
   107 #define CX_ARRAY_SWAP_SBO_SIZE 128
   108 #endif
   110 bool CX_DISABLE_ARRAY_LIST_SWAP_SBO = false;
   112 void cx_array_swap(
   113         void *arr,
   114         size_t elem_size,
   115         size_t idx1,
   116         size_t idx2
   117 ) {
   118     assert(arr != NULL);
   120     // short circuit
   121     if (idx1 == idx2) return;
   123     char sbo_mem[CX_ARRAY_SWAP_SBO_SIZE];
   124     void *tmp;
   126     // decide if we can use the local buffer
   127     if (elem_size > CX_ARRAY_SWAP_SBO_SIZE || CX_DISABLE_ARRAY_LIST_SWAP_SBO) {
   128         tmp = malloc(elem_size);
   129         // we don't want to enforce error handling
   130         if (tmp == NULL) abort();
   131     } else {
   132         tmp = sbo_mem;
   133     }
   135     // calculate memory locations
   136     char *left = arr, *right = arr;
   137     left += idx1 * elem_size;
   138     right += idx2 * elem_size;
   140     // three-way swap
   141     memcpy(tmp, left, elem_size);
   142     memcpy(left, right, elem_size);
   143     memcpy(right, tmp, elem_size);
   145     // free dynamic memory, if it was needed
   146     if (tmp != sbo_mem) {
   147         free(tmp);
   148     }
   149 }
   151 // HIGH LEVEL ARRAY LIST FUNCTIONS
   153 typedef struct {
   154     struct cx_list_s base;
   155     void *data;
   156     size_t capacity;
   157     struct cx_array_reallocator_s reallocator;
   158 } cx_array_list;
   160 static void *cx_arl_realloc(
   161         void *array,
   162         size_t capacity,
   163         size_t elem_size,
   164         struct cx_array_reallocator_s *alloc
   165 ) {
   166     // retrieve the pointer to the list allocator
   167     CxAllocator const *al = alloc->ptr1;
   169     // use the list allocator to reallocate the memory
   170     return cxRealloc(al, array, capacity * elem_size);
   171 }
   173 static void cx_arl_destructor(struct cx_list_s *list) {
   174     cx_array_list *arl = (cx_array_list *) list;
   176     char *ptr = arl->data;
   178     if (list->simple_destructor) {
   179         for (size_t i = 0; i < list->size; i++) {
   180             cx_invoke_simple_destructor(list, ptr);
   181             ptr += list->item_size;
   182         }
   183     }
   184     if (list->advanced_destructor) {
   185         for (size_t i = 0; i < list->size; i++) {
   186             cx_invoke_advanced_destructor(list, ptr);
   187             ptr += list->item_size;
   188         }
   189     }
   191     cxFree(list->allocator, arl->data);
   192     cxFree(list->allocator, list);
   193 }
   195 static size_t cx_arl_insert_array(
   196         struct cx_list_s *list,
   197         size_t index,
   198         void const *array,
   199         size_t n
   200 ) {
   201     // out of bounds and special case check
   202     if (index > list->size || n == 0) return 0;
   204     // get a correctly typed pointer to the list
   205     cx_array_list *arl = (cx_array_list *) list;
   207     // do we need to move some elements?
   208     if (index < list->size) {
   209         char const *first_to_move = (char const *) arl->data;
   210         first_to_move += index * list->item_size;
   211         size_t elems_to_move = list->size - index;
   212         size_t start_of_moved = index + n;
   214         if (CX_ARRAY_COPY_SUCCESS != cx_array_copy(
   215                 &arl->data,
   216                 &list->size,
   217                 &arl->capacity,
   218                 start_of_moved,
   219                 first_to_move,
   220                 list->item_size,
   221                 elems_to_move,
   222                 &arl->reallocator
   223         )) {
   224             // if moving existing elems is unsuccessful, abort
   225             return 0;
   226         }
   227     }
   229     // note that if we had to move the elements, the following operation
   230     // is guaranteed to succeed, because we have the memory already allocated
   231     // therefore, it is impossible to leave this function with an invalid array
   233     // place the new elements
   234     if (CX_ARRAY_COPY_SUCCESS == cx_array_copy(
   235             &arl->data,
   236             &list->size,
   237             &arl->capacity,
   238             index,
   239             array,
   240             list->item_size,
   241             n,
   242             &arl->reallocator
   243     )) {
   244         return n;
   245     } else {
   246         // array list implementation is "all or nothing"
   247         return 0;
   248     }
   249 }
   251 static int cx_arl_insert_element(
   252         struct cx_list_s *list,
   253         size_t index,
   254         void const *element
   255 ) {
   256     return 1 != cx_arl_insert_array(list, index, element, 1);
   257 }
   259 static int cx_arl_insert_iter(
   260         struct cx_mut_iterator_s *iter,
   261         void const *elem,
   262         int prepend
   263 ) {
   264     struct cx_list_s *list = iter->src_handle;
   265     if (iter->index < list->size) {
   266         int result = cx_arl_insert_element(
   267                 list,
   268                 iter->index + 1 - prepend,
   269                 elem
   270         );
   271         if (result == 0 && prepend != 0) {
   272             iter->index++;
   273             iter->elem_handle = ((char *) iter->elem_handle) + list->item_size;
   274         }
   275         return result;
   276     } else {
   277         int result = cx_arl_insert_element(list, list->size, elem);
   278         iter->index = list->size;
   279         return result;
   280     }
   281 }
   283 static int cx_arl_remove(
   284         struct cx_list_s *list,
   285         size_t index
   286 ) {
   287     cx_array_list *arl = (cx_array_list *) list;
   289     // out-of-bounds check
   290     if (index >= list->size) {
   291         return 1;
   292     }
   294     // content destruction
   295     cx_invoke_destructor(list, ((char *) arl->data) + index * list->item_size);
   297     // short-circuit removal of last element
   298     if (index == list->size - 1) {
   299         list->size--;
   300         return 0;
   301     }
   303     // just move the elements starting at index to the left
   304     int result = cx_array_copy(
   305             &arl->data,
   306             &list->size,
   307             &arl->capacity,
   308             index,
   309             ((char *) arl->data) + (index + 1) * list->item_size,
   310             list->item_size,
   311             list->size - index - 1,
   312             &arl->reallocator
   313     );
   314     if (result == 0) {
   315         // decrease the size
   316         list->size--;
   317     }
   318     return result;
   319 }
   321 static void cx_arl_clear(struct cx_list_s *list) {
   322     if (list->size == 0) return;
   324     cx_array_list *arl = (cx_array_list *) list;
   325     char *ptr = arl->data;
   327     if (list->simple_destructor) {
   328         for (size_t i = 0; i < list->size; i++) {
   329             cx_invoke_simple_destructor(list, ptr);
   330             ptr += list->item_size;
   331         }
   332     }
   333     if (list->advanced_destructor) {
   334         for (size_t i = 0; i < list->size; i++) {
   335             cx_invoke_advanced_destructor(list, ptr);
   336             ptr += list->item_size;
   337         }
   338     }
   340     memset(arl->data, 0, list->size * list->item_size);
   341     list->size = 0;
   342 }
   344 static int cx_arl_swap(
   345         struct cx_list_s *list,
   346         size_t i,
   347         size_t j
   348 ) {
   349     if (i >= list->size || j >= list->size) return 1;
   350     cx_array_list *arl = (cx_array_list *) list;
   351     cx_array_swap(arl->data, list->item_size, i, j);
   352     return 0;
   353 }
   355 static void *cx_arl_at(
   356         struct cx_list_s const *list,
   357         size_t index
   358 ) {
   359     if (index < list->size) {
   360         cx_array_list const *arl = (cx_array_list const *) list;
   361         char *space = arl->data;
   362         return space + index * list->item_size;
   363     } else {
   364         return NULL;
   365     }
   366 }
   368 static ssize_t cx_arl_find_remove(
   369         struct cx_list_s *list,
   370         void const *elem,
   371         bool remove
   372 ) {
   373     assert(list->cmpfunc != NULL);
   374     assert(list->size < SIZE_MAX / 2);
   375     char *cur = ((cx_array_list const *) list)->data;
   377     for (ssize_t i = 0; i < (ssize_t) list->size; i++) {
   378         if (0 == list->cmpfunc(elem, cur)) {
   379             if (remove) {
   380                 if (0 == cx_arl_remove(list, i)) {
   381                     return i;
   382                 } else {
   383                     return -1;
   384                 }
   385             } else {
   386                 return i;
   387             }
   388         }
   389         cur += list->item_size;
   390     }
   392     return -1;
   393 }
   395 static void cx_arl_sort(struct cx_list_s *list) {
   396     assert(list->cmpfunc != NULL);
   397     qsort(((cx_array_list *) list)->data,
   398           list->size,
   399           list->item_size,
   400           list->cmpfunc
   401     );
   402 }
   404 static int cx_arl_compare(
   405         struct cx_list_s const *list,
   406         struct cx_list_s const *other
   407 ) {
   408     assert(list->cmpfunc != NULL);
   409     if (list->size == other->size) {
   410         char const *left = ((cx_array_list const *) list)->data;
   411         char const *right = ((cx_array_list const *) other)->data;
   412         for (size_t i = 0; i < list->size; i++) {
   413             int d = list->cmpfunc(left, right);
   414             if (d != 0) {
   415                 return d;
   416             }
   417             left += list->item_size;
   418             right += other->item_size;
   419         }
   420         return 0;
   421     } else {
   422         return list->size < other->size ? -1 : 1;
   423     }
   424 }
   426 static void cx_arl_reverse(struct cx_list_s *list) {
   427     if (list->size < 2) return;
   428     void *data = ((cx_array_list const *) list)->data;
   429     size_t half = list->size / 2;
   430     for (size_t i = 0; i < half; i++) {
   431         cx_array_swap(data, list->item_size, i, list->size - 1 - i);
   432     }
   433 }
   435 static bool cx_arl_iter_valid(void const *it) {
   436     struct cx_iterator_s const *iter = it;
   437     struct cx_list_s const *list = iter->src_handle;
   438     return iter->index < list->size;
   439 }
   441 static void *cx_arl_iter_current(void const *it) {
   442     struct cx_iterator_s const *iter = it;
   443     return iter->elem_handle;
   444 }
   446 static void cx_arl_iter_next(void *it) {
   447     struct cx_iterator_base_s *itbase = it;
   448     if (itbase->remove) {
   449         struct cx_mut_iterator_s *iter = it;
   450         itbase->remove = false;
   451         cx_arl_remove(iter->src_handle, iter->index);
   452     } else {
   453         struct cx_iterator_s *iter = it;
   454         iter->index++;
   455         iter->elem_handle =
   456                 ((char *) iter->elem_handle)
   457                 + ((struct cx_list_s const *) iter->src_handle)->item_size;
   458     }
   459 }
   461 static void cx_arl_iter_prev(void *it) {
   462     struct cx_iterator_base_s *itbase = it;
   463     struct cx_mut_iterator_s *iter = it;
   464     cx_array_list *const list = iter->src_handle;
   465     if (itbase->remove) {
   466         itbase->remove = false;
   467         cx_arl_remove(iter->src_handle, iter->index);
   468     }
   469     iter->index--;
   470     if (iter->index < list->base.size) {
   471         iter->elem_handle = ((char *) list->data)
   472                             + iter->index * list->base.item_size;
   473     }
   474 }
   476 static bool cx_arl_iter_flag_rm(void *it) {
   477     struct cx_iterator_base_s *iter = it;
   478     if (iter->mutating) {
   479         iter->remove = true;
   480         return true;
   481     } else {
   482         return false;
   483     }
   484 }
   486 static struct cx_iterator_s cx_arl_iterator(
   487         struct cx_list_s const *list,
   488         size_t index,
   489         bool backwards
   490 ) {
   491     struct cx_iterator_s iter;
   493     iter.index = index;
   494     iter.src_handle = list;
   495     iter.elem_handle = cx_arl_at(list, index);
   496     iter.base.valid = cx_arl_iter_valid;
   497     iter.base.current = cx_arl_iter_current;
   498     iter.base.next = backwards ? cx_arl_iter_prev : cx_arl_iter_next;
   499     iter.base.flag_removal = cx_arl_iter_flag_rm;
   500     iter.base.remove = false;
   501     iter.base.mutating = false;
   503     return iter;
   504 }
   506 static cx_list_class cx_array_list_class = {
   507         cx_arl_destructor,
   508         cx_arl_insert_element,
   509         cx_arl_insert_array,
   510         cx_arl_insert_iter,
   511         cx_arl_remove,
   512         cx_arl_clear,
   513         cx_arl_swap,
   514         cx_arl_at,
   515         cx_arl_find_remove,
   516         cx_arl_sort,
   517         cx_arl_compare,
   518         cx_arl_reverse,
   519         cx_arl_iterator,
   520 };
   522 CxList *cxArrayListCreate(
   523         CxAllocator const *allocator,
   524         cx_compare_func comparator,
   525         size_t item_size,
   526         size_t initial_capacity
   527 ) {
   528     if (allocator == NULL) {
   529         allocator = cxDefaultAllocator;
   530     }
   532     cx_array_list *list = cxCalloc(allocator, 1, sizeof(cx_array_list));
   533     if (list == NULL) return NULL;
   535     list->base.cl = &cx_array_list_class;
   536     list->base.allocator = allocator;
   537     list->capacity = initial_capacity;
   539     if (item_size > 0) {
   540         list->base.item_size = item_size;
   541         list->base.cmpfunc = comparator;
   542     } else {
   543         item_size = sizeof(void *);
   544         list->base.cmpfunc = comparator == NULL ? cx_cmp_ptr : comparator;
   545         cxListStorePointers((CxList *) list);
   546     }
   548     // allocate the array after the real item_size is known
   549     list->data = cxCalloc(allocator, initial_capacity, item_size);
   550     if (list->data == NULL) {
   551         cxFree(allocator, list);
   552         return NULL;
   553     }
   555     // configure the reallocator
   556     list->reallocator.realloc = cx_arl_realloc;
   557     list->reallocator.ptr1 = (void *) allocator;
   559     return (CxList *) list;
   560 }

mercurial