DiaDes  0.1
DIAgnosis of Discrete-Event System
Enumerators.hh
Go to the documentation of this file.
1 
9 #ifndef __DIADES__UTILS__ENUMERATORS__HH__
10 #define __DIADES__UTILS__ENUMERATORS__HH__
11 
12 #include<vector>
13 
14 namespace Diades
15 {
16  namespace Utils
17  {
18 
19  template<typename T>
21  {
22  public:
23  using Value = T;
24  private:
25  std::vector<T> _start;
26  std::vector<T> _end;
27  std::vector<T> _current;
28  bool _completed;
29  bool _empty;
30  size_t _counter;
31 
32 
33  void checkEmpty()
34  {
35  _empty = false;
36  size_t index = 0;
37  while(!_empty && (index < _start.size()))
38  {
39  _empty = _start[index] == _end[index];
40  ++index;
41  }
42  }
43 
44  public:
45 
49  RangesEnumeration() = default;
50 
51 
56  RangesEnumeration(RangesEnumeration const& other) = default;
62  RangesEnumeration& operator=(RangesEnumeration const& other) = default;
67  RangesEnumeration(RangesEnumeration&& other) = default;
73  RangesEnumeration& operator=(RangesEnumeration&& other) = default;
74 
78  virtual ~RangesEnumeration() = default;
79 
80  void
81  setup(std::vector<T> && start, const std::vector<T> && end)
82  {
83  _start = start;
84  _current = start;
85  _end = end;
86  checkEmpty();
87  _completed = (_current == _end);
88  _counter = 0;
89  }
90 
91  RangesEnumeration(const std::vector<T> & start, const std::vector<T> & end) :
92  _start(start), _end(end), _current(start), _completed(start == end), _empty(false), _counter(0)
93  {
94  checkEmpty();
95  }
96 
100  size_t
101  count() const
102  {
103  return _counter;
104  }
105 
106  const std::vector<T> &
107  current() const
108  {
109  return _current;
110  }
111 
112  void
114  {
115  _current = _start;
116  _completed = (_start == _end);
117  _counter = 0;
118  }
119 
120  bool
122  {
123  if(!empty())
124  {
125  if(!_completed)
126  {
127  ++_counter;
128  size_t index = 0;
129  ++_current[index];
130  auto indexMax = _current.size() - 1;
131  while((index != indexMax) && (_current[index] == _end[index]))
132  {
133  _current[index] = _start[index];
134  ++index;
135  ++_current[index];
136  }
137  _completed = _current[index] == _end[index];
138  }
139  return !_completed;
140  }
141  return false;
142  }
143 
144  bool
145  empty() const
146  {
147  return _empty;
148  }
149 
150  bool
151  completed() const
152  {
153  return _completed;
154  }
155  };
156  }
157 }
158 
159 
160 #endif /* __DIADES__UTILS__ENUMERATORS_HH */
161 
RangesEnumeration(const std::vector< T > &start, const std::vector< T > &end)
Definition: Enumerators.hh:91
RangesEnumeration & operator=(RangesEnumeration const &other)=default
Namespace of the Diades project.
void setup(std::vector< T > &&start, const std::vector< T > &&end)
Definition: Enumerators.hh:81
virtual ~RangesEnumeration()=default
const std::vector< T > & current() const
Definition: Enumerators.hh:107