]> PatchyGit - patchyircd.git/commitdiff
simplify map_dynarr traversal
authorEgor <egor@patchysicp.xyz>
Sun, 22 Oct 2023 20:37:00 +0000 (23:37 +0300)
committerEgor <egor@patchysicp.xyz>
Sun, 22 Oct 2023 20:37:00 +0000 (23:37 +0300)
i've checked that we indeed do not rely on modification during traversal

src/irc.c
src/main.c
src/map.h
src/map_dynarr.c

index d2a3243..c5961dc 100644 (file)
--- 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;
                }
index 1e4ce2a..cf2763b 100644 (file)
@@ -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) {
index a6add0c..f98d0a0 100644 (file)
--- 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 */
index 43c5805..4da6c25 100644 (file)
@@ -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;
 }