From e2d634662178d985b90a06deafbe6f24d4ecb519 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Mon, 26 Dec 2022 17:56:39 +0100 Subject: Add n##_remove() and s/size/length --- src/gevector.h | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'src') 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) \ { \ -- cgit v1.2.3