aboutsummaryrefslogtreecommitdiff
path: root/src/vector.c
diff options
context:
space:
mode:
authorDanny van Kooten <dannyvankooten@users.noreply.github.com>2020-03-12 15:07:09 +0100
committerDanny van Kooten <dannyvankooten@users.noreply.github.com>2020-03-12 15:07:09 +0100
commit791e03d8199fa1177f970f45a8a436912d229d1c (patch)
treebd24f5ad87bca8e932e9db8176d46a31d578c43c /src/vector.c
parenta39e8c713e0a8ea9f2f8e455c1ce2b4d0c3f5115 (diff)
downloadunja-791e03d8199fa1177f970f45a8a436912d229d1c.tar.gz
unja-791e03d8199fa1177f970f45a8a436912d229d1c.zip
add vector type
Diffstat (limited to 'src/vector.c')
-rw-r--r--src/vector.c23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/vector.c b/src/vector.c
new file mode 100644
index 0000000..46a65e5
--- /dev/null
+++ b/src/vector.c
@@ -0,0 +1,23 @@
+#include <stdlib.h>
+#include "vector.h"
+
+/* create a new vector of the given capacity */
+struct vector* vector_new(int cap) {
+ struct vector *l = malloc(sizeof *l);
+ l->size = 0;
+ l->cap = cap;
+ l->values = malloc(l->cap * sizeof *l->values);
+ return l;
+}
+
+/* push a new value to the end of the vector's memory */
+int vector_push(struct vector *vec, void *value) {
+ vec->values[vec->size++] = value;
+ return vec->size - 1;
+}
+
+/* free vector related memory */
+void vector_free(struct vector *l) {
+ free(l->values);
+ free(l);
+} \ No newline at end of file