ucx/list.c

changeset 123
7fb0f74517c5
parent 122
540d99722f1f
child 125
fca8efb122de
     1.1 --- a/ucx/list.c	Mon Jul 22 11:53:39 2013 +0200
     1.2 +++ b/ucx/list.c	Mon Jul 22 13:45:49 2013 +0200
     1.3 @@ -116,6 +116,18 @@
     1.4      return (UcxList*)e;
     1.5  }
     1.6  
     1.7 +ssize_t ucx_list_indexof(const UcxList *list, const UcxList *elem) {
     1.8 +    ssize_t index = 0;
     1.9 +    while (list) {
    1.10 +        if (list == elem) {
    1.11 +            return index;
    1.12 +        }
    1.13 +        list = list->next;
    1.14 +        index++;
    1.15 +    }
    1.16 +    return -1;
    1.17 +}
    1.18 +
    1.19  UcxList *ucx_list_get(const UcxList *l, int index) {
    1.20      if (l == NULL) return NULL;
    1.21  
    1.22 @@ -128,13 +140,19 @@
    1.23      return (UcxList*)(index == 0 ? e : NULL);
    1.24  }
    1.25  
    1.26 +ssize_t ucx_list_find(UcxList *l, void *elem, cmp_func fnc, void *cmpdata) {
    1.27 +    ssize_t index = 0;
    1.28 +    UCX_FOREACH(e, l) {
    1.29 +        if (fnc(elem, e->data, cmpdata) == 0) {
    1.30 +            return index;
    1.31 +        }
    1.32 +        index++;
    1.33 +    }
    1.34 +    return -1;
    1.35 +}
    1.36 +
    1.37  int ucx_list_contains(UcxList *l, void *elem, cmp_func fnc, void *cmpdata) {
    1.38 -    UCX_FOREACH(e, l) {
    1.39 -        if (!fnc(elem, e->data, cmpdata)) {
    1.40 -            return 1;
    1.41 -        }
    1.42 -    }
    1.43 -    return 0;
    1.44 +    return ucx_list_find(l, elem, fnc, cmpdata) > -1;
    1.45  }
    1.46  
    1.47  size_t ucx_list_size(const UcxList *l) {

mercurial