src/array_list.c

Sun, 20 Nov 2022 16:21:03 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 20 Nov 2022 16:21:03 +0100
changeset 619
5e58187ac707
parent 616
af7d8a29fbc5
child 620
f220695aded6
permissions
-rw-r--r--

#219 array list: implement insert via iterator

     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 /* HIGH LEVEL ARRAY LIST FUNCTIONS */
   106 typedef struct {
   107     struct cx_list_s base;
   108     void *data;
   109     struct cx_array_reallocator_s reallocator;
   110 } cx_array_list;
   112 static void *cx_arl_realloc(
   113         void *array,
   114         size_t capacity,
   115         size_t elem_size,
   116         struct cx_array_reallocator_s *alloc
   117 ) {
   118     /* retrieve the pointer to the list allocator */
   119     CxAllocator const *al = alloc->ptr1;
   121     /* use the list allocator to reallocate the memory */
   122     return cxRealloc(al, array, capacity * elem_size);
   123 }
   125 static void cx_arl_destructor(struct cx_list_s *list) {
   126     cx_array_list *arl = (cx_array_list *) list;
   127     cxFree(list->allocator, arl->data);
   128 }
   130 static int cx_arl_add(
   131         struct cx_list_s *list,
   132         void const *elem
   133 ) {
   134     cx_array_list *arl = (cx_array_list *) list;
   135     return cx_array_copy(
   136             &arl->data,
   137             &list->size,
   138             &list->capacity,
   139             list->size,
   140             elem,
   141             list->itemsize,
   142             1,
   143             &arl->reallocator
   144     );
   145 }
   147 static int cx_arl_insert(
   148         struct cx_list_s *list,
   149         size_t index,
   150         void const *elem
   151 ) {
   152     if (index > list->size) {
   153         return 1;
   154     } else if (index == list->size) {
   155         return cx_arl_add(list, elem);
   156     } else {
   157         cx_array_list *arl = (cx_array_list *) list;
   159         /* move elements starting at index to the right */
   160         if (cx_array_copy(
   161                 &arl->data,
   162                 &list->size,
   163                 &list->capacity,
   164                 index + 1,
   165                 ((char *) arl->data) + index * list->itemsize,
   166                 list->itemsize,
   167                 list->size - index,
   168                 &arl->reallocator
   169         )) {
   170             return 1;
   171         }
   173         /* place the element */
   174         memcpy(((char *) arl->data) + index * list->itemsize,
   175                elem, list->itemsize);
   177         return 0;
   178     }
   179 }
   181 static int cx_arl_insert_iter(
   182         struct cx_iterator_s *iter,
   183         void const *elem,
   184         int prepend
   185 ) {
   186     struct cx_list_s *list = iter->src_handle;
   187     if (iter->index < list->size) {
   188         int result = cx_arl_insert(
   189                 list,
   190                 iter->index + 1 - prepend,
   191                 elem
   192         );
   193         if (result == 0 && prepend != 0) {
   194             iter->index++;
   195             iter->elem_handle = ((char *) iter->elem_handle) + list->itemsize;
   196         }
   197         return result;
   198     } else {
   199         int result = cx_arl_add(list, elem);
   200         iter->index = list->size;
   201         return result;
   202     }
   203 }
   205 static int cx_arl_remove(
   206         struct cx_list_s *list,
   207         size_t index
   208 ) {
   209     /* out-of-bounds check */
   210     if (index >= list->size) {
   211         return 1;
   212     }
   214     cx_array_list *arl = (cx_array_list *) list;
   216     /* just move the elements starting at index to the left */
   217     int result = cx_array_copy(
   218             &arl->data,
   219             &list->size,
   220             &list->capacity,
   221             index,
   222             ((char *) arl->data) + (index + 1) * list->itemsize,
   223             list->itemsize,
   224             list->size - index,
   225             &arl->reallocator
   226     );
   227     if (result == 0) {
   228         /* decrease the size */
   229         list->size--;
   230     }
   231     return result;
   232 }
   234 static void *cx_arl_at(
   235         struct cx_list_s const *list,
   236         size_t index
   237 ) {
   238     if (index < list->size) {
   239         cx_array_list const *arl = (cx_array_list const *) list;
   240         char *space = arl->data;
   241         return space + index * list->itemsize;
   242     } else {
   243         return NULL;
   244     }
   245 }
   247 static size_t cx_arl_find(
   248         struct cx_list_s const *list,
   249         void const *elem
   250 ) {
   251     char *cur = ((cx_array_list const *) list)->data;
   253     for (size_t i = 0; i < list->size; i++) {
   254         if (0 == list->cmpfunc(elem, cur)) {
   255             return i;
   256         }
   257         cur += list->itemsize;
   258     }
   260     return list->size;
   261 }
   263 static void cx_arl_sort(struct cx_list_s *list) {
   264     qsort(((cx_array_list *) list)->data,
   265           list->size,
   266           list->itemsize,
   267           list->cmpfunc
   268     );
   269 }
   271 static int cx_arl_compare(
   272         struct cx_list_s const *list,
   273         struct cx_list_s const *other
   274 ) {
   276 }
   278 static void cx_arl_reverse(struct cx_list_s *list) {
   280 }
   282 static bool cx_arl_iter_valid(struct cx_iterator_s const *iter) {
   283     struct cx_list_s const *list = iter->src_handle;
   284     return iter->index < list->size;
   285 }
   287 static void *cx_arl_iter_current(struct cx_iterator_s const *iter) {
   288     return iter->elem_handle;
   289 }
   291 static void cx_arl_iter_next(struct cx_iterator_s *iter) {
   292     if (iter->remove) {
   293         iter->remove = false;
   294         cx_arl_remove(iter->src_handle, iter->index);
   295     } else {
   296         iter->index++;
   297         iter->elem_handle = cx_arl_at(iter->src_handle, iter->index);
   298     }
   299 }
   301 static struct cx_iterator_s cx_arl_iterator(
   302         struct cx_list_s *list,
   303         size_t index
   304 ) {
   305     struct cx_iterator_s iter;
   307     iter.index = index;
   308     iter.src_handle = list;
   309     iter.elem_handle = cx_arl_at(list, index);
   310     iter.valid = cx_arl_iter_valid;
   311     iter.current = cx_arl_iter_current;
   312     iter.next = cx_arl_iter_next;
   313     iter.remove = false;
   315     return iter;
   316 }
   318 static cx_list_class cx_array_list_class = {
   319         cx_arl_destructor,
   320         cx_arl_add,
   321         cx_arl_insert,
   322         cx_arl_insert_iter,
   323         cx_arl_remove,
   324         cx_arl_at,
   325         cx_arl_find,
   326         cx_arl_sort,
   327         cx_arl_compare,
   328         cx_arl_reverse,
   329         cx_arl_iterator,
   330 };
   332 CxList *cxArrayListCreate(
   333         CxAllocator const *allocator,
   334         CxListComparator comparator,
   335         size_t item_size,
   336         size_t initial_capacity
   337 ) {
   338     cx_array_list *list = cxCalloc(allocator, 1, sizeof(cx_array_list));
   339     if (list == NULL) return NULL;
   341     list->data = cxCalloc(allocator, initial_capacity, item_size);
   342     if (list->data == NULL) {
   343         cxFree(allocator, list);
   344         return NULL;
   345     }
   347     list->base.cl = &cx_array_list_class;
   348     list->base.allocator = allocator;
   349     list->base.cmpfunc = comparator;
   350     list->base.itemsize = item_size;
   351     list->base.capacity = initial_capacity;
   353     /* configure the reallocator */
   354     list->reallocator.realloc = cx_arl_realloc;
   355     list->reallocator.ptr1 = (void *) allocator;
   357     return (CxList *) list;
   358 }

mercurial