]> PatchyGit - patchyircd.git/commitdiff
the great trie_find renaming
authorEgor <egor@patchysicp.xyz>
Sun, 22 Oct 2023 20:52:19 +0000 (23:52 +0300)
committerEgor <egor@patchysicp.xyz>
Sun, 22 Oct 2023 20:52:19 +0000 (23:52 +0300)
src/irc.c
src/main.c
src/map.h
src/map_dynarr.c
src/map_trie.c
src/net.c

index c5961dc..559bcef 100644 (file)
--- a/src/irc.c
+++ b/src/irc.c
@@ -28,20 +28,20 @@ void chanlog() {
        }
 }
 void sendbuf_chan_caps(struct channel *chan, unsigned positive, unsigned negative) {
-       struct trie_find *f = NULL;
-       if (trie_findfirst(&f, &chan->members) == 1)
+       map_find_t f = {0};
+       if (map_findfirst(&f, &chan->members) == 1)
                do {
                        struct client_data *cl = f->value;
                        if (!(~cl->caps & positive) && !(cl->caps & negative))
                                sendbuf(cl);
-               } while (trie_findnext(&f) == 1);
+               } while (map_findnext(&f) == 1);
 }
 void sendbuf_chan(struct channel *chan) {
        sendbuf_chan_caps(chan, 0, 0);
 }
 void sendbuf_list_sans_self_caps(struct client_data *client, map_t *ls, int cookie, int positive, int negative) {
-       struct trie_find *f = NULL;
-       if (trie_findfirst(&f, ls) == 1)
+       map_find_t f = {0};
+       if (map_findfirst(&f, ls) == 1)
                do {
                        struct client_data *cl = f->value;
                        if (client != cl) {
@@ -54,7 +54,7 @@ void sendbuf_list_sans_self_caps(struct client_data *client, map_t *ls, int cook
                                if (!(~cl->caps & positive) && !(cl->caps & negative))
                                        sendbuf(cl);
                        }
-               } while (trie_findnext(&f) == 1);
+               } while (map_findnext(&f) == 1);
 }
 void sendbuf_list_sans_self(struct client_data *client, map_t *ls, int cookie) {
        sendbuf_list_sans_self_caps(client, ls, cookie, 0, 0);
@@ -63,24 +63,24 @@ int get_cookie() {
        static int cookie = 0;
        if (++cookie == 0) {
                // handle overflow
-               struct trie_find *f = NULL;
-               if (trie_findfirst(&f, &client_map) == 1)
+               map_find_t f = {0};
+               if (map_findfirst(&f, &client_map) == 1)
                        do {
                                struct client_data *cl = f->value;
                                cl->cookie = 0;
-                       } while (trie_findnext(&f) == 1);
+                       } while (map_findnext(&f) == 1);
                cookie++;
        }
        return cookie;
 }
 void sendbuf_visible_caps(struct client_data *client, int positive, int negative) {
        int cookie = get_cookie();
-       struct trie_find *f = NULL;
-       if (trie_findfirst(&f, &client->channels) == 1)
+       map_find_t f = {0};
+       if (map_findfirst(&f, &client->channels) == 1)
                do {
                        struct channel *chan = f->value;
                        sendbuf_list_sans_self_caps(client, &chan->members, cookie, positive, negative);
-               } while (trie_findnext(&f) == 1);
+               } while (map_findnext(&f) == 1);
 }
 void sendbuf_visible(struct client_data *client) {
        sendbuf_visible_caps(client, 0, 0);
@@ -100,14 +100,14 @@ const char *format_umodes(struct client_data *cl) {
 }
 int match_list(struct client_data *cl, map_t *list)
 {
-       struct trie_find *f = NULL;
-       if (trie_findfirst(&f, list) == 1)
+       map_find_t f = {0};
+       if (map_findfirst(&f, list) == 1)
                do {
                        if (irc_glob(cl->nuh, f->key)) {
-                               free(f);
+                               map_findfree(&f);
                                return 1;
                        }
-               } while (trie_findnext(&f) == 1);
+               } while (map_findnext(&f) == 1);
        return 0;
 }
 int parsePacket(struct client_data *client) {
@@ -365,13 +365,13 @@ int processPacket(struct client_data *client, char *source, char *command, int a
                if (!(client->umode & UMODE_OPER))
                        return err_noprivileges(client), sendbuf(client), 0;
                msg_wallops(client, args[0]);
-               struct trie_find *f = NULL;
-               if (trie_findfirst(&f, &client_map) == 1)
+               map_find_t f = {0};
+               if (map_findfirst(&f, &client_map) == 1)
                        do {
                                struct client_data *cl = f->value;
                                if (cl->umode & UMODE_WALLOPS)
                                        sendbuf(cl);
-                       } while (trie_findnext(&f) == 1);
+                       } while (map_findnext(&f) == 1);
        } else if(strcmp(command, "KILL") == 0) {
                if (!is_registered(client))
                        return err_notregistered(client), sendbuf(client), 0;
@@ -385,13 +385,13 @@ int processPacket(struct client_data *client, char *source, char *command, int a
                if (client == cl)
                        return 0; // FIXME: error message
                msg_kill(client, cl, args[1] ? args[1] : "(no reason)");
-               struct trie_find *f = NULL;
-               if (trie_findfirst(&f, &client_map) == 1)
+               map_find_t f = {0};
+               if (map_findfirst(&f, &client_map) == 1)
                        do {
                                struct client_data *cl2 = f->value;
                                if (cl2->umode & UMODE_OPER)
                                        sendbuf(cl2);
-                       } while (trie_findnext(&f) == 1);
+                       } while (map_findnext(&f) == 1);
                // FIXME: add appropraite QUIT message
                killConnection(cl, 0);
        } else if(strcmp(command, "JEDGAR") == 0) {
@@ -416,12 +416,12 @@ int processPacket(struct client_data *client, char *source, char *command, int a
                if (!args[0])
                        return err_needmoreparams(client, command), sendbuf(client), 0;
                if (!strcmp(args[0], "0")) {
-                       struct trie_find *f = NULL;
-                       while (trie_findfirst(&f, &client->channels) == 1) {
+                       map_find_t f = {0};
+                       while (map_findfirst(&f, &client->channels) == 1) {
                                struct channel *chan = f->value;
                                channel_part(client, chan, "I fell for a classic prank", NULL);
                        }
-                       free(f);
+                       map_findfree(&f);
                        return 0;
                }
                char *p = args[0];
@@ -487,12 +487,12 @@ int processPacket(struct client_data *client, char *source, char *command, int a
                        return err_notregistered(client), sendbuf(client), 0;
                if (!args[0]) {
                        // List pending invites
-                       struct trie_find *f = NULL;
-                       if (trie_findfirst(&f, &client->invites) == 1)
+                       map_find_t f = {0};
+                       if (map_findfirst(&f, &client->invites) == 1)
                                do {
                                        struct channel *chan = f->value;
                                        rpl_invitelist(client, chan), sendbuf(client);
-                               } while(trie_findnext(&f) == 1);
+                               } while(map_findnext(&f) == 1);
                        return rpl_endofinvitelist(client), sendbuf(client), 0;
                }
                if (!args[1])
@@ -736,7 +736,7 @@ int processPacket(struct client_data *client, char *source, char *command, int a
                                if (!map_get(&chan->o, client->key))
                                        return 0; // TODO: error
                                if (argc == 2) { // Type A channel modes
-                                       struct trie_find *f = NULL;
+                                       map_find_t f = {0};
                                        int all = !strcmp(args[1], "+0");
                                        int any = 0;
                                        if (all) {
@@ -745,46 +745,46 @@ int processPacket(struct client_data *client, char *source, char *command, int a
                                                any = 1;
                                        }
                                        if (all || !strcmp(args[1], "+b") || !strcmp(args[1], "b")) {
-                                               if (trie_findfirst(&f, &chan->listb) == 1)
+                                               if (map_findfirst(&f, &chan->listb) == 1)
                                                        do {
                                                                rpl_banlist(client, chan, f->key), sendbuf(client);
-                                                       } while(trie_findnext(&f) == 1);
+                                                       } while(map_findnext(&f) == 1);
                                                rpl_endofbanlist(client, chan), sendbuf(client);
                                                any = 1;
                                        }
                                        if (all || !strcmp(args[1], "+e") || !strcmp(args[1], "e")) {
-                                               if (trie_findfirst(&f, &chan->liste) == 1)
+                                               if (map_findfirst(&f, &chan->liste) == 1)
                                                        do {
                                                                rpl_exceptlist(client, chan, f->key), sendbuf(client);
-                                                       } while(trie_findnext(&f) == 1);
+                                                       } while(map_findnext(&f) == 1);
                                                rpl_endofexceptlist(client, chan), sendbuf(client);
                                                any = 1;
                                        }
                                        if (all || !strcmp(args[1], "+I") || !strcmp(args[1], "I")) {
-                                               if (trie_findfirst(&f, &chan->listI) == 1)
+                                               if (map_findfirst(&f, &chan->listI) == 1)
                                                        do {
                                                                rpl_invexlist(client, chan, f->key), sendbuf(client);
-                                                       } while(trie_findnext(&f) == 1);
+                                                       } while(map_findnext(&f) == 1);
                                                rpl_endofinvexlist(client, chan), sendbuf(client);
                                                any = 1;
                                        }
                                        if (all || !strcmp(args[1], "+q") || !strcmp(args[1], "q")) {
-                                               if (trie_findfirst(&f, &chan->listq) == 1)
+                                               if (map_findfirst(&f, &chan->listq) == 1)
                                                        do {
                                                                rpl_quietlist(client, chan, f->key), sendbuf(client);
-                                                       } while(trie_findnext(&f) == 1);
+                                                       } while(map_findnext(&f) == 1);
                                                rpl_endofquietlist(client, chan), sendbuf(client);
                                                any = 1;
                                        }
                                        if (all || !strcmp(args[1], "+w") || !strcmp(args[1], "w")) {
-                                               if (trie_findfirst(&f, &chan->listwo) == 1)
+                                               if (map_findfirst(&f, &chan->listwo) == 1)
                                                        do {
                                                                rpl_accesslist(client, chan, 'o', f->key), sendbuf(client);
-                                                       } while(trie_findnext(&f) == 1);
-                                               if (trie_findfirst(&f, &chan->listwv) == 1)
+                                                       } while(map_findnext(&f) == 1);
+                                               if (map_findfirst(&f, &chan->listwv) == 1)
                                                        do {
                                                                rpl_accesslist(client, chan, 'v', f->key), sendbuf(client);
-                                                       } while(trie_findnext(&f) == 1);
+                                                       } while(map_findnext(&f) == 1);
                                                rpl_endofaccesslist(client, chan), sendbuf(client);
                                                any = 1;
                                        }
@@ -966,13 +966,13 @@ int processPacket(struct client_data *client, char *source, char *command, int a
                if (!is_registered(client))
                        return err_notregistered(client), sendbuf(client), 0;
                rpl_liststart(client), sendbuf(client);
-               struct trie_find *f = NULL;
-               if (trie_findfirst(&f, &channel_map) == 1)
+               map_find_t f = {0};
+               if (map_findfirst(&f, &channel_map) == 1)
                        do {
                                struct channel *chan = f->value;
                                if (!(chan->dmode & DMODE_SECRET) || map_get(&chan->members, client->key))
                                        rpl_list(client, chan), sendbuf(client);
-                       } while(trie_findnext(&f) == 1);
+                       } while(map_findnext(&f) == 1);
                rpl_listend(client), sendbuf(client);
        } else if(strcmp(command, "NAMES") == 0) {
                if (!is_registered(client))
@@ -1018,12 +1018,12 @@ int processPacket(struct client_data *client, char *source, char *command, int a
                        struct channel *chan = findChannel(args[0]);
                        // TODO: handle chanmode +s, umode +i
                        if (chan) {
-                               struct trie_find *f = NULL;
-                               if (trie_findfirst(&f, &chan->members) == 1)
+                               map_find_t f = {0};
+                               if (map_findfirst(&f, &chan->members) == 1)
                                        do {
                                                struct client_data *cl = f->value;
                                                rpl_whoreply(client, cl, chan), sendbuf(client);
-                                       } while(trie_findnext(&f) == 1);
+                                       } while(map_findnext(&f) == 1);
                        }
                } else {
                        struct client_data *cl = findClient(args[0]);
@@ -1053,14 +1053,14 @@ int processPacket(struct client_data *client, char *source, char *command, int a
                        rpl_whoisidle(client, cl), sendbuf(client);
                        if (cl->away)
                                rpl_away(client, cl), sendbuf(client);
-                       struct trie_find *f = NULL;
-                       if (trie_findfirst(&f, &cl->channels) == 1)
+                       map_find_t f = {0};
+                       if (map_findfirst(&f, &cl->channels) == 1)
                                do {
                                        struct channel *chan = f->value;
                                        if ((cl->umode & UMODE_INVISIBLE || chan->dmode & DMODE_SECRET) && !map_get(&chan->members, client->key))
                                                continue;
                                        rpl_whoischannels(client, cl, chan), sendbuf(client);
-                               } while (trie_findnext(&f) == 1);
+                               } while (map_findnext(&f) == 1);
                        if (cl->umode & UMODE_REGISTERED)
                                rpl_whoisaccount(client, cl), sendbuf(client);
                        if (client == cl || client->umode & UMODE_OPER)
@@ -1131,11 +1131,11 @@ void newUser(struct client_data *client) {
        sendLUSERS(client);
        rpl_umodeis(client),  sendbuf(client);
        sendMOTD(client);
-       struct trie_find *f = NULL;
-       if (trie_findfirst(&f, &autojoin) == 1)
+       map_find_t f = {0};
+       if (map_findfirst(&f, &autojoin) == 1)
                do {
                        channel_join(client, f->key, NULL);
-               } while (trie_findnext(&f) == 1);
+               } while (map_findnext(&f) == 1);
 }
 void sendLUSERS(struct client_data *client)
 {
@@ -1144,8 +1144,8 @@ void sendLUSERS(struct client_data *client)
        int invisible = 0;
        int ops = 0;
        int channels = 0;
-       struct trie_find *f = NULL;
-       if (trie_findfirst(&f, &client_map) == 1)
+       map_find_t f = {0};
+       if (map_findfirst(&f, &client_map) == 1)
                do {
                        struct client_data *cl = f->value;
                        registered++;
@@ -1153,11 +1153,11 @@ void sendLUSERS(struct client_data *client)
                                invisible++;
                        if (cl->umode & UMODE_OPER)
                                ops++;
-               } while (trie_findnext(&f) == 1);
-       if (trie_findfirst(&f, &client_map) == 1)
+               } while (map_findnext(&f) == 1);
+       if (map_findfirst(&f, &client_map) == 1)
                do {
                        channels++;
-               } while (trie_findnext(&f) == 1);
+               } while (map_findnext(&f) == 1);
        for (struct client_data *cl = clients; cl; cl = cl->next)
                total++;
 
@@ -1218,8 +1218,8 @@ void setNick(struct client_data *client, char* nickname) {
                sendbuf(client);
                sendbuf_visible(client);
                map_unset(&client_map, client->key, NULL);
-               struct trie_find *f = NULL;
-               if (trie_findfirst(&f, &client->channels) == 1)
+               map_find_t f = {0};
+               if (map_findfirst(&f, &client->channels) == 1)
                        do {
                                struct channel *chan = f->value;
                                map_unset(&chan->members, client->key, NULL);
@@ -1234,7 +1234,7 @@ void setNick(struct client_data *client, char* nickname) {
                                if (old)
                                        if (map_set(&chan->v, key, client, NULL) == -1)
                                                abort();
-                       } while(trie_findnext(&f) == 1);
+                       } while(map_findnext(&f) == 1);
                free(client->nick);
                free(client->key);
        }
@@ -1266,9 +1266,9 @@ void channel_sendtopic(struct client_data *client, struct channel *chan, int joi
 }
 void channel_sendnames(struct client_data *client, struct channel *chan)
 {
-       struct trie_find *f = NULL;
+       map_find_t f = {0};
        databuf.len = 0;
-       if (trie_findfirst(&f, &chan->members) == 1)
+       if (map_findfirst(&f, &chan->members) == 1)
                do {
                        struct client_data *cl = f->value;
                        if (rpl_namreply(client, chan, cl)) {
@@ -1276,7 +1276,7 @@ void channel_sendnames(struct client_data *client, struct channel *chan)
                                databuf.len = 0;
                                rpl_namreply(client, chan, cl);
                        }
-               } while(trie_findnext(&f) == 1);
+               } while(map_findnext(&f) == 1);
        if (rpl_namreply(client, chan, NULL))
                sendbuf(client);
        rpl_endofnames(client, chan->name);
@@ -1383,12 +1383,12 @@ void channel_part(struct client_data *client, struct channel *chan, const char *
 void channel_kill(struct channel *chan)
 {
        map_unset(&channel_map, chan->key, NULL);
-       struct trie_find *f = NULL;
-       if (trie_findfirst(&f, &chan->invitees) == 1)
+       map_find_t f = {0};
+       if (map_findfirst(&f, &chan->invitees) == 1)
                do {
                        struct client_data *cl = f->value;
                        map_unset(&cl->invites, chan->key, NULL);
-               } while(trie_findnext(&f) == 1);
+               } while(map_findnext(&f) == 1);
        free(chan->key);
        free(chan->name);
        free(chan->topic);
index cf2763b..1d90bb3 100644 (file)
@@ -420,12 +420,12 @@ int main(int argc, char *argv[])
        free(databuf.buf);
        /* free any persistent channels */
        {
-               struct trie_find *f = NULL;
-               while (trie_findfirst(&f, &channel_map) == 1) {
+               map_find_t f = {0};
+               while (map_findfirst(&f, &channel_map) == 1) {
                        struct channel *chan = f->value;
                        channel_kill(chan);
                }
-               free(f);
+               map_findfree(&f);
        }
        map_free(&client_map);
        map_free(&channel_map);
index f98d0a0..fc87a90 100644 (file)
--- a/src/map.h
+++ b/src/map.h
@@ -22,12 +22,14 @@ struct map_dynarr {
        size_t len, cap;
 };
 typedef struct map_dynarr map_t;
-struct trie_find {
+struct map_dynarr_find {
        char *key;
        void *value;
        map_t *_root;
        size_t _index;
 };
+typedef struct map_dynarr_find *map_find_t;
+#define map_findfree(f) (free(*(f)))
 #else
 /* all complexity is in terms of n = strlen(key) and s = size of the character set */
 struct trie_node {
@@ -46,6 +48,8 @@ struct trie_find {
        size_t cap;
        char key[1];
 };
+typedef struct trie_find *map_find_t;
+#define map_findfree(f) (free(*(f)))
 #endif
 
 /* A map abstract data type with following properties:
@@ -78,6 +82,6 @@ NODISCARD int map_set(map_t *root, const char *key, const void *value, void **ol
  * Trie   complexity: O(ns)
  * Dynarr complexity: O(n) */
 void map_free(map_t *root);
-NODISCARD int trie_findfirst(struct trie_find **pf, map_t *root);
-NODISCARD int trie_findnext(struct trie_find **pf);
+NODISCARD int map_findfirst(map_find_t *pf, map_t *root);
+NODISCARD int map_findnext(map_find_t *pf);
 #endif
index 4da6c25..6e6cb1e 100644 (file)
@@ -74,13 +74,13 @@ void map_free(map_t *root)
        root->ptr = 0;
        root->len = root->cap = 0;
 }
-int trie_findfirst(struct trie_find **pf, map_t *root)
+int map_findfirst(map_find_t *pf, map_t *root)
 {
        if (!root->len)
                return 0;
-       struct trie_find *f;
+       map_find_t f;
        if (!*pf) {
-               f = malloc(sizeof(struct trie_find));
+               f = malloc(sizeof(*f));
                if (!f)
                        return -1;
                *pf = f;
@@ -89,11 +89,11 @@ int trie_findfirst(struct trie_find **pf, map_t *root)
        }
        f->_root = root;
        f->_index = -1;
-       return trie_findnext(pf);
+       return map_findnext(pf);
 }
-int trie_findnext(struct trie_find **pf)
+int map_findnext(map_find_t *pf)
 {
-       struct trie_find *f = *pf;
+       map_find_t f = *pf;
        f->_index++;
        if (f->_index == f->_root->len) {
                *pf = 0;
index fbb2012..19723cc 100644 (file)
@@ -134,14 +134,14 @@ void map_free(map_t *root) {
        }
        *root = 0;
 }
-int trie_findnext(struct trie_find **pf) {
-       struct trie_find *f = *pf;
+int map_findnext(map_find_t *pf) {
+       map_find_t f = *pf;
        int ent = 0; // go to the first child node
        for (;;) {
                if (!trie_is_end(ent, f->n)) {
                        if (f->len == f->cap) {
                                f->cap *= 2;
-                               void *nf = realloc(f, sizeof(struct trie_find)+f->cap);
+                               void *nf = realloc(f, sizeof(*f)+f->cap);
                                if (!nf) {
                                        *pf = NULL;
                                        free(f);
@@ -169,14 +169,14 @@ int trie_findnext(struct trie_find **pf) {
                }
        }
 }
-int trie_findfirst(struct trie_find **pf, map_t *root) {
+int map_findfirst(map_find_t *pf, map_t *root) {
        if (!*root)
                return 0;
-       struct trie_find *f;
+       map_find_t f;
        if (*pf) {
                f = *pf;
        } else {
-               f = *pf = malloc(sizeof(struct trie_find)+16);
+               f = *pf = malloc(sizeof(*f)+16);
                if (!f)
                        return -1;
                f->len = 0;
@@ -188,5 +188,5 @@ int trie_findfirst(struct trie_find **pf, map_t *root) {
                f->value = f->n->value;
                return 1;
        }
-       return trie_findnext(pf);
+       return map_findnext(pf);
 }
index c240866..2ebaed7 100644 (file)
--- a/src/net.c
+++ b/src/net.c
@@ -64,21 +64,21 @@ void killConnection(struct client_data *cl, int byChoice)
        }
        update_poll_reg(cl);
        /* Part all channels */
-       struct trie_find *f = NULL;
-       while (trie_findfirst(&f, &cl->channels) == 1) {
+       map_find_t f = {0};
+       while (map_findfirst(&f, &cl->channels) == 1) {
                struct channel *chan = f->value;
                channel_part(cl, chan, NULL, NULL);
        }
-       free(f);
+       map_findfree(&f);
        /* Remove from client_map and get rid of the pending invites */
        if (cl->key) {
                map_unset(&client_map, cl->key, NULL);
-               struct trie_find *f = NULL;
-               if (trie_findfirst(&f, &cl->invites) == 1)
+               map_find_t f = {0};
+               if (map_findfirst(&f, &cl->invites) == 1)
                        do {
                                struct channel *chan = f->value;
                                map_unset(&chan->invitees, cl->key, NULL);
-                       } while(trie_findnext(&f) == 1);
+                       } while(map_findnext(&f) == 1);
        }
        map_free(&cl->channels);
        map_free(&cl->invites);