src/array_list.c

Thu, 17 Nov 2022 18:46:55 +0100

author
Mike Becker <universe@uap-core.de>
date
Thu, 17 Nov 2022 18:46:55 +0100
changeset 614
7aaec630cf15
parent 613
85c08391a090
child 615
b52b66dcd44b
permissions
-rw-r--r--

#219 array list: implement find

     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     return 1;
   187 }
   189 static int cx_arl_remove(
   190         struct cx_list_s *list,
   191         size_t index
   192 ) {
   193     /* out-of-bounds check */
   194     if (index >= list->size) {
   195         return 1;
   196     }
   198     cx_array_list *arl = (cx_array_list *) list;
   200     /* just move the elements starting at index to the left */
   201     int result = cx_array_copy(
   202             &arl->data,
   203             &list->size,
   204             &list->capacity,
   205             index,
   206             ((char *) arl->data) + (index + 1) * list->itemsize,
   207             list->itemsize,
   208             list->size - index,
   209             &arl->reallocator
   210     );
   211     if (result == 0) {
   212         /* decrease the size */
   213         list->size--;
   214     }
   215     return result;
   216 }
   218 static void *cx_arl_at(
   219         struct cx_list_s const *list,
   220         size_t index
   221 ) {
   222     if (index < list->size) {
   223         cx_array_list const *arl = (cx_array_list const *) list;
   224         char *space = arl->data;
   225         return space + index * list->itemsize;
   226     } else {
   227         return NULL;
   228     }
   229 }
   231 static size_t cx_arl_find(
   232         struct cx_list_s const *list,
   233         void const *elem
   234 ) {
   235     char *cur = ((cx_array_list const *) list)->data;
   237     for (size_t i = 0; i < list->size; i++) {
   238         if (0 == list->cmpfunc(elem, cur)) {
   239             return i;
   240         }
   241         cur += list->itemsize;
   242     }
   244     return list->size;
   245 }
   247 static void cx_arl_sort(struct cx_list_s *list) {
   249 }
   251 static int cx_arl_compare(
   252         struct cx_list_s const *list,
   253         struct cx_list_s const *other
   254 ) {
   256 }
   258 static void cx_arl_reverse(struct cx_list_s *list) {
   260 }
   262 static struct cx_iterator_s cx_arl_iterator(
   263         struct cx_list_s *list,
   264         size_t index
   265 ) {
   266     struct cx_iterator_s iter;
   268     return iter;
   269 }
   271 static cx_list_class cx_array_list_class = {
   272         cx_arl_destructor,
   273         cx_arl_add,
   274         cx_arl_insert,
   275         cx_arl_insert_iter,
   276         cx_arl_remove,
   277         cx_arl_at,
   278         cx_arl_find,
   279         cx_arl_sort,
   280         cx_arl_compare,
   281         cx_arl_reverse,
   282         cx_arl_iterator,
   283 };
   285 CxList *cxArrayListCreate(
   286         CxAllocator const *allocator,
   287         CxListComparator comparator,
   288         size_t item_size,
   289         size_t initial_capacity
   290 ) {
   291     cx_array_list *list = cxCalloc(allocator, 1, sizeof(cx_array_list));
   292     if (list == NULL) return NULL;
   294     list->data = cxCalloc(allocator, initial_capacity, item_size);
   295     if (list->data == NULL) {
   296         cxFree(allocator, list);
   297         return NULL;
   298     }
   300     list->base.cl = &cx_array_list_class;
   301     list->base.allocator = allocator;
   302     list->base.cmpfunc = comparator;
   303     list->base.itemsize = item_size;
   304     list->base.capacity = initial_capacity;
   306     /* configure the reallocator */
   307     list->reallocator.realloc = cx_arl_realloc;
   308     list->reallocator.ptr1 = (void *) allocator;
   310     return (CxList *) list;
   311 }

mercurial