7 #include <boost/multi_index_container.hpp> 8 #include <boost/multi_index/tag.hpp> 9 #include <boost/multi_index/member.hpp> 10 #include <boost/multi_index/hashed_index.hpp> 11 #include <boost/multi_index/sequenced_index.hpp> 20 template <
typename Key,
typename Value>
22 typedef std::pair<Key, Value> value_type;
24 typedef boost::multi_index_container <value_type,
25 boost::multi_index::indexed_by <
26 boost::multi_index::hashed_unique<BOOST_MULTI_INDEX_MEMBER(value_type, Key, first)>,
27 boost::multi_index::sequenced<>
31 typedef typename cache_type::template nth_index<1>::type::const_iterator iterator;
32 typedef iterator const_iterator;
34 typedef typename cache_type::template nth_index<1>::type::const_reverse_iterator reverse_iterator;
35 typedef reverse_iterator const_reverse_iterator;
48 std::pair<bool, Value>
query(
const Key& key) {
49 auto& hash_container = m_cache.template get<0>();
50 auto& seq_container = m_cache.template get<1>();
51 auto iter = hash_container.find(key);
52 if (iter == hash_container.end()) {
54 return {
false, Value()};
57 seq_container.relocate(seq_container.begin(), m_cache.template project<1>(iter));
58 return {
true, iter->second};
67 void insert(
const Key& key,
const Value& value) {
68 auto& hash_container = m_cache.template get<0>();
69 auto& seq_container = m_cache.template get<1>();
70 auto iter = hash_container.find(key);
71 if (iter == hash_container.end()) {
72 seq_container.push_front(value_type{key, value});
74 seq_container.pop_back();
77 hash_container.replace(iter, value_type{key, value});
78 seq_container.relocate(seq_container.begin(), m_cache.template project<1>(iter));
82 void erase(
const Key& key) {
83 auto& hash_container = m_cache.template get<0>();
84 auto iter = hash_container.find(key);
85 if (iter != hash_container.end()) {
86 hash_container.erase(iter);
94 auto& seq_container = m_cache.template get<1>();
95 return seq_container.begin();
101 const_iterator
end()
const {
102 auto& seq_container = m_cache.template get<1>();
103 return seq_container.end();
108 auto& seq_container = m_cache.template get<1>();
109 return seq_container.rbegin();
113 const_reverse_iterator
rend()
const {
114 auto& seq_container = m_cache.template get<1>();
115 return seq_container.rend();
121 return m_cache.size();
147 size_t m_limit = (size_t)(-1);
void insert(const Key &key, const Value &value)
void set_size_limit(size_t limit)
Sets the upper limit on the size of the cache.
const_iterator end() const
size_t misses() const
Returns the number of query misses.
const_iterator begin() const
const_reverse_iterator rend() const
size_t hits() const
Returns the number of query hits.
size_t size() const
Returns the current size of the cache.
const_reverse_iterator rbegin() const
std::pair< bool, Value > query(const Key &key)
size_t get_size_limit() const
Gets the upper limit of the size of the cache.