Implement util::hash_table::operator[] and at()

This commit is contained in:
Nikita Lisitsa 2024-01-29 17:44:11 +03:00
parent 368d1edd71
commit 64a6713b61

View file

@ -2,6 +2,7 @@
#include <psemek/util/hash.hpp>
#include <psemek/util/span.hpp>
#include <psemek/util/at.hpp>
#include <memory>
#include <optional>
@ -437,6 +438,32 @@ namespace psemek::util
return impl_.end();
}
template <typename Key1>
Value & operator[] (Key1 const & key)
{
if (auto it = find(key); it != end())
return it->second;
return insert({Key(key), Value{}}).first->second;
}
template <typename Key1>
Value & at(Key1 const & key)
{
auto it = find(key);
if (it == end())
throw util::key_error{key};
return it->second;
}
template <typename Key1>
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();