From 153cc879863f598fc504fc934d8518382abcbef4 Mon Sep 17 00:00:00 2001 From: lisyarus Date: Tue, 12 Mar 2024 20:29:29 +0300 Subject: [PATCH] Add non-template access methods to util::hash_table --- libs/util/include/psemek/util/hash_table.hpp | 33 ++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/libs/util/include/psemek/util/hash_table.hpp b/libs/util/include/psemek/util/hash_table.hpp index 5fef85b0..fdab55c2 100644 --- a/libs/util/include/psemek/util/hash_table.hpp +++ b/libs/util/include/psemek/util/hash_table.hpp @@ -386,12 +386,22 @@ namespace psemek::util return {result.first.as_const(), result.second}; } + iterator find(T const & key) const + { + return impl_.find(key).as_const(); + } + template iterator find(Key const & key) const { return impl_.find(key).as_const(); } + bool contains(T const & key) const + { + return find(key) != end(); + } + template bool contains(Key const & key) const { @@ -497,6 +507,13 @@ namespace psemek::util return impl_.end(); } + Value & operator[] (Key const & key) + { + if (auto it = find(key); it != end()) + return it->second; + return insert({Key(key), Value{}}).first->second; + } + template Value & operator[] (Key1 const & key) { @@ -505,6 +522,14 @@ namespace psemek::util return insert({Key(key), Value{}}).first->second; } + Value & at(Key const & key) + { + auto it = find(key); + if (it == end()) + throw util::key_error{key}; + return it->second; + } + template Value & at(Key1 const & key) { @@ -514,6 +539,14 @@ namespace psemek::util return it->second; } + Value const & at(Key const & key) const + { + auto it = find(key); + if (it == end()) + throw util::key_error{key}; + return it->second; + } + template Value const & at(Key1 const & key) const {