universe@606: /* universe@606: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@606: * universe@606: * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. universe@606: * universe@606: * Redistribution and use in source and binary forms, with or without universe@606: * modification, are permitted provided that the following conditions are met: universe@606: * universe@606: * 1. Redistributions of source code must retain the above copyright universe@606: * notice, this list of conditions and the following disclaimer. universe@606: * universe@606: * 2. Redistributions in binary form must reproduce the above copyright universe@606: * notice, this list of conditions and the following disclaimer in the universe@606: * documentation and/or other materials provided with the distribution. universe@606: * universe@606: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@606: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@606: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@606: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@606: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@606: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@606: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@606: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@606: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@606: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@606: * POSSIBILITY OF SUCH DAMAGE. universe@606: */ universe@606: universe@606: #include "cx/array_list.h" universe@610: #include universe@610: #include universe@611: #include universe@606: universe@607: /* LOW LEVEL ARRAY LIST FUNCTIONS */ universe@607: universe@612: enum cx_array_copy_result cx_array_copy( universe@610: void **target, universe@610: size_t *size, universe@610: size_t *capacity, universe@610: size_t index, universe@610: void const *src, universe@610: size_t elem_size, universe@610: size_t elem_count, universe@610: struct cx_array_reallocator_s *reallocator universe@610: ) { universe@610: /* assert pointers */ universe@610: assert(target != NULL); universe@610: assert(size != NULL); universe@610: assert(src != NULL); universe@607: universe@610: /* determine capacity */ universe@610: size_t cap = capacity == NULL ? *size : *capacity; universe@610: universe@610: /* check if resize is required */ universe@610: size_t newsize = index + elem_count; universe@610: bool needrealloc = newsize > cap; universe@610: universe@610: /* reallocate if possible */ universe@610: if (needrealloc) { universe@610: /* a reallocator and a capacity variable must be available */ universe@610: if (reallocator == NULL || capacity == NULL) { universe@610: return CX_ARRAY_COPY_REALLOC_NOT_SUPPORTED; universe@610: } universe@610: universe@611: /* check, if we need to repair the src pointer */ universe@611: uintptr_t targetaddr = (uintptr_t) *target; universe@611: uintptr_t srcaddr = (uintptr_t) src; universe@611: bool repairsrc = targetaddr <= srcaddr universe@611: && srcaddr < targetaddr + cap * elem_size; universe@611: universe@610: /* increase capacity linearly */ universe@610: cap += 16; universe@610: universe@610: /* perform reallocation */ universe@610: void *newmem = reallocator->realloc( universe@610: *target, cap, elem_size, reallocator universe@610: ); universe@610: if (newmem == NULL) { universe@610: return CX_ARRAY_COPY_REALLOC_FAILED; universe@610: } universe@610: universe@611: /* repair src pointer, if necessary */ universe@611: if (repairsrc) { universe@611: src = ((char *) newmem) + (srcaddr - targetaddr); universe@611: } universe@611: universe@610: /* store new pointer and capacity */ universe@610: *target = newmem; universe@610: *capacity = cap; universe@610: } universe@610: universe@610: /* determine target pointer */ universe@610: char *start = *target; universe@610: start += index * elem_size; universe@610: universe@610: /* copy elements and set new size */ universe@611: memmove(start, src, elem_count * elem_size); universe@610: *size = newsize; universe@610: universe@610: /* return successfully */ universe@610: return CX_ARRAY_COPY_SUCCESS; universe@610: } universe@607: universe@607: /* HIGH LEVEL ARRAY LIST FUNCTIONS */ universe@607: universe@607: typedef struct { universe@607: struct cx_list_s base; universe@607: void *data; universe@610: struct cx_array_reallocator_s reallocator; universe@607: } cx_array_list; universe@607: universe@610: static void *cx_arl_realloc( universe@610: void *array, universe@610: size_t capacity, universe@610: size_t elem_size, universe@610: struct cx_array_reallocator_s *alloc universe@610: ) { universe@610: /* retrieve the pointer to the list allocator */ universe@610: CxAllocator const *al = alloc->ptr1; universe@610: universe@610: /* use the list allocator to reallocate the memory */ universe@610: return cxRealloc(al, array, capacity * elem_size); universe@610: } universe@610: universe@607: static void cx_arl_destructor(struct cx_list_s *list) { universe@610: cx_array_list *arl = (cx_array_list *) list; universe@607: cxFree(list->allocator, arl->data); universe@607: } universe@607: universe@607: static int cx_arl_add( universe@607: struct cx_list_s *list, universe@607: void const *elem universe@607: ) { universe@610: cx_array_list *arl = (cx_array_list *) list; universe@610: return cx_array_copy( universe@610: &arl->data, universe@610: &list->size, universe@610: &list->capacity, universe@610: list->size, universe@610: elem, universe@610: list->itemsize, universe@610: 1, universe@610: &arl->reallocator universe@610: ); universe@607: } universe@607: universe@607: static int cx_arl_insert( universe@607: struct cx_list_s *list, universe@607: size_t index, universe@607: void const *elem universe@607: ) { universe@611: if (index > list->size) { universe@611: return 1; universe@611: } else if (index == list->size) { universe@611: return cx_arl_add(list, elem); universe@611: } else { universe@611: cx_array_list *arl = (cx_array_list *) list; universe@611: universe@611: /* move elements starting at index to the right */ universe@611: if (cx_array_copy( universe@611: &arl->data, universe@611: &list->size, universe@611: &list->capacity, universe@611: index + 1, universe@611: ((char *) arl->data) + index * list->itemsize, universe@611: list->itemsize, universe@611: list->size - index, universe@611: &arl->reallocator universe@611: )) { universe@611: return 1; universe@611: } universe@611: universe@611: /* place the element */ universe@611: memcpy(((char *) arl->data) + index * list->itemsize, universe@611: elem, list->itemsize); universe@611: universe@611: return 0; universe@611: } universe@607: } universe@607: universe@607: static int cx_arl_insert_iter( universe@607: struct cx_iterator_s *iter, universe@607: void const *elem, universe@607: int prepend universe@607: ) { universe@607: return 1; universe@607: } universe@607: universe@607: static int cx_arl_remove( universe@607: struct cx_list_s *list, universe@607: size_t index universe@607: ) { universe@613: /* out-of-bounds check */ universe@613: if (index >= list->size) { universe@613: return 1; universe@613: } universe@613: universe@613: cx_array_list *arl = (cx_array_list *) list; universe@613: universe@613: /* just move the elements starting at index to the left */ universe@613: int result = cx_array_copy( universe@613: &arl->data, universe@613: &list->size, universe@613: &list->capacity, universe@613: index, universe@613: ((char *) arl->data) + (index + 1) * list->itemsize, universe@613: list->itemsize, universe@613: list->size - index, universe@613: &arl->reallocator universe@613: ); universe@613: if (result == 0) { universe@613: /* decrease the size */ universe@613: list->size--; universe@613: } universe@613: return result; universe@607: } universe@607: universe@610: static void *cx_arl_at( universe@607: struct cx_list_s const *list, universe@607: size_t index universe@607: ) { universe@610: if (index < list->size) { universe@610: cx_array_list const *arl = (cx_array_list const *) list; universe@610: char *space = arl->data; universe@610: return space + index * list->itemsize; universe@610: } else { universe@610: return NULL; universe@610: } universe@607: } universe@607: universe@607: static size_t cx_arl_find( universe@607: struct cx_list_s const *list, universe@607: void const *elem universe@607: ) { universe@607: return 0; universe@607: } universe@607: universe@607: static void cx_arl_sort(struct cx_list_s *list) { universe@607: universe@607: } universe@607: universe@607: static int cx_arl_compare( universe@607: struct cx_list_s const *list, universe@607: struct cx_list_s const *other universe@607: ) { universe@607: universe@607: } universe@607: universe@607: static void cx_arl_reverse(struct cx_list_s *list) { universe@607: universe@607: } universe@607: universe@607: static struct cx_iterator_s cx_arl_iterator( universe@607: struct cx_list_s *list, universe@607: size_t index universe@607: ) { universe@607: struct cx_iterator_s iter; universe@607: universe@607: return iter; universe@607: } universe@607: universe@607: static cx_list_class cx_array_list_class = { universe@607: cx_arl_destructor, universe@607: cx_arl_add, universe@607: cx_arl_insert, universe@607: cx_arl_insert_iter, universe@607: cx_arl_remove, universe@607: cx_arl_at, universe@607: cx_arl_find, universe@607: cx_arl_sort, universe@607: cx_arl_compare, universe@607: cx_arl_reverse, universe@607: cx_arl_iterator, universe@607: }; universe@607: universe@606: CxList *cxArrayListCreate( universe@606: CxAllocator const *allocator, universe@606: CxListComparator comparator, universe@606: size_t item_size, universe@606: size_t initial_capacity universe@606: ) { universe@607: cx_array_list *list = cxCalloc(allocator, 1, sizeof(cx_array_list)); universe@607: if (list == NULL) return NULL; universe@607: universe@607: list->data = cxCalloc(allocator, initial_capacity, item_size); universe@607: if (list->data == NULL) { universe@607: cxFree(allocator, list); universe@607: return NULL; universe@607: } universe@607: universe@607: list->base.cl = &cx_array_list_class; universe@607: list->base.allocator = allocator; universe@607: list->base.cmpfunc = comparator; universe@607: list->base.itemsize = item_size; universe@607: list->base.capacity = initial_capacity; universe@607: universe@610: /* configure the reallocator */ universe@610: list->reallocator.realloc = cx_arl_realloc; universe@610: list->reallocator.ptr1 = (void *) allocator; universe@610: universe@607: return (CxList *) list; universe@606: }