diff options
author | Ren Kararou <[email protected]> | 2025-01-31 01:34:25 -0600 |
---|---|---|
committer | Ren Kararou <[email protected]> | 2025-01-31 01:34:25 -0600 |
commit | 7f6011bc44e0d7643b3f1eafefbd4fa1390c1661 (patch) | |
tree | a2e6980561800fc70ad36642ebd19eccf5ff343a | |
parent | 46ac53232eaec4c9e52ba42524c9a749090d963f (diff) | |
download | libspicy-7f6011bc44e0d7643b3f1eafefbd4fa1390c1661.tar.gz libspicy-7f6011bc44e0d7643b3f1eafefbd4fa1390c1661.tar.bz2 libspicy-7f6011bc44e0d7643b3f1eafefbd4fa1390c1661.zip |
implement the easy parts of resizable array
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | inc/resarr.h | 29 | ||||
-rw-r--r-- | makefile | 5 | ||||
-rw-r--r-- | src/resarr.c | 73 |
4 files changed, 107 insertions, 3 deletions
diff --git a/.gitignore b/.gitignore index de9f715..96b6b5d 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,6 @@ *.pch build/ obj/ + +# to spite luna +Makefile diff --git a/inc/resarr.h b/inc/resarr.h new file mode 100644 index 0000000..92a1f85 --- /dev/null +++ b/inc/resarr.h @@ -0,0 +1,29 @@ +#ifndef LIBSPICY_RESARR_H +#define LIBSPICY_RESARR_H + +#include <stdlib.h> + +typedef struct ResArr { + size_t elemsize; + size_t elemcount; + size_t allocsize; + void *array; +} stype_resarr; + +#define resarr_access(arr, idx) ((idx) <= (arr).size ? (arr).array[(idx)] : NULL ) + +stype_resarr resarr_create(size_t elemsize); +int resarr_reserve(stype_resarr *arr, size_t elemcount); +int resarr_reserve_exact(stype_resarr *arr, size_t elemcount); +int resarr_push(stype_resarr *arr, void *elem); +int resarr_pop(stype_resarr *arr); +size_t resarr_size(stype_resarr arr); +int resarr_insert(stype_resarr *arr, void *elem, size_t before, size_t count); +int resarr_insert_single(stype_resarr *arr, void *elem, size_t before); +int resarr_delete(stype_resarr *arr, size_t start, size_t end); +int resarr_swap(stype_resarr *first, stype_resarr *second); +int resarr_clear(stype_resarr arr); +int resarr_destroy(stype_resarr *arr); + +#endif + diff --git a/makefile b/makefile index 8ac6378..9eb0e07 100644 --- a/makefile +++ b/makefile @@ -1,11 +1,10 @@ CC:=clang AR:=llvm-ar CFLAGS:=-march=native -O3 -funroll-loops -Wall -Wextra -Werror -fPIC -LDFLAGS:=-flto=thin -lpthread +LDFLAGS:=-flto=thin ifeq ($(shell uname),SunOS) CC = gcc -LDFLAGS += -lsocket CFLAGS += -std=gnu11 else CFLAGS += -std=c11 @@ -13,7 +12,7 @@ endif INCLUDES=-Iinc/ -OBJECTS=obj/salloc.o obj/arena.o +OBJECTS=obj/salloc.o obj/arena.o obj/resarr.o .PHONY: all all: build/lib/libspicy.so build/lib/static/libspicy.a includes diff --git a/src/resarr.c b/src/resarr.c new file mode 100644 index 0000000..4a72217 --- /dev/null +++ b/src/resarr.c @@ -0,0 +1,73 @@ +#include "resarr.h" + +#include <stdlib.h> +#include <stdint.h> +#include <string.h> + +stype_resarr resarr_create(size_t elemsize) { + return (stype_resarr) { .elemsize = elemsize, .elemcount = 0, .allocsize = 0, .array = NULL }; +} + +int resarr_reserve(stype_resarr *arr, size_t elemcount) { + if (arr == NULL) return -1; + size_t ec = 1; + while (ec < elemcount) { + ec *= 2; + } + if (ec * arr->elemsize < arr->allocsize) return -1; + arr->array = realloc(arr->array, (ec * arr->elemsize)); + if (arr->array == NULL) return -1; + arr->allocsize = ec * arr->elemsize; + return 0; +} + +int resarr_reserve_exact(stype_resarr *arr, size_t elemcount) { + if (arr == NULL) return -1; + if (elemcount * arr->elemsize < arr->allocsize) return -1; + arr->array = realloc(arr->array, (elemcount * arr->elemsize)); + if (arr->array == NULL) return -1; + arr->allocsize = elemcount * arr->elemsize; + return 0; +} + +int resarr_push(stype_resarr *arr, void *elem) { + if (arr == NULL) return -1; + if (arr->elemsize * arr->elemcount == arr->allocsize) { + arr->array = realloc(arr->array, (arr->allocsize * 2)); + if (arr->array == NULL) return -1; + arr->allocsize *= 2; + } + memcpy( + ((uintptr_t *)arr->array + (arr->elemsize * arr->elemcount)), + (uintptr_t *)elem, + arr->elemsize + ); + return 0; +} + +int resarr_pop(stype_resarr *arr) { + if (arr == NULL) return -1; + if (arr->array == NULL) return -1; + memset( + ((uintptr_t *)arr->array + (arr->elemsize * arr->elemcount)), + 0, + arr->elemsize + ); + return 0; +} + +size_t resarr_size(stype_resarr arr) { + return arr.elemcount; +} + +// TODO: insert{,_single}, delete, swap, clear + +int resarr_destroy(stype_resarr *arr) { + if (arr == NULL) return -1; + free(arr->array); + arr->elemcount = 0; + arr->elemsize = 0; + arr->allocsize = 0; + return 0; +} + |