diff array.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 9cc5d6d0563e
children b3ba5f5df3b9
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;
 }
 
 /*