diff array.h @ 62:d10ab6bc555d

HG self failure
author David Demelier <markand@malikania.fr>
date Wed, 09 Nov 2011 19:26:09 +0100
parents
children f773c76b1f3c
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/array.h	Wed Nov 09 19:26:09 2011 +0100
@@ -0,0 +1,83 @@
+/*
+ * array.h -- manipulate dynamic arrays
+ *
+ * Copyright (c) 2011, David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef _ARRAY_H_
+#define _ARRAY_H_
+
+/* Add some nonnull attributes for gcc/clang */
+#ifdef __GNUC__
+#  define __at_malloc		__attribute__ ((malloc))
+#  define __at_nonnull(...)	__attribute__ ((nonnull (__VA_ARGS__)))
+#else
+#  define __at_malloc
+#  define __at_nonnull(...)
+#endif
+
+#define ARRAY_DEFAULT_BSIZE	128
+
+enum array_type {
+	ARRAY_FIXED	= 0,
+	ARRAY_AUTO	= 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(_R) */
+};
+
+typedef void (*array_map_fn)(void *, void *);
+typedef int (*array_cmp_fn)(void *, void *);
+
+struct array	*array_new(enum array_type, size_t, int) __at_malloc;
+int		array_push(struct array *, const void *) __at_nonnull(1, 2);
+int		array_insert(struct array *, const void *, int) __at_nonnull(1, 2);
+int		array_append(struct array *, const void *) __at_nonnull(1, 2);
+void		array_pop(struct array *) __at_nonnull(1);
+void		array_unqueue(struct array *) __at_nonnull(1);
+void		array_remove(struct array *, int) __at_nonnull(1);
+void		array_unref(struct array *, const void *) __at_nonnull(1);
+int		array_iswap(struct array *, int, int) __at_nonnull(1);
+int		array_pswap(struct array *, const void *, const void *) __at_nonnull(1);
+void		array_map(const struct array *, array_map_fn, void *) __at_nonnull(1, 2);
+void		*array_find(const struct array *, array_cmp_fn, int *, void *) __at_nonnull(1, 2);
+void		array_clear(struct array *) __at_nonnull(1);
+void		array_free(struct array *) __at_nonnull(1);
+
+#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_R(a, var)							\
+	for ((a)->i = 0, var = ARRAY_TAIL((a));					\
+	    (a)->i < (a)->length; ++(a)->i, --var)
+
+#define ARRAY_FOREACH(a, var, type)						\
+	for ((a)->i = 0, var = ARRAY_HEAD((a), type);				\
+		(a)->i < (a)->length;						\
+		++(a)->i, var = ARRAY_INDEX((a), (a)->i, type))
+
+#endif /* _ARRAY_H_ */