aboutsummaryrefslogtreecommitdiff
path: root/array.h
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2016-02-17 20:20:01 +0100
committerMarc André Tanner <mat@brain-dump.org>2016-02-18 16:50:39 +0100
commit58df45177a7db63223c8459be7aef727a0cc22f7 (patch)
tree3ae992da687b2ce485eeec62cd208ff3cce8362c /array.h
parent900d0568099315f42ab004e3a158d23ef7fadd94 (diff)
downloadvis-58df45177a7db63223c8459be7aef727a0cc22f7.tar.gz
vis-58df45177a7db63223c8459be7aef727a0cc22f7.tar.xz
Add a simple dynamically growing array data structure
Diffstat (limited to 'array.h')
-rw-r--r--array.h31
1 files changed, 31 insertions, 0 deletions
diff --git a/array.h b/array.h
new file mode 100644
index 0000000..2d9c123
--- /dev/null
+++ b/array.h
@@ -0,0 +1,31 @@
+#ifndef ARRAY_H
+#define ARRAY_H
+
+#include <stddef.h>
+#include <stdbool.h>
+
+typedef struct { /* a dynamically growing array */
+ void **items; /* NULL if empty */
+ size_t len; /* number of currently stored items */
+ size_t count; /* maximal capacity of the array */
+} Array;
+
+/* initalize a (stack allocated) Array instance */
+void array_init(Array*);
+/* release/free the storage space used to hold items, reset size to zero */
+void array_release(Array*);
+/* like above but also call free(3) for each stored pointer */
+void array_release_full(Array*);
+/* remove all elements, set array length to zero, keep allocated memory */
+void array_clear(Array*);
+/* reserve space to store at least `count' elements in this aray */
+bool array_reserve(Array*, size_t count);
+/* get/set the i-th (zero based) element of the array */
+void *array_get(Array*, size_t idx);
+bool array_set(Array*, size_t idx, void *item);
+/* add a new element to the end of the array */
+bool array_add(Array*, void *item);
+/* return the number of elements currently stored in the array */
+size_t array_length(Array*);
+
+#endif