From 177b855c651812d18166b8d23e5e0ed8b035006d Mon Sep 17 00:00:00 2001 From: Egor Date: Tue, 3 Feb 2026 03:34:54 +0200 Subject: [PATCH] map: make map_base virtual --- src/map.hpp | 59 +++++++++++++++++++++++++---------------------------- 1 file changed, 28 insertions(+), 31 deletions(-) diff --git a/src/map.hpp b/src/map.hpp index df7b68d..32935f6 100644 --- a/src/map.hpp +++ b/src/map.hpp @@ -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_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> 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(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_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 -struct map_base2 : map_base { +struct map_base2 : map_base1_5 { using value_type = V; struct iterator { using iterator_category = std::bidirectional_iterator_tag; -- 2.56.0