src/linked_list.c

Mon, 27 Sep 2021 18:33:30 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 27 Sep 2021 18:33:30 +0200
changeset 438
cd3069757010
parent 437
9d4971ea0625
child 439
9a5adedd6de6
permissions
-rw-r--r--

add function cx_linked_list_at()

This commit also makes glue functions static.

universe@398 1 /*
universe@398 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@398 3 *
universe@398 4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
universe@398 5 *
universe@398 6 * Redistribution and use in source and binary forms, with or without
universe@398 7 * modification, are permitted provided that the following conditions are met:
universe@398 8 *
universe@398 9 * 1. Redistributions of source code must retain the above copyright
universe@398 10 * notice, this list of conditions and the following disclaimer.
universe@398 11 *
universe@398 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@398 13 * notice, this list of conditions and the following disclaimer in the
universe@398 14 * documentation and/or other materials provided with the distribution.
universe@398 15 *
universe@398 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@398 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@398 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@398 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@398 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@398 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@398 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@398 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@398 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@398 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@398 26 * POSSIBILITY OF SUCH DAMAGE.
universe@398 27 */
universe@398 28
universe@398 29 #include "cx/linked_list.h"
universe@401 30 #include <stdint.h>
universe@401 31 #include <string.h>
universe@398 32
universe@398 33 /* LOW LEVEL LINKED LIST FUNCTIONS */
universe@398 34
universe@398 35 #define CX_LL_PTR(cur, off) ((void**)(((char*)cur)+off))
universe@398 36
universe@438 37 void *cx_linked_list_at(void *start, size_t start_index, ptrdiff_t loc_advance, size_t index) {
universe@438 38 size_t i = start_index;
universe@438 39 void* cur = start;
universe@438 40 while (i != index && cur != NULL) {
universe@438 41 cur = *CX_LL_PTR(cur, loc_advance);
universe@438 42 i < index ? i++ : i--;
universe@438 43 }
universe@438 44 return cur;
universe@438 45 }
universe@438 46
universe@400 47 void *cx_linked_list_last(void **begin, void **end, ptrdiff_t loc_next) {
universe@398 48 if (end != NULL) {
universe@398 49 return *end;
universe@398 50 } else {
universe@398 51 if (begin == NULL || *begin == NULL)
universe@398 52 return NULL;
universe@398 53
universe@398 54 void *cur = *begin;
universe@398 55 void *last;
universe@398 56 do {
universe@398 57 last = cur;
universe@398 58 } while ((cur = *CX_LL_PTR(cur, loc_next)) != NULL);
universe@398 59
universe@398 60 return last;
universe@398 61 }
universe@398 62 }
universe@398 63
universe@406 64 int cx_linked_list_add(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node) {
universe@398 65 void *last = cx_linked_list_last(begin, end, loc_next);
universe@398 66 if (last == NULL) {
universe@398 67 if (begin == NULL) {
universe@428 68 // no current list and no begin ptr to write to - we don't find something to append to
universe@398 69 return 1;
universe@398 70 } else {
universe@428 71 // start fresh list
universe@406 72 *begin = new_node;
universe@398 73 }
universe@428 74 } else {
universe@428 75 // if there is a last node, update its next pointer
universe@428 76 void **next = CX_LL_PTR(last, loc_next);
universe@428 77 *next = new_node;
universe@398 78 }
universe@398 79
universe@428 80 // if there is an end pointer, update it
universe@408 81 if (end != NULL) {
universe@408 82 *end = cx_linked_list_last(&new_node, NULL, loc_next);
universe@408 83 }
universe@428 84
universe@428 85 // if the nodes use a prev pointer, update it
universe@398 86 if (loc_prev >= 0) {
universe@406 87 void **prev = CX_LL_PTR(new_node, loc_prev);
universe@398 88 *prev = last;
universe@398 89 }
universe@398 90
universe@398 91 return 0;
universe@398 92 }
universe@398 93
universe@398 94 /* HIGH LEVEL LINKED LIST IMPLEMENTATION */
universe@398 95
universe@402 96 struct cx_linked_list_node {
universe@402 97 void *prev;
universe@402 98 void *next;
universe@403 99 char payload[];
universe@402 100 };
universe@402 101
universe@398 102 typedef struct {
universe@435 103 cx_list_s base;
universe@401 104 void *begin;
universe@401 105 void *end;
universe@406 106 ptrdiff_t loc_prev;
universe@406 107 ptrdiff_t loc_next;
universe@398 108 } cx_linked_list;
universe@398 109
universe@438 110 static int cx_ll_add(cx_list_s *list, void *elem) {
universe@437 111 cx_linked_list *ll = (cx_linked_list *) list;
universe@398 112
universe@435 113 struct cx_linked_list_node *node = cxMalloc(list->allocator,
universe@406 114 sizeof(struct cx_linked_list_node) + list->itemsize);
universe@398 115 if (node == NULL)
universe@398 116 return 1;
universe@398 117
universe@435 118 node->next = node->prev = NULL;
universe@406 119 memcpy(node->payload, elem, list->itemsize);
universe@399 120
universe@398 121 int ret = cx_linked_list_add(
universe@437 122 &ll->begin, &ll->end,
universe@402 123 offsetof(struct cx_linked_list_node, prev),
universe@402 124 offsetof(struct cx_linked_list_node, next),
universe@398 125 node
universe@398 126 );
universe@398 127 if (ret == 0) {
universe@401 128 list->size++;
universe@398 129 return 0;
universe@398 130 } else {
universe@398 131 return ret;
universe@398 132 }
universe@398 133 }
universe@398 134
universe@438 135 static int cx_ll_insert(cx_list_s *list, size_t index, void *elem) {
universe@437 136 cx_linked_list *ll = (cx_linked_list *) list;
universe@398 137 // TODO: implement using low level API
universe@398 138 return 1;
universe@398 139 }
universe@398 140
universe@438 141 static int cx_ll_remove(cx_list_s *list, size_t index) {
universe@438 142 if (index >= list->size) {
universe@438 143 return 1;
universe@438 144 }
universe@437 145 cx_linked_list *ll = (cx_linked_list *) list;
universe@398 146 // TODO: implement using low level API
universe@438 147 return 0;
universe@398 148 }
universe@398 149
universe@438 150 static size_t cx_ll_find(cx_list_s *list, void *elem) {
universe@437 151 CxListComparator cmp = list->cmpfunc;
universe@437 152 cx_linked_list *ll = (cx_linked_list *) list;
universe@437 153
universe@437 154 size_t index;
universe@437 155 struct cx_linked_list_node* node = ll->begin;
universe@437 156 for (index = 0 ; index < list->size ; index++) {
universe@437 157 void* current = node->payload;
universe@437 158 if (cmp(current, elem) == 0) {
universe@437 159 return index;
universe@437 160 }
universe@437 161 node = node->next;
universe@437 162 }
universe@437 163 return index;
universe@398 164 }
universe@398 165
universe@438 166 static void *cx_ll_last(cx_list_s *list) {
universe@435 167 cx_linked_list *linkedList = (cx_linked_list *) list;
universe@404 168 struct cx_linked_list_node *last = cx_linked_list_last(
universe@435 169 NULL, &linkedList->end, offsetof(struct cx_linked_list_node, next));
universe@404 170 return &last->payload;
universe@404 171 }
universe@398 172
universe@398 173 cx_list_class cx_linked_list_class = {
universe@398 174 cx_ll_add,
universe@398 175 cx_ll_insert,
universe@398 176 cx_ll_remove,
universe@404 177 cx_ll_find,
universe@404 178 cx_ll_last
universe@398 179 };
universe@398 180
universe@406 181 CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator, size_t item_size) {
universe@435 182 cx_linked_list* list = cxMalloc(allocator, sizeof(cx_linked_list));
universe@398 183 if (list == NULL)
universe@398 184 return NULL;
universe@398 185
universe@435 186 list->base.cl = &cx_linked_list_class;
universe@435 187 list->base.allocator = allocator;
universe@435 188 list->base.cmpfunc = comparator;
universe@435 189 list->base.itemsize = item_size;
universe@435 190 list->base.capacity = SIZE_MAX;
universe@406 191
universe@435 192 list->begin = NULL;
universe@435 193 list->loc_prev = offsetof(struct cx_linked_list_node, prev);
universe@435 194 list->loc_next = offsetof(struct cx_linked_list_node, next);
universe@406 195
universe@435 196 CxList result = (CxList) list;
universe@435 197 cxLinkedListRecalculateSize(result);
universe@435 198 return result;
universe@406 199 }
universe@406 200
universe@409 201 void cxLinkedListDestroy(CxList list) {
universe@436 202 cx_linked_list* ll = (cx_linked_list*) list;
universe@436 203
universe@436 204 struct cx_linked_list_node* node = ll->begin;
universe@436 205 while (node) {
universe@436 206 void* next = node->next;
universe@436 207 cxFree(list->allocator, node);
universe@436 208 node = next;
universe@436 209 }
universe@436 210
universe@435 211 cxFree(list->allocator, list);
universe@409 212 }
universe@409 213
universe@406 214 size_t cxLinkedListRecalculateSize(CxList list) {
universe@435 215 cx_linked_list *ll = (cx_linked_list *) list;
universe@406 216
universe@406 217 if (ll->begin == NULL) {
universe@435 218 ll->base.size = 0;
universe@407 219 ll->end = NULL;
universe@406 220 return 0;
universe@406 221 } else {
universe@406 222 void *cur = ll->begin;
universe@407 223 void *last;
universe@435 224 size_t size = 0;
universe@406 225 do {
universe@407 226 last = cur;
universe@406 227 size++;
universe@406 228 } while ((cur = *CX_LL_PTR(cur, ll->loc_next)) != NULL);
universe@407 229 ll->end = last;
universe@435 230 ll->base.size = size;
universe@406 231 return size;
universe@398 232 }
universe@406 233 }

mercurial