17#include <unordered_map>
23template <
class Key,
class T,
class Hash = std::hash<Key>,
class KeyEqual = std::equal_to<Key>,
24 class Allocator = std::allocator<std::pair<const Key, T>>>
29 using UnorderedMapType = std::unordered_map<Key, T, Hash, KeyEqual, Allocator>;
30 using value_type =
typename UnorderedMapType::value_type;
31 using size_type =
typename UnorderedMapType::size_type;
32 using const_iterator =
typename UnorderedMapType::const_iterator;
41 using Action = std::tuple<ActionKind, Key, T>;
55 const_iterator begin()
const {
return map_.begin(); }
56 const_iterator cbegin()
const {
return map_.cbegin(); }
57 const_iterator end()
const {
return map_.end(); }
58 const_iterator cend()
const {
return map_.cend(); }
60 [[nodiscard]]
bool empty()
const {
return map_.empty(); }
61 size_type size()
const {
return map_.size(); }
75 void insert(
const Key &k,
const T &v) {
76 auto it =
map_.find(k);
77 if (it ==
map_.end()) {
96 const auto it =
map_.find(key);
97 if (it ==
map_.end()) {
98 throw std::runtime_error(
"ScopedUnorderedMap has no entry for the key {}" + std::to_string(key));
109 const T &
at(
const Key &key)
const {
return map_.at(key); }
111 size_type count(
const Key &key)
const {
return map_.count(key); }
113 const_iterator find(
const Key &key)
const {
return map_.find(key); }
116 void push() { stack_.push_back(
actions_.size()); }
118 if (stack_.empty()) {
119 throw std::runtime_error(
"ScopedUnorderedMap cannot be popped because it's scope is empty.");
121 size_type idx = stack_.back();
123 const Action &item{
actions_.back()};
125 const Key &k{std::get<1>(item)};
126 const T &v{std::get<2>(item)};
127 auto it =
map_.find(k);
145 std::vector<size_type> stack_;
ActionKind
Action to perform on the scoped unordered map.
@ UPDATE
Update(k, v) means that (k, v) was replaced by a new value.
@ INSERT
Insert(k, v) means that (k, v) is inserted.
std::vector< Action > actions_
Vector of actions that have been applied.
const T & operator[](const Key &key) const
Lookup the value for the given key.
UnorderedMapType map_
Actual map.
const T & at(const Key &key) const
Lookup the value for the given key.
void clear()
Clear the map.
void insert(const Key &k, const T &v)
Insert a new key-value pair.
Global namespace for the dlinear library.