Gamedev Framework (gf)  0.7.0
A C++14 framework for 2D games
Range.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2018 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 #ifndef GF_RANGE_H
22 #define GF_RANGE_H
23 
24 #include <cstddef>
25 #include <iterator>
26 
27 #include "Vector.h"
28 
29 namespace gf {
30 #ifndef DOXYGEN_SHOULD_SKIP_THIS
31 inline namespace v1 {
32 #endif
33 
41  template<typename T>
42  struct Range {
43  T lo;
44  T hi;
45 
50  struct Iterator : public std::iterator<std::input_iterator_tag, T> {
51  T index;
52 
58  constexpr Iterator(T iteratorIndex) noexcept
59  : index(iteratorIndex)
60  {
61 
62  }
63 
69  T operator*() noexcept {
70  return index;
71  }
72 
78  Iterator& operator++() noexcept {
79  ++index;
80  return *this;
81  }
82 
88  Iterator operator++(int) noexcept {
89  Iterator copy = *this;
90  ++index;
91  return copy;
92  }
93 
100  constexpr bool operator!=(const Iterator& other) const noexcept {
101  return index != other.index;
102  }
103 
110  constexpr bool operator==(const Iterator& other) const noexcept {
111  return index == other.index;
112  }
113 
114  };
115 
122  constexpr bool contains(T value) const noexcept {
123  return lo <= value && value < hi;
124  }
125 
132  constexpr Iterator begin() const noexcept {
133  return Iterator(lo);
134  }
135 
142  constexpr Iterator end() const noexcept {
143  return Iterator(hi);
144  }
145 
151  constexpr T length() const noexcept {
152  return hi - lo;
153  }
154 
161  constexpr bool isEmpty() const noexcept {
162  return lo >= hi;
163  }
164 
171  constexpr bool isValid() const noexcept {
172  return lo <= hi;
173  }
174 
175  };
176 
184 
192 
200 
208 
209 // MSVC does not like extern template
210 #ifndef _MSC_VER
211  extern template struct Range<float>;
212  extern template struct Range<int>;
213  extern template struct Range<unsigned>;
214 #endif
215 
216 
217 
224  template<typename T>
225  struct PositionRange {
228 
232  struct Iterator : public std::iterator<std::input_iterator_tag, T> {
235 
242  constexpr Iterator(Range<T> iteratorRange, Vector<T, 2> iteratorPosition) noexcept
243  : range(iteratorRange)
244  , position(iteratorPosition)
245  {
246 
247  }
248 
254  Vector<T, 2> operator*() noexcept {
255  return position;
256  }
257 
263  Iterator& operator++() noexcept {
264  step();
265  return *this;
266  }
267 
273  Iterator operator++(int) noexcept {
274  Iterator copy = *this;
275  step();
276  return copy;
277  }
278 
285  constexpr bool operator!=(const Iterator& other) const noexcept {
286  return position.x != other.position.x || position.y != other.position.y;
287  }
288 
295  constexpr bool operator==(const Iterator& other) const noexcept {
296  return position.x == other.position.x && position.y == other.position.y;
297  }
298 
299  private:
300  void step() {
301  ++position.x;
302 
303  if (position.x >= range.hi) {
304  position.x = range.lo;
305  ++position.y;
306  }
307  }
308  };
309 
316  constexpr Iterator begin() const noexcept {
317  return Iterator(first, { first.lo, second.lo });
318  }
319 
326  constexpr Iterator end() const noexcept {
327  return Iterator(first, { first.lo, second.hi });
328  }
329 
330  };
331 
332 
333 #ifndef DOXYGEN_SHOULD_SKIP_THIS
334 }
335 #endif
336 }
337 
338 #endif // GF_RANGE_H
T x
First coordinate in the (x,y) representation.
Definition: Vector.h:509
A range iterator.
Definition: Range.h:50
constexpr bool operator==(const Iterator &other) const noexcept
Equality operator.
Definition: Range.h:295
constexpr Iterator(T iteratorIndex) noexcept
Constructor.
Definition: Range.h:58
A half-open range of values.
Definition: Range.h:42
constexpr bool operator!=(const Iterator &other) const noexcept
Inequality operator.
Definition: Range.h:100
T operator*() noexcept
Dereference operator.
Definition: Range.h:69
Range< T > first
The range in the first dimension.
Definition: Range.h:226
Iterator & operator++() noexcept
Increment operator (prefix)
Definition: Range.h:263
constexpr bool isEmpty() const noexcept
Check if the range is empty.
Definition: Range.h:161
T lo
The lower endpoint (included)
Definition: Range.h:43
constexpr Iterator end() const noexcept
Get a end iterator.
Definition: Range.h:326
Iterator operator++(int) noexcept
Increment operator (postfix)
Definition: Range.h:88
constexpr bool operator==(const Iterator &other) const noexcept
Equality operator.
Definition: Range.h:110
constexpr Iterator end() const noexcept
Get a end iterator.
Definition: Range.h:142
constexpr bool operator!=(const Iterator &other) const noexcept
Inequality operator.
Definition: Range.h:285
Iterator operator++(int) noexcept
Increment operator (postfix)
Definition: Range.h:273
T y
Second coordinate in the (x,y) representation.
Definition: Vector.h:520
Range< T > second
The range in the second dimension.
Definition: Range.h:227
T hi
The higher endpoint (excluded)
Definition: Range.h:44
An iterator for a 2D range.
Definition: Range.h:232
constexpr bool contains(T value) const noexcept
Check if a value is in a range.
Definition: Range.h:122
constexpr Iterator begin() const noexcept
Get a begin iterator.
Definition: Range.h:132
The namespace for gf classes.
Definition: Action.h:34
Range< T > range
Definition: Range.h:233
A 2D vector.
Definition: Vector.h:316
constexpr Iterator begin() const noexcept
Get a begin iterator.
Definition: Range.h:316
A 2D range.
Definition: Range.h:225
constexpr Iterator(Range< T > iteratorRange, Vector< T, 2 > iteratorPosition) noexcept
Constructor.
Definition: Range.h:242
Vector< T, 2 > position
Definition: Range.h:234
T index
The index in the range.
Definition: Range.h:51
Vector< T, 2 > operator*() noexcept
Dereference operator.
Definition: Range.h:254
constexpr T length() const noexcept
Get the length of the range.
Definition: Range.h:151
Iterator & operator++() noexcept
Increment operator (prefix)
Definition: Range.h:78
constexpr bool isValid() const noexcept
Check is the range is valid.
Definition: Range.h:171