universe@398: /* universe@398: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@398: * universe@398: * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. universe@398: * universe@398: * Redistribution and use in source and binary forms, with or without universe@398: * modification, are permitted provided that the following conditions are met: universe@398: * universe@398: * 1. Redistributions of source code must retain the above copyright universe@398: * notice, this list of conditions and the following disclaimer. universe@398: * universe@398: * 2. Redistributions in binary form must reproduce the above copyright universe@398: * notice, this list of conditions and the following disclaimer in the universe@398: * documentation and/or other materials provided with the distribution. universe@398: * universe@398: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@398: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@398: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@398: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@398: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@398: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@398: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@398: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@398: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@398: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@398: * POSSIBILITY OF SUCH DAMAGE. universe@398: */ universe@398: universe@398: #include "cx/linked_list.h" universe@401: #include universe@401: #include universe@398: universe@398: /* LOW LEVEL LINKED LIST FUNCTIONS */ universe@398: universe@398: #define CX_LL_PTR(cur, off) ((void**)(((char*)cur)+off)) universe@398: universe@400: void *cx_linked_list_last(void **begin, void **end, ptrdiff_t loc_next) { universe@398: if (end != NULL) { universe@398: return *end; universe@398: } else { universe@398: if (begin == NULL || *begin == NULL) universe@398: return NULL; universe@398: universe@398: void *cur = *begin; universe@398: void *last; universe@398: do { universe@398: last = cur; universe@398: } while ((cur = *CX_LL_PTR(cur, loc_next)) != NULL); universe@398: universe@398: return last; universe@398: } universe@398: } universe@398: universe@406: int cx_linked_list_add(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node) { universe@398: void *last = cx_linked_list_last(begin, end, loc_next); universe@398: if (last == NULL) { universe@398: if (begin == NULL) { universe@428: // no current list and no begin ptr to write to - we don't find something to append to universe@398: return 1; universe@398: } else { universe@428: // start fresh list universe@406: *begin = new_node; universe@398: } universe@428: } else { universe@428: // if there is a last node, update its next pointer universe@428: void **next = CX_LL_PTR(last, loc_next); universe@428: *next = new_node; universe@398: } universe@398: universe@428: // if there is an end pointer, update it universe@408: if (end != NULL) { universe@408: *end = cx_linked_list_last(&new_node, NULL, loc_next); universe@408: } universe@428: universe@428: // if the nodes use a prev pointer, update it universe@398: if (loc_prev >= 0) { universe@406: void **prev = CX_LL_PTR(new_node, loc_prev); universe@398: *prev = last; universe@398: } universe@398: universe@398: return 0; universe@398: } universe@398: universe@398: /* HIGH LEVEL LINKED LIST IMPLEMENTATION */ universe@398: universe@402: struct cx_linked_list_node { universe@402: void *prev; universe@402: void *next; universe@403: char payload[]; universe@402: }; universe@402: universe@398: typedef struct { universe@435: cx_list_s base; universe@401: void *begin; universe@401: void *end; universe@406: ptrdiff_t loc_prev; universe@406: ptrdiff_t loc_next; universe@398: } cx_linked_list; universe@398: universe@435: int cx_ll_add(cx_list_s *list, void *elem) { universe@435: cx_linked_list *linkedlist = (cx_linked_list *) list; universe@398: universe@435: struct cx_linked_list_node *node = cxMalloc(list->allocator, universe@406: sizeof(struct cx_linked_list_node) + list->itemsize); universe@398: if (node == NULL) universe@398: return 1; universe@398: universe@435: node->next = node->prev = NULL; universe@406: memcpy(node->payload, elem, list->itemsize); universe@399: universe@398: int ret = cx_linked_list_add( universe@435: &linkedlist->begin, universe@435: &linkedlist->end, universe@402: offsetof(struct cx_linked_list_node, prev), universe@402: offsetof(struct cx_linked_list_node, next), universe@398: node universe@398: ); universe@398: if (ret == 0) { universe@401: list->size++; universe@398: return 0; universe@398: } else { universe@398: return ret; universe@398: } universe@398: } universe@398: universe@435: int cx_ll_insert(cx_list_s *list, size_t index, void *elem) { universe@435: cx_linked_list *linkedList = (cx_linked_list *) list; universe@398: // TODO: implement using low level API universe@398: return 1; universe@398: } universe@398: universe@435: void *cx_ll_remove(cx_list_s *list, size_t index) { universe@435: cx_linked_list *linkedList = (cx_linked_list *) list; universe@398: // TODO: implement using low level API universe@398: return NULL; universe@398: } universe@398: universe@435: size_t cx_ll_find(cx_list_s *list, void *elem) { universe@435: cx_linked_list *linkedList = (cx_linked_list *) list; universe@398: // TODO: implement using low level API universe@398: return 0; universe@398: } universe@398: universe@435: void *cx_ll_last(cx_list_s *list) { universe@435: cx_linked_list *linkedList = (cx_linked_list *) list; universe@404: struct cx_linked_list_node *last = cx_linked_list_last( universe@435: NULL, &linkedList->end, offsetof(struct cx_linked_list_node, next)); universe@404: return &last->payload; universe@404: } universe@398: universe@398: cx_list_class cx_linked_list_class = { universe@398: cx_ll_add, universe@398: cx_ll_insert, universe@398: cx_ll_remove, universe@404: cx_ll_find, universe@404: cx_ll_last universe@398: }; universe@398: universe@406: CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator, size_t item_size) { universe@435: cx_linked_list* list = cxMalloc(allocator, sizeof(cx_linked_list)); universe@398: if (list == NULL) universe@398: return NULL; universe@398: universe@435: list->base.cl = &cx_linked_list_class; universe@435: list->base.allocator = allocator; universe@435: list->base.cmpfunc = comparator; universe@435: list->base.itemsize = item_size; universe@435: list->base.capacity = SIZE_MAX; universe@406: universe@435: list->begin = NULL; universe@435: list->loc_prev = offsetof(struct cx_linked_list_node, prev); universe@435: list->loc_next = offsetof(struct cx_linked_list_node, next); universe@406: universe@435: CxList result = (CxList) list; universe@435: cxLinkedListRecalculateSize(result); universe@435: return result; universe@406: } universe@406: universe@409: void cxLinkedListDestroy(CxList list) { universe@436: cx_linked_list* ll = (cx_linked_list*) list; universe@436: universe@436: struct cx_linked_list_node* node = ll->begin; universe@436: while (node) { universe@436: void* next = node->next; universe@436: cxFree(list->allocator, node); universe@436: node = next; universe@436: } universe@436: universe@435: cxFree(list->allocator, list); universe@409: } universe@409: universe@406: size_t cxLinkedListRecalculateSize(CxList list) { universe@435: cx_linked_list *ll = (cx_linked_list *) list; universe@406: universe@406: if (ll->begin == NULL) { universe@435: ll->base.size = 0; universe@407: ll->end = NULL; universe@406: return 0; universe@406: } else { universe@406: void *cur = ll->begin; universe@407: void *last; universe@435: size_t size = 0; universe@406: do { universe@407: last = cur; universe@406: size++; universe@406: } while ((cur = *CX_LL_PTR(cur, ll->loc_next)) != NULL); universe@407: ll->end = last; universe@435: ll->base.size = size; universe@406: return size; universe@398: } universe@406: }