]> PatchyGit - patchyircd.git/commitdiff
map: make map_base virtual
authorEgor <egor@patchysicp.xyz>
Tue, 3 Feb 2026 01:34:54 +0000 (03:34 +0200)
committerEgor <egor@patchysicp.xyz>
Tue, 3 Feb 2026 01:34:54 +0000 (03:34 +0200)
src/map.hpp

index df7b68d..32935f6 100644 (file)
@@ -164,32 +164,39 @@ struct MapValueLess {
        }
 };
 struct map_base {
+       map_base& operator=(map_base&&) = delete;
+       virtual ~map_base() = default;
+       virtual void *_get(const char *key) =0;
+       virtual void _set(const char *key, const void *value, void **oldvalue) =0;
+       virtual std::pair<char *, void *>_next(size_t& index) =0;
+       struct iter {
+               map_base *root;
+               size_t index;
+               auto next() {
+                       return root->_next(index);
+               }
+       };
+};
+
+struct map_base1_5 : map_base {
        struct map_value {
                char *key;
                void *value;
        };
        FlatSet<map_value, MapValueLess<map_value>> m;
 
-       map_base& operator=(map_base&&) = delete;
-       ~map_base() {
+       ~map_base1_5() {
                clear();
        }
-       /* Return the associated value for a given key.
-        * Dynarr complexity: O(log n) */
-       void *_get(const char *key) {
+       void *_get(const char *key) final {
                auto it = m.find(std::string_view(key));
                return it != m.end() ? it->value : nullptr;
        }
-       /* _get() but lowercase the key */
        void *_iget(const char *key) {
                auto it = m.find(LowercasingView(key));
                return it != m.end() ? it->value : nullptr;
        }
-       /* Associate a new value with a given key.
-        * If oldvalue is not NULL, it receives the old value of key.
-        * No allocation failures on value = NULL.
-        * Dynarr complexity: O(n) */
-       void _set(const char *key, const void *value, void **oldvalue) {
+       void _set(const char *key, const void *value, void **oldvalue) final {
                if (value) {
                        map_value mv;
                        mv.key = const_cast<char *>(key);
@@ -214,34 +221,24 @@ struct map_base {
                        }
                }
        }
-       /* Removes all keys from the map and frees any associated memory.
-        * The map_t is reset to zero-initialized state and can be used again.
-        * Dynarr complexity: O(n) */
+       std::pair<char *, void *>_next(size_t& index) final {
+               map_value kv{0, 0};
+               auto it = m.begin() + index;
+               if (it < m.end()) {
+                       kv = *it;
+                       index++;
+               }
+               return std::make_pair(kv.key, kv.value);
+       }
        void clear() {
                for (auto &mv : m)
                        delete[] mv.key;
                m.clear();
        }
-       /* Iterate over a map.
-        * Do not modify the map while iterating.
-        */
-       struct iter {
-               map_base *root;
-               size_t index;
-               auto next() {
-                       map_value kv{0, 0};
-                       auto it = root->m.begin() + index;
-                       if (it < root->m.end()) {
-                               kv = *it;
-                               index++;
-                       }
-                       return std::make_pair(kv.key, kv.value);
-               }
-       };
 };
 
 template<typename T, typename V>
-struct map_base2 : map_base {
+struct map_base2 : map_base1_5 {
        using value_type = V;
        struct iterator {
                using iterator_category = std::bidirectional_iterator_tag;