1.1 --- a/src/linked_list.c Mon Sep 27 18:33:30 2021 +0200 1.2 +++ b/src/linked_list.c Mon Sep 27 18:50:07 2021 +0200 1.3 @@ -99,6 +99,9 @@ 1.4 char payload[]; 1.5 }; 1.6 1.7 +#define CX_LL_LOC_PREV offsetof(struct cx_linked_list_node, prev) 1.8 +#define CX_LL_LOC_NEXT offsetof(struct cx_linked_list_node, next) 1.9 + 1.10 typedef struct { 1.11 cx_list_s base; 1.12 void *begin; 1.13 @@ -120,8 +123,7 @@ 1.14 1.15 int ret = cx_linked_list_add( 1.16 &ll->begin, &ll->end, 1.17 - offsetof(struct cx_linked_list_node, prev), 1.18 - offsetof(struct cx_linked_list_node, next), 1.19 + CX_LL_LOC_PREV, CX_LL_LOC_NEXT, 1.20 node 1.21 ); 1.22 if (ret == 0) { 1.23 @@ -147,6 +149,17 @@ 1.24 return 0; 1.25 } 1.26 1.27 +static void *cx_ll_at(cx_list_s *list, size_t index) { 1.28 + cx_linked_list *ll = (cx_linked_list *) list; 1.29 + struct cx_linked_list_node *node; 1.30 + if (index > list->size / 2) { 1.31 + node = cx_linked_list_at(ll->begin, 0, CX_LL_LOC_NEXT, index); 1.32 + } else { 1.33 + node = cx_linked_list_at(ll->end, list->size, CX_LL_LOC_PREV, index); 1.34 + } 1.35 + return &node->payload; 1.36 +} 1.37 + 1.38 static size_t cx_ll_find(cx_list_s *list, void *elem) { 1.39 CxListComparator cmp = list->cmpfunc; 1.40 cx_linked_list *ll = (cx_linked_list *) list; 1.41 @@ -164,9 +177,8 @@ 1.42 } 1.43 1.44 static void *cx_ll_last(cx_list_s *list) { 1.45 - cx_linked_list *linkedList = (cx_linked_list *) list; 1.46 - struct cx_linked_list_node *last = cx_linked_list_last( 1.47 - NULL, &linkedList->end, offsetof(struct cx_linked_list_node, next)); 1.48 + cx_linked_list *ll = (cx_linked_list *) list; 1.49 + struct cx_linked_list_node *last = cx_linked_list_last(NULL, &ll->end, CX_LL_LOC_NEXT); 1.50 return &last->payload; 1.51 } 1.52 1.53 @@ -174,6 +186,7 @@ 1.54 cx_ll_add, 1.55 cx_ll_insert, 1.56 cx_ll_remove, 1.57 + cx_ll_at, 1.58 cx_ll_find, 1.59 cx_ll_last 1.60 }; 1.61 @@ -190,8 +203,8 @@ 1.62 list->base.capacity = SIZE_MAX; 1.63 1.64 list->begin = NULL; 1.65 - list->loc_prev = offsetof(struct cx_linked_list_node, prev); 1.66 - list->loc_next = offsetof(struct cx_linked_list_node, next); 1.67 + list->loc_prev = CX_LL_LOC_PREV; 1.68 + list->loc_next = CX_LL_LOC_NEXT; 1.69 1.70 CxList result = (CxList) list; 1.71 cxLinkedListRecalculateSize(result);