Mon, 08 Feb 2021 00:14:07 +0100
adds cxLinkedListWrap and cxLinkedListRecalculateSize
src/cx/linked_list.h | file | annotate | diff | comparison | revisions | |
src/cx/list.h | file | annotate | diff | comparison | revisions | |
src/linked_list.c | file | annotate | diff | comparison | revisions |
1.1 --- a/src/cx/linked_list.h Sun Feb 07 21:29:51 2021 +0100 1.2 +++ b/src/cx/linked_list.h Mon Feb 08 00:14:07 2021 +0100 1.3 @@ -34,10 +34,22 @@ 1.4 1.5 void *cx_linked_list_last(void **begin, void **end, ptrdiff_t loc_next); 1.6 1.7 -int cx_linked_list_add(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *newnode); 1.8 - 1.9 -CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator, size_t itemsize); 1.10 +int cx_linked_list_add(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node); 1.11 1.12 extern cx_list_class cx_linked_list_class; 1.13 1.14 +typedef struct { 1.15 + void **begin; 1.16 + void **end; 1.17 + ptrdiff_t loc_prev; 1.18 + ptrdiff_t loc_next; 1.19 + size_t item_size; 1.20 +} CxLinkedListDesc; 1.21 + 1.22 +CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator, size_t item_size); 1.23 + 1.24 +CxList cxLinkedListWrap(CxAllocator allocator, CxListComparator comparator, CxLinkedListDesc desc); 1.25 + 1.26 +size_t cxLinkedListRecalculateSize(CxList list); 1.27 + 1.28 #endif /* UCX_LINKED_LIST_H */
2.1 --- a/src/cx/list.h Sun Feb 07 21:29:51 2021 +0100 2.2 +++ b/src/cx/list.h Mon Feb 08 00:14:07 2021 +0100 2.3 @@ -40,7 +40,7 @@ 2.4 size_t itemsize; 2.5 size_t size; 2.6 size_t capacity; 2.7 - void *listdata; 2.8 + char listdata[]; 2.9 } cx_list; 2.10 2.11 typedef struct {
3.1 --- a/src/linked_list.c Sun Feb 07 21:29:51 2021 +0100 3.2 +++ b/src/linked_list.c Mon Feb 08 00:14:07 2021 +0100 3.3 @@ -51,7 +51,7 @@ 3.4 } 3.5 } 3.6 3.7 -int cx_linked_list_add(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *newnode) { 3.8 +int cx_linked_list_add(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node) { 3.9 // TODO: how do we report error messages? 3.10 if (loc_next < 0 || (begin == NULL && end == NULL)) { 3.11 return 1; 3.12 @@ -62,15 +62,15 @@ 3.13 if (begin == NULL) { 3.14 return 1; 3.15 } else { 3.16 - *begin = newnode; 3.17 + *begin = new_node; 3.18 return 0; 3.19 } 3.20 } 3.21 3.22 void **next = CX_LL_PTR(last, loc_next); 3.23 - *next = newnode; 3.24 + *next = new_node; 3.25 if (loc_prev >= 0) { 3.26 - void **prev = CX_LL_PTR(newnode, loc_prev); 3.27 + void **prev = CX_LL_PTR(new_node, loc_prev); 3.28 *prev = last; 3.29 } 3.30 3.31 @@ -88,19 +88,21 @@ 3.32 typedef struct { 3.33 void *begin; 3.34 void *end; 3.35 + ptrdiff_t loc_prev; 3.36 + ptrdiff_t loc_next; 3.37 } cx_linked_list; 3.38 3.39 int cx_ll_add(cx_list *list, void *elem) { 3.40 - cx_linked_list *listdata = list->listdata; 3.41 + cx_linked_list *listdata = (cx_linked_list *) list->listdata; 3.42 CxAllocator allocator = list->allocator; 3.43 3.44 struct cx_linked_list_node *node = cxMalloc(allocator, 3.45 - sizeof(struct cx_linked_list_node) + list->itemsize); 3.46 + sizeof(struct cx_linked_list_node) + list->itemsize); 3.47 if (node == NULL) 3.48 return 1; 3.49 3.50 node->next = NULL; 3.51 - memcpy(&node->payload, elem, list->itemsize); 3.52 + memcpy(node->payload, elem, list->itemsize); 3.53 3.54 int ret = cx_linked_list_add( 3.55 &listdata->begin, 3.56 @@ -118,27 +120,27 @@ 3.57 } 3.58 3.59 int cx_ll_insert(cx_list *list, size_t index, void *elem) { 3.60 - cx_linked_list *listdata = list->listdata; 3.61 + cx_linked_list *listdata = (cx_linked_list *) list->listdata; 3.62 // TODO: implement using low level API 3.63 return 1; 3.64 } 3.65 3.66 void *cx_ll_remove(cx_list *list, size_t index) { 3.67 - cx_linked_list *listdata = list->listdata; 3.68 + cx_linked_list *listdata = (cx_linked_list *) list->listdata; 3.69 // TODO: implement using low level API 3.70 return NULL; 3.71 } 3.72 3.73 size_t cx_ll_find(cx_list *list, void *elem) { 3.74 - cx_linked_list *listdata = list->listdata; 3.75 + cx_linked_list *listdata = (cx_linked_list *) list->listdata; 3.76 // TODO: implement using low level API 3.77 return 0; 3.78 } 3.79 3.80 void *cx_ll_last(cx_list *list) { 3.81 - cx_linked_list *ll = list->listdata; 3.82 + cx_linked_list *listdata = (cx_linked_list *) list->listdata; 3.83 struct cx_linked_list_node *last = cx_linked_list_last( 3.84 - NULL, &ll->end, offsetof(struct cx_linked_list_node, next)); 3.85 + NULL, &listdata->end, offsetof(struct cx_linked_list_node, next)); 3.86 return &last->payload; 3.87 } 3.88 3.89 @@ -150,20 +152,50 @@ 3.90 cx_ll_last 3.91 }; 3.92 3.93 -CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator, size_t itemsize) { 3.94 - CxList list = cxMalloc(allocator, sizeof(list)); 3.95 +CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator, size_t item_size) { 3.96 + CxLinkedListDesc desc; 3.97 + desc.item_size = item_size; 3.98 + desc.begin = desc.end = NULL; 3.99 + desc.loc_prev = offsetof(struct cx_linked_list_node, prev); 3.100 + desc.loc_next = offsetof(struct cx_linked_list_node, next); 3.101 + 3.102 + return cxLinkedListWrap(allocator, comparator, desc); 3.103 +} 3.104 + 3.105 +CxList cxLinkedListWrap(CxAllocator allocator, CxListComparator comparator, CxLinkedListDesc desc) { 3.106 + CxList list = cxMalloc(allocator, sizeof(list) + sizeof(cx_linked_list)); 3.107 if (list == NULL) 3.108 return NULL; 3.109 3.110 list->cl = &cx_linked_list_class; 3.111 list->data.allocator = allocator; 3.112 list->data.cmpfunc = comparator; 3.113 - list->data.size = 0; 3.114 - list->data.itemsize = itemsize; 3.115 + list->data.itemsize = desc.item_size; 3.116 list->data.capacity = SIZE_MAX; 3.117 - list->data.listdata = cxCalloc(allocator, 1, sizeof(cx_linked_list)); 3.118 - if (list->data.listdata == NULL) { 3.119 - cxFree(allocator, list); 3.120 - return NULL; 3.121 + 3.122 + cx_linked_list *ll = (cx_linked_list *) list->data.listdata; 3.123 + ll->begin = desc.begin ? *desc.begin : NULL; 3.124 + ll->end = desc.end ? *desc.end : NULL; 3.125 + ll->loc_prev = desc.loc_prev; 3.126 + ll->loc_next = desc.loc_next; 3.127 + cxLinkedListRecalculateSize(list); 3.128 + 3.129 + return list; 3.130 +} 3.131 + 3.132 +size_t cxLinkedListRecalculateSize(CxList list) { 3.133 + cx_linked_list *ll = (cx_linked_list *) list->data.listdata; 3.134 + 3.135 + if (ll->begin == NULL) { 3.136 + list->data.size = 0; 3.137 + return 0; 3.138 + } else { 3.139 + void *cur = ll->begin; 3.140 + size_t size; 3.141 + do { 3.142 + size++; 3.143 + } while ((cur = *CX_LL_PTR(cur, ll->loc_next)) != NULL); 3.144 + list->data.size = size; 3.145 + return size; 3.146 } 3.147 -} 3.148 \ No newline at end of file 3.149 +}