From 9945737a8f0612b8980d9da3db3e6a4ab8a247f4 Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Fri, 25 Oct 2019 09:27:56 +0000 Subject: Add bigkv --- src/libdatastruct/bigkv_free.c | 13 +++++++++ src/libdatastruct/bigkv_init.c | 63 ++++++++++++++++++++++++++++++++++++++++ src/libdatastruct/bigkv_search.c | 12 ++++++++ src/libdatastruct/bigkv_zero.c | 5 ++++ 4 files changed, 93 insertions(+) create mode 100644 src/libdatastruct/bigkv_free.c create mode 100644 src/libdatastruct/bigkv_init.c create mode 100644 src/libdatastruct/bigkv_search.c create mode 100644 src/libdatastruct/bigkv_zero.c (limited to 'src/libdatastruct') diff --git a/src/libdatastruct/bigkv_free.c b/src/libdatastruct/bigkv_free.c new file mode 100644 index 0000000..9b43c52 --- /dev/null +++ b/src/libdatastruct/bigkv_free.c @@ -0,0 +1,13 @@ +/* ISC license. */ + +#include +#include +#include +#include + +void bigkv_free (bigkv_t *b) +{ + avltree_free(&b->map) ; + genalloc_free(bigkv_node_t, &b->nodes) ; + stralloc_free(&b->storage) ; +} diff --git a/src/libdatastruct/bigkv_init.c b/src/libdatastruct/bigkv_init.c new file mode 100644 index 0000000..aa7f296 --- /dev/null +++ b/src/libdatastruct/bigkv_init.c @@ -0,0 +1,63 @@ +/* ISC license. */ + +#include +#include +#include + +#include +#include +#include +#include + +static void *bigkv_dtok (uint32_t d, void *p) +{ + bigkv_t *b = p ; + return b->storage.s + genalloc_s(bigkv_node_t, &b->nodes)[d].k ; +} + +static int bigkv_cmp (void const *a, void const *b, void *p) +{ + (void)p ; + return strcmp((char const *)a, (char const *)b) ; +} + +int bigkv_init (bigkv_t *b, char const *const *argv, char delim, char const *prefix, char const *stop, uint32_t options) +{ + size_t prefixlen = prefix ? strlen(prefix) : 0 ; + avltree_init(&b->map, 3, 3, 8, &bigkv_dtok, &bigkv_cmp, b) ; + for (; *argv && !(stop && !strcmp(*argv, stop)) ; argv++) + { + bigkv_node_t node = { .k = b->storage.len } ; + uint32_t d ; + char const *s = *argv ; + char *p ; + if (prefixlen) + { + if (!strncmp(s, prefix, prefixlen)) break ; + s += prefixlen ; + } + if (avltree_search(&b->map, s, &d)) + { + if (options & BIGKV_OPTIONS_NODUP) goto invalid ; + else avltree_delete(&b->map, s) ; + } + if (!stralloc_cats(&b->storage, s) || !stralloc_0(&b->storage)) goto err ; + p = strchr(s, delim) ; + if (p) + { + node.v = node.k + (p - s) + 1 ; + b->storage.s[node.k + (p - s)] = 0 ; + } + else node.v = b->storage.len - 1 ; + d = genalloc_len(bigkv_node_t, &b->nodes) ; + if (!genalloc_append(bigkv_node_t, &b->nodes, &node)) goto err ; + if (!avltree_insert(&b->map, d)) goto err ; + } + return 1 ; + + invalid: + errno = EINVAL ; + err: + bigkv_free(b) ; + return 0 ; +} diff --git a/src/libdatastruct/bigkv_search.c b/src/libdatastruct/bigkv_search.c new file mode 100644 index 0000000..bb91e3a --- /dev/null +++ b/src/libdatastruct/bigkv_search.c @@ -0,0 +1,12 @@ +/* ISC license. */ + +#include +#include +#include + +char const *bigkv_search (bigkv_t const *b, char const *k) +{ + uint32_t i ; + if (!avltree_search(&b->map, k, &i)) return 0 ; + return b->storage.s + genalloc_s(bigkv_node_t, &b->nodes)[i].v ; +} diff --git a/src/libdatastruct/bigkv_zero.c b/src/libdatastruct/bigkv_zero.c new file mode 100644 index 0000000..5fc262f --- /dev/null +++ b/src/libdatastruct/bigkv_zero.c @@ -0,0 +1,5 @@ +/* ISC license. */ + +#include + +bigkv_t const bigkv_zero = BIGKV_ZERO ; -- cgit v1.2.3