From 64a6713b61586a1b822a5a9aa4a2e07465eb5840 Mon Sep 17 00:00:00 2001 From: lisyarus Date: Mon, 29 Jan 2024 17:44:11 +0300 Subject: [PATCH] Implement util::hash_table::operator[] and at() --- libs/util/include/psemek/util/hash_table.hpp | 27 ++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/libs/util/include/psemek/util/hash_table.hpp b/libs/util/include/psemek/util/hash_table.hpp index b35cf3ee..63e9149d 100644 --- a/libs/util/include/psemek/util/hash_table.hpp +++ b/libs/util/include/psemek/util/hash_table.hpp @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -437,6 +438,32 @@ namespace psemek::util return impl_.end(); } + template + Value & operator[] (Key1 const & key) + { + if (auto it = find(key); it != end()) + return it->second; + return insert({Key(key), Value{}}).first->second; + } + + template + Value & at(Key1 const & key) + { + auto it = find(key); + if (it == end()) + throw util::key_error{key}; + return it->second; + } + + template + Value const & at(Key1 const & key) const + { + auto it = find(key); + if (it == end()) + throw util::key_error{key}; + return it->second; + } + void clear() { impl_.clear();