aboutsummaryrefslogtreecommitdiff
path: root/src/vec.c
diff options
context:
space:
mode:
authorKimplul <kimi.h.kuparinen@gmail.com>2024-02-24 17:47:39 +0200
committerKimplul <kimi.h.kuparinen@gmail.com>2024-02-24 17:47:39 +0200
commit6b5838f72fe535afb542e888d7d2d2da3571bea2 (patch)
treeaa615a75223027a87ff56c089c0c5b2cc45d00c7 /src/vec.c
downloadqbt-6b5838f72fe535afb542e888d7d2d2da3571bea2.tar.gz
qbt-6b5838f72fe535afb542e888d7d2d2da3571bea2.zip
initial commit
+ Now to do the actual hard parts, heh
Diffstat (limited to 'src/vec.c')
-rw-r--r--src/vec.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/vec.c b/src/vec.c
new file mode 100644
index 0000000..4ba92fb
--- /dev/null
+++ b/src/vec.c
@@ -0,0 +1,49 @@
+#include <stdlib.h>
+#include <assert.h>
+#include <string.h>
+
+#include <qbt/vec.h>
+
+struct vec vec_create(size_t ns)
+{
+ return (struct vec) {
+ .n = 0,
+ .s = 1,
+ .ns = ns,
+ .buf = malloc(ns),
+ };
+}
+
+size_t vec_len(struct vec *v)
+{
+ return v->n;
+}
+
+void *vec_at(struct vec *v, size_t i)
+{
+ assert(i < v->n && "out of vector bounds");
+ return v->buf + i * v->ns;
+}
+
+void *vec_pop(struct vec *v)
+{
+ assert(v->n && "attempting to pop empty vector");
+ v->n--;
+ return v->buf + v->n * v->ns;
+}
+
+void vec_append(struct vec *v, void *n)
+{
+ v->n++;
+ if (v->n >= v->s) {
+ v->s *= 2;
+ v->buf = realloc(v->buf, v->s * v->ns);
+ }
+
+ void *p = vec_at(v, v->n - 1);
+ memcpy(p, n, v->ns);
+}
+
+void vec_destroy(struct vec *v) {
+ free(v->buf);
+}