adds ucx_list_prepend_once() and ucx_list_prepend_once_a()

Fri, 11 May 2018 17:40:16 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 11 May 2018 17:40:16 +0200
changeset 291
deb0035635eb
parent 290
d5d6ab809ad3
child 292
d9abf53b8397

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
     1.1 --- a/src/list.c	Wed May 09 20:15:10 2018 +0200
     1.2 +++ b/src/list.c	Fri May 11 17:40:16 2018 +0200
     1.3 @@ -142,6 +142,39 @@
     1.4      }
     1.5  }
     1.6  
     1.7 +UcxList *ucx_list_prepend_once(UcxList *l, void *data,
     1.8 +        cmp_func cmpfnc, void *cmpdata) {
     1.9 +    return ucx_list_prepend_once_a(ucx_default_allocator(), l,
    1.10 +            data, cmpfnc, cmpdata);
    1.11 +}
    1.12 +
    1.13 +UcxList *ucx_list_prepend_once_a(UcxAllocator *alloc, UcxList *l, void *data,
    1.14 +        cmp_func cmpfnc, void *cmpdata) {
    1.15 +
    1.16 +    UcxList* first = ucx_list_first(l);
    1.17 +    {
    1.18 +        UcxList *e = first;
    1.19 +        while (e) {
    1.20 +            if (cmpfnc(e->data, data, cmpdata) == 0) {
    1.21 +                return l;
    1.22 +            }
    1.23 +            e = e->next;
    1.24 +        }
    1.25 +    }
    1.26 +    
    1.27 +    UcxList *nl = ucx_list_append_a(alloc, NULL, data);
    1.28 +    if (!nl) {
    1.29 +        return NULL;
    1.30 +    }
    1.31 +
    1.32 +    if (first) {
    1.33 +        nl->next = first;
    1.34 +        first->prev = nl;
    1.35 +    }
    1.36 +    
    1.37 +    return nl;
    1.38 +}
    1.39 +
    1.40  UcxList *ucx_list_prepend(UcxList *l, void *data) {
    1.41      return ucx_list_prepend_a(ucx_default_allocator(), l, data);
    1.42  }
     2.1 --- a/src/ucx/list.h	Wed May 09 20:15:10 2018 +0200
     2.2 +++ b/src/ucx/list.h	Fri May 11 17:40:16 2018 +0200
     2.3 @@ -232,8 +232,6 @@
     2.4   * Inserts an element at the end of the list, if it is not present in the list,
     2.5   * using a UcxAllocator.
     2.6   * 
     2.7 - * See ucx_list_append() for details.
     2.8 - * 
     2.9   * @param allocator the allocator to use
    2.10   * @param list the list where to append the data, or <code>NULL</code> to
    2.11   * create a new list
    2.12 @@ -248,6 +246,38 @@
    2.13          UcxList *list, void *data, cmp_func cmpfnc, void *cmpdata);
    2.14  
    2.15  /**
    2.16 + * Inserts an element at the beginning of the list, if it is not present
    2.17 + * in the list.
    2.18 + * 
    2.19 + * 
    2.20 + * @param list the list where to prepend the data, or <code>NULL</code> to
    2.21 + * create a new list
    2.22 + * @param data the data to insert
    2.23 + * @param cmpfnc the compare function
    2.24 + * @param cmpdata additional data for the compare function
    2.25 + * @return a pointer to the new list head
    2.26 + * @see ucx_list_prepend()
    2.27 + */
    2.28 +UcxList *ucx_list_prepend_once(UcxList *list, void *data,
    2.29 +        cmp_func cmpfnc, void *cmpdata);
    2.30 +
    2.31 +/**
    2.32 + * Inserts an element at the beginning of the list, if it is not present in
    2.33 + * the list, using a UcxAllocator.
    2.34 + * 
    2.35 + * @param allocator the allocator to use
    2.36 + * @param list the list where to prepend the data, or <code>NULL</code> to
    2.37 + * create a new list
    2.38 + * @param data the data to insert
    2.39 + * @param cmpfnc the compare function
    2.40 + * @param cmpdata additional data for the compare function
    2.41 + * @return a pointer to the new list head
    2.42 + * @see ucx_list_prepend_a()
    2.43 + */
    2.44 +UcxList *ucx_list_prepend_once_a(UcxAllocator *allocator,
    2.45 +        UcxList *list, void *data, cmp_func cmpfnc, void *cmpdata);
    2.46 +
    2.47 +/**
    2.48   * Inserts an element at the beginning of the list.
    2.49   * 
    2.50   * You <i>should</i> overwrite the old list pointer by calling

mercurial