12 #ifndef __STOUT_BOUNDEDHASHMAP_HPP__ 13 #define __STOUT_BOUNDEDHASHMAP_HPP__ 26 template <
typename Key,
typename Value>
30 typedef std::pair<Key, Value>
entry;
31 typedef std::list<entry>
list;
39 void set(
const Key& key,
const Value& value)
47 typename list::iterator iter =
48 entries_.insert(entries_.end(), std::make_pair(key, value));
55 if (keys_.size() > capacity_) {
56 typename list::iterator firstEntry = entries_.begin();
57 keys_.erase(firstEntry->first);
58 entries_.erase(firstEntry);
60 CHECK(keys_.size() == capacity_);
63 keys_[key]->second = value;
70 return keys_.at(key)->second;
75 Value&
at(
const Key& key)
77 return keys_.at(key)->second;
80 const Value&
at(
const Key& key)
const 82 return keys_.at(key)->second;
93 typename list::iterator entry = keys_[key];
95 entries_.erase(entry);
105 std::list<Key> result;
107 foreach (
const entry& entry, entries_) {
108 result.push_back(entry.first);
117 std::list<Value> result;
119 foreach (
const entry& entry, entries_) {
120 result.push_back(entry.second);
133 return keys_.empty();
145 typename list::iterator
begin() {
return entries_.begin(); }
146 typename list::iterator
end() {
return entries_.end(); }
148 typename list::const_iterator
begin()
const {
return entries_.cbegin(); }
149 typename list::const_iterator
end()
const {
return entries_.cend(); }
157 #endif // __STOUT_BOUNDEDHASHMAP_HPP__ BoundedHashMap(size_t capacity)
Definition: boundedhashmap.hpp:34
Definition: option.hpp:29
std::pair< Key, Value > entry
Definition: boundedhashmap.hpp:30
list::const_iterator begin() const
Definition: boundedhashmap.hpp:148
Value & at(const Key &key)
Definition: boundedhashmap.hpp:75
std::list< entry > list
Definition: boundedhashmap.hpp:31
const Value & at(const Key &key) const
Definition: boundedhashmap.hpp:80
size_t size() const
Definition: boundedhashmap.hpp:126
list::iterator begin()
Definition: boundedhashmap.hpp:145
bool contains(const Key &key) const
Definition: boundedhashmap.hpp:85
std::list< Key > keys() const
Definition: boundedhashmap.hpp:103
size_t erase(const Key &key)
Definition: boundedhashmap.hpp:90
std::list< Value > values() const
Definition: boundedhashmap.hpp:115
Definition: boundedhashmap.hpp:27
void clear()
Definition: boundedhashmap.hpp:136
hashmap< Key, typename list::iterator > map
Definition: boundedhashmap.hpp:32
list::const_iterator end() const
Definition: boundedhashmap.hpp:149
bool empty() const
Definition: boundedhashmap.hpp:131
bool contains(const Key &key) const
Definition: hashmap.hpp:86
list::iterator end()
Definition: boundedhashmap.hpp:146