Mercurial > code
changeset 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 | f773c76b1f3c |
children | 18e8c7911825 |
files | array.c array.h parray.c parray.h |
diffstat | 4 files changed, 24 insertions(+), 30 deletions(-) [+] |
line wrap: on
line diff
--- a/array.c Wed Nov 09 21:08:43 2011 +0100 +++ b/array.c Thu Nov 10 18:27:04 2011 +0100 @@ -229,7 +229,7 @@ */ void -array_map(const struct array *arr, void (*fn)(void *, void *), void *udata) +array_map(const struct array *arr, array_map_fn fn, void *udata) { int i; @@ -239,28 +239,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 is filled with the array data at the correct index. If the comparison + * function nevers returns 1, array_find returns -1. */ -void * -array_find(const struct array *arr, int (*fn)(void *, void *), int *ix, void *u) +int +array_find(const struct array *arr, array_cmp_fn fn, void *data, void *u) { int st, i; - void *data; for (i = st = 0; i < arr->length && st != 1; ++i) st = fn((char *)arr->data + OFFSET(i), u); - if (st) { - data = (char *)arr->data + OFFSET(--i); - if (ix) - *ix = i; - } else - data = NULL; + if (st) + memcpy(data, (char *)arr->data + OFFSET(--i), arr->unit); + else + i = -1; - return data; + return i; } /*
--- a/array.h Wed Nov 09 21:08:43 2011 +0100 +++ b/array.h Thu Nov 10 18:27:04 2011 +0100 @@ -63,7 +63,7 @@ int array_iswap(struct array *, int, int) __at_nonnull(1); int array_pswap(struct array *, const void *, const void *) __at_nonnull(1); void array_map(const struct array *, array_map_fn, void *) __at_nonnull(1, 2); -void *array_find(const struct array *, array_cmp_fn, int *, void *) __at_nonnull(1, 2); +int array_find(const struct array *, array_cmp_fn, void *, void *) __at_nonnull(1, 2); void array_clear(struct array *) __at_nonnull(1); void array_free(struct array *) __at_nonnull(1);
--- 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; } /*
--- a/parray.h Wed Nov 09 21:08:43 2011 +0100 +++ b/parray.h Thu Nov 10 18:27:04 2011 +0100 @@ -62,7 +62,7 @@ int parray_iswap(struct parray *, int, int) __at_nonnull(1); int parray_pswap(struct parray *, const void *, const void *) __at_nonnull(1); void parray_map(const struct parray *, parray_map_fn, void *) __at_nonnull(1, 2); -void *parray_find(const struct parray *, parray_cmp_fn, int *, void *) __at_nonnull(1, 2); +int parray_find(const struct parray *, parray_cmp_fn, void **, void *) __at_nonnull(1, 2); void parray_clear(struct parray *) __at_nonnull(1); void parray_free(struct parray *) __at_nonnull(1);