diff -r cd3069757010 -r 9a5adedd6de6 src/linked_list.c --- a/src/linked_list.c Mon Sep 27 18:33:30 2021 +0200 +++ b/src/linked_list.c Mon Sep 27 18:50:07 2021 +0200 @@ -99,6 +99,9 @@ char payload[]; }; +#define CX_LL_LOC_PREV offsetof(struct cx_linked_list_node, prev) +#define CX_LL_LOC_NEXT offsetof(struct cx_linked_list_node, next) + typedef struct { cx_list_s base; void *begin; @@ -120,8 +123,7 @@ int ret = cx_linked_list_add( &ll->begin, &ll->end, - offsetof(struct cx_linked_list_node, prev), - offsetof(struct cx_linked_list_node, next), + CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node ); if (ret == 0) { @@ -147,6 +149,17 @@ return 0; } +static void *cx_ll_at(cx_list_s *list, size_t index) { + cx_linked_list *ll = (cx_linked_list *) list; + struct cx_linked_list_node *node; + if (index > list->size / 2) { + node = cx_linked_list_at(ll->begin, 0, CX_LL_LOC_NEXT, index); + } else { + node = cx_linked_list_at(ll->end, list->size, CX_LL_LOC_PREV, index); + } + return &node->payload; +} + static size_t cx_ll_find(cx_list_s *list, void *elem) { CxListComparator cmp = list->cmpfunc; cx_linked_list *ll = (cx_linked_list *) list; @@ -164,9 +177,8 @@ } static void *cx_ll_last(cx_list_s *list) { - cx_linked_list *linkedList = (cx_linked_list *) list; - struct cx_linked_list_node *last = cx_linked_list_last( - NULL, &linkedList->end, offsetof(struct cx_linked_list_node, next)); + cx_linked_list *ll = (cx_linked_list *) list; + struct cx_linked_list_node *last = cx_linked_list_last(NULL, &ll->end, CX_LL_LOC_NEXT); return &last->payload; } @@ -174,6 +186,7 @@ cx_ll_add, cx_ll_insert, cx_ll_remove, + cx_ll_at, cx_ll_find, cx_ll_last }; @@ -190,8 +203,8 @@ list->base.capacity = SIZE_MAX; list->begin = NULL; - list->loc_prev = offsetof(struct cx_linked_list_node, prev); - list->loc_next = offsetof(struct cx_linked_list_node, next); + list->loc_prev = CX_LL_LOC_PREV; + list->loc_next = CX_LL_LOC_NEXT; CxList result = (CxList) list; cxLinkedListRecalculateSize(result);