diff array.c @ 22:ecdf21f1d0c6

Added a new function array_swap() that swap elements by index
author David Demelier <markand@malikania.fr>
date Wed, 14 Sep 2011 20:24:14 +0200
parents e9a7b671d707
children 216b6e6a539c
line wrap: on
line diff
--- a/array.c	Fri Sep 09 17:32:53 2011 +0200
+++ b/array.c	Wed Sep 14 20:24:14 2011 +0200
@@ -34,11 +34,12 @@
 	if (unit == 0 || !(arr = malloc(sizeof (struct array))))
 		return NULL;
 
+	arr->tmp	= NULL;
+	arr->length	= 0;
+	arr->flags	= flags;
 	arr->bsize	= (bsize == 0) ? ARRAY_DEFAULT_BSIZE : bsize;
-	arr->flags	= flags;
 	arr->unit	= unit;
-	arr->size	= SIZE(bsize);
-	arr->length	= 0;
+	arr->size	= SIZE(arr->bsize);
 
 	if (!(arr->data = malloc(arr->size))) {
 		free(arr);
@@ -165,6 +166,35 @@
 }
 
 /*
+ * Swap the two elements referenced by index `i1' and `i2'. This function needs
+ * to allocate data to swap elements thus if the functions fails it returns -1
+ * otherwise 0 is returned.
+ */
+
+int
+array_swap(struct array *arr, int i1, int i2)
+{
+	/* Out of bounds */
+	if (i1 > arr->length - 1|| i1 < 0 || i2 > arr->length - 1|| i2 < 0)
+		return -1;
+
+	/*
+	 * Only allocate at this time, the user may do not want to use this
+	 * function.
+	 */
+
+	if (!arr->tmp && !(arr->tmp = malloc(arr->unit)))
+		return -1;
+
+	memcpy(arr->tmp, arr->data + SIZE(i1), arr->unit);
+	memcpy(arr->data + SIZE(i1), arr->data + SIZE(i2), arr->unit);
+	memcpy(arr->data + SIZE(i2), arr->tmp, arr->unit);
+
+	return 0;
+}
+
+
+/*
  * Apply the function `fn' on each object and give the optional `udata'
  * argument to the function too.
  */
@@ -225,7 +255,11 @@
 {
 	array_clear(arr);
 
-	free(arr->data);
+	if (arr->data)
+		free(arr->data);
+	if (arr->tmp)
+		free(arr->tmp);
+
 	free(arr);
 }