Mon, 27 Sep 2021 18:50:07 +0200
add high-level function cxListAt()
src/cx/list.h | file | annotate | diff | comparison | revisions | |
src/linked_list.c | file | annotate | diff | comparison | revisions | |
src/list.c | file | annotate | diff | comparison | revisions |
1.1 --- a/src/cx/list.h Mon Sep 27 18:33:30 2021 +0200 1.2 +++ b/src/cx/list.h Mon Sep 27 18:50:07 2021 +0200 1.3 @@ -47,6 +47,8 @@ 1.4 1.5 int (*remove)(cx_list_s *list, size_t index); 1.6 1.7 + void *(*at)(cx_list_s *list, size_t index); 1.8 + 1.9 size_t (*find)(cx_list_s *list, void *elem); 1.10 1.11 void *(*last)(cx_list_s *list); 1.12 @@ -69,6 +71,8 @@ 1.13 1.14 int cxListRemove(CxList list, size_t index); 1.15 1.16 +void *cxListAt(CxList list, size_t index); 1.17 + 1.18 size_t cxListFind(CxList list, void *elem); 1.19 1.20 void *cxListLast(CxList list);
2.1 --- a/src/linked_list.c Mon Sep 27 18:33:30 2021 +0200 2.2 +++ b/src/linked_list.c Mon Sep 27 18:50:07 2021 +0200 2.3 @@ -99,6 +99,9 @@ 2.4 char payload[]; 2.5 }; 2.6 2.7 +#define CX_LL_LOC_PREV offsetof(struct cx_linked_list_node, prev) 2.8 +#define CX_LL_LOC_NEXT offsetof(struct cx_linked_list_node, next) 2.9 + 2.10 typedef struct { 2.11 cx_list_s base; 2.12 void *begin; 2.13 @@ -120,8 +123,7 @@ 2.14 2.15 int ret = cx_linked_list_add( 2.16 &ll->begin, &ll->end, 2.17 - offsetof(struct cx_linked_list_node, prev), 2.18 - offsetof(struct cx_linked_list_node, next), 2.19 + CX_LL_LOC_PREV, CX_LL_LOC_NEXT, 2.20 node 2.21 ); 2.22 if (ret == 0) { 2.23 @@ -147,6 +149,17 @@ 2.24 return 0; 2.25 } 2.26 2.27 +static void *cx_ll_at(cx_list_s *list, size_t index) { 2.28 + cx_linked_list *ll = (cx_linked_list *) list; 2.29 + struct cx_linked_list_node *node; 2.30 + if (index > list->size / 2) { 2.31 + node = cx_linked_list_at(ll->begin, 0, CX_LL_LOC_NEXT, index); 2.32 + } else { 2.33 + node = cx_linked_list_at(ll->end, list->size, CX_LL_LOC_PREV, index); 2.34 + } 2.35 + return &node->payload; 2.36 +} 2.37 + 2.38 static size_t cx_ll_find(cx_list_s *list, void *elem) { 2.39 CxListComparator cmp = list->cmpfunc; 2.40 cx_linked_list *ll = (cx_linked_list *) list; 2.41 @@ -164,9 +177,8 @@ 2.42 } 2.43 2.44 static void *cx_ll_last(cx_list_s *list) { 2.45 - cx_linked_list *linkedList = (cx_linked_list *) list; 2.46 - struct cx_linked_list_node *last = cx_linked_list_last( 2.47 - NULL, &linkedList->end, offsetof(struct cx_linked_list_node, next)); 2.48 + cx_linked_list *ll = (cx_linked_list *) list; 2.49 + struct cx_linked_list_node *last = cx_linked_list_last(NULL, &ll->end, CX_LL_LOC_NEXT); 2.50 return &last->payload; 2.51 } 2.52 2.53 @@ -174,6 +186,7 @@ 2.54 cx_ll_add, 2.55 cx_ll_insert, 2.56 cx_ll_remove, 2.57 + cx_ll_at, 2.58 cx_ll_find, 2.59 cx_ll_last 2.60 }; 2.61 @@ -190,8 +203,8 @@ 2.62 list->base.capacity = SIZE_MAX; 2.63 2.64 list->begin = NULL; 2.65 - list->loc_prev = offsetof(struct cx_linked_list_node, prev); 2.66 - list->loc_next = offsetof(struct cx_linked_list_node, next); 2.67 + list->loc_prev = CX_LL_LOC_PREV; 2.68 + list->loc_next = CX_LL_LOC_NEXT; 2.69 2.70 CxList result = (CxList) list; 2.71 cxLinkedListRecalculateSize(result);
3.1 --- a/src/list.c Mon Sep 27 18:33:30 2021 +0200 3.2 +++ b/src/list.c Mon Sep 27 18:50:07 2021 +0200 3.3 @@ -40,6 +40,10 @@ 3.4 return list->cl->remove(list, index); 3.5 } 3.6 3.7 +void *cxListAt(CxList list, size_t index) { 3.8 + return list->cl->at(list, index); 3.9 +} 3.10 + 3.11 size_t cxListFind(CxList list, void *elem) { 3.12 return list->cl->find(list, elem); 3.13 }