From: Egor Date: Sun, 8 Feb 2026 12:14:15 +0000 (+0200) Subject: map: reduce api surface X-Git-Url: https://git.patchysicp.xyz/?a=commitdiff_plain;h=be9b4dc3b2e95772609748bbaeed133d1dea8df2;p=patchyircd.git map: reduce api surface --- diff --git a/src/irc.cpp b/src/irc.cpp index 384358a..72cbbcb 100644 --- a/src/irc.cpp +++ b/src/irc.cpp @@ -619,8 +619,8 @@ saslcleanup: if (!args[0]) return err_needmoreparams(client, command), sendbuf(client), false; if (!strcmp(args[0], "0")) { - for (auto chan : client->channels.pop_view()) - channel_part(client, chan, "I fell for a classic prank", NULL); + for (auto it = client->channels.rbegin(); it != client->channels.rend(); ++it) + channel_part(client, *it, "I fell for a classic prank", NULL); return false; } char *p = args[0]; @@ -1136,16 +1136,14 @@ privmsg_notice_common: if (strchr(args[1], ' ')) return err_needmoreparams(client, command), sendbuf(client), false; /* FIXME: wrong error */ stringlower(args[1]); - size_t monitor_count = client->monitor.size(); char *p = args[1]; do { char *nick = p; - if (monitor_count == MONITOR_LIMIT) + if (client->monitor.size() == MONITOR_LIMIT) return rpl_monlistfull(client, MONITOR_LIMIT,nick), sendbuf(client), false; if ((p = strchr(nick, ','))) *p++ = '\0'; - if (client->monitor.add(nick)) - monitor_count++; + client->monitor.add(nick); Client *cl = client_map.get(nick); if (cl) rpl_mononline(client, cl), sendbuf(client); @@ -1332,7 +1330,7 @@ void setNick(Client *client, char* nickname) { return; } - if (Client *other = client_map.iget(nickname); other) { + if (Client *other = client_map.get(LowercasingView(nickname)); other) { if (client != other) err_nicknameinuse(client, nickname), sendbuf(client); return; @@ -1764,8 +1762,8 @@ void channel_topic(Client *src, Channel *chan, const char *topic, const char *wh sendbuf_chan(chan); } Client *findClient(const char *nickname) { - return client_map.iget(nickname); + return client_map.get(LowercasingView(nickname)); } Channel *findChannel(const char *channel) { - return channel_map.iget(channel); + return channel_map.get(LowercasingView(channel)); } diff --git a/src/main.cpp b/src/main.cpp index d761417..10e99f5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -372,8 +372,8 @@ int main(int argc, char *argv[]) tls_free(tls_ctx); #endif /* free any persistent channels */ - for (auto chan : channel_map.pop_view()) - delete chan; + for (auto it = channel_map.rbegin(); it != channel_map.rend(); ++it) + delete *it; client_map.clear(); channel_map.clear(); if (fchanlog) diff --git a/src/map.hpp b/src/map.hpp index 6ae8a39..c2ea4a8 100644 --- a/src/map.hpp +++ b/src/map.hpp @@ -56,13 +56,8 @@ public: using iterator = KeyContainer::const_iterator; iterator begin() noexcept { return m_vec.cbegin(); } iterator end() noexcept { return m_vec.cend(); } - - KeyContainer extract() && { - return std::move(m_vec); - } - void replace(KeyContainer&& cont) { - m_vec = std::move(cont); - } + auto rbegin() noexcept { return m_vec.crbegin(); } + auto rend() noexcept { return m_vec.crend(); } iterator lower_bound(const Key& key) { return std::partition_point(begin(), end(), [&](const auto &e){ return cmp(e, key); }); @@ -88,6 +83,14 @@ public: return end(); } + bool contains(const Key& key) { + return find(key) != end(); + } + template requires is_transparent + bool contains(const K& key) { + return find(key) != end(); + } + std::pair insert(const Key& key) { auto it = lower_bound(key); if (it != end() && !cmp(key, *it)) { @@ -201,38 +204,24 @@ struct map_base2 { } using value_type = T; - using iterator = decltype(m)::iterator; - static_assert(std::bidirectional_iterator); auto begin() { return m.begin(); } auto end() { return m.end(); } - struct reverse { - map_base2 &obj; - auto begin() { return std::make_reverse_iterator(obj.end()); } - auto end() { return std::make_reverse_iterator(obj.begin()); } - }; - // Iterate over keys of the map in such a way that you can delete them. - auto pop_view() { - return reverse{*this}; - } + auto rbegin() { return m.rbegin(); } + auto rend() { return m.rend(); } size_t size() { return m.size(); } -public: template bool has(const K& k) { - return m.find(k) != m.end(); + return m.contains(k); } template - bool add(const K &v) { - return m.insert(v).second; + void add(const K &v) { + m.insert(v); } template - bool remove(const K &k) { - if (auto it = m.find(k); it != m.end()) { - m.erase(it); - return true; - } - return false; + void remove(const K &k) { + m.erase(k); } template @@ -240,9 +229,6 @@ public: auto it = m.find(k); return it != m.end() ? *it : T(); } - T iget(const char *k) { - return get(LowercasingView(k)); - } }; struct clientmap : map_base2 { @@ -267,7 +253,6 @@ using channelmap = map_base2; struct stringset : map_base2 { template std::string get(const K& k) = delete; - std::string iget(const char *k) = delete; }; #endif diff --git a/src/net.cpp b/src/net.cpp index f416180..f6d0e80 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -73,8 +73,8 @@ void killConnection(Client *cl, int byChoice) netpoll::change_type(cl->fd, SOCKTYPE_DYING); update_poll_reg(cl); /* Part all channels */ - for (auto chan : cl->channels.pop_view()) - channel_part(cl, chan, NULL, NULL); + for (auto it = cl->channels.rbegin(); it != cl->channels.rend(); ++it) + channel_part(cl, *it, NULL, NULL); /* Remove from client_map and get rid of the pending invites */ if (cl->key) { admin_hook("nickdel", cl, 0, 0, cl->key);