diff options
Diffstat (limited to 'src/gevector.h')
-rw-r--r-- | src/gevector.h | 21 |
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) \ { \ |