Sun, 07 Feb 2021 19:42:12 +0100
adds first draft for linked list implementation
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@398 | 30 | #include <stddef.h> |
universe@398 | 31 | |
universe@398 | 32 | /* LOW LEVEL LINKED LIST FUNCTIONS */ |
universe@398 | 33 | |
universe@398 | 34 | #define CX_LL_PTR(cur, off) ((void**)(((char*)cur)+off)) |
universe@398 | 35 | |
universe@398 | 36 | void *cx_linked_list_last(void **begin, void **end, off_t loc_next) { |
universe@398 | 37 | if (end != NULL) { |
universe@398 | 38 | return *end; |
universe@398 | 39 | } else { |
universe@398 | 40 | if (begin == NULL || *begin == NULL) |
universe@398 | 41 | return NULL; |
universe@398 | 42 | |
universe@398 | 43 | void *cur = *begin; |
universe@398 | 44 | void *last; |
universe@398 | 45 | do { |
universe@398 | 46 | last = cur; |
universe@398 | 47 | } while ((cur = *CX_LL_PTR(cur, loc_next)) != NULL); |
universe@398 | 48 | |
universe@398 | 49 | return last; |
universe@398 | 50 | } |
universe@398 | 51 | } |
universe@398 | 52 | |
universe@398 | 53 | int cx_linked_list_add(void **begin, void **end, off_t loc_next, off_t loc_prev, void *newnode) { |
universe@398 | 54 | // TODO: how do we report error messages? |
universe@398 | 55 | if (loc_next < 0 || (begin == NULL && end == NULL)) { |
universe@398 | 56 | return 1; |
universe@398 | 57 | } |
universe@398 | 58 | |
universe@398 | 59 | void *last = cx_linked_list_last(begin, end, loc_next); |
universe@398 | 60 | if (last == NULL) { |
universe@398 | 61 | if (begin == NULL) { |
universe@398 | 62 | return 1; |
universe@398 | 63 | } else { |
universe@398 | 64 | *begin = newnode; |
universe@398 | 65 | return 0; |
universe@398 | 66 | } |
universe@398 | 67 | } |
universe@398 | 68 | |
universe@398 | 69 | void **next = CX_LL_PTR(last, loc_next); |
universe@398 | 70 | *next = newnode; |
universe@398 | 71 | if (loc_prev >= 0) { |
universe@398 | 72 | void **prev = CX_LL_PTR(newnode, loc_prev); |
universe@398 | 73 | *prev = last; |
universe@398 | 74 | } |
universe@398 | 75 | |
universe@398 | 76 | return 0; |
universe@398 | 77 | } |
universe@398 | 78 | |
universe@398 | 79 | /* HIGH LEVEL LINKED LIST IMPLEMENTATION */ |
universe@398 | 80 | |
universe@398 | 81 | typedef struct cx_list_node_s cx_list_node; |
universe@398 | 82 | |
universe@398 | 83 | struct cx_list_node_s { |
universe@398 | 84 | cx_list_node *next; |
universe@398 | 85 | cx_list_node *prev; |
universe@398 | 86 | void *data; |
universe@398 | 87 | }; |
universe@398 | 88 | |
universe@398 | 89 | typedef struct { |
universe@398 | 90 | cx_list_node *begin; |
universe@398 | 91 | cx_list_node *end; |
universe@398 | 92 | size_t size; |
universe@398 | 93 | } cx_linked_list; |
universe@398 | 94 | |
universe@398 | 95 | int cx_ll_add(cx_list *list, void *elem) { |
universe@398 | 96 | cx_linked_list *listdata = list->listdata; |
universe@398 | 97 | CxAllocator allocator = list->allocator; |
universe@398 | 98 | |
universe@398 | 99 | struct cx_list_node_s *node = cxMalloc(allocator, sizeof(struct cx_list_node_s)); |
universe@398 | 100 | if (node == NULL) |
universe@398 | 101 | return 1; |
universe@398 | 102 | |
universe@398 | 103 | int ret = cx_linked_list_add( |
universe@398 | 104 | (void **) &listdata->begin, |
universe@398 | 105 | (void **) &listdata->end, |
universe@398 | 106 | offsetof(struct cx_list_node_s, next), |
universe@398 | 107 | offsetof(struct cx_list_node_s, prev), |
universe@398 | 108 | node |
universe@398 | 109 | ); |
universe@398 | 110 | if (ret == 0) { |
universe@398 | 111 | listdata->size++; |
universe@398 | 112 | return 0; |
universe@398 | 113 | } else { |
universe@398 | 114 | return ret; |
universe@398 | 115 | } |
universe@398 | 116 | } |
universe@398 | 117 | |
universe@398 | 118 | int cx_ll_insert(cx_list *list, size_t index, void *elem) { |
universe@398 | 119 | cx_linked_list *listdata = list->listdata; |
universe@398 | 120 | // TODO: implement using low level API |
universe@398 | 121 | return 1; |
universe@398 | 122 | } |
universe@398 | 123 | |
universe@398 | 124 | void *cx_ll_remove(cx_list *list, size_t index) { |
universe@398 | 125 | cx_linked_list *listdata = list->listdata; |
universe@398 | 126 | // TODO: implement using low level API |
universe@398 | 127 | return NULL; |
universe@398 | 128 | } |
universe@398 | 129 | |
universe@398 | 130 | size_t cx_ll_find(cx_list *list, void *elem) { |
universe@398 | 131 | cx_linked_list *listdata = list->listdata; |
universe@398 | 132 | // TODO: implement using low level API |
universe@398 | 133 | return 0; |
universe@398 | 134 | } |
universe@398 | 135 | |
universe@398 | 136 | size_t cx_ll_size(cx_list *list) { |
universe@398 | 137 | cx_linked_list *listdata = list->listdata; |
universe@398 | 138 | return listdata->size; |
universe@398 | 139 | } |
universe@398 | 140 | |
universe@398 | 141 | cx_list_class cx_linked_list_class = { |
universe@398 | 142 | cx_ll_add, |
universe@398 | 143 | cx_ll_insert, |
universe@398 | 144 | cx_ll_remove, |
universe@398 | 145 | cx_ll_find, |
universe@398 | 146 | cx_ll_size |
universe@398 | 147 | }; |
universe@398 | 148 | |
universe@398 | 149 | CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator) { |
universe@398 | 150 | CxList list = cxMalloc(allocator, sizeof(list)); |
universe@398 | 151 | if (list == NULL) |
universe@398 | 152 | return NULL; |
universe@398 | 153 | |
universe@398 | 154 | list->cl = &cx_linked_list_class; |
universe@398 | 155 | list->data.allocator = allocator; |
universe@398 | 156 | list->data.cmpfunc = comparator; |
universe@398 | 157 | list->data.listdata = cxCalloc(allocator, 1, sizeof(cx_linked_list)); |
universe@398 | 158 | if (list->data.listdata == NULL) { |
universe@398 | 159 | cxFree(allocator, list); |
universe@398 | 160 | return NULL; |
universe@398 | 161 | } |
universe@398 | 162 | } |