cached_lazy_list.hpp
Go to the documentation of this file.
1 
6 #ifndef CACHED_LAZY_LIST_HPP
7 #define CACHED_LAZY_LIST_HPP 1
8 
9 #include <vector>
10 #include "lazy_list.hpp"
11 
12 using std::vector;
13 using std::size_t;
14 using std::pair;
15 
17 template<class T> class CachedLazyList
18 {
19 public:
20 
24  explicit CachedLazyList(const LazyList<T>& ll):
25  ll_(ll), cached_values_(ll.size(),pair<bool,T>(false,T())) {}
26 
30  size_t size(void) const
31  {
32  return ll_.size();
33  }
34 
39  const T& operator[](size_t i) const
40  {
41  assert(i<size());
42  if(!cached_values_[i].first)
43  cached_values_[i] = pair<bool,T>(true,ll_[i]);
44  return cached_values_[i].second;
45  }
46 
48  void reset(void) const
49  {
50  cached_values_.resize(ll_.size());
51  for(size_t i=0;i<cached_values_.size();++i)
52  cached_values_[i].first = false;
53  }
54 
56  virtual ~CachedLazyList(void) {}
57 
58 private:
59  const LazyList<T>& ll_;
60  mutable vector<pair<bool,T> > cached_values_;
61 };
62 
63 #endif // CACHED_LAZY_LIST_HPP
Lazily evaluated list.
virtual ~CachedLazyList(void)
Class destructor.
Cached lazy list.
void reset(void) const
Marks all terms for recalculation.
const T & operator[](size_t i) const
Element access function.
Ordered list whose terms are evaluated lazily.
Definition: lazy_list.hpp:17
size_t size(void) const
Returns the size of the list.
CachedLazyList(const LazyList< T > &ll)
Class constructor.