Euphoria
iterate.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "assert/assert.h"
4 
5 
6 namespace eu::core
7 {
8  // todo(Gustav): consider removing or simplifying this, it looks confusing and complex
9  template<typename T>
10  struct StepIterator
11  {
13  T to;
14  T step;
15  bool ended;
16  bool last;
17 
18  StepIterator(T c, T t, T s, bool e)
19  : current(c), to(t), step(s), ended(e), last(is_ended(c, t, s))
20  {
21  }
22 
23  bool operator!=(const StepIterator<T>& rhs) const
24  {
25  ASSERT(to == rhs.to);
26  ASSERT(step == rhs.step);
27 
28  return ended != rhs.ended;
29  }
30 
31  T operator*() const
32  {
33  return current;
34  }
35 
36  void operator++()
37  {
38  current += step;
39  const auto curr = is_ended(current, to, step);
40  if(curr != last)
41  {
42  ended = true;
43  }
44  }
45 
46  static bool is_ended(T c, T t, T s)
47  {
48  if(s > 0) { return c >= t;}
49  else { return c > t;}
50  }
51  };
52 
53 
54  template<typename T>
56  {
57  T from;
58  T to;
59  T step;
60 
62  {
63  return StepIterator<T>{from, to, step, false};
64  }
65 
67  {
68  return StepIterator<T>{from, to, step, true};
69  }
70  };
71 
72  template<typename T>
73  StepIteratorCreator<T> iterate(const T from, T to, T step=1)
74  {
75  return StepIteratorCreator<T>{from, to, step};
76  }
77 }
78 
#define ASSERT(x)
Definition: assert.h:29
StepIteratorCreator< T > iterate(const T from, T to, T step=1)
Definition: iterate.h:73
StepIterator< T > begin() const
Definition: iterate.h:61
StepIterator< T > end() const
Definition: iterate.h:66
bool operator!=(const StepIterator< T > &rhs) const
Definition: iterate.h:23
T operator*() const
Definition: iterate.h:31
static bool is_ended(T c, T t, T s)
Definition: iterate.h:46
StepIterator(T c, T t, T s, bool e)
Definition: iterate.h:18