Belle II Software  release-05-01-25
MRUCache.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2010-2011 Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Martin Ritter *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #pragma once
12 
13 #include <boost/multi_index_container.hpp>
14 #include <boost/multi_index/sequenced_index.hpp>
15 #include <boost/multi_index/hashed_index.hpp>
16 #include <boost/multi_index/member.hpp>
17 
18 namespace Belle2 {
50  template <class KEY, class VALUE> class MRUCache {
51  public:
53  typedef std::pair<KEY, VALUE> value_type;
55  typedef boost::multi_index_container <
56  value_type,
57  boost::multi_index::indexed_by <
58  boost::multi_index::sequenced<>,
59  boost::multi_index::hashed_unique <
60  boost::multi_index::member<value_type, KEY, &value_type::first>
61  >
62  >
65  typedef typename container_type::const_iterator iterator;
67  typedef typename container_type::template nth_index<1>::type::const_iterator hash_iterator;
68 
73  explicit MRUCache(size_t maxSize): m_maxSize(maxSize), m_hits(0), m_misses(0), m_overflows(0) {}
74 
81  void insert(const KEY& key, const VALUE& value) { insert(value_type(key, value)); }
82 
88  void insert(const value_type& item)
89  {
90  std::pair<iterator, bool> p = m_container.push_front(item);
91  if (!p.second) { /* duplicate item, put existing in front */
92  m_container.replace(p.first, item);
93  update(p.first);
94  } else if (m_container.size() > m_maxSize) { /* keep the length <= maxSize */
95  ++m_overflows;
96  m_container.pop_back();
97  }
98  }
99 
106  bool retrieve(const KEY& key, VALUE& value)
107  {
108  hash_iterator it = m_container.template get<1>().find(key);
109  if (it == m_container.template get<1>().end()) {
110  ++m_misses;
111  return false;
112  }
113  update(m_container.template project<0>(it));
114  value = it->second;
115  ++m_hits;
116  return true;
117  }
118 
120  iterator begin() const { return m_container.begin(); }
122  iterator end() const { return m_container.end(); }
123 
125  size_t size() const { return m_container.size(); }
127  void clear()
128  {
129  m_container.clear();
130  m_hits = 0;
131  m_misses = 0;
132  m_overflows = 0;
133  }
134 
136  void setMaxSize(size_t maxSize) { m_maxSize = maxSize; }
138  size_t getMaxSize() const { return m_maxSize; }
140  unsigned int getHits() const { return m_hits; }
142  unsigned int getMisses() const { return m_misses; }
144  unsigned int getOverflows() const { return m_overflows; }
145 
146  protected:
152  void update(const iterator& item)
153  {
154  m_container.relocate(m_container.begin(), item);
155  }
156 
160  size_t m_maxSize;
162  unsigned int m_hits;
164  unsigned int m_misses;
166  unsigned int m_overflows;
167  };
169 }
Belle2::MRUCache::m_container
container_type m_container
Container for the items.
Definition: MRUCache.h:166
Belle2::MRUCache::value_type
std::pair< KEY, VALUE > value_type
type of elements stored in the cache
Definition: MRUCache.h:61
Belle2::MRUCache::update
void update(const iterator &item)
Update an item, thus marking it as recently accessed and putting it to the front of the list.
Definition: MRUCache.h:160
Belle2::MRUCache::setMaxSize
void setMaxSize(size_t maxSize)
Set maximum number of cache entries.
Definition: MRUCache.h:144
Belle2::MRUCache::retrieve
bool retrieve(const KEY &key, VALUE &value)
Retrieve a value from the cache if it exists.
Definition: MRUCache.h:114
Belle2::MRUCache::getHits
unsigned int getHits() const
Get number of cache hits since creation/last clear.
Definition: MRUCache.h:148
Belle2::MRUCache::MRUCache
MRUCache(size_t maxSize)
Constructor setting the maximum number of cached items.
Definition: MRUCache.h:81
Belle2::MRUCache::begin
iterator begin() const
Return iterator to the begin of the cache.
Definition: MRUCache.h:128
Belle2::MRUCache::getMisses
unsigned int getMisses() const
Get number of cache misses since creation/last clear.
Definition: MRUCache.h:150
Belle2::MRUCache::getMaxSize
size_t getMaxSize() const
Get maximum number of cache entries.
Definition: MRUCache.h:146
Belle2::MRUCache::iterator
container_type::const_iterator iterator
iterator over all cached items, sorted by access: most recent used first
Definition: MRUCache.h:73
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::MRUCache::container_type
boost::multi_index_container< value_type, boost::multi_index::indexed_by< boost::multi_index::sequenced<>, boost::multi_index::hashed_unique< boost::multi_index::member< value_type, KEY, &value_type::first > > > > container_type
type of container for the elements
Definition: MRUCache.h:71
Belle2::MRUCache::m_overflows
unsigned int m_overflows
Number of overflows.
Definition: MRUCache.h:174
Belle2::MRUCache::insert
void insert(const KEY &key, const VALUE &value)
Insert a key value pair into the cache.
Definition: MRUCache.h:89
Belle2::MRUCache::m_misses
unsigned int m_misses
Number of misses.
Definition: MRUCache.h:172
Belle2::MRUCache::m_hits
unsigned int m_hits
Number of hits.
Definition: MRUCache.h:170
Belle2::MRUCache::end
iterator end() const
Return iterator to the end of the cache.
Definition: MRUCache.h:130
Belle2::MRUCache::getOverflows
unsigned int getOverflows() const
Get number of overflows (dropped items) since creation/last clear.
Definition: MRUCache.h:152
Belle2::MRUCache::size
size_t size() const
Return actual size of the cache.
Definition: MRUCache.h:133
Belle2::MRUCache::clear
void clear()
Clear cache.
Definition: MRUCache.h:135
Belle2::MRUCache::m_maxSize
size_t m_maxSize
Maximum size of the cache.
Definition: MRUCache.h:168
Belle2::MRUCache::hash_iterator
container_type::template nth_index< 1 >::type::const_iterator hash_iterator
iterator over the hash index to the items
Definition: MRUCache.h:75