diff -r 1e3cb39815f8 -r 28bc3e10ac28 src/linked_list.c --- a/src/linked_list.c Tue Oct 05 12:25:23 2021 +0200 +++ b/src/linked_list.c Tue Oct 05 13:03:45 2021 +0200 @@ -172,6 +172,14 @@ return cx_ll_insert(list, list->size, elem); } +static int cx_pll_insert(cx_list_s *list, size_t index, void *elem) { + return cx_ll_insert(list, index, &elem); +} + +static int cx_pll_add(cx_list_s *list, void *elem) { + return cx_ll_insert(list, list->size, &elem); +} + static int cx_ll_remove(cx_list_s *list, size_t index) { cx_linked_list *ll = (cx_linked_list *) list; cx_linked_list_node *node = cx_ll_node_at(ll, index); @@ -205,7 +213,13 @@ static void *cx_ll_at(cx_list_s *list, size_t index) { cx_linked_list *ll = (cx_linked_list *) list; cx_linked_list_node *node = cx_ll_node_at(ll, index); - return node == NULL ? NULL : &node->payload; + return node == NULL ? NULL : node->payload; +} + +static void *cx_pll_at(cx_list_s *list, size_t index) { + cx_linked_list *ll = (cx_linked_list *) list; + cx_linked_list_node *node = cx_ll_node_at(ll, index); + return node == NULL ? NULL : *(void**)node->payload; } static size_t cx_ll_find(cx_list_s *list, void *elem) { @@ -224,10 +238,32 @@ return index; } +static size_t cx_pll_find(cx_list_s *list, void *elem) { + CxListComparator cmp = list->cmpfunc; + cx_linked_list *ll = (cx_linked_list *) list; + + size_t index; + cx_linked_list_node *node = ll->begin; + for (index = 0; index < list->size; index++) { + void *current = *(void**)node->payload; + if (cmp(current, elem) == 0) { + return index; + } + node = node->next; + } + return index; +} + static void *cx_ll_last(cx_list_s *list) { cx_linked_list *ll = (cx_linked_list *) list; cx_linked_list_node *last = ll->end; - return last == NULL ? NULL : &last->payload; + return last == NULL ? NULL : last->payload; +} + +static void *cx_pll_last(cx_list_s *list) { + cx_linked_list *ll = (cx_linked_list *) list; + cx_linked_list_node *last = ll->end; + return last == NULL ? NULL : *(void**)last->payload; } static cx_list_class cx_linked_list_class = { @@ -239,6 +275,15 @@ cx_ll_last }; +static cx_list_class cx_pointer_linked_list_class = { + cx_pll_add, + cx_pll_insert, + cx_ll_remove, + cx_pll_at, + cx_pll_find, + cx_pll_last +}; + CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator, size_t item_size) { cx_linked_list *list = cxMalloc(allocator, sizeof(cx_linked_list)); if (list == NULL) @@ -257,6 +302,24 @@ return (CxList) list; } +CxList cxPointerLinkedListCreate(CxAllocator allocator, CxListComparator comparator) { + cx_linked_list *list = cxMalloc(allocator, sizeof(cx_linked_list)); + if (list == NULL) + return NULL; + + list->base.cl = &cx_pointer_linked_list_class; + list->base.allocator = allocator; + list->base.cmpfunc = comparator; + list->base.itemsize = sizeof(void*); + list->base.capacity = SIZE_MAX; + list->base.size = 0; + + list->begin = NULL; + list->end = NULL; + + return (CxList) list; +} + void cxLinkedListDestroy(CxList list) { cx_linked_list *ll = (cx_linked_list *) list;