sources/range.h

changeset 5
146825d63b9a
parent 1
4dd5bde4e777
child 69
eb4c25284a19
equal deleted inserted replaced
4:ec5387e121fa 5:146825d63b9a
61 METHOD end() const -> Iterator; 61 METHOD end() const -> Iterator;
62 METHOD min() const -> T; 62 METHOD min() const -> T;
63 METHOD max() const -> T; 63 METHOD max() const -> T;
64 METHOD check_bounds() -> void; 64 METHOD check_bounds() -> void;
65 METHOD contains (const T& c) const -> bool; 65 METHOD contains (const T& c) const -> bool;
66 METHOD contains_exclusive (const T& c) const -> bool; 66 METHOD contains_exclusively (const T& c) const -> bool;
67 METHOD overlaps (Range<T> const& other) const -> bool; 67 METHOD overlaps (Range<T> const& other) const -> bool;
68 METHOD operator== (Range<T> const& other) const -> bool; 68 METHOD operator== (Range<T> const& other) const -> bool;
69 METHOD operator!= (Range<T> const& other) const -> bool; 69 METHOD operator!= (Range<T> const& other) const -> bool;
70 }; 70 };
71 71
72 // 72 // -------------------------------------------------------------------------------------------------
73 // ------------------------------------------------------------------------------------------------- 73 //
74 //
75
76 template<typename T> inline METHOD
77 Range<T>::Iterator::operator*() -> T&
78 {
79 return value;
80 }
81
82 //
83 // -------------------------------------------------------------------------------------------------
84 //
85
86 template<typename T> inline METHOD
87 Range<T>::Iterator::operator== (const Iterator& other) const -> bool
88 {
89 return value == other.value;
90 }
91
92 //
93 // -------------------------------------------------------------------------------------------------
94 //
95
96 template<typename T> inline METHOD
97 Range<T>::Iterator::operator!= (const Iterator& other) const -> bool
98 {
99 return value != other.value;
100 }
101
102 //
103 // -------------------------------------------------------------------------------------------------
104 //
105
106 template<typename T> inline METHOD
107 Range<T>::Iterator::operator++() -> Iterator&
108 {
109 value += step;
110 return *this;
111 }
112
113 //
114 // -------------------------------------------------------------------------------------------------
115 //
116
117 template<typename T> 74 template<typename T>
118 Range<T>::Range (const T& a, const T& b, const T& step) : 75 Range<T>::Range (const T& a, const T& b, const T& step) :
119 m_a (a), 76 m_a (a),
120 m_b (b), 77 m_b (b),
121 m_step (step) 78 m_step (step)
122 { 79 {
123 check_bounds(); 80 check_bounds();
124 } 81 }
125 82
126 // 83 // -------------------------------------------------------------------------------------------------
127 // ------------------------------------------------------------------------------------------------- 84 //
128 //
129
130 template<typename T> 85 template<typename T>
131 Range<T>::Range() : 86 Range<T>::Range() :
132 m_a (T()), 87 m_a (T()),
133 m_b (T()) {} 88 m_b (T()) {}
134 89
135 // 90 // -------------------------------------------------------------------------------------------------
136 // ------------------------------------------------------------------------------------------------- 91 //
137 // 92 template<typename T> inline METHOD
138 93 Range<T>::Iterator::operator*() -> T&
94 {
95 return value;
96 }
97
98 // -------------------------------------------------------------------------------------------------
99 //
100 template<typename T> inline METHOD
101 Range<T>::Iterator::operator== (const Iterator& other) const -> bool
102 {
103 return value == other.value;
104 }
105
106 // -------------------------------------------------------------------------------------------------
107 //
108 template<typename T> inline METHOD
109 Range<T>::Iterator::operator!= (const Iterator& other) const -> bool
110 {
111 return value != other.value;
112 }
113
114 // -------------------------------------------------------------------------------------------------
115 //
116 template<typename T> inline METHOD
117 Range<T>::Iterator::operator++() -> Iterator&
118 {
119 value += step;
120 return *this;
121 }
122
123 // -------------------------------------------------------------------------------------------------
124 //
139 template<typename T> METHOD 125 template<typename T> METHOD
140 Range<T>::check_bounds() -> void 126 Range<T>::check_bounds() -> void
141 { 127 {
142 if (m_b < m_a) 128 if (m_b < m_a)
143 std::swap (m_a, m_b); 129 std::swap (m_a, m_b);
144 } 130 }
145 131
146 // 132 // -------------------------------------------------------------------------------------------------
147 // ------------------------------------------------------------------------------------------------- 133 //
148 //
149
150 template<typename T> METHOD 134 template<typename T> METHOD
151 Range<T>::contains (const T& c) const -> bool 135 Range<T>::contains (const T& c) const -> bool
152 { 136 {
153 return (c >= m_a) and (c <= m_b); 137 return (c >= m_a) and (c <= m_b);
154 } 138 }
155 139
156 // 140 // -------------------------------------------------------------------------------------------------
157 // ------------------------------------------------------------------------------------------------- 141 //
158 // 142 template<typename T> METHOD
159 143 Range<T>::contains_exclusively (const T& c) const -> bool
160 template<typename T> METHOD
161 Range<T>::contains_exclusive (const T& c) const -> bool
162 { 144 {
163 return (c > m_a) and (c < m_b); 145 return (c > m_a) and (c < m_b);
164 } 146 }
165 147
166 // 148 // -------------------------------------------------------------------------------------------------
167 // ------------------------------------------------------------------------------------------------- 149 //
168 //
169
170 template<typename T> METHOD 150 template<typename T> METHOD
171 Range<T>::overlaps (Range<T> const& other) const -> bool 151 Range<T>::overlaps (Range<T> const& other) const -> bool
172 { 152 {
173 return contains (other.m_a) or contains (other.m_b); 153 return contains (other.m_a) or contains (other.m_b);
174 } 154 }
175 155
176 // 156 // -------------------------------------------------------------------------------------------------
177 // ------------------------------------------------------------------------------------------------- 157 //
178 //
179
180 template<typename T> METHOD 158 template<typename T> METHOD
181 Range<T>::operator== (Range<T> const& other) const -> bool 159 Range<T>::operator== (Range<T> const& other) const -> bool
182 { 160 {
183 return m_a == other.m_a and m_b == other.m_b; 161 return m_a == other.m_a and m_b == other.m_b;
184 } 162 }
185 163
186 // 164 // -------------------------------------------------------------------------------------------------
187 // ------------------------------------------------------------------------------------------------- 165 //
188 //
189
190 template<typename T> METHOD 166 template<typename T> METHOD
191 Range<T>::operator!= (Range<T> const& other) const -> bool 167 Range<T>::operator!= (Range<T> const& other) const -> bool
192 { 168 {
193 return not operator== (other); 169 return not operator== (other);
194 } 170 }
195 171
196 // 172 // -------------------------------------------------------------------------------------------------
197 // ------------------------------------------------------------------------------------------------- 173 //
198 //
199
200 template<typename T> METHOD 174 template<typename T> METHOD
201 Range<T>::min() const -> T 175 Range<T>::min() const -> T
202 { 176 {
203 return m_a; 177 return m_a;
204 } 178 }
205 179
206 // 180 // -------------------------------------------------------------------------------------------------
207 // ------------------------------------------------------------------------------------------------- 181 //
208 //
209
210 template<typename T> METHOD 182 template<typename T> METHOD
211 Range<T>::max() const -> T 183 Range<T>::max() const -> T
212 { 184 {
213 return m_b; 185 return m_b;
214 } 186 }
215 187
216 // 188 // -------------------------------------------------------------------------------------------------
217 // ------------------------------------------------------------------------------------------------- 189 //
218 //
219
220 template<typename T> METHOD 190 template<typename T> METHOD
221 Range<T>::begin() const -> Iterator 191 Range<T>::begin() const -> Iterator
222 { 192 {
223 Iterator it; 193 Iterator it;
224 it.value = min(); 194 it.value = min();
225 it.step = m_step; 195 it.step = m_step;
226 return it; 196 return it;
227 } 197 }
228 198
229 // 199 // -------------------------------------------------------------------------------------------------
230 // ------------------------------------------------------------------------------------------------- 200 //
231 //
232
233 template<typename T> METHOD 201 template<typename T> METHOD
234 Range<T>::end() const -> Iterator 202 Range<T>::end() const -> Iterator
235 { 203 {
236 Iterator it; 204 Iterator it;
237 it.value = max() + 1; 205 it.value = max() + 1;

mercurial