ed
time_cache.h
Go to the documentation of this file.
1 #ifndef ED_TIME_CACHE_H_
2 #define ED_TIME_CACHE_H_
3 
4 #include "ed/time.h"
5 #include <map>
6 
7 namespace ed
8 {
9 
10 template<typename T>
11 class TimeCache
12 {
13 
14 public:
15 
17 
18  TimeCache() : max_size_(0) {}
19 
21 
22  void insert(const Time& t, const T& value)
23  {
24  if (max_size_ > 0 && cache_.size() == max_size_)
25  cache_.erase(cache_.begin());
26  cache_[t] = value;
27  }
28 
29  void getLowerUpper(const Time& t, const_iterator& lower, const_iterator& upper) const
30  {
31  if (cache_.empty())
32  {
33  lower = cache_.end();
34  upper = cache_.end();
35  return;
36  }
37 
38  // std::map::upper_bound returns first iterator after key
39  upper = cache_.upper_bound(t);
40 
41  if (upper == cache_.begin())
42  {
43  lower = cache_.end();
44  }
45  else
46  {
47  lower = upper;
48  --lower;
49  }
50  }
51 
52  inline const_iterator begin() const { return cache_.begin(); }
53  inline const_iterator end() const { return cache_.end(); }
54 
55  inline unsigned int size() const { return cache_.size(); }
56 
57  void setMaxSize(unsigned int n) { max_size_ = n; }
58 
59 private:
60 
61  // Cache with items ordered in time
63 
64  unsigned int max_size_;
65 
66 };
67 
68 } // end namespace ed
69 
70 #endif
ed::TimeCache::getLowerUpper
void getLowerUpper(const Time &t, const_iterator &lower, const_iterator &upper) const
Definition: time_cache.h:29
t
Timer t
ed::TimeCache::max_size_
unsigned int max_size_
Definition: time_cache.h:64
ed::Time
Definition: time.h:9
ed::TimeCache::const_iterator
std::map< Time, T >::const_iterator const_iterator
Definition: time_cache.h:16
ed::TimeCache
Definition: time_cache.h:11
time.h
map
ed::TimeCache::TimeCache
TimeCache()
Definition: time_cache.h:18
ed::TimeCache::setMaxSize
void setMaxSize(unsigned int n)
Definition: time_cache.h:57
ed::TimeCache::cache_
std::map< Time, T > cache_
Definition: time_cache.h:62
ed
Definition: convex_hull.h:8
ed::TimeCache::~TimeCache
~TimeCache()
Definition: time_cache.h:20
ed::TimeCache::size
unsigned int size() const
Definition: time_cache.h:55
ed::TimeCache::begin
const_iterator begin() const
Definition: time_cache.h:52
ed::TimeCache::end
const_iterator end() const
Definition: time_cache.h:53
ed::TimeCache::insert
void insert(const Time &t, const T &value)
Definition: time_cache.h:22