Implement util::hash_table::operator[] and at()
This commit is contained in:
parent
368d1edd71
commit
64a6713b61
1 changed files with 27 additions and 0 deletions
|
|
@ -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();
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue