aboutsummaryrefslogtreecommitdiff
path: root/src/vec.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/vec.h')
-rw-r--r--src/vec.h119
1 files changed, 0 insertions, 119 deletions
diff --git a/src/vec.h b/src/vec.h
deleted file mode 100644
index f5a6fd9..0000000
--- a/src/vec.h
+++ /dev/null
@@ -1,119 +0,0 @@
-#ifndef VEC_TYPE
-#error "Need vector type"
-#endif
-
-#ifndef VEC_NAME
-#error "Need vector name"
-#endif
-
-#include <stddef.h>
-#include <stdlib.h>
-#include <string.h>
-#include <assert.h>
-
-#define UNDERSCORE2(a, b) a##_##b
-#define UNDERSCORE(a, b) UNDERSCORE2(a, b)
-#define VEC(n) UNDERSCORE(VEC_NAME, n)
-
-
-#define VEC_STRUCT VEC_NAME
-struct VEC_STRUCT {
- size_t n;
- size_t s;
- VEC_TYPE *buf;
-};
-
-#ifndef VEC_H
-#define VEC_H
-
-#define foreach_vec(iter, v) \
- for (size_t iter = 0; iter < (v).n; ++iter)
-
-#define vec_uninit(v) \
- (v.buf == NULL)
-#endif
-
-static inline struct VEC_STRUCT VEC(create)()
-{
- const size_t s = 8;
- return (struct VEC_STRUCT) {
- .n = 0,
- .s = s,
- .buf = malloc(s * sizeof(VEC_TYPE)),
- };
-}
-
-static inline size_t VEC(len)(struct VEC_STRUCT *v)
-{
- return v->n;
-}
-
-static inline VEC_TYPE *VEC(at)(struct VEC_STRUCT *v, size_t i)
-{
- assert(i < v->n && "out of vector bounds");
- return &v->buf[i];
-}
-
-static inline VEC_TYPE *VEC(back)(struct VEC_STRUCT *v)
-{
- assert(v->n);
- return &v->buf[v->n - 1];
-}
-
-static inline VEC_TYPE *VEC(pop)(struct VEC_STRUCT *v)
-{
- assert(v->n && "attempting to pop empty vector");
- v->n--;
- return &v->buf[v->n];
-}
-
-static inline void VEC(append)(struct VEC_STRUCT *v, VEC_TYPE n)
-{
- v->n++;
- if (v->n >= v->s) {
- v->s *= 2;
- v->buf = realloc(v->buf, v->s * sizeof(VEC_TYPE));
- assert(v->buf);
- }
-
- v->buf[v->n - 1] = n;
-}
-
-static inline void VEC(reset)(struct VEC_STRUCT *v)
-{
- v->n = 0;
-}
-
-static inline void VEC(destroy)(struct VEC_STRUCT *v) {
- free(v->buf);
-}
-
-typedef int (*vec_comp_t)(void *a, void *b);
-static inline void VEC(sort)(struct VEC_STRUCT *v, vec_comp_t comp)
-{
- qsort(v->buf, v->n, sizeof(VEC_TYPE), (__compar_fn_t)comp);
-}
-
-static inline void VEC(reserve)(struct VEC_STRUCT *v, size_t n)
-{
- if (v->n >= n)
- return;
-
- v->n = n;
- if (v->s >= v->n)
- return;
-
- while (v->s < v->n)
- v->s *= 2;
-
- v->buf = realloc(v->buf, v->s * sizeof(VEC_TYPE));
-}
-
-static inline void VEC(shrink)(struct VEC_STRUCT *v, size_t n)
-{
- /* assert(v->n >= n); */
- v->n = n;
-}
-
-#undef VEC_TYPE
-#undef VEC_NAME