changeset 43:f1e184940197

Same last commit for array too
author David Demelier <markand@malikania.fr>
date Sun, 02 Oct 2011 17:28:43 +0200
parents 30499f543188
children 295e76f7bd28
files array.c array.h parray.c parray.h
diffstat 4 files changed, 58 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/array.c	Sun Oct 02 11:26:30 2011 +0200
+++ b/array.c	Sun Oct 02 17:28:43 2011 +0200
@@ -169,7 +169,7 @@
  */
 
 int
-array_swap(struct array *arr, int i1, int i2)
+array_iswap(struct array *arr, int i1, int i2)
 {
 	/* Out of bounds */
 	if (i1 >= arr->length || i1 < 0 || i2 >= arr->length || i2 < 0)
@@ -198,6 +198,32 @@
 }
 
 /*
+ * Swap the two elements referenced by data `o1' and `o2'. This function
+ * may be slow on large arrays since it must travel all the object
+ * to find the indexes.
+ */
+
+int
+array_pswap(struct array *arr, const void *o1, const void *o2)
+{
+	int found, i1, i2;
+
+	for (i1 = found = 0; !found && i1 < arr->length; ++i1)
+		found = memcmp(arr->data + OFFSET(i1), o1, arr->unit) == 0;
+
+	if (!found)
+		return -1;
+
+	for (i2 = found = 0; !found && i2 < arr->length; ++i2)
+		found = memcmp(arr->data + OFFSET(i2), o2, arr->unit) == 0;
+
+	if (!found)
+		return -1;
+
+	return array_iswap(arr, --i1, --i2);
+}
+
+/*
  * Apply the function `fn' on each object and give the optional `udata'
  * argument to the function too.
  */
--- a/array.h	Sun Oct 02 11:26:30 2011 +0200
+++ b/array.h	Sun Oct 02 17:28:43 2011 +0200
@@ -48,7 +48,8 @@
 void	array_unqueue(struct array *);
 void	array_remove(struct array *, int);
 void	array_unref(struct array *, const void *);
-int	array_swap(struct array *, int, int);
+int	array_iswap(struct array *, int, int);
+int	array_pswap(struct array *, const void *, const void *);
 void	array_map(const struct array *, void (*fn)(void *, void *), void *);
 void	*array_find(const struct array *, int (*fn)(void *, void *), int *, void *);
 void	array_clear(struct array *);
--- a/parray.c	Sun Oct 02 11:26:30 2011 +0200
+++ b/parray.c	Sun Oct 02 17:28:43 2011 +0200
@@ -166,7 +166,7 @@
  */
 
 int
-parray_swap(struct parray *arr, int i1, int i2)
+parray_iswap(struct parray *arr, int i1, int i2)
 {
 	void *tmp;
 
@@ -182,6 +182,32 @@
 }
 
 /*
+ * Swap the two elements referenced by data `o1' and `o2'. This function
+ * may be slow on large arrays since it must travel all the object
+ * to find the indexes.
+ */
+
+int
+parray_pswap(struct parray *arr, const void *o1, const void *o2)
+{
+	int found, i1, i2;
+
+	for (i1 = found = 0; !found && i1 < arr->length; ++i1)
+		found = arr->datas[i1] == o1;
+
+	if (!found)
+		return -1;
+
+	for (i2 = found = 0; !found && i2 < arr->length; ++i2)
+		found = arr->datas[i2] == o2;
+
+	if (!found)
+		return -1;
+
+	return parray_iswap(arr, --i1, --i2);
+}
+
+/*
  * Apply the function `fn' on each object and give the optional `udata'
  * argument to the function too.
  */
--- a/parray.h	Sun Oct 02 11:26:30 2011 +0200
+++ b/parray.h	Sun Oct 02 17:28:43 2011 +0200
@@ -46,7 +46,8 @@
 void	parray_unqueue(struct parray *);
 void	parray_remove(struct parray *, int);
 void	parray_unref(struct parray *, const void *);
-int	parray_swap(struct parray *, int, int);
+int	parray_iswap(struct parray *, int, int);
+int	parray_pswap(struct parray *, const void *, const void *);
 void	parray_map(const struct parray *, void (*fn)(void *, void *), void *);
 void	*parray_find(const struct parray *, int (*fn)(void *, void *), int *, void *);
 void	parray_clear(struct parray *);