aboutsummaryrefslogtreecommitdiff
path: root/test/vec.h
diff options
context:
space:
mode:
authorKimplul <kimi.h.kuparinen@gmail.com>2025-07-10 19:44:04 +0300
committerKimplul <kimi.h.kuparinen@gmail.com>2025-07-10 19:44:04 +0300
commit645a8026b6f6a89cda5a24d96cf3de39c3ab32e1 (patch)
tree9388612433ba6a1325ece8a50830b9299e7cb598 /test/vec.h
downloadngc-645a8026b6f6a89cda5a24d96cf3de39c3ab32e1.tar.gz
ngc-645a8026b6f6a89cda5a24d96cf3de39c3ab32e1.zip
initial commit
Diffstat (limited to 'test/vec.h')
-rw-r--r--test/vec.h49
1 files changed, 49 insertions, 0 deletions
diff --git a/test/vec.h b/test/vec.h
new file mode 100644
index 0000000..810e663
--- /dev/null
+++ b/test/vec.h
@@ -0,0 +1,49 @@
+#ifndef VEC_H
+#define VEC_H
+
+#include <stddef.h>
+#include <assert.h>
+#include <stdlib.h>
+
+typedef any {};
+
+typedef vec[any type]() {
+ struct <> {
+ size_t n;
+ size_t s;
+ type *buf;
+ };
+
+ static inline struct <> <>create()
+ {
+ return (struct <>){
+ .n = 0,
+ .s = 0,
+ .buf = NULL
+ };
+ }
+
+ static inline void <>append(struct <> *v, type n)
+ {
+ v->n++;
+ if (v->n > v->s) {
+ v->s = v->s == 0 ? 1 : 2 * v->s;
+ v->buf = realloc(v->buf, v->s * sizeof(type));
+ }
+
+ v->buf[v->n - 1] = n;
+ }
+
+ static inline type *<>at(struct <> *v, size_t i)
+ {
+ assert(i < v->n);
+ return &v->buf[i];
+ }
+
+ static inline void <>destroy(struct <> *v)
+ {
+ free(v->buf);
+ }
+}
+
+#endif /* VEC_H */