From 4d1c5d09e9f80014a5c9ff9d5636c2d922ba2002 Mon Sep 17 00:00:00 2001 From: Egor Date: Sun, 22 Oct 2023 23:37:00 +0300 Subject: [PATCH] simplify map_dynarr traversal i've checked that we indeed do not rely on modification during traversal --- src/irc.c | 9 ++++----- src/main.c | 2 +- src/map.h | 2 +- src/map_dynarr.c | 38 ++++++++------------------------------ 4 files changed, 14 insertions(+), 37 deletions(-) diff --git a/src/irc.c b/src/irc.c index d2a3243..c5961dc 100644 --- a/src/irc.c +++ b/src/irc.c @@ -417,11 +417,10 @@ int processPacket(struct client_data *client, char *source, char *command, int a return err_needmoreparams(client, command), sendbuf(client), 0; if (!strcmp(args[0], "0")) { struct trie_find *f = NULL; - if (trie_findfirst(&f, &client->channels) == 1) - do { - struct channel *chan = f->value; - channel_part(client, chan, "I fell for a classic prank", NULL); - } while(trie_findfirst(&f, &client->channels) == 1); + while (trie_findfirst(&f, &client->channels) == 1) { + struct channel *chan = f->value; + channel_part(client, chan, "I fell for a classic prank", NULL); + } free(f); return 0; } diff --git a/src/main.c b/src/main.c index 1e4ce2a..cf2763b 100644 --- a/src/main.c +++ b/src/main.c @@ -418,7 +418,7 @@ int main(int argc, char *argv[]) tls_free(tls_ctx); #endif free(databuf.buf); - /* free any persisten channels */ + /* free any persistent channels */ { struct trie_find *f = NULL; while (trie_findfirst(&f, &channel_map) == 1) { diff --git a/src/map.h b/src/map.h index a6add0c..f98d0a0 100644 --- a/src/map.h +++ b/src/map.h @@ -26,7 +26,7 @@ struct trie_find { char *key; void *value; map_t *_root; - int _cap; + size_t _index; }; #else /* all complexity is in terms of n = strlen(key) and s = size of the character set */ diff --git a/src/map_dynarr.c b/src/map_dynarr.c index 43c5805..4da6c25 100644 --- a/src/map_dynarr.c +++ b/src/map_dynarr.c @@ -74,55 +74,33 @@ void map_free(map_t *root) root->ptr = 0; root->len = root->cap = 0; } -static int fill(struct trie_find **pf, int index) -{ - struct trie_find *f = *pf; - int keylen = strlen(f->_root->ptr[index].key)+1; - if (keylen > f->_cap) { - while (keylen > f->_cap) - f->_cap *= 2; - struct trie_find *nf = realloc(f, sizeof(struct trie_find)+f->_cap); - if (!nf) { - *pf = 0; - free(f); - return -1; - } - *pf = f = nf; - f->key = (char*)&f[1]; - } - memcpy(f->key, f->_root->ptr[index].key, keylen); - f->value = f->_root->ptr[index].value; - return 1; -} int trie_findfirst(struct trie_find **pf, map_t *root) { if (!root->len) return 0; struct trie_find *f; if (!*pf) { - f = malloc(sizeof(struct trie_find)+32); + f = malloc(sizeof(struct trie_find)); if (!f) return -1; - f->key = (char*)&f[1]; - f->key[0] = 0; - f->_cap = 32; *pf = f; } else { f = *pf; } f->_root = root; - return fill(pf, 0); + f->_index = -1; + return trie_findnext(pf); } int trie_findnext(struct trie_find **pf) { struct trie_find *f = *pf; - int found; - int i = ent_find(f->_root, f->key, &found); - i += found; - if (f->_root->len == (size_t)i) { + f->_index++; + if (f->_index == f->_root->len) { *pf = 0; free(f); return 0; } - return fill(pf, i); + f->key = f->_root->ptr[f->_index].key; + f->value = f->_root->ptr[f->_index].value; + return 1; } -- 2.56.0