Gamedev Framework (gf)  0.3.0
A C++11 framework for 2D games
Range.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016 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 "Portability.h"
28 
29 namespace gf {
30 #ifndef DOXYGEN_SHOULD_SKIP_THIS
31 inline namespace v1 {
32 #endif
33 
34  /**
35  * @ingroup core
36  * @brief A half-open range of values
37  *
38  * gf::Range represents a half-open range of values.
39  *
40  */
41  template<typename T>
42  struct Range {
43  T lo; ///< The lower endpoint (included)
44  T hi; ///< The higher endpoint (excluded)
45 
46  /**
47  * @ingroup core
48  * @brief A range iterator
49  */
50  struct Iterator : public std::iterator<std::input_iterator_tag, T> {
51  T index; ///< The index in the range
52 
53  /**
54  * @brief Constructor
55  *
56  * @param iteratorIndex Index in the range
57  */
58  constexpr Iterator(T iteratorIndex) noexcept
59  : index(iteratorIndex)
60  {
61 
62  }
63 
64  /**
65  * @brief Dereference operator
66  *
67  * @return The index
68  */
69  T operator*() noexcept {
70  return index;
71  }
72 
73  /**
74  * @brief Increment operator
75  *
76  * @return The iterator
77  */
78  Iterator& operator++() noexcept {
79  ++index;
80  return *this;
81  }
82 
83  /**
84  * @brief Inequality operator
85  *
86  * @param other Another iterator
87  * @return True if the iterator are different
88  */
89  constexpr bool operator!=(const Iterator& other) const noexcept {
90  return index != other.index;
91  }
92 
93  /**
94  * @brief Equality operator
95  *
96  * @param other Another iterator
97  * @return True if the iterator are the same
98  */
99  constexpr bool operator==(const Iterator& other) const noexcept {
100  return index == other.index;
101  }
102  };
103 
104  /**
105  * @brief Check if a value is in a range
106  *
107  * @param value The value to test
108  * @return True if the value is in the range
109  */
110  constexpr bool contains(T value) const noexcept {
111  return lo <= value && value < hi;
112  }
113 
114  /**
115  * @brief Get a begin iterator
116  *
117  * @return A begin iterator
118  * @sa end()
119  */
120  constexpr Iterator begin() const noexcept {
121  return Iterator(lo);
122  }
123 
124  /**
125  * @brief Get a end iterator
126  *
127  * @return A end iterator
128  * @sa begin()
129  */
130  constexpr Iterator end() const noexcept {
131  return Iterator(hi);
132  }
133 
134  /**
135  * @brief Get the length of the range
136  *
137  * @return The length of the range
138  */
139  constexpr T length() const noexcept {
140  return hi - lo;
141  }
142 
143  /**
144  * @brief Check if the range is empty
145  *
146  * @return True if the range is empty
147  * @sa isValid()
148  */
149  constexpr bool isEmpty() const noexcept {
150  return lo >= hi;
151  }
152 
153  /**
154  * @brief Check is the range is valid
155  *
156  * @return True if the range is valid
157  * @sa isEmpty()
158  */
159  constexpr bool isValid() const noexcept {
160  return lo <= hi;
161  }
162 
163  };
164 
165  /**
166  * @ingroup core
167  * @brief A float range
168  */
169  using RangeF = Range<float>;
170 
171  /**
172  * @ingroup core
173  * @brief A int range
174  */
175  using RangeI = Range<int>;
176 
177  /**
178  * @ingroup core
179  * @brief A unsigned range
180  */
181  using RangeU = Range<unsigned>;
182 
183  /**
184  * @ingroup core
185  * @brief A `std::size_t` range
186  */
187  using RangeZ = Range<std::size_t>;
188 
189 // MSVC does not like extern template
190 #ifndef _MSC_VER
191  extern template struct Range<float>;
192  extern template struct Range<int>;
193  extern template struct Range<unsigned>;
194 #endif
195 
196 #ifndef DOXYGEN_SHOULD_SKIP_THIS
197 }
198 #endif
199 }
200 
201 #endif // GF_RANGE_H
A range iterator.
Definition: Range.h:50
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:89
T operator*() noexcept
Dereference operator.
Definition: Range.h:69
constexpr bool isEmpty() const noexcept
Check if the range is empty.
Definition: Range.h:149
T lo
The lower endpoint (included)
Definition: Range.h:43
constexpr bool operator==(const Iterator &other) const noexcept
Equality operator.
Definition: Range.h:99
constexpr Iterator end() const noexcept
Get a end iterator.
Definition: Range.h:130
T hi
The higher endpoint (excluded)
Definition: Range.h:44
constexpr bool contains(T value) const noexcept
Check if a value is in a range.
Definition: Range.h:110
constexpr Iterator begin() const noexcept
Get a begin iterator.
Definition: Range.h:120
Definition: Action.h:34
T index
The index in the range.
Definition: Range.h:51
constexpr T length() const noexcept
Get the length of the range.
Definition: Range.h:139
Iterator & operator++() noexcept
Increment operator.
Definition: Range.h:78
constexpr bool isValid() const noexcept
Check is the range is valid.
Definition: Range.h:159