# HG changeset patch # User Mike Becker # Date 1632757763 -7200 # Node ID 9d4971ea062534928a4020f334ce110e1a7d48b1 # Parent ca9ce2297c29801f4e3e382a55c079506f7c5a94 implement linked list find diff -r ca9ce2297c29 -r 9d4971ea0625 src/linked_list.c --- a/src/linked_list.c Mon Sep 27 17:00:19 2021 +0200 +++ b/src/linked_list.c Mon Sep 27 17:49:23 2021 +0200 @@ -98,7 +98,7 @@ } cx_linked_list; int cx_ll_add(cx_list_s *list, void *elem) { - cx_linked_list *linkedlist = (cx_linked_list *) list; + cx_linked_list *ll = (cx_linked_list *) list; struct cx_linked_list_node *node = cxMalloc(list->allocator, sizeof(struct cx_linked_list_node) + list->itemsize); @@ -109,8 +109,7 @@ memcpy(node->payload, elem, list->itemsize); int ret = cx_linked_list_add( - &linkedlist->begin, - &linkedlist->end, + &ll->begin, &ll->end, offsetof(struct cx_linked_list_node, prev), offsetof(struct cx_linked_list_node, next), node @@ -124,21 +123,31 @@ } int cx_ll_insert(cx_list_s *list, size_t index, void *elem) { - cx_linked_list *linkedList = (cx_linked_list *) list; + cx_linked_list *ll = (cx_linked_list *) list; // TODO: implement using low level API return 1; } void *cx_ll_remove(cx_list_s *list, size_t index) { - cx_linked_list *linkedList = (cx_linked_list *) list; + cx_linked_list *ll = (cx_linked_list *) list; // TODO: implement using low level API return NULL; } size_t cx_ll_find(cx_list_s *list, void *elem) { - cx_linked_list *linkedList = (cx_linked_list *) list; - // TODO: implement using low level API - return 0; + CxListComparator cmp = list->cmpfunc; + cx_linked_list *ll = (cx_linked_list *) list; + + size_t index; + struct cx_linked_list_node* node = ll->begin; + for (index = 0 ; index < list->size ; index++) { + void* current = node->payload; + if (cmp(current, elem) == 0) { + return index; + } + node = node->next; + } + return index; } void *cx_ll_last(cx_list_s *list) {