src/array_list.c

Tue, 07 Feb 2023 20:08:45 +0100

author
Mike Becker <universe@uap-core.de>
date
Tue, 07 Feb 2023 20:08:45 +0100
changeset 650
77021e06b1a8
parent 643
5700ba9154ab
child 654
c9d008861178
permissions
-rw-r--r--

fix code not compiling under windows+mingw

     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 void *cx_arl_at(
   295         struct cx_list_s const *list,
   296         size_t index
   297 ) {
   298     if (index < list->size) {
   299         cx_array_list const *arl = (cx_array_list const *) list;
   300         char *space = arl->data;
   301         return space + index * list->itemsize;
   302     } else {
   303         return NULL;
   304     }
   305 }
   307 static size_t cx_arl_find(
   308         struct cx_list_s const *list,
   309         void const *elem
   310 ) {
   311     char *cur = ((cx_array_list const *) list)->data;
   313     for (size_t i = 0; i < list->size; i++) {
   314         if (0 == list->cmpfunc(elem, cur)) {
   315             return i;
   316         }
   317         cur += list->itemsize;
   318     }
   320     return list->size;
   321 }
   323 static void cx_arl_sort(struct cx_list_s *list) {
   324     qsort(((cx_array_list *) list)->data,
   325           list->size,
   326           list->itemsize,
   327           list->cmpfunc
   328     );
   329 }
   331 static int cx_arl_compare(
   332         struct cx_list_s const *list,
   333         struct cx_list_s const *other
   334 ) {
   335     if (list->size == other->size) {
   336         char const *left = ((cx_array_list const *) list)->data;
   337         char const *right = ((cx_array_list const *) other)->data;
   338         for (size_t i = 0; i < list->size; i++) {
   339             int d = list->cmpfunc(left, right);
   340             if (d != 0) {
   341                 return d;
   342             }
   343             left += list->itemsize;
   344             right += other->itemsize;
   345         }
   346         return 0;
   347     } else {
   348         return list->size < other->size ? -1 : 1;
   349     }
   350 }
   352 static void cx_arl_reverse(struct cx_list_s *list) {
   353     if (list->size < 2) return;
   354     void *data = ((cx_array_list const *) list)->data;
   355     size_t half = list->size / 2;
   356     for (size_t i = 0; i < half; i++) {
   357         cx_array_swap(data, list->itemsize, i, list->size - 1 - i);
   358     }
   359 }
   361 static bool cx_arl_iter_valid(void const *it) {
   362     struct cx_iterator_s const *iter = it;
   363     struct cx_list_s const *list = iter->src_handle;
   364     return iter->index < list->size;
   365 }
   367 static void *cx_arl_iter_current(void const *it) {
   368     struct cx_iterator_s const *iter = it;
   369     return iter->elem_handle;
   370 }
   372 static void cx_arl_iter_next(void *it) {
   373     struct cx_iterator_base_s *itbase = it;
   374     if (itbase->remove) {
   375         struct cx_mut_iterator_s *iter = it;
   376         itbase->remove = false;
   377         cx_arl_remove(iter->src_handle, iter->index);
   378     } else {
   379         struct cx_iterator_s *iter = it;
   380         iter->index++;
   381         iter->elem_handle =
   382                 ((char *) iter->elem_handle)
   383                 + ((struct cx_list_s const *) iter->src_handle)->itemsize;
   384     }
   385 }
   387 static bool cx_arl_iter_flag_rm(void *it) {
   388     struct cx_iterator_base_s *iter = it;
   389     if (iter->mutating) {
   390         iter->remove = true;
   391         return true;
   392     } else {
   393         return false;
   394     }
   395 }
   397 static struct cx_iterator_s cx_arl_iterator(
   398         struct cx_list_s const *list,
   399         size_t index
   400 ) {
   401     struct cx_iterator_s iter;
   403     iter.index = index;
   404     iter.src_handle = list;
   405     iter.elem_handle = cx_arl_at(list, index);
   406     iter.base.valid = cx_arl_iter_valid;
   407     iter.base.current = cx_arl_iter_current;
   408     iter.base.next = cx_arl_iter_next;
   409     iter.base.flag_removal = cx_arl_iter_flag_rm;
   410     iter.base.remove = false;
   411     iter.base.mutating = false;
   413     return iter;
   414 }
   416 static cx_list_class cx_array_list_class = {
   417         cx_arl_destructor,
   418         cx_arl_insert_element,
   419         cx_arl_insert_array,
   420         cx_arl_insert_iter,
   421         cx_arl_remove,
   422         cx_arl_at,
   423         cx_arl_find,
   424         cx_arl_sort,
   425         cx_arl_compare,
   426         cx_arl_reverse,
   427         cx_arl_iterator,
   428 };
   430 CxList *cxArrayListCreate(
   431         CxAllocator const *allocator,
   432         CxListComparator comparator,
   433         size_t item_size,
   434         size_t initial_capacity
   435 ) {
   436     cx_array_list *list = cxCalloc(allocator, 1, sizeof(cx_array_list));
   437     if (list == NULL) return NULL;
   439     list->data = cxCalloc(allocator, initial_capacity, item_size);
   440     if (list->data == NULL) {
   441         cxFree(allocator, list);
   442         return NULL;
   443     }
   445     list->base.cl = &cx_array_list_class;
   446     list->base.allocator = allocator;
   447     list->base.cmpfunc = comparator;
   448     list->base.itemsize = item_size;
   449     list->base.capacity = initial_capacity;
   451     // configure the reallocator
   452     list->reallocator.realloc = cx_arl_realloc;
   453     list->reallocator.ptr1 = (void *) allocator;
   455     return (CxList *) list;
   456 }

mercurial