nCine  2025.04.r498-9d36345
A cross-platform 2D game engine
iterator.h
1 #ifndef NCTL_ITERATOR
2 #define NCTL_ITERATOR
3 
4 namespace nctl {
5 
8 {};
11 {};
14 {};
15 
17 template <class Iterator>
19 {};
20 
22 // OPERATIONS
24 
25 namespace {
26 
28  template <class Iterator>
29  inline void advance(Iterator &it, int n, RandomAccessIteratorTag)
30  {
31  it += n;
32  }
33 
35  template <class Iterator>
36  inline void advance(Iterator &it, int n, BidirectionalIteratorTag)
37  {
38  if (n < 0)
39  {
40  while (n++)
41  --it;
42  }
43  else
44  {
45  while (n--)
46  ++it;
47  }
48  }
49 
51  template <class Iterator>
52  inline void advance(Iterator &it, int n, ForwardIteratorTag)
53  {
54  if (n > 0)
55  {
56  while (n--)
57  ++it;
58  }
59  }
60 
61 }
62 
64 template <class Iterator>
65 inline void advance(Iterator &it, int n)
66 {
67  advance(it, n, IteratorTraits<Iterator>::IteratorCategory());
68 }
69 
71 template <class Iterator>
72 inline Iterator next(Iterator it, unsigned int n)
73 {
74  advance(it, n);
75  return it;
76 }
77 
79 template <class Iterator>
80 inline Iterator next(Iterator it)
81 {
82  advance(it, 1);
83  return it;
84 }
85 
87 template <class Iterator>
88 inline Iterator prev(Iterator it, unsigned int n)
89 {
90  advance(it, -n);
91  return it;
92 }
93 
95 template <class Iterator>
96 inline Iterator prev(Iterator it)
97 {
98  advance(it, -1);
99  return it;
100 }
101 
102 namespace {
103 
105  template <class RandomAccessIterator>
106  inline int distance(RandomAccessIterator &first, const RandomAccessIterator &last, RandomAccessIteratorTag)
107  {
108  return last - first;
109  }
110 
112  template <class ForwardIterator>
113  inline int distance(ForwardIterator &first, const ForwardIterator &last, ForwardIteratorTag)
114  {
115  int counter = 0;
116 
117  for (; first != last; ++first)
118  counter++;
119 
120  return counter;
121  }
122 
123 }
124 
126 template <class Iterator>
127 inline int distance(Iterator first, const Iterator last)
128 {
129  return distance(first, last, IteratorTraits<Iterator>::IteratorCategory());
130 }
131 
133 // REVERSE RANGE ADAPTER
135 
136 template <class T>
138 {
139  T &iterable;
140 };
141 
142 template <class T>
143 auto begin(ReversionWrapper<T> c) -> decltype(rBegin(c.iterable))
144 {
145  return rBegin(c.iterable);
146 }
147 
148 template <class T>
149 auto end(ReversionWrapper<T> c) -> decltype(rEnd(c.iterable))
150 {
151  return rEnd(c.iterable);
152 }
153 
154 template <class T>
155 ReversionWrapper<T> reverse(T &&iterable)
156 {
157  return { iterable };
158 }
159 
161 // RANGE
163 
164 template <class Container>
165 typename Container::Iterator begin(Container &c)
166 {
167  return c.begin();
168 }
169 
170 template <class Container>
171 typename Container::ConstIterator cBegin(const Container &c)
172 {
173  return c.begin();
174 }
175 
176 template <class Container>
177 typename Container::Iterator end(Container &c)
178 {
179  return c.end();
180 }
181 
182 template <class Container>
183 typename Container::ConstIterator cEnd(const Container &c)
184 {
185  return c.end();
186 }
187 
188 template <class Container>
189 typename Container::ReverseIterator rBegin(Container &c)
190 {
191  return c.rBegin();
192 }
193 
194 template <class Container>
195 typename Container::ConstReverseIterator crBegin(const Container &c)
196 {
197  return c.rBegin();
198 }
199 
200 template <class Container>
201 typename Container::ReverseIterator rEnd(Container &c)
202 {
203  return c.rEnd();
204 }
205 
206 template <class Container>
207 typename Container::ConstReverseIterator crEnd(const Container &c)
208 {
209  return c.rEnd();
210 }
211 
212 }
213 
214 #endif
Dispatching tag for iterators that can move both ways, one element at a time.
Definition: iterator.h:11
Dispatching tag for iterators that can only move forward, one element at a time.
Definition: iterator.h:8
Base iterator traits structure.
Definition: iterator.h:19
Dispatching tag for iterators that can jump arbitrary distances in both ways.
Definition: iterator.h:14
Definition: iterator.h:138