summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2022-12-26 17:56:39 +0100
committerThomas Voss <mail@thomasvoss.com> 2022-12-26 17:56:42 +0100
commite2d634662178d985b90a06deafbe6f24d4ecb519 (patch)
tree11d8e6ae06353cad0295abd2f3ba9093c8f75d20 /src
parent0154d95e53f89acb5e1e845bf22e4ef52a50d088 (diff)
Add n##_remove() and s/size/length
Diffstat (limited to 'src')
-rw-r--r--src/gevector.h21
1 files changed, 15 insertions, 6 deletions
diff --git a/src/gevector.h b/src/gevector.h
index 6827e66..ce47c6a 100644
--- a/src/gevector.h
+++ b/src/gevector.h
@@ -7,13 +7,14 @@
struct n { \
double gfactor; \
size_t capacity; \
- size_t size; \
+ size_t length; \
t *items; \
}; \
\
int n##_new(struct n *, size_t, double); \
int n##_append(struct n *, t); \
int n##_insert(struct n *, t, size_t); \
+ void n##_remove(struct n *, size_t); \
int n##_resize(struct n *, size_t);
#define GEVECTOR_IMPL(t, n) \
@@ -23,7 +24,7 @@
*vec = (struct n) { \
.gfactor = gfactor == 0 ? 1.5f : gfactor, \
.capacity = capacity == 0 ? 16 : capacity, \
- .size = 0, \
+ .length = 0, \
.items = calloc(capacity == 0 ? 16 : capacity, \
sizeof(t)) \
}; \
@@ -33,13 +34,13 @@
int \
n##_append(struct n *vec, t item) \
{ \
- return n##_insert(vec, item, vec->size); \
+ return n##_insert(vec, item, vec->length); \
} \
\
int \
n##_insert(struct n *vec, t item, size_t i) \
{ \
- if (vec->size == vec->capacity) { \
+ if (vec->length == vec->capacity) { \
size_t new_capacity = \
(size_t) (vec->capacity * vec->gfactor); \
if (new_capacity == vec->capacity) \
@@ -47,13 +48,21 @@
if (n##_resize(vec, new_capacity) == -1) \
return -1; \
} \
- for (size_t j = vec->size; j > i; j--) \
+ for (size_t j = vec->length; j > i; j--) \
vec->items[j] = vec->items[j - 1]; \
vec->items[i] = item; \
- vec->size++; \
+ vec->length++; \
return 0; \
} \
\
+ void \
+ n##_remove(struct n *vec, size_t i) \
+ { \
+ for (size_t j = i; j < vec->length - 1; j++) \
+ vec->items[j] = vec->items[j + 1]; \
+ vec->length--; \
+ } \
+ \
int \
n##_resize(struct n *vec, size_t new_capacity) \
{ \