Mercurial > code
diff parray.c @ 67:cff6869fbc94
Modified [p]array_find to return the index for a better usage
author | David Demelier <markand@malikania.fr> |
---|---|
date | Thu, 10 Nov 2011 18:27:04 +0100 |
parents | 2901c594eaef |
children | 4d723e81b685 |
line wrap: on
line diff
--- a/parray.c Wed Nov 09 21:08:43 2011 +0100 +++ b/parray.c Thu Nov 10 18:27:04 2011 +0100 @@ -210,7 +210,7 @@ */ void -parray_map(const struct parray *arr, void (*fn)(void *, void *), void *udata) +parray_map(const struct parray *arr, parray_map_fn fn, void *udata) { int i; @@ -220,28 +220,25 @@ /* * Compare each object with the user supplied function. If the `fn' function - * returns 1 then the data is returned. Optional idx argument can be set to - * indicate the data position. If the data was not found the function returns - * NULL. + * returns 1 then the index of the data position is returned and the parameter + * data points to the array data at the correct index. If the comparison + * function nevers returns 1, array_find returns -1. */ -void * -parray_find(const struct parray *arr, int (*fn)(void *, void *), int *ix, void *u) +int +parray_find(const struct parray *arr, parray_cmp_fn fn, void **ptr, void *u) { int st, i; - void *data; for (i = st = 0; i < arr->length && st != 1; ++i) st = fn(arr->datas[i], u); - if (st) { - data = arr->datas[--i]; - if (ix) - *ix = i; - } else - data = NULL; + if (st) + *ptr = arr->datas[--i]; + else + i = -1; - return data; + return i; } /*