summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2022-12-31 02:31:24 +0100
committerThomas Voss <mail@thomasvoss.com> 2022-12-31 02:31:24 +0100
commitded2768def4108e78eeb9f9bbefe5ea8dfa3a170 (patch)
tree112bc7527b76609c210724d7d094fedf4776cf8d
parent09925a143f29c390e0c539006b38bdca4955643a (diff)
Make vector lengths “public”
-rw-r--r--examples/gevector.c11
-rw-r--r--src/gevector.h73
2 files changed, 48 insertions, 36 deletions
diff --git a/examples/gevector.c b/examples/gevector.c
index df6212e..93a1960 100644
--- a/examples/gevector.c
+++ b/examples/gevector.c
@@ -9,19 +9,16 @@ int
main(void)
{
ivec_t *vec = malloc(sizeof(ivec_t));
- ivec_new(vec, 2, 0);
+ ivec_new(vec, 3, 0);
ivec_push(vec, 42);
ivec_push(vec, 69);
ivec_push(vec, 420);
ivec_insert(vec, 1337, 2);
- GEVECTOR_FOREACH(int, num, *vec)
- printf("%d\n", num);
+ for (size_t i = 0; i < vec->len; i++)
+ printf("%d\n", ivec_get(vec, i));
- GEVECTOR_ENUMERATE(int, i, num, *vec)
- printf("vec->data[%zu] == %d\n", i, num);
-
- free(vec->data);
+ ivec_free(vec);
free(vec);
}
diff --git a/src/gevector.h b/src/gevector.h
index 67f1aed..e36cf09 100644
--- a/src/gevector.h
+++ b/src/gevector.h
@@ -11,17 +11,21 @@
#define GEVECTOR_DEF_API(t, n) GEVECTOR_DEF_API_HELPER(t, n, n##_t)
#define GEVECTOR_DEF_API_HELPER(t, n, n_t) \
typedef struct { \
- double gfac; \
- size_t cap; \
size_t len; \
- t *data; \
+ double __gfac; \
+ size_t __cap; \
+ t *__data; \
} n_t; \
\
- int n##_new(n_t *, size_t, double); \
- int n##_push(n_t *, t); \
- int n##_insert(n_t *, t, size_t); \
- void n##_remove(n_t *, size_t); \
- int n##_resize(n_t *, size_t);
+ int n##_new(n_t *, size_t, double); \
+ t n##_get(n_t *, size_t); \
+ int n##_insert(n_t *, t, size_t); \
+ int n##_push(n_t *, t); \
+ t n##_pop(n_t *); \
+ void n##_remove(n_t *, size_t); \
+ int n##_resize(n_t *, size_t); \
+ size_t n##_len(n_t *); \
+ void n##_free(n_t *);
#define GEVECTOR_DEF_IMPL(t, n) GEVECTOR_DEF_IMPL_HELPER(t, n, n##_t)
#define GEVECTOR_DEF_IMPL_HELPER(t, n, n_t) \
@@ -29,12 +33,18 @@
n##_new(n_t *vec, size_t cap, double gfac) \
{ \
*vec = (n_t) { \
- .gfac = gfac == 0 ? 1.5f : gfac, \
- .cap = cap == 0 ? 16 : cap, \
- .len = 0, \
- .data = calloc(cap == 0 ? 16 : cap, sizeof(t)) \
+ .len = 0, \
+ .__gfac = gfac == 0 ? 1.5f : gfac, \
+ .__cap = cap == 0 ? 16 : cap, \
}; \
- return vec->data == NULL ? -1 : 0; \
+ vec->__data = malloc(vec->__cap * sizeof(t)); \
+ return vec->__data == NULL ? -1 : 0; \
+ } \
+ \
+ t \
+ n##_get(n_t *vec, size_t i) \
+ { \
+ return vec->__data[i]; \
} \
\
int \
@@ -42,20 +52,26 @@
{ \
return n##_insert(vec, elem, vec->len); \
} \
+ \
+ t \
+ n##_pop(n_t *vec) \
+ { \
+ return vec->__data[--vec->len]; \
+ } \
\
int \
n##_insert(n_t *vec, t elem, size_t i) \
{ \
- if (vec->len == vec->cap) { \
- size_t ncap = (size_t) (vec->cap * vec->gfac); \
- if (ncap == vec->cap) \
+ if (vec->len == vec->__cap) { \
+ size_t ncap = (size_t) (vec->__cap * vec->__gfac); \
+ if (ncap == vec->__cap) \
ncap++; \
if (n##_resize(vec, ncap) == -1) \
return -1; \
} \
for (size_t j = vec->len; j > i; j--) \
- vec->data[j] = vec->data[j - 1]; \
- vec->data[i] = elem; \
+ vec->__data[j] = vec->__data[j - 1]; \
+ vec->__data[i] = elem; \
vec->len++; \
return 0; \
} \
@@ -64,25 +80,24 @@
n##_remove(n_t *vec, size_t i) \
{ \
for (size_t j = i; j < vec->len - 1; j++) \
- vec->data[j] = vec->data[j + 1]; \
+ vec->__data[j] = vec->__data[j + 1]; \
vec->len--; \
} \
\
int \
n##_resize(n_t *vec, size_t ncap) \
{ \
- vec->data = realloc(vec->data, sizeof(t) * ncap); \
- if (vec->data == NULL) \
+ vec->__data = realloc(vec->__data, sizeof(t) * ncap); \
+ if (vec->__data == NULL) \
return -1; \
- vec->cap = ncap; \
+ vec->__cap = ncap; \
return 0; \
+ } \
+ \
+ void \
+ n##_free(n_t *vec) \
+ { \
+ free(vec->__data); \
}
-#define GEVECTOR_ENUMERATE(t, i, e, v) \
- for (bool __ge_c = true; __ge_c; __ge_c = false) \
- for (t e = (v).data[0]; __ge_c; __ge_c = false) \
- for (size_t i = 0; i < (v).len; e = (v).data[++i])
-
-#define GEVECTOR_FOREACH(t, e, v) GEVECTOR_ENUMERATE(t, __ge_i, e, v)
-
#endif /* !LIBGE_GEVECTOR_H */