src/array_list.c

Thu, 23 Feb 2023 22:24:26 +0100

author
Mike Becker <universe@uap-core.de>
date
Thu, 23 Feb 2023 22:24:26 +0100
changeset 660
4738a9065907
parent 655
7340c4255f1f
child 662
d0d95740071b
permissions
-rw-r--r--

add some asserts

     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     assert(arr != NULL);
   117     // short circuit
   118     if (idx1 == idx2) return;
   120     char sbo_mem[CX_ARRAY_SWAP_SBO_SIZE];
   121     void *tmp;
   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();
   128     } else {
   129         tmp = sbo_mem;
   130     }
   132     // calculate memory locations
   133     char *left = arr, *right = arr;
   134     left += idx1 * elem_size;
   135     right += idx2 * elem_size;
   137     // three-way swap
   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) {
   144         free(tmp);
   145     }
   146 }
   148 // HIGH LEVEL ARRAY LIST FUNCTIONS
   150 typedef struct {
   151     struct cx_list_s base;
   152     void *data;
   153     struct cx_array_reallocator_s reallocator;
   154 } cx_array_list;
   156 static void *cx_arl_realloc(
   157         void *array,
   158         size_t capacity,
   159         size_t elem_size,
   160         struct cx_array_reallocator_s *alloc
   161 ) {
   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);
   167 }
   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);
   172 }
   174 static size_t cx_arl_insert_array(
   175         struct cx_list_s *list,
   176         size_t index,
   177         void const *array,
   178         size_t n
   179 ) {
   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(
   194                 &arl->data,
   195                 &list->size,
   196                 &list->capacity,
   197                 start_of_moved,
   198                 first_to_move,
   199                 list->itemsize,
   200                 elems_to_move,
   201                 &arl->reallocator
   202         )) {
   203             // if moving existing elems is unsuccessful, abort
   204             return 0;
   205         }
   206     }
   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(
   214             &arl->data,
   215             &list->size,
   216             &list->capacity,
   217             index,
   218             array,
   219             list->itemsize,
   220             n,
   221             &arl->reallocator
   222     )) {
   223         return n;
   224     } else {
   225         // array list implementation is "all or nothing"
   226         return 0;
   227     }
   228 }
   230 static int cx_arl_insert_element(
   231         struct cx_list_s *list,
   232         size_t index,
   233         void const *element
   234 ) {
   235     return 1 != cx_arl_insert_array(list, index, element, 1);
   236 }
   238 static int cx_arl_insert_iter(
   239         struct cx_mut_iterator_s *iter,
   240         void const *elem,
   241         int prepend
   242 ) {
   243     struct cx_list_s *list = iter->src_handle;
   244     if (iter->index < list->size) {
   245         int result = cx_arl_insert_element(
   246                 list,
   247                 iter->index + 1 - prepend,
   248                 elem
   249         );
   250         if (result == 0 && prepend != 0) {
   251             iter->index++;
   252             iter->elem_handle = ((char *) iter->elem_handle) + list->itemsize;
   253         }
   254         return result;
   255     } else {
   256         int result = cx_arl_insert_element(list, list->size, elem);
   257         iter->index = list->size;
   258         return result;
   259     }
   260 }
   262 static int cx_arl_remove(
   263         struct cx_list_s *list,
   264         size_t index
   265 ) {
   266     // out-of-bounds check
   267     if (index >= list->size) {
   268         return 1;
   269     }
   271     // short-circuit removal of last element
   272     if (index == list->size - 1) {
   273         list->size--;
   274         return 0;
   275     }
   277     // just move the elements starting at index to the left
   278     cx_array_list *arl = (cx_array_list *) list;
   279     int result = cx_array_copy(
   280             &arl->data,
   281             &list->size,
   282             &list->capacity,
   283             index,
   284             ((char *) arl->data) + (index + 1) * list->itemsize,
   285             list->itemsize,
   286             list->size - index - 1,
   287             &arl->reallocator
   288     );
   289     if (result == 0) {
   290         // decrease the size
   291         list->size--;
   292     }
   293     return result;
   294 }
   296 static int cx_arl_swap(
   297         struct cx_list_s *list,
   298         size_t i,
   299         size_t j
   300 ) {
   301     if (i >= list->size || j >= list->size) return 1;
   302     cx_array_list *arl = (cx_array_list *) list;
   303     cx_array_swap(arl->data, list->itemsize, i, j);
   304     return 0;
   305 }
   307 static void *cx_arl_at(
   308         struct cx_list_s const *list,
   309         size_t index
   310 ) {
   311     if (index < list->size) {
   312         cx_array_list const *arl = (cx_array_list const *) list;
   313         char *space = arl->data;
   314         return space + index * list->itemsize;
   315     } else {
   316         return NULL;
   317     }
   318 }
   320 static size_t cx_arl_find(
   321         struct cx_list_s const *list,
   322         void const *elem
   323 ) {
   324     assert(list->cmpfunc != NULL);
   325     char *cur = ((cx_array_list const *) list)->data;
   327     for (size_t i = 0; i < list->size; i++) {
   328         if (0 == list->cmpfunc(elem, cur)) {
   329             return i;
   330         }
   331         cur += list->itemsize;
   332     }
   334     return list->size;
   335 }
   337 static void cx_arl_sort(struct cx_list_s *list) {
   338     assert(list->cmpfunc != NULL);
   339     qsort(((cx_array_list *) list)->data,
   340           list->size,
   341           list->itemsize,
   342           list->cmpfunc
   343     );
   344 }
   346 static int cx_arl_compare(
   347         struct cx_list_s const *list,
   348         struct cx_list_s const *other
   349 ) {
   350     assert(list->cmpfunc != NULL);
   351     if (list->size == other->size) {
   352         char const *left = ((cx_array_list const *) list)->data;
   353         char const *right = ((cx_array_list const *) other)->data;
   354         for (size_t i = 0; i < list->size; i++) {
   355             int d = list->cmpfunc(left, right);
   356             if (d != 0) {
   357                 return d;
   358             }
   359             left += list->itemsize;
   360             right += other->itemsize;
   361         }
   362         return 0;
   363     } else {
   364         return list->size < other->size ? -1 : 1;
   365     }
   366 }
   368 static void cx_arl_reverse(struct cx_list_s *list) {
   369     if (list->size < 2) return;
   370     void *data = ((cx_array_list const *) list)->data;
   371     size_t half = list->size / 2;
   372     for (size_t i = 0; i < half; i++) {
   373         cx_array_swap(data, list->itemsize, i, list->size - 1 - i);
   374     }
   375 }
   377 static bool cx_arl_iter_valid(void const *it) {
   378     struct cx_iterator_s const *iter = it;
   379     struct cx_list_s const *list = iter->src_handle;
   380     return iter->index < list->size;
   381 }
   383 static void *cx_arl_iter_current(void const *it) {
   384     struct cx_iterator_s const *iter = it;
   385     return iter->elem_handle;
   386 }
   388 static void cx_arl_iter_next(void *it) {
   389     struct cx_iterator_base_s *itbase = it;
   390     if (itbase->remove) {
   391         struct cx_mut_iterator_s *iter = it;
   392         itbase->remove = false;
   393         cx_arl_remove(iter->src_handle, iter->index);
   394     } else {
   395         struct cx_iterator_s *iter = it;
   396         iter->index++;
   397         iter->elem_handle =
   398                 ((char *) iter->elem_handle)
   399                 + ((struct cx_list_s const *) iter->src_handle)->itemsize;
   400     }
   401 }
   403 static void cx_arl_iter_prev(void *it) {
   404     struct cx_iterator_base_s *itbase = it;
   405     struct cx_mut_iterator_s *iter = it;
   406     cx_array_list *const list = iter->src_handle;
   407     if (itbase->remove) {
   408         itbase->remove = false;
   409         cx_arl_remove(iter->src_handle, iter->index);
   410     }
   411     iter->index--;
   412     if (iter->index < list->base.size) {
   413         iter->elem_handle = ((char *) list->data)
   414                             + iter->index * list->base.itemsize;
   415     }
   416 }
   418 static bool cx_arl_iter_flag_rm(void *it) {
   419     struct cx_iterator_base_s *iter = it;
   420     if (iter->mutating) {
   421         iter->remove = true;
   422         return true;
   423     } else {
   424         return false;
   425     }
   426 }
   428 static struct cx_iterator_s cx_arl_iterator(
   429         struct cx_list_s const *list,
   430         size_t index,
   431         bool backwards
   432 ) {
   433     struct cx_iterator_s iter;
   435     iter.index = index;
   436     iter.src_handle = list;
   437     iter.elem_handle = cx_arl_at(list, index);
   438     iter.base.valid = cx_arl_iter_valid;
   439     iter.base.current = cx_arl_iter_current;
   440     iter.base.next = backwards ? cx_arl_iter_prev : cx_arl_iter_next;
   441     iter.base.flag_removal = cx_arl_iter_flag_rm;
   442     iter.base.remove = false;
   443     iter.base.mutating = false;
   445     return iter;
   446 }
   448 static cx_list_class cx_array_list_class = {
   449         cx_arl_destructor,
   450         cx_arl_insert_element,
   451         cx_arl_insert_array,
   452         cx_arl_insert_iter,
   453         cx_arl_remove,
   454         cx_arl_swap,
   455         cx_arl_at,
   456         cx_arl_find,
   457         cx_arl_sort,
   458         cx_arl_compare,
   459         cx_arl_reverse,
   460         cx_arl_iterator,
   461 };
   463 CxList *cxArrayListCreate(
   464         CxAllocator const *allocator,
   465         CxListComparator comparator,
   466         size_t item_size,
   467         size_t initial_capacity
   468 ) {
   469     cx_array_list *list = cxCalloc(allocator, 1, sizeof(cx_array_list));
   470     if (list == NULL) return NULL;
   472     list->data = cxCalloc(allocator, initial_capacity, item_size);
   473     if (list->data == NULL) {
   474         cxFree(allocator, list);
   475         return NULL;
   476     }
   478     list->base.cl = &cx_array_list_class;
   479     list->base.allocator = allocator;
   480     list->base.cmpfunc = comparator;
   481     list->base.itemsize = item_size;
   482     list->base.capacity = initial_capacity;
   484     // configure the reallocator
   485     list->reallocator.realloc = cx_arl_realloc;
   486     list->reallocator.ptr1 = (void *) allocator;
   488     return (CxList *) list;
   489 }

mercurial