Hey, So I've been using avltree & gensetdyn (amongst others) lately in a little project of mine, and I have some questions:
- When using avltree_iter() we need to pass a avliterfunc_t_ref, which takes 3 arguments. If I understand correctly, first is the uint32_t index, then the tree's "internal" (unsigned int) index, and my pointer. So if I wanted, from there, to remove an item from the avltree, I could either use avltree_deletenode(tree, ARG2) or avltree_delete(tree, KEY) where KEY is the one from dtok(ARG1,p) -- would all that be correct, or did I miss/misunderstood something? - So it means avltree_deletenode() needs the avltree's own/internal index, whereas gensetdyn_delete() needs "our" uint32_t index, correct? I.e. if I have that uint32_t and want to remove things from both the avltree & gensetdyn (I'm using the later to store the actual data indexed via the former, btw) then for avltree I need to get the key (e.g. via t->dtok(u,p)) - I think there's a bug in skalibs right now, avltree_deletenode() is broken and things don't even compile; I believe the attached patch is a correct fix for it. (I ended up using avltree_delete directly, guessing you usually do that as well maybe?) - And on a completely unrelated topic, is there really no way (macro/function) in skalibs to remove an item from a genalloc? I get it for stralloc, it's not really thought of as an array but a string, you rarely want to remove one char from somehere in the middle, whatever. But with a genalloc, I'd expect one to (often) be wanting to remove an item from the array, and not always/only the last one, yet I can't see how that's supposed to be done? (Again, as in via a macro or something, of course one can - as I do - handle the memmove oneself) Maybe I'm the odd one for wanting to do such a thing on a regular basis, but something like in the (other) attached patch, I feel, might be useful. I know it certainly wasn't the first time I have to write that sort of things... Thanks for the help! Cheers,
>From c9d5bb194408f22db8ef859203f28c6748e78ab9 Mon Sep 17 00:00:00 2001 From: Olivier Brunel <[email protected]> Date: Thu, 12 Apr 2018 18:05:14 +0200 Subject: [PATCH] Fix avltree_deletenode Signed-off-by: Olivier Brunel <[email protected]> --- src/include/skalibs/avltree.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/include/skalibs/avltree.h b/src/include/skalibs/avltree.h index 1ce7385..8234d95 100644 --- a/src/include/skalibs/avltree.h +++ b/src/include/skalibs/avltree.h @@ -48,7 +48,7 @@ extern int avltree_newnode (avltree *, uint32_t, uint32_t *) ; #define avltree_insertnode(t, i) avltree_setroot(t, avlnode_insertnode(avltree_nodes(t), avltree_totalsize(t), avltree_root(t), i, (t)->dtok, (t)->kcmp, (t)->external)) extern int avltree_insert (avltree *, uint32_t) ; -#define avltree_deletenode(t, i) avltree_delete(t, (*(t)->dtok)(avltree_data(t, i))) +#define avltree_deletenode(t, i) avltree_delete(t, (*(t)->dtok)(avltree_data(t, i),(t)->external)) extern int avltree_delete (avltree *, void const *) ; #define avltree_iter(t, f, p) avlnode_iter(avltree_nodes(t), avltree_totalsize(t), avltree_root(t), f, p) -- 2.15.1
>From 0c8186bfedc2dfa6d1bd083dafc3ec9a7c46828d Mon Sep 17 00:00:00 2001 From: Olivier Brunel <[email protected]> Date: Thu, 12 Apr 2018 18:51:40 +0200 Subject: [PATCH] Add genalloc_delete Signed-off-by: Olivier Brunel <[email protected]> --- src/include/skalibs/genalloc.h | 2 ++ src/libstddjb/genalloc_delete_size.c | 12 ++++++++++++ 2 files changed, 14 insertions(+) create mode 100644 src/libstddjb/genalloc_delete_size.c diff --git a/src/include/skalibs/genalloc.h b/src/include/skalibs/genalloc.h index da83c2f..acf2bec 100644 --- a/src/include/skalibs/genalloc.h +++ b/src/include/skalibs/genalloc.h @@ -28,6 +28,8 @@ typedef stralloc genalloc, *genalloc_ref ; #define genalloc_reverse(type, g) stralloc_reverse_blocks((g), sizeof(type)) #define genalloc_insertb(type, g, offset, s, n) stralloc_insertb((g), (offset)*sizeof(type), (char const *)(s), (n)*sizeof(type)) #define genalloc_insert(type, g1, offset, g2) stralloc_insert((g1), (offset)*sizeof(type), (g2)) +#define genalloc_delete(type,g,i) genalloc_delete_size((g),sizeof(type),i) +extern void genalloc_delete_size (genalloc *, size_t, size_t) ; #define genalloc_deepfree(type, g, f) genalloc_deepfree_size(g, (freefunc_t_ref)(f), sizeof(type)) extern void genalloc_deepfree_size (genalloc *, freefunc_t_ref, size_t) ; diff --git a/src/libstddjb/genalloc_delete_size.c b/src/libstddjb/genalloc_delete_size.c new file mode 100644 index 0000000..a289000 --- /dev/null +++ b/src/libstddjb/genalloc_delete_size.c @@ -0,0 +1,12 @@ +/* ISC license. */ + +#include <string.h> +#include <skalibs/stralloc.h> +#include <skalibs/genalloc.h> + +void genalloc_delete_size (genalloc *g, size_t s, size_t i) +{ + size_t len = g->len / s; + if (len > i + 1) memmove (g->s + i * s, g->s + (i + 1) * s, (len - i - 1) * s) ; + g->len -= s ; +} -- 2.15.1
