Documentation of CSL
counter.h
Go to the documentation of this file.
1 // This file is part of MARTY.
2 //
3 // MARTY is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // MARTY is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with MARTY. If not, see <https://www.gnu.org/licenses/>.
15 
23 #ifndef COUNTER_H_INCLUDED
24 #define COUNTER_H_INCLUDED
25 
26 #include <iostream>
27 #include <memory>
28 #include <list>
29 #include <vector>
30 
31 template<class T>
32 class counter;
33 
34 template<class T>
36 
37  private:
38 
39  counter<T>* ptr;
40 
41  public:
42 
44  :ptr(t_ptr)
45  {
46 
47  }
48 
49  template<class U>
51  :ptr(other.ptr)
52  {
53 
54  }
55 
56  counter_iterator(counter_iterator<T> const& other) = default;
57  counter_iterator& operator=(counter_iterator<T> const& other) = default;
58  counter_iterator(counter_iterator<T>&& other) = default;
59  counter_iterator& operator=(counter_iterator<T>&& other) = default;
60  ~counter_iterator() = default;
61 
62  counter_iterator& operator++()
63  {
64  ptr = (*ptr).next;
65  return *this;
66  }
67 
68  counter_iterator operator++(int)
69  {
70  counter_iterator<T> copy(ptr);
71  ++(*this);
72  return copy;
73  }
74 
75  T& operator*()
76  {
77  return (*ptr).value;
78  }
79 
80  T operator*() const
81  {
82  return (*ptr).value;
83  }
84 
85  bool operator==(counter_iterator<T> const& other) const
86  {
87  return ptr == other.ptr;
88  }
89 
90  bool operator!=(counter_iterator<T> const& other) const
91  {
92  return ptr != other.ptr;
93  }
94 };
95 
96 template<class T>
97 class counter {
98 
99  friend class counter_iterator<T>;
100 
101  public:
102 
103  using iterator = counter_iterator<T>;
104  using const_iterator = counter_iterator<const T>;
105 
106  private:
107 
108  constexpr static char separator = '\'';
109  constexpr static counter* end_value = nullptr;
110 
111  size_t max = 0;
112  T value;
113  counter* next = end_value;
114 
115  public:
116 
117  counter()
118  {
119 
120  }
121 
122  counter(size_t t_max)
123  :max(t_max),
124  value(T(0))
125  {
126 
127  }
128 
129  template<class Iterator>
130  counter(Iterator iter,
131  Iterator end)
132  {
133  max = *iter;
134  if (++iter != end)
135  next = new counter(iter, end);
136  }
137 
138  template<class Iterable>
139  counter(Iterable const& containor)
140  :counter(containor.begin(), containor.end())
141  {
142 
143  }
144 
145  counter(const counter& other)
146  :max(other.max),
147  value(other.value)
148  {
149  if (other.next != end_value)
150  next = new counter(*other.next);
151  }
152 
153  counter& operator=(counter const& other)
154  {
155  max = other.max;
156  value = other.value;
157  if (other.next != end_value)
158  next = new counter(*other.next);
159  else
160  next = end_value;
161 
162  return *this;
163  }
164 
165  counter(counter&& other) = default;
166  counter& operator=(counter&& other) = default;
167 
168  ~counter()
169  {
170  if (next != end_value)
171  delete next;
172  }
173 
174  size_t size() const
175  {
176  return (next != end_value) ? (1 + next->size()) : 1;
177  }
178 
179  iterator begin()
180  {
181  return counter_iterator<T>(this);
182  }
183 
184  iterator end()
185  {
186  return end_value;
187  }
188 
189  const_iterator begin() const
190  {
191  return counter_iterator<const T>(this);
192  }
193 
194  const_iterator end() const
195  {
196  return end_value;
197  }
198 
199  void restart()
200  {
201  for (auto& value : *this)
202  value = 0;
203  }
204 
205  template<class Iterator>
206  void getCount(Iterator iter) const
207  {
208  *iter = value;
209  if (next != end_value)
210  next->getCount(++iter);
211  }
212 
213  template<class Iterator>
214  void setCount(Iterator iter)
215  {
216  value = *iter;
217  if (next != end_value)
218  next->setCount(++iter);
219  }
220 
221  void push_back(T max,
222  T value)
223  {
224  if (next != end_value)
225  (*next).push_back(max, value);
226  else
227  next = new counter<T>(max, value);
228  }
229 
230  counter& operator++()
231  {
232  ++value;
233  if (value == max) {
234  value = 0;
235  if (next != end_value)
236  ++(*next);
237  }
238  return *this;
239  }
240 
241  counter operator++(int)
242  {
243  counter other(*this);
244  ++(*this);
245  return other;
246  }
247 
248  T& operator[](size_t pos)
249  {
250  if (pos == 0)
251  return value;
252  return (*next)[pos-1];
253  }
254 
255  T operator[](size_t pos) const
256  {
257  if (pos == 0)
258  return value;
259  return (*next)[pos-1];
260  }
261 
262  operator bool() const
263  {
264  return (value != 0 or (next != end_value and bool(*next)));
265  }
266 
267  friend
268  std::ostream& operator<<(std::ostream& out, counter const& c)
269  {
270  out << c.value;
271  if (c.next != counter::end_value)
272  out << counter::separator << *c.next;
273  return out;
274  }
275 };
276 
277 
278 #endif
Definition: counter.h:35
Definition: counter.h:32