From f9fdee69c27db8763557f44243faff7097144fe5 Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Mon, 5 May 2025 12:10:41 -0400 Subject: [PATCH] MT#62181 introduce hash_map template The existing hash_table and ht_map_bucket implementations are a nice performance optimisation, but are over-engineered for certain use cases which don't see a lot of activity. They also make housekeeping and iterating the table contents more difficult. Provide a simpler alternative which is directly based on STL map and unordered_map. Retain semantic compabilitity by including a mutex and also providing a dump() method. Change-Id: I62664b5bb4b1bbef10ba8c85ac1e1221e71bb9de --- core/hash_table.h | 54 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/core/hash_table.h b/core/hash_table.h index b5c24b33..6b7db479 100644 --- a/core/hash_table.h +++ b/core/hash_table.h @@ -36,8 +36,10 @@ #include #include +#include using std::list; using std::map; +using std::unordered_map; using std::less; @@ -255,6 +257,58 @@ public: unsigned long get_size() { return size; } }; +template +class hash_map : public AmMutex +{ +public: + using parent = Container; + using iterator = parent::iterator; + using const_iterator = parent::const_iterator; + + hash_map() {} + virtual ~hash_map() {} + + virtual void dump_elmt(const Key& k, const Value& v) const = 0; + + // debug method + void dump() { + std::lock_guard _l(*this); + + if(empty()) + return; + + for(auto it = cbegin(); + it != cend(); ++it) { + dump_elmt(it->first, it->second); + } + } + + // wrappers + bool empty() const { return elems.empty(); } + iterator begin() { return elems.begin(); } + const_iterator cbegin() const { return elems.cbegin(); } + iterator end() { return elems.end(); } + const_iterator cend() const { return elems.cend(); } + size_t erase(const Key& k) { return elems.erase(k); } + iterator erase(iterator it) { return elems.erase(it); } + iterator find(const Key& k) { return elems.find(k); } + const_iterator find(const Key& k) const { return elems.find(k); } + std::pair insert(const std::pair& value) { return elems.insert(value); } + +private: + Container elems; +}; + +template +class ordered_hash_map : public hash_map> +{ +}; + +template +class unordered_hash_map : public hash_map> +{ +}; + #endif