Fri, 11 May 2018 17:40:16 +0200
adds ucx_list_prepend_once() and ucx_list_prepend_once_a()
src/list.c | file | annotate | diff | comparison | revisions | |
src/ucx/list.h | file | annotate | diff | comparison | revisions |
--- 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); }
--- 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 <code>NULL</code> 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 <code>NULL</code> 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 <code>NULL</code> 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 <i>should</i> overwrite the old list pointer by calling