diff array.c @ 142:e3cf5ac9a5aa

[p]array_insert now returns -1 or index of added element
author David Demelier <markand@malikania.fr>
date Tue, 08 May 2012 15:02:17 +0200
parents c6d9eb5702e8
children 535f12e0a5af
line wrap: on
line diff
--- a/array.c	Wed May 02 20:20:22 2012 +0200
+++ b/array.c	Tue May 08 15:02:17 2012 +0200
@@ -86,7 +86,7 @@
 /*
  * Add to the head of array. NOTE: this may be very slow when adding a lot
  * of object (about 100000). If you need to add a lot of data please consider
- * using linked list instead.
+ * using linked list instead. Returns -1 on failure or 0 on success.
  */
 
 int
@@ -103,7 +103,7 @@
 
 /*
  * Insert the data at the specified index. The function returns -1 on
- * allocation failure or when the index is outof bounds otherwise 0 is returned.
+ * allocation failure or the position of the added element.
  */
 
 int
@@ -119,11 +119,12 @@
 	    (char *)arr->data + OFFSET(index), OFFSET(arr->length++ - index));
 	memcpy((char *)arr->data + OFFSET(index), data, arr->unit);
 
-	return 0;
+	return index;
 }
 
 /*
- * Append the data to the end of array.
+ * Append the data to the end of array. Returns -1 on failure or the position
+ * of the added element.
  */
 
 int
@@ -134,7 +135,7 @@
 
 	memcpy((char *)arr->data + OFFSET(arr->length++), data, arr->unit);
 
-	return 0;
+	return (arr->length - 1);
 }
 
 /*
@@ -267,7 +268,7 @@
  */
 
 void
-array_map(const struct array *arr, array_map_fn fn, void *udata)
+array_map(const struct array *arr, array_map_t fn, void *udata)
 {
 	int i;
 
@@ -282,7 +283,7 @@
  */
 
 int
-array_find(const struct array *arr, array_cmp_fn fn, void *dst, void *u)
+array_find(const struct array *arr, array_cmp_t fn, void *dst, void *u)
 {
 	int st, i;