changeset 118:3c51b1f2974e

Same for array
author David Demelier <markand@malikania.fr>
date Tue, 28 Feb 2012 21:15:08 +0100
parents bbe86bdb55da
children d6a78dea70c9
files array.c array.h
diffstat 2 files changed, 16 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/array.c	Tue Feb 28 20:57:48 2012 +0100
+++ b/array.c	Tue Feb 28 21:15:08 2012 +0100
@@ -51,7 +51,7 @@
  * l -> optional array block size of type int
  * m -> malloc function that must matches void * (*malloc)(size_t)
  * r -> realloc function that must matches void * (*realloc)(void *, size_t)
- * t -> type of array of type enum array_type
+ * t -> type of array of type int
  */
 
 void
@@ -106,9 +106,12 @@
 int
 array_insert(struct array *arr, const void *data, int index)
 {
-	if (index > arr->length - 1 || index < 0 || array_grow(arr) < 0)
-		return -1;
+	if (index < 0)
+		return array_push(arr, data);
+	if (index >= arr->length)
+		return array_append(arr, data);
 
+	/* Good place */
 	memmove((char *)arr->data + OFFSET(index + 1),
 	    (char *)arr->data + OFFSET(index), OFFSET(arr->length++ - index));
 	memcpy((char *)arr->data + OFFSET(index), data, arr->unit);
--- a/array.h	Tue Feb 28 20:57:48 2012 +0100
+++ b/array.h	Tue Feb 28 21:15:08 2012 +0100
@@ -25,19 +25,16 @@
 #define ARRAY_DEFAULT_BSIZE	128
 #endif
 
-enum array_type {
-	ARRAY_AUTO	= 0,
-	ARRAY_FIXED	= 1
-};
+#define ARRAY_AUTO	0
+#define ARRAY_FIXED	1
 
 struct array {
-	enum array_type	type;	/* array's type (default FIXED) */
-	void		*data;	/* array of data */
-	int		length;	/* number of element inside */
-	size_t		size;	/* current buffer size (allocated memory) */
-	size_t		unit;	/* unit size (sizeof the object) */
-	int		bsize;	/* block size (used when growing array) */
-	int		i;	/* only for ARRAY_FOREACH */
+	int	type;	/* array's type (default FIXED) */
+	void	*data;	/* array of data */
+	int	length;	/* number of element inside */
+	size_t	size;	/* current buffer size (allocated memory) */
+	size_t	unit;	/* unit size (sizeof the object) */
+	int	bsize;	/* block size (used when growing array) */
 
 	/* Own allocation functions */
 	void * (*malloc)(size_t);
@@ -54,8 +51,8 @@
 int	array_append(struct array *, const void *);
 void	array_pop(struct array *);
 void	array_unqueue(struct array *);
-void	array_remove(struct array *, int);
-void	array_unref(struct array *, const void *);
+void	array_iremove(struct array *, int);
+void	array_premove(struct array *, const void *);
 int	array_iswap(struct array *, int, int);
 int	array_pswap(struct array *, const void *, const void *);
 void	array_map(const struct array *, array_map_fn, void *);
@@ -63,21 +60,9 @@
 void	array_clear(struct array *);
 void	array_free(struct array *);
 
-#define ARRAY_HEAD(a, type)							\
-	(((type *)a->data)[0])
-#define ARRAY_TAIL(a, type)							\
-	(((type *)a->data)[(a->length == 0) ? 0 : a->length - 1])
-#define ARRAY_INDEX(a, i, type)							\
-	((type *)(a)->data)[((i) < 0)						\
-	    ? 0 : ((i) >= (a)->length) ? (a)->length - 1 : (i)]
-
 #define ARRAY_FOREACH(a, var)							\
 	for ((a)->i = 0, (var) = (a)->data;					\
 		(a)->i < (a)->length;						\
 		++(a)->i, ++(var))
 
-/* Only for ARRAY_FIXED */
-#define ARRAY_FULL(a)								\
-	((a)->length == (a)->bsize)
-
 #endif /* _ARRAY_H_ */