From a5d1c6da7e5617c15dd729a7907fb38cc25c2e1b Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Sat, 24 Dec 2022 02:26:00 +0100 Subject: Add vectors --- Makefile | 6 +---- examples/Makefile | 8 ++++--- examples/gevector.c | 25 +++++++++++++++++++++ src/gevector.h | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 8 deletions(-) create mode 100644 examples/gevector.c create mode 100644 src/gevector.h diff --git a/Makefile b/Makefile index b89064d..869e959 100644 --- a/Makefile +++ b/Makefile @@ -9,8 +9,4 @@ DGDIR = ${DESTDIR}${GDIR} install: mkdir -p ${DPREFIX}/include ${DGDIR} - cp src/gehashmap.h ${DPREFIX}/include - #cp man/*.3 ${MANDIR}/man3 - #cp man/*.3head ${MANDIR}/man3head - #sed '/^\.ds doc-str-Lb-liblux/d' ${GDIR}/mdoc.local >${DGDIR}/mdoc.local - #grep -v '^\.\\"' man/Lb-desc.tmac >>${DGDIR}/mdoc.local + cp src/*.h ${DPREFIX}/include diff --git a/examples/Makefile b/examples/Makefile index 25d3fa5..af41d5d 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -4,11 +4,13 @@ CC = cc CFLAGS = -Og -g -ggdb LDFLAGS = -I../src -srcs = gehashmap +progs = gehashmap gevector -all: ${srcs} +all: ${progs} gehashmap: gehashmap.c +gevector: gevector.c + clean: - rm -f ${srcs} + rm -f ${progs} diff --git a/examples/gevector.c b/examples/gevector.c new file mode 100644 index 0000000..e554005 --- /dev/null +++ b/examples/gevector.c @@ -0,0 +1,25 @@ +#include +#include + +#include + +GEVECTOR_API(int, ivec); +GEVECTOR_IMPL(int, ivec); + +int +main(void) +{ + struct ivec *vec = malloc(sizeof(struct ivec)); + ivec_new(vec, 2, 0); + + ivec_append(vec, 42); + ivec_append(vec, 69); + ivec_append(vec, 420); + ivec_insert(vec, 1337, 2); + + for (size_t i = 0; i < vec->size; i++) + printf("%d\n", vec->items[i]); + + free(vec->items); + free(vec); +} diff --git a/src/gevector.h b/src/gevector.h new file mode 100644 index 0000000..cf0b6f9 --- /dev/null +++ b/src/gevector.h @@ -0,0 +1,63 @@ +#ifndef LIBGE_GEVECTOR_H +#define LIBGE_GEVECTOR_H + +#include + +#define GEVECTOR_API(t, n) \ + struct n { \ + double gfactor; \ + size_t capacity; \ + size_t size; \ + t *items; \ + }; \ + \ + int n##_new(struct n *, size_t, double); \ + int n##_append(struct n *, t); \ + int n##_insert(struct n *, t, size_t); \ + int n##_resize(struct n *, size_t); + +#define GEVECTOR_IMPL(t, n) \ + int \ + n##_new(struct n *vec, size_t capacity, double gfactor) \ + { \ + *vec = (struct n) { \ + .gfactor = gfactor == 0 ? 1.5f : gfactor, \ + .capacity = capacity, \ + .size = 0, \ + .items = calloc(capacity == 0 ? 16 : capacity, \ + sizeof(t)) \ + }; \ + return vec->items == NULL ? -1 : 0; \ + } \ + \ + int \ + n##_append(struct n *vec, t item) \ + { \ + return n##_insert(vec, item, vec->size); \ + } \ + \ + int \ + n##_insert(struct n *vec, t item, size_t i) \ + { \ + if (vec->size == vec->capacity \ + && n##_resize(vec, (size_t) (vec->capacity \ + * vec->gfactor)) \ + == -1) \ + return -1; \ + for (size_t j = vec->size; j > i; j--) \ + vec->items[j] = vec->items[j - 1]; \ + vec->items[i] = item; \ + vec->size++; \ + } \ + \ + int \ + n##_resize(struct n *vec, size_t new_capacity) \ + { \ + vec->items = realloc(vec->items, sizeof(t) * new_capacity); \ + if (vec->items == NULL) \ + return -1; \ + vec->capacity = new_capacity; \ + return 0; \ + } + +#endif /* !LIBGE_GEVECTOR_H */ -- cgit v1.2.3