src/array_list.c

Sun, 20 Nov 2022 16:58:51 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 20 Nov 2022 16:58:51 +0100
changeset 623
21082350a590
parent 622
3d93cd78aa20
child 624
b0bdff7d8203
permissions
-rw-r--r--

#219 array list: implement reverse

     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>
    32 #include <stdint.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 newsize = index + elem_count;
    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         /* increase capacity linearly */
    72         cap += 16;
    74         /* perform reallocation */
    75         void *newmem = reallocator->realloc(
    76                 *target, cap, elem_size, reallocator
    77         );
    78         if (newmem == NULL) {
    79             return CX_ARRAY_COPY_REALLOC_FAILED;
    80         }
    82         /* repair src pointer, if necessary */
    83         if (repairsrc) {
    84             src = ((char *) newmem) + (srcaddr - targetaddr);
    85         }
    87         /* store new pointer and capacity */
    88         *target = newmem;
    89         *capacity = cap;
    90     }
    92     /* determine target pointer */
    93     char *start = *target;
    94     start += index * elem_size;
    96     /* copy elements and set new size */
    97     memmove(start, src, elem_count * elem_size);
    98     *size = newsize;
   100     /* return successfully */
   101     return CX_ARRAY_COPY_SUCCESS;
   102 }
   104 #define CX_ARRAY_SWAP_SBO_SIZE 512
   106 void cx_array_swap(
   107         void *arr,
   108         size_t elem_size,
   109         size_t idx1,
   110         size_t idx2
   111 ) {
   112     /* short circuit */
   113     if (idx1 == idx2) return;
   115     char sbo_mem[CX_ARRAY_SWAP_SBO_SIZE];
   116     void *tmp;
   118     /* decide if we can use the local buffer */
   119     if (elem_size > CX_ARRAY_SWAP_SBO_SIZE) {
   120         tmp = malloc(elem_size);
   121         /* we don't want to enforce error handling */
   122         if (tmp == NULL) abort();
   123     } else {
   124         tmp = sbo_mem;
   125     }
   127     /* calculate memory locations */
   128     char *left = arr, *right = arr;
   129     left += idx1 * elem_size;
   130     right += idx2 * elem_size;
   132     /* three-way swap */
   133     memcpy(tmp, left, elem_size);
   134     memcpy(left, right, elem_size);
   135     memcpy(right, tmp, elem_size);
   137     /* free dynamic memory, if it was needed */
   138     if (tmp != sbo_mem) {
   139         free(tmp);
   140     }
   141 }
   143 /* HIGH LEVEL ARRAY LIST FUNCTIONS */
   145 typedef struct {
   146     struct cx_list_s base;
   147     void *data;
   148     struct cx_array_reallocator_s reallocator;
   149 } cx_array_list;
   151 static void *cx_arl_realloc(
   152         void *array,
   153         size_t capacity,
   154         size_t elem_size,
   155         struct cx_array_reallocator_s *alloc
   156 ) {
   157     /* retrieve the pointer to the list allocator */
   158     CxAllocator const *al = alloc->ptr1;
   160     /* use the list allocator to reallocate the memory */
   161     return cxRealloc(al, array, capacity * elem_size);
   162 }
   164 static void cx_arl_destructor(struct cx_list_s *list) {
   165     cx_array_list *arl = (cx_array_list *) list;
   166     cxFree(list->allocator, arl->data);
   167 }
   169 static int cx_arl_add(
   170         struct cx_list_s *list,
   171         void const *elem
   172 ) {
   173     cx_array_list *arl = (cx_array_list *) list;
   174     return cx_array_copy(
   175             &arl->data,
   176             &list->size,
   177             &list->capacity,
   178             list->size,
   179             elem,
   180             list->itemsize,
   181             1,
   182             &arl->reallocator
   183     );
   184 }
   186 static int cx_arl_insert(
   187         struct cx_list_s *list,
   188         size_t index,
   189         void const *elem
   190 ) {
   191     if (index > list->size) {
   192         return 1;
   193     } else if (index == list->size) {
   194         return cx_arl_add(list, elem);
   195     } else {
   196         cx_array_list *arl = (cx_array_list *) list;
   198         /* move elements starting at index to the right */
   199         if (cx_array_copy(
   200                 &arl->data,
   201                 &list->size,
   202                 &list->capacity,
   203                 index + 1,
   204                 ((char *) arl->data) + index * list->itemsize,
   205                 list->itemsize,
   206                 list->size - index,
   207                 &arl->reallocator
   208         )) {
   209             return 1;
   210         }
   212         /* place the element */
   213         memcpy(((char *) arl->data) + index * list->itemsize,
   214                elem, list->itemsize);
   216         return 0;
   217     }
   218 }
   220 static int cx_arl_insert_iter(
   221         struct cx_iterator_s *iter,
   222         void const *elem,
   223         int prepend
   224 ) {
   225     struct cx_list_s *list = iter->src_handle;
   226     if (iter->index < list->size) {
   227         int result = cx_arl_insert(
   228                 list,
   229                 iter->index + 1 - prepend,
   230                 elem
   231         );
   232         if (result == 0 && prepend != 0) {
   233             iter->index++;
   234             iter->elem_handle = ((char *) iter->elem_handle) + list->itemsize;
   235         }
   236         return result;
   237     } else {
   238         int result = cx_arl_add(list, elem);
   239         iter->index = list->size;
   240         return result;
   241     }
   242 }
   244 static int cx_arl_remove(
   245         struct cx_list_s *list,
   246         size_t index
   247 ) {
   248     /* out-of-bounds check */
   249     if (index >= list->size) {
   250         return 1;
   251     }
   253     cx_array_list *arl = (cx_array_list *) list;
   255     /* just move the elements starting at index to the left */
   256     int result = cx_array_copy(
   257             &arl->data,
   258             &list->size,
   259             &list->capacity,
   260             index,
   261             ((char *) arl->data) + (index + 1) * list->itemsize,
   262             list->itemsize,
   263             list->size - index,
   264             &arl->reallocator
   265     );
   266     if (result == 0) {
   267         /* decrease the size */
   268         list->size--;
   269     }
   270     return result;
   271 }
   273 static void *cx_arl_at(
   274         struct cx_list_s const *list,
   275         size_t index
   276 ) {
   277     if (index < list->size) {
   278         cx_array_list const *arl = (cx_array_list const *) list;
   279         char *space = arl->data;
   280         return space + index * list->itemsize;
   281     } else {
   282         return NULL;
   283     }
   284 }
   286 static size_t cx_arl_find(
   287         struct cx_list_s const *list,
   288         void const *elem
   289 ) {
   290     char *cur = ((cx_array_list const *) list)->data;
   292     for (size_t i = 0; i < list->size; i++) {
   293         if (0 == list->cmpfunc(elem, cur)) {
   294             return i;
   295         }
   296         cur += list->itemsize;
   297     }
   299     return list->size;
   300 }
   302 static void cx_arl_sort(struct cx_list_s *list) {
   303     qsort(((cx_array_list *) list)->data,
   304           list->size,
   305           list->itemsize,
   306           list->cmpfunc
   307     );
   308 }
   310 static int cx_arl_compare(
   311         struct cx_list_s const *list,
   312         struct cx_list_s const *other
   313 ) {
   314     if (list->size == other->size) {
   315         char const *left = ((cx_array_list const *) list)->data;
   316         char const *right = ((cx_array_list const *) other)->data;
   317         for (size_t i = 0; i < list->size; i++) {
   318             int d = list->cmpfunc(left, right);
   319             if (d != 0) {
   320                 return d;
   321             }
   322             left += list->itemsize;
   323             right += other->itemsize;
   324         }
   325         return 0;
   326     } else {
   327         return list->size < other->size ? -1 : 1;
   328     }
   329 }
   331 static void cx_arl_reverse(struct cx_list_s *list) {
   332     if (list->size < 2) return;
   333     void *data = ((cx_array_list const *) list)->data;
   334     size_t half = list->size / 2;
   335     for (size_t i = 0; i < half; i++) {
   336         cx_array_swap(data, list->itemsize, i, list->size - 1 - i);
   337     }
   338 }
   340 static bool cx_arl_iter_valid(struct cx_iterator_s const *iter) {
   341     struct cx_list_s const *list = iter->src_handle;
   342     return iter->index < list->size;
   343 }
   345 static void *cx_arl_iter_current(struct cx_iterator_s const *iter) {
   346     return iter->elem_handle;
   347 }
   349 static void cx_arl_iter_next(struct cx_iterator_s *iter) {
   350     if (iter->remove) {
   351         iter->remove = false;
   352         cx_arl_remove(iter->src_handle, iter->index);
   353     } else {
   354         iter->index++;
   355         iter->elem_handle =
   356                 ((char *) iter->elem_handle)
   357                 + ((struct cx_list_s const *) iter->src_handle)->itemsize;
   358     }
   359 }
   361 static struct cx_iterator_s cx_arl_iterator(
   362         struct cx_list_s *list,
   363         size_t index
   364 ) {
   365     struct cx_iterator_s iter;
   367     iter.index = index;
   368     iter.src_handle = list;
   369     iter.elem_handle = cx_arl_at(list, index);
   370     iter.valid = cx_arl_iter_valid;
   371     iter.current = cx_arl_iter_current;
   372     iter.next = cx_arl_iter_next;
   373     iter.remove = false;
   375     return iter;
   376 }
   378 static cx_list_class cx_array_list_class = {
   379         cx_arl_destructor,
   380         cx_arl_add,
   381         cx_arl_insert,
   382         cx_arl_insert_iter,
   383         cx_arl_remove,
   384         cx_arl_at,
   385         cx_arl_find,
   386         cx_arl_sort,
   387         cx_arl_compare,
   388         cx_arl_reverse,
   389         cx_arl_iterator,
   390 };
   392 CxList *cxArrayListCreate(
   393         CxAllocator const *allocator,
   394         CxListComparator comparator,
   395         size_t item_size,
   396         size_t initial_capacity
   397 ) {
   398     cx_array_list *list = cxCalloc(allocator, 1, sizeof(cx_array_list));
   399     if (list == NULL) return NULL;
   401     list->data = cxCalloc(allocator, initial_capacity, item_size);
   402     if (list->data == NULL) {
   403         cxFree(allocator, list);
   404         return NULL;
   405     }
   407     list->base.cl = &cx_array_list_class;
   408     list->base.allocator = allocator;
   409     list->base.cmpfunc = comparator;
   410     list->base.itemsize = item_size;
   411     list->base.capacity = initial_capacity;
   413     /* configure the reallocator */
   414     list->reallocator.realloc = cx_arl_realloc;
   415     list->reallocator.ptr1 = (void *) allocator;
   417     return (CxList *) list;
   418 }

mercurial