aoc-2019-c

git clone https://git.sinitax.com/sinitax/aoc-2019-c
Log | Files | Refs | README | sfeed.txt

commit 14f907f61140e0c85eb8b890c8740cdce690a689
Author: Louis Burda <quent.burda@gmail.com>
Date:   Fri, 15 Mar 2024 22:52:17 +0100

Squashed 'src/lib/libdvec/' content from commit 6746f4e

git-subtree-dir: src/lib/libdvec
git-subtree-split: 6746f4e40a7b07dab80766155058d381e435e767

Diffstat:
A.gitignore | 7+++++++
A.gitmodules | 3+++
ALICENSE | 21+++++++++++++++++++++
AMakefile | 59+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Abuild.jst.tmpl | 69+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aconfigure | 8++++++++
Ainclude/dvec.h | 133+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Alib/liballoc | 1+
Alibdvec.api | 22++++++++++++++++++++++
Alibdvec.lds | 26++++++++++++++++++++++++++
Asrc/dvec.c | 336+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/test.c | 68++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
12 files changed, 753 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1,7 @@ +compile_commands.json +build +build.jst +.cache +vgcore* +test +.gdb_history diff --git a/.gitmodules b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "lib/liballoc"] + path = lib/liballoc + url = git@sinitax.com:sinitax/liballoc diff --git a/LICENSE b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 Louis Burda + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Makefile b/Makefile @@ -0,0 +1,59 @@ +PREFIX ?= /usr/local +LIBDIR ?= /lib +INCLDIR ?= /include + +CFLAGS = -I include -I lib/liballoc/include +CFLAGS += -Wunused-function -Wunused-variable -Wno-prototype +CFLAGS += -Wconversion -Wsign-compare -Werror + +ifeq "$(DEBUG)" "1" +CFLAGS += -Og -g +else +CFLAGS += -O2 +endif + +ifeq "$(ASSERT_ARGS)" "1" +CFLAGS += -DLIBDVEC_ASSERT_ARGS=1 +endif + +ifeq "$(ASSERT_ALLOC)" "1" +CFLAGS += -DLIBDVEC_ASSERT_ALLOC=1 +endif + +all: build/libdvec.so build/libdvec.a build/test + +clean: + rm -rf build + +cleanall: clean + make -C lib/liballoc cleanall + +build: + mkdir build + +lib/liballoc/build/liballoc.a: + make -C lib/liballoc build/liballoc.a + +build/libdvec.a: src/dvec.c include/dvec.h libdvec.api | build + $(CC) -o build/tmp.o src/dvec.c $(CFLAGS) -r + objcopy --keep-global-symbols=libdvec.api build/tmp.o build/fixed.o + ar rcs $@ build/fixed.o + +build/libdvec.so: src/dvec.c include/dvec.h libdvec.lds | build + $(CC) -o $@ src/dvec.c -fPIC $(CFLAGS) \ + -shared -Wl,-version-script libdvec.lds + +build/test: src/test.c build/libdvec.a lib/liballoc/build/liballoc.a + $(CC) -o $@ $^ $(CFLAGS) + +install: + install -m644 include/dvec.h -t "$(DESTDIR)$(PREFIX)$(INCLDIR)" + install -m755 build/libdvec.so -t "$(DESTDIR)$(PREFIX)$(LIBDIR)" + install -m755 build/libdvec.a -t "$(DESTDIR)$(PREFIX)$(LIBDIR)" + +uninstall: + rm -f "$(DESTDIR)$(PREFIX)$(INCLDIR)/dvec.h" + rm -f "$(DESTDIR)$(PREFIX)$(LIBDIR)/libdvec.so" + rm -f "$(DESTDIR)$(PREFIX)$(LIBDIR)/libdvec.a" + +.PHONY: all clean cleanall install uninstall diff --git a/build.jst.tmpl b/build.jst.tmpl @@ -0,0 +1,69 @@ +#default PREFIX /usr/local +#default INCLDIR /include +#default LIBDIR /lib +#default CC gcc + +#ifdef LIBDVEC_DEBUG +#define DEBUG 1 +#endif + +#ifdef DEBUG +#define OPT_CFLAGS -Og -g +#else +#define OPT_CFLAGS -O2 +#endif + +cflags = -Wunused-function -Wunused-variable -Wconversion -Wformat + -I include -I lib/liballoc/include + #{OPT_CFLAGS} #{LIBDVEC_EXTRA_CFLAGS} #{EXTRA_CFLAGS} + +rule liba + #{CC} -o $out.tmp.o $in $cflags -r + objcopy --keep-global-symbols=libdvec.api $out.tmp.o $out.fixed.o + ar rcs $out $out.fixed.o + rm $out.tmp.o $out.fixed.o + +rule libso + #{CC} -o $out $in $cflags -shared -Wl,-version-script libdvec.lds + +rule cc + #{CC} -o $out $in $cflags + +rule mkdir + mkdir $out + +target build + mkdir + +target lib/liballoc/build/liballoc.a + just lib/liballoc + +target build/libdvec.a + liba src/dvec.c | include/dvec.h build + +target build/libdvec.so + libso src/dvec.c | include/dvec.h build + +target build/test + cc src/test.c build/libdvec.a lib/liballoc/build/liballoc.a | build + +command clean + rm -rf build + +command cleanall + just clean + just -C lib/liballoc cleanall + +command install + install -m755 build/libdvec.a -t "#{DESTDIR}#{PREFIX}#{LIBDIR}" + install -m755 build/libdvec.so -t "#{DESTDIR}#{PREFIX}#{LIBDIR}" + install -m644 include/dvec.h -t "#{DESTDIR}#{PREFIX}#{INCLDIR}" + +command uninstall + rm -f "#{DESTDIR}#{PREFIX}#{LIBDIR}/libdvec.a" + rm -f "#{DESTDIR}#{PREFIX}#{LIBDIR}/libdvec.so" + rm -f "#{DESTDIR}#{PREFIX}#{INCLDIR}/dvec.h" + +command all + just build/libdvec.a build/libdvec.so build/test + diff --git a/configure b/configure @@ -0,0 +1,8 @@ +#!/bin/sh + +tmpl "$@" build.jst.tmpl > build.jst +for lib in ./lib/*; do + pushd $lib + ./configure "$@" + popd +done diff --git a/include/dvec.h b/include/dvec.h @@ -0,0 +1,133 @@ +#pragma once + +#include "allocator.h" + +#include <sys/types.h> +#include <stdbool.h> +#include <stddef.h> + +#define DVEC_ITER(dvec, p) (p) = NULL; ((p) = dvec_iter_fwd((dvec), (p))); +#define DVEC_ITER_BWD(dvec, p) (p) = NULL; ((p) = dvec_iter_bwd((dvec), (p))); + +#define DVEC_STRERR_INIT \ + [DVEC_OK] = "Success" + +#ifdef LIBDVEC_ASSERT_ARGS +#include "stdlib.h" +#define LIBDVEC_ABORT_ON_ARGS(cond) do { if (cond) abort(); } while (0) +#else +#define LIBDVEC_ABORT_ON_ARGS(cond) +#endif + +#ifdef LIBDVEC_ASSERT_ALLOC +#include "stdlib.h" +#define LIBDVEC_ABORT_ON_ALLOC(cond) do { if (cond) abort(); } while (0) +#else +#define LIBDVEC_ABORT_ON_ALLOC(cond) +#endif + +enum { + DVEC_OK = 0, +}; + +struct dvec { + size_t dsize; + size_t len, cap; + + void *data; + + const struct allocator *allocator; +}; + +typedef bool (*dvec_sort_order_fn)(const void *p1, const void *p2, void *u); +typedef int (*dvec_search_cmp_fn)(const void *cur, void *user); + +int dvec_init(struct dvec *dvec, size_t dsize, size_t cap, + const struct allocator *allocator); +int dvec_deinit(struct dvec *dvec); + +struct dvec *dvec_alloc(size_t dsize, size_t cap, + const struct allocator *allocator, int *rc); +int dvec_free(struct dvec *dvec); + +int dvec_copy(struct dvec *dst, struct dvec *src); +void dvec_move(struct dvec *dst, struct dvec *src); +void dvec_swap(struct dvec *dst, struct dvec *src); + +void dvec_clear(struct dvec *dvec); +int dvec_reserve(struct dvec *dvec, size_t cap); +int dvec_shrink(struct dvec *dvec); + +int dvec_add(struct dvec *dvec, size_t index, size_t count); +void dvec_rm(struct dvec *dvec, size_t index, size_t count); +void dvec_replace(struct dvec *dvec, size_t index, + const void *data, size_t count); + +void *dvec_iter_fwd(const struct dvec *dvec, const void *p); +void *dvec_iter_bwd(const struct dvec *dvec, const void *p); + +int dvec_bubble_sort(struct dvec *dvec, + bool reverse, dvec_sort_order_fn in_order, void *user); + +ssize_t dvec_binary_search(struct dvec *dvec, dvec_search_cmp_fn search_cmp, + void *user, ssize_t *lower, ssize_t *higher); + +static inline void * +dvec_at(const struct dvec *dvec, size_t index) +{ + LIBDVEC_ABORT_ON_ARGS(!dvec || index >= dvec->len); + + return dvec->data + index * dvec->dsize; +} + +static inline void * +dvec_at_back(const struct dvec *dvec, size_t index) +{ + LIBDVEC_ABORT_ON_ARGS(!dvec || !index || index >= dvec->len); + + return dvec->data + (dvec->len - 1 - index) * dvec->dsize; +} + +static inline void * +dvec_front(const struct dvec *dvec) +{ + LIBDVEC_ABORT_ON_ARGS(!dvec); + + return dvec->data; +} + +static inline void * +dvec_back(const struct dvec *dvec) +{ + LIBDVEC_ABORT_ON_ARGS(!dvec); + + return dvec->data + (dvec->len - 1) * dvec->dsize; +} + +static inline bool +dvec_empty(const struct dvec *dvec) +{ + LIBDVEC_ABORT_ON_ARGS(!dvec); + + return !dvec->len; +} + +static inline size_t +dvec_len(const struct dvec *dvec) +{ + LIBDVEC_ABORT_ON_ARGS(!dvec); + + return dvec->len; +} + +static inline int +dvec_add_back(struct dvec *dvec, size_t count) +{ + return dvec_add(dvec, dvec->len, count); +} + +static inline void +dvec_rm_back(struct dvec *dvec, size_t count) +{ + dvec_rm(dvec, dvec->len - count, count); +} diff --git a/lib/liballoc b/lib/liballoc @@ -0,0 +1 @@ +Subproject commit 3f388a2659ae2d121322101930d33412815d84e6 diff --git a/libdvec.api b/libdvec.api @@ -0,0 +1,22 @@ +dvec_init +dvec_deinit + +dvec_alloc +dvec_free + +dvec_copy +dvec_swap + +dvec_clear +dvec_reserve +dvec_shrink + +dvec_add +dvec_rm +dvec_replace + +dvec_iter_fwd +dvec_iter_bwd + +dvec_bubble_sort +dvec_binary_search diff --git a/libdvec.lds b/libdvec.lds @@ -0,0 +1,26 @@ +LIBDVEC_1.1.2 { + global: + dvec_init; + dvec_deinit; + + dvec_alloc; + dvec_free; + + dvec_copy; + dvec_swap; + + dvec_clear; + dvec_reserve; + dvec_shrink; + + dvec_add; + dvec_rm; + dvec_replace; + + dvec_iter_fwd; + dvec_iter_bwd; + + dvec_bubble_sort; + dvec_binary_search; + local: *; +}; diff --git a/src/dvec.c b/src/dvec.c @@ -0,0 +1,336 @@ +#include "dvec.h" + +#include <string.h> +#include <stdlib.h> + +int +dvec_init(struct dvec *dvec, size_t dsize, size_t cap, + const struct allocator *allocator) +{ + int rc; + + LIBDVEC_ABORT_ON_ARGS(!dvec || !dsize || !allocator); + + dvec->allocator = allocator; + dvec->dsize = dsize; + dvec->cap = cap; + dvec->len = 0; + dvec->data = NULL; + if (dvec->cap) { + dvec->data = allocator->alloc(allocator, cap * dsize, &rc); + LIBDVEC_ABORT_ON_ALLOC(!dvec->data); + if (!dvec->data) return -rc; + } + + return 0; +} + +int +dvec_deinit(struct dvec *dvec) +{ + int rc; + + LIBDVEC_ABORT_ON_ARGS(!dvec); + + rc = dvec->allocator->free(dvec->allocator, dvec->data); + LIBDVEC_ABORT_ON_ALLOC(rc); + + return -rc; +} + +struct dvec * +dvec_alloc(size_t dsize, size_t cap, + const struct allocator *allocator, int *_rc) +{ + struct dvec *dvec; + int *rc, stub; + + LIBDVEC_ABORT_ON_ARGS(!allocator); + + rc = _rc ? _rc : &stub; + + dvec = allocator->alloc(allocator, sizeof(struct dvec), rc); + LIBDVEC_ABORT_ON_ALLOC(!dvec); + if (!dvec) { + *rc = -*rc; + return NULL; + } + + *rc = dvec_init(dvec, dsize, cap, allocator); + if (*rc) { + allocator->free(allocator, dvec); + return NULL; + } + + return dvec; +} + +int +dvec_free(struct dvec *dvec) +{ + const struct allocator *allocator; + int rc; + + LIBDVEC_ABORT_ON_ARGS(!dvec); + + allocator = dvec->allocator; + + rc = dvec_deinit(dvec); + if (rc) return rc; + + rc = allocator->free(allocator, dvec); + LIBDVEC_ABORT_ON_ALLOC(rc); + if (rc) return -rc; + + return 0; +} + +int +dvec_copy(struct dvec *dst, struct dvec *src) +{ + int rc; + + LIBDVEC_ABORT_ON_ARGS(!dst || !src || dst->dsize != src->dsize); + + rc = dvec_reserve(dst, src->len); + if (rc) return rc; + + dst->len = src->len; + memcpy(dst->data, src->data, src->len * src->dsize); + + return 0; +} + +void +dvec_move(struct dvec *dst, struct dvec *src) +{ + LIBDVEC_ABORT_ON_ARGS(!dst || !src); + + memcpy(dst, src, sizeof(struct dvec)); +} + +void +dvec_swap(struct dvec *dst, struct dvec *src) +{ + struct dvec tmp; + + LIBDVEC_ABORT_ON_ARGS(!dst || !src); + + memcpy(&tmp, dst, sizeof(struct dvec)); + memcpy(dst, src, sizeof(struct dvec)); + memcpy(src, &tmp, sizeof(struct dvec)); +} + +void +dvec_clear(struct dvec *dvec) +{ + LIBDVEC_ABORT_ON_ARGS(!dvec); + + dvec->len = 0; +} + +int +dvec_reserve(struct dvec *dvec, size_t len) +{ + void *data; + size_t cap; + int rc; + + LIBDVEC_ABORT_ON_ARGS(!dvec); + + if (len <= dvec->cap) return 0; + + cap = 2 * dvec->cap; + if (len > cap) cap = len; + + data = dvec->allocator->realloc(dvec->allocator, + dvec->data, cap * dvec->dsize, &rc); + LIBDVEC_ABORT_ON_ALLOC(!data); + if (!data) return -rc; + dvec->data = data; + dvec->cap = cap; + + return 0; +} + +int +dvec_shrink(struct dvec *dvec) +{ + void *data; + size_t cap; + int rc; + + LIBDVEC_ABORT_ON_ARGS(!dvec); + + if (!dvec->len) { + dvec->cap = 0; + rc = dvec->allocator->free(dvec->allocator, dvec->data); + LIBDVEC_ABORT_ON_ALLOC(rc); + if (rc) return -rc; + } else { + cap = dvec->len; + data = dvec->allocator->realloc(dvec->allocator, + &dvec->data, cap * dvec->dsize, &rc); + LIBDVEC_ABORT_ON_ALLOC(!data); + if (!data) return -rc; + dvec->data = data; + dvec->cap = cap; + } + + return 0; +} + +int +dvec_add(struct dvec *dvec, size_t index, size_t len) +{ + int rc; + + LIBDVEC_ABORT_ON_ARGS(!dvec || index > dvec->len); + + rc = dvec_reserve(dvec, dvec->len + len); + if (rc) return rc; + + if (index < dvec->len) { + memmove(dvec->data + (index + len) * dvec->dsize, + dvec->data + index * dvec->dsize, + (dvec->len - index) * dvec->dsize); + } + + dvec->len += len; + + return 0; +} + +void +dvec_rm(struct dvec *dvec, size_t index, size_t count) +{ + LIBDVEC_ABORT_ON_ARGS(!dvec || index + count > dvec->len); + + if (index + count < dvec->len) { + memmove(dvec->data + index * dvec->dsize, + dvec->data + (index + count) * dvec->dsize, + (dvec->len - (index + count)) * dvec->dsize); + } + + dvec->len -= count; +} + +void +dvec_replace(struct dvec *dvec, size_t index, const void *data, size_t count) +{ + LIBDVEC_ABORT_ON_ARGS(!dvec || !data || index + count > dvec->len); + + memcpy(dvec->data + index * dvec->dsize, data, count * dvec->dsize); +} + +void * +dvec_iter_fwd(const struct dvec *dvec, const void *p) +{ + LIBDVEC_ABORT_ON_ARGS(!dvec); + LIBDVEC_ABORT_ON_ARGS(p && (p < dvec->data)); + LIBDVEC_ABORT_ON_ARGS(p && (p >= dvec->data + dvec->len * dvec->dsize)); + LIBDVEC_ABORT_ON_ARGS(p && ((size_t) (p - dvec->data) % dvec->dsize)); + + if (p == NULL) { + if (dvec->len > 0) + return dvec->data; + else + return NULL; + } else if (p < dvec->data + dvec->dsize * (dvec->len - 1)) { + return (void *) p + dvec->dsize; + } else { + return NULL; + } +} + +void * +dvec_iter_bwd(const struct dvec *dvec, const void *p) +{ + LIBDVEC_ABORT_ON_ARGS(!dvec); + LIBDVEC_ABORT_ON_ARGS(p && (p < dvec->data)); + LIBDVEC_ABORT_ON_ARGS(p && (p >= dvec->data + dvec->len * dvec->dsize)); + LIBDVEC_ABORT_ON_ARGS(p && ((size_t) (p - dvec->data) % dvec->dsize)); + + if (p == NULL) { + if (dvec->len > 0) + return dvec->data + (dvec->len - 1) * dvec->dsize; + else + return NULL; + } else if (p > dvec->data) { + return (void *) p - dvec->dsize; + } else { + return NULL; + } +} + +int +dvec_bubble_sort(struct dvec *dvec, bool reverse, + dvec_sort_order_fn in_order, void *user) +{ + void *cur, *next, *tmp; + size_t i, k; + int rc; + + rc = dvec_reserve(dvec, dvec->len + 1); + if (rc) return rc; + + tmp = dvec->data + dvec->len * dvec->dsize; + + for (i = 1; i < dvec->len; i++) { + cur = dvec->data; + for (k = 0; k < dvec->len - i; k++) { + next = cur + dvec->dsize; + if (in_order(cur, next, user) == reverse) { + memcpy(tmp, cur, dvec->dsize); + memcpy(cur, next, dvec->dsize); + memcpy(next, tmp, dvec->dsize); + } + cur = next; + } + } + + return 0; +} + +ssize_t +dvec_binary_search(struct dvec *dvec, dvec_search_cmp_fn search_cmp, + void *user, ssize_t *lower, ssize_t *higher) +{ + size_t min, max, pos; + int rc; + + min = 0; + max = dvec->len; + + if (lower) *lower = -1; + if (higher) *higher = -1; + while (min < max) { + pos = min + (max - min) / 2; + rc = search_cmp(dvec_at(dvec, pos), user); + if (!rc) { + if (lower && pos > 0) + *lower = (ssize_t) pos - 1; + if (higher && pos < dvec->len - 1) + *higher = (ssize_t) pos + 1; + return (ssize_t) pos; + } else if (rc > 0) { + if (pos + 1 == max) { + if (lower) *lower = (ssize_t) min; + if (higher && min < dvec->len - 1) + *higher = (ssize_t) min + 1; + break; + } + min = pos + 1; + } else { + if (min == pos) { + if (higher) *higher = (ssize_t) min; + if (lower && min > 0) + *lower = (ssize_t) min - 1; + break; + } + max = pos; + } + } + + return -1; +} diff --git a/src/test.c b/src/test.c @@ -0,0 +1,68 @@ +#include "allocator.h" +#include "dvec.h" + +#include <err.h> +#include <stdbool.h> +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +#define LIBDVEC_ERR(rc) errx(1, "libdvec: %s", \ + rc < 0 ? strerror(-rc) : dvec_err[rc]) + +static const char *dvec_err[] = { + DVEC_STRERR_INIT +}; + +bool +str_sort_order(const void *p1, const void *p2, void *user) +{ + const char *s1 = *(const char **)p1; + const char *s2 = *(const char **)p2; + + return strcmp(s1, s2) <= 0; +} + +int +str_search(const void *p, void *user) +{ + const char *str = *(const char **)p, *cmp = user; + + return strcmp(cmp, str); +} + +int +main(int argc, const char **argv) +{ + struct dvec dvec; + ssize_t below, above, on; + int i, rc; + const char **val; + + rc = dvec_init(&dvec, sizeof(const char *), 16, &stdlib_heap_allocator); + if (rc) LIBDVEC_ERR(rc); + + for (i = 1; i < argc; i++) { + rc = dvec_add_back(&dvec, 1); + if (rc) LIBDVEC_ERR(rc); + val = dvec_back(&dvec); + *val = argv[i]; + } + + rc = dvec_bubble_sort(&dvec, false, str_sort_order, NULL); + if (rc) LIBDVEC_ERR(rc); + + on = dvec_binary_search(&dvec, str_search, "abc", &below, &above); + + for (DVEC_ITER(&dvec, val)) + printf("%s\n", *val); + + printf("dvec len: %lu\n", dvec.len); + + printf("\n"); + printf("below: %li\n", below); + printf("on: %li\n", on); + printf("above: %li\n", above); + + dvec_deinit(&dvec); +}