TCPSPSuite
fast_reset_vector.hpp
1 #ifndef FAST_RESET_VECTOR_H
2 #define FAST_RESET_VECTOR_H
3 
4 #include <stddef.h>
5 #include <utility>
6 #include <vector>
7 
8 template<class T>
9 class FastResetVector {
10 public:
11  FastResetVector(size_t size, T && init_value_in)
12  : init_value(init_value_in), round(1), data(size, std::pair<T, unsigned int>(init_value, 0))
13  {}
14 
15  void reset() {
16  this->round++;
17  }
18 
19  const T & operator[](size_t index) const
20  {
21  if (data[index].second != this->round) {
22  return this->init_value;
23  } else {
24  return data[index].first;
25  }
26  }
27 
28  T & operator[](size_t index)
29  {
30  if (data[index].second != this->round) {
31  data[index] = {this->init_value, this->round};
32  }
33 
34  return data[index].first;
35  }
36 
37 private:
38  T init_value;
39  unsigned int round;
40  std::vector<std::pair<T, unsigned int>> data;
41 };
42 
43 #endif