# HG changeset patch # User Mike Becker # Date 1526053216 -7200 # Node ID deb0035635ebdfd60d186f3ac66dd1d91b3a14b5 # Parent d5d6ab809ad3ce10d2439962a5832ff4b4e9cd55 adds ucx_list_prepend_once() and ucx_list_prepend_once_a() diff -r d5d6ab809ad3 -r deb0035635eb src/list.c --- a/src/list.c Wed May 09 20:15:10 2018 +0200 +++ b/src/list.c Fri May 11 17:40:16 2018 +0200 @@ -142,6 +142,39 @@ } } +UcxList *ucx_list_prepend_once(UcxList *l, void *data, + cmp_func cmpfnc, void *cmpdata) { + return ucx_list_prepend_once_a(ucx_default_allocator(), l, + data, cmpfnc, cmpdata); +} + +UcxList *ucx_list_prepend_once_a(UcxAllocator *alloc, UcxList *l, void *data, + cmp_func cmpfnc, void *cmpdata) { + + UcxList* first = ucx_list_first(l); + { + UcxList *e = first; + while (e) { + if (cmpfnc(e->data, data, cmpdata) == 0) { + return l; + } + e = e->next; + } + } + + UcxList *nl = ucx_list_append_a(alloc, NULL, data); + if (!nl) { + return NULL; + } + + if (first) { + nl->next = first; + first->prev = nl; + } + + return nl; +} + UcxList *ucx_list_prepend(UcxList *l, void *data) { return ucx_list_prepend_a(ucx_default_allocator(), l, data); } diff -r d5d6ab809ad3 -r deb0035635eb src/ucx/list.h --- a/src/ucx/list.h Wed May 09 20:15:10 2018 +0200 +++ b/src/ucx/list.h Fri May 11 17:40:16 2018 +0200 @@ -232,8 +232,6 @@ * Inserts an element at the end of the list, if it is not present in the list, * using a UcxAllocator. * - * See ucx_list_append() for details. - * * @param allocator the allocator to use * @param list the list where to append the data, or NULL to * create a new list @@ -248,6 +246,38 @@ UcxList *list, void *data, cmp_func cmpfnc, void *cmpdata); /** + * Inserts an element at the beginning of the list, if it is not present + * in the list. + * + * + * @param list the list where to prepend the data, or NULL to + * create a new list + * @param data the data to insert + * @param cmpfnc the compare function + * @param cmpdata additional data for the compare function + * @return a pointer to the new list head + * @see ucx_list_prepend() + */ +UcxList *ucx_list_prepend_once(UcxList *list, void *data, + cmp_func cmpfnc, void *cmpdata); + +/** + * Inserts an element at the beginning of the list, if it is not present in + * the list, using a UcxAllocator. + * + * @param allocator the allocator to use + * @param list the list where to prepend the data, or NULL to + * create a new list + * @param data the data to insert + * @param cmpfnc the compare function + * @param cmpdata additional data for the compare function + * @return a pointer to the new list head + * @see ucx_list_prepend_a() + */ +UcxList *ucx_list_prepend_once_a(UcxAllocator *allocator, + UcxList *list, void *data, cmp_func cmpfnc, void *cmpdata); + +/** * Inserts an element at the beginning of the list. * * You should overwrite the old list pointer by calling