src/array_list.c

Mon, 18 Dec 2023 16:14:07 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 18 Dec 2023 16:14:07 +0100
changeset 763
741a2040fa33
parent 735
b686d0c98c62
child 764
ccbdbd088455
permissions
-rw-r--r--

make cx_cmp_ptr default comparator for pointer lists - relates to #340

     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 void cx_array_swap(
   111         void *arr,
   112         size_t elem_size,
   113         size_t idx1,
   114         size_t idx2
   115 ) {
   116     assert(arr != NULL);
   118     // short circuit
   119     if (idx1 == idx2) return;
   121     char sbo_mem[CX_ARRAY_SWAP_SBO_SIZE];
   122     void *tmp;
   124     // decide if we can use the local buffer
   125     if (elem_size > CX_ARRAY_SWAP_SBO_SIZE) {
   126         tmp = malloc(elem_size);
   127         // we don't want to enforce error handling
   128         if (tmp == NULL) abort();
   129     } else {
   130         tmp = sbo_mem;
   131     }
   133     // calculate memory locations
   134     char *left = arr, *right = arr;
   135     left += idx1 * elem_size;
   136     right += idx2 * elem_size;
   138     // three-way swap
   139     memcpy(tmp, left, elem_size);
   140     memcpy(left, right, elem_size);
   141     memcpy(right, tmp, elem_size);
   143     // free dynamic memory, if it was needed
   144     if (tmp != sbo_mem) {
   145         free(tmp);
   146     }
   147 }
   149 // HIGH LEVEL ARRAY LIST FUNCTIONS
   151 typedef struct {
   152     struct cx_list_s base;
   153     void *data;
   154     size_t capacity;
   155     struct cx_array_reallocator_s reallocator;
   156 } cx_array_list;
   158 static void *cx_arl_realloc(
   159         void *array,
   160         size_t capacity,
   161         size_t elem_size,
   162         struct cx_array_reallocator_s *alloc
   163 ) {
   164     // retrieve the pointer to the list allocator
   165     CxAllocator const *al = alloc->ptr1;
   167     // use the list allocator to reallocate the memory
   168     return cxRealloc(al, array, capacity * elem_size);
   169 }
   171 static void cx_arl_destructor(struct cx_list_s *list) {
   172     cx_array_list *arl = (cx_array_list *) list;
   174     char *ptr = arl->data;
   176     if (list->simple_destructor) {
   177         for (size_t i = 0; i < list->size; i++) {
   178             cx_invoke_simple_destructor(list, ptr);
   179             ptr += list->item_size;
   180         }
   181     }
   182     if (list->advanced_destructor) {
   183         for (size_t i = 0; i < list->size; i++) {
   184             cx_invoke_advanced_destructor(list, ptr);
   185             ptr += list->item_size;
   186         }
   187     }
   189     cxFree(list->allocator, arl->data);
   190     cxFree(list->allocator, list);
   191 }
   193 static size_t cx_arl_insert_array(
   194         struct cx_list_s *list,
   195         size_t index,
   196         void const *array,
   197         size_t n
   198 ) {
   199     // out of bounds and special case check
   200     if (index > list->size || n == 0) return 0;
   202     // get a correctly typed pointer to the list
   203     cx_array_list *arl = (cx_array_list *) list;
   205     // do we need to move some elements?
   206     if (index < list->size) {
   207         char const *first_to_move = (char const *) arl->data;
   208         first_to_move += index * list->item_size;
   209         size_t elems_to_move = list->size - index;
   210         size_t start_of_moved = index + n;
   212         if (CX_ARRAY_COPY_SUCCESS != cx_array_copy(
   213                 &arl->data,
   214                 &list->size,
   215                 &arl->capacity,
   216                 start_of_moved,
   217                 first_to_move,
   218                 list->item_size,
   219                 elems_to_move,
   220                 &arl->reallocator
   221         )) {
   222             // if moving existing elems is unsuccessful, abort
   223             return 0;
   224         }
   225     }
   227     // note that if we had to move the elements, the following operation
   228     // is guaranteed to succeed, because we have the memory already allocated
   229     // therefore, it is impossible to leave this function with an invalid array
   231     // place the new elements
   232     if (CX_ARRAY_COPY_SUCCESS == cx_array_copy(
   233             &arl->data,
   234             &list->size,
   235             &arl->capacity,
   236             index,
   237             array,
   238             list->item_size,
   239             n,
   240             &arl->reallocator
   241     )) {
   242         return n;
   243     } else {
   244         // array list implementation is "all or nothing"
   245         return 0;
   246     }
   247 }
   249 static int cx_arl_insert_element(
   250         struct cx_list_s *list,
   251         size_t index,
   252         void const *element
   253 ) {
   254     return 1 != cx_arl_insert_array(list, index, element, 1);
   255 }
   257 static int cx_arl_insert_iter(
   258         struct cx_mut_iterator_s *iter,
   259         void const *elem,
   260         int prepend
   261 ) {
   262     struct cx_list_s *list = iter->src_handle;
   263     if (iter->index < list->size) {
   264         int result = cx_arl_insert_element(
   265                 list,
   266                 iter->index + 1 - prepend,
   267                 elem
   268         );
   269         if (result == 0 && prepend != 0) {
   270             iter->index++;
   271             iter->elem_handle = ((char *) iter->elem_handle) + list->item_size;
   272         }
   273         return result;
   274     } else {
   275         int result = cx_arl_insert_element(list, list->size, elem);
   276         iter->index = list->size;
   277         return result;
   278     }
   279 }
   281 static int cx_arl_remove(
   282         struct cx_list_s *list,
   283         size_t index
   284 ) {
   285     cx_array_list *arl = (cx_array_list *) list;
   287     // out-of-bounds check
   288     if (index >= list->size) {
   289         return 1;
   290     }
   292     // content destruction
   293     cx_invoke_destructor(list, ((char *) arl->data) + index * list->item_size);
   295     // short-circuit removal of last element
   296     if (index == list->size - 1) {
   297         list->size--;
   298         return 0;
   299     }
   301     // just move the elements starting at index to the left
   302     int result = cx_array_copy(
   303             &arl->data,
   304             &list->size,
   305             &arl->capacity,
   306             index,
   307             ((char *) arl->data) + (index + 1) * list->item_size,
   308             list->item_size,
   309             list->size - index - 1,
   310             &arl->reallocator
   311     );
   312     if (result == 0) {
   313         // decrease the size
   314         list->size--;
   315     }
   316     return result;
   317 }
   319 static void cx_arl_clear(struct cx_list_s *list) {
   320     if (list->size == 0) return;
   322     cx_array_list *arl = (cx_array_list *) list;
   323     char *ptr = arl->data;
   325     if (list->simple_destructor) {
   326         for (size_t i = 0; i < list->size; i++) {
   327             cx_invoke_simple_destructor(list, ptr);
   328             ptr += list->item_size;
   329         }
   330     }
   331     if (list->advanced_destructor) {
   332         for (size_t i = 0; i < list->size; i++) {
   333             cx_invoke_advanced_destructor(list, ptr);
   334             ptr += list->item_size;
   335         }
   336     }
   338     memset(arl->data, 0, list->size * list->item_size);
   339     list->size = 0;
   340 }
   342 static int cx_arl_swap(
   343         struct cx_list_s *list,
   344         size_t i,
   345         size_t j
   346 ) {
   347     if (i >= list->size || j >= list->size) return 1;
   348     cx_array_list *arl = (cx_array_list *) list;
   349     cx_array_swap(arl->data, list->item_size, i, j);
   350     return 0;
   351 }
   353 static void *cx_arl_at(
   354         struct cx_list_s const *list,
   355         size_t index
   356 ) {
   357     if (index < list->size) {
   358         cx_array_list const *arl = (cx_array_list const *) list;
   359         char *space = arl->data;
   360         return space + index * list->item_size;
   361     } else {
   362         return NULL;
   363     }
   364 }
   366 static ssize_t cx_arl_find(
   367         struct cx_list_s const *list,
   368         void const *elem
   369 ) {
   370     assert(list->cmpfunc != NULL);
   371     assert(list->size < SIZE_MAX / 2);
   372     char *cur = ((cx_array_list const *) list)->data;
   374     for (ssize_t i = 0; i < (ssize_t) list->size; i++) {
   375         if (0 == list->cmpfunc(elem, cur)) {
   376             return i;
   377         }
   378         cur += list->item_size;
   379     }
   381     return -1;
   382 }
   384 static void cx_arl_sort(struct cx_list_s *list) {
   385     assert(list->cmpfunc != NULL);
   386     qsort(((cx_array_list *) list)->data,
   387           list->size,
   388           list->item_size,
   389           list->cmpfunc
   390     );
   391 }
   393 static int cx_arl_compare(
   394         struct cx_list_s const *list,
   395         struct cx_list_s const *other
   396 ) {
   397     assert(list->cmpfunc != NULL);
   398     if (list->size == other->size) {
   399         char const *left = ((cx_array_list const *) list)->data;
   400         char const *right = ((cx_array_list const *) other)->data;
   401         for (size_t i = 0; i < list->size; i++) {
   402             int d = list->cmpfunc(left, right);
   403             if (d != 0) {
   404                 return d;
   405             }
   406             left += list->item_size;
   407             right += other->item_size;
   408         }
   409         return 0;
   410     } else {
   411         return list->size < other->size ? -1 : 1;
   412     }
   413 }
   415 static void cx_arl_reverse(struct cx_list_s *list) {
   416     if (list->size < 2) return;
   417     void *data = ((cx_array_list const *) list)->data;
   418     size_t half = list->size / 2;
   419     for (size_t i = 0; i < half; i++) {
   420         cx_array_swap(data, list->item_size, i, list->size - 1 - i);
   421     }
   422 }
   424 static bool cx_arl_iter_valid(void const *it) {
   425     struct cx_iterator_s const *iter = it;
   426     struct cx_list_s const *list = iter->src_handle;
   427     return iter->index < list->size;
   428 }
   430 static void *cx_arl_iter_current(void const *it) {
   431     struct cx_iterator_s const *iter = it;
   432     return iter->elem_handle;
   433 }
   435 static void cx_arl_iter_next(void *it) {
   436     struct cx_iterator_base_s *itbase = it;
   437     if (itbase->remove) {
   438         struct cx_mut_iterator_s *iter = it;
   439         itbase->remove = false;
   440         cx_arl_remove(iter->src_handle, iter->index);
   441     } else {
   442         struct cx_iterator_s *iter = it;
   443         iter->index++;
   444         iter->elem_handle =
   445                 ((char *) iter->elem_handle)
   446                 + ((struct cx_list_s const *) iter->src_handle)->item_size;
   447     }
   448 }
   450 static void cx_arl_iter_prev(void *it) {
   451     struct cx_iterator_base_s *itbase = it;
   452     struct cx_mut_iterator_s *iter = it;
   453     cx_array_list *const list = iter->src_handle;
   454     if (itbase->remove) {
   455         itbase->remove = false;
   456         cx_arl_remove(iter->src_handle, iter->index);
   457     }
   458     iter->index--;
   459     if (iter->index < list->base.size) {
   460         iter->elem_handle = ((char *) list->data)
   461                             + iter->index * list->base.item_size;
   462     }
   463 }
   465 static bool cx_arl_iter_flag_rm(void *it) {
   466     struct cx_iterator_base_s *iter = it;
   467     if (iter->mutating) {
   468         iter->remove = true;
   469         return true;
   470     } else {
   471         return false;
   472     }
   473 }
   475 static struct cx_iterator_s cx_arl_iterator(
   476         struct cx_list_s const *list,
   477         size_t index,
   478         bool backwards
   479 ) {
   480     struct cx_iterator_s iter;
   482     iter.index = index;
   483     iter.src_handle = list;
   484     iter.elem_handle = cx_arl_at(list, index);
   485     iter.base.valid = cx_arl_iter_valid;
   486     iter.base.current = cx_arl_iter_current;
   487     iter.base.next = backwards ? cx_arl_iter_prev : cx_arl_iter_next;
   488     iter.base.flag_removal = cx_arl_iter_flag_rm;
   489     iter.base.remove = false;
   490     iter.base.mutating = false;
   492     return iter;
   493 }
   495 static cx_list_class cx_array_list_class = {
   496         cx_arl_destructor,
   497         cx_arl_insert_element,
   498         cx_arl_insert_array,
   499         cx_arl_insert_iter,
   500         cx_arl_remove,
   501         cx_arl_clear,
   502         cx_arl_swap,
   503         cx_arl_at,
   504         cx_arl_find,
   505         cx_arl_sort,
   506         cx_arl_compare,
   507         cx_arl_reverse,
   508         cx_arl_iterator,
   509 };
   511 CxList *cxArrayListCreate(
   512         CxAllocator const *allocator,
   513         cx_compare_func comparator,
   514         size_t item_size,
   515         size_t initial_capacity
   516 ) {
   517     if (allocator == NULL) {
   518         allocator = cxDefaultAllocator;
   519     }
   521     cx_array_list *list = cxCalloc(allocator, 1, sizeof(cx_array_list));
   522     if (list == NULL) return NULL;
   524     list->base.cl = &cx_array_list_class;
   525     list->base.allocator = allocator;
   526     list->capacity = initial_capacity;
   528     if (item_size > 0) {
   529         list->base.item_size = item_size;
   530         list->base.cmpfunc = comparator;
   531     } else {
   532         item_size = sizeof(void *);
   533         list->base.cmpfunc = comparator == NULL ? cx_cmp_ptr : comparator;
   534         cxListStorePointers((CxList *) list);
   535     }
   537     // allocate the array after the real item_size is known
   538     list->data = cxCalloc(allocator, initial_capacity, item_size);
   539     if (list->data == NULL) {
   540         cxFree(allocator, list);
   541         return NULL;
   542     }
   544     // configure the reallocator
   545     list->reallocator.realloc = cx_arl_realloc;
   546     list->reallocator.ptr1 = (void *) allocator;
   548     return (CxList *) list;
   549 }

mercurial