Library of Assembled Shared Sources
integral_range.inl
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2011 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43namespace lass
44{
45namespace stde
46{
47
48// --- const_integral_iterator ---------------------------------------------------------------------
49
50template <typename I>
51const_integral_iterator<I>::const_integral_iterator(value_type value, value_type step):
52 value_(value),
53 step_(step)
54{
55}
56
57
58
59template <typename I> inline
60typename const_integral_iterator<I>::pointer
61const_integral_iterator<I>::operator->() const
62{
63 return &value_;
64}
65
66
67
68template <typename I> inline
69typename const_integral_iterator<I>::reference
70const_integral_iterator<I>::operator*() const
71{
72 return value_;
73}
74
75
76
77template <typename I> inline
78typename const_integral_iterator<I>::value_type
79const_integral_iterator<I>::operator[](difference_type n) const
80{
81 return value_ + n * step_;
82}
83
84
85
86template <typename I> inline
87typename const_integral_iterator<I>::self_type&
88const_integral_iterator<I>::operator++()
89{
90 value_ += step_;
91 return *this;
92}
93
94
95
96template <typename I> inline
97typename const_integral_iterator<I>::self_type
98const_integral_iterator<I>::operator++(int)
99{
100 self_type temp(*this);
101 ++*this;
102 return temp;
103}
104
105
106
107template <typename I> inline
108typename const_integral_iterator<I>::self_type&
109const_integral_iterator<I>::operator--()
110{
111 value_ -= step_;
112 return *this;
113}
114
115
116
117template <typename I> inline
118typename const_integral_iterator<I>::self_type
119const_integral_iterator<I>::operator--(int)
120{
121 self_type temp(*this);
122 --*this;
123 return temp;
124}
125
126
127
128template <typename I> inline
129typename const_integral_iterator<I>::self_type&
130const_integral_iterator<I>::operator+=(difference_type n)
131{
132 value_ += n * step_;
133 return *this;
134}
135
136
137
138template <typename I> inline
139typename const_integral_iterator<I>::self_type&
140const_integral_iterator<I>::operator-=(difference_type n)
141{
142 value_ -= n * step_;
143 return *this;
144}
145
146
147
148template <typename I> inline
149typename const_integral_iterator<I>::self_type
150const_integral_iterator<I>::operator+(difference_type n) const
151{
152 self_type temp(*this);
153 temp += n;
154 return temp;
155}
156
157
158
159template <typename I> inline
160typename const_integral_iterator<I>::self_type
161const_integral_iterator<I>::operator-(difference_type n) const
162{
163 self_type temp(*this);
164 temp -= n;
165 return temp;
166}
167
168
169
170template <typename I> inline
171typename const_integral_iterator<I>::difference_type
172const_integral_iterator<I>::operator-(const self_type& other) const
173{
174 LASS_ASSERT(step_ == other.step_);
175 LASS_ASSERT((value_ - other.value_) % step_ == 0);
176 return (value_ - other.value_) / step_;
177}
178
179
180
181template <typename I> inline
182bool operator==(const const_integral_iterator<I>& a, const const_integral_iterator<I>& b)
183{
184 LASS_ASSERT(a.step_ == b.step_);
185 return a.value_ == b.value_;
186}
187
188
189
190template <typename I> inline
191bool operator!=(const const_integral_iterator<I>& a, const const_integral_iterator<I>& b)
192{
193 return !(a == b);
194}
195
196
197
198template <typename I> inline
199bool operator<(const const_integral_iterator<I>& a, const const_integral_iterator<I>& b)
200{
201 LASS_ASSERT(a.step_ == b.step_);
202 return a.value_ < b.value_;
203}
204
205
206
207template <typename I> inline
208bool operator>(const const_integral_iterator<I>& a, const const_integral_iterator<I>& b)
209{
210 return b < a;
211}
212
213
214
215template <typename I> inline
216bool operator<=(const const_integral_iterator<I>& a, const const_integral_iterator<I>& b)
217{
218 return !(b < a);
219}
220
221
222
223template <typename I> inline
224bool operator>=(const const_integral_iterator<I>& a, const const_integral_iterator<I>& b)
225{
226 return !(a < b);
227}
228
229
230
231// --- integral_range_t ----------------------------------------------------------------------------
232
233template <typename I> inline
234integral_range_t<I>::integral_range_t():
235 first_(0),
236 last_(0),
237 step_(0)
238{
239}
240
241
242
243template <typename I> inline
244integral_range_t<I>::integral_range_t(value_type last):
245 first_(0),
246 last_(last),
247 step_(last >= 0 ? 1 : -1)
248{
249}
250
251
252
253template <typename I> inline
254integral_range_t<I>::integral_range_t(value_type first, value_type last):
255 first_(first),
256 last_(last),
257 step_(last >= first ? 1 : -1)
258{
259}
260
261
262
263template <typename I> inline
264integral_range_t<I>::integral_range_t(value_type first, value_type last, value_type step):
265 first_(first),
266 last_(last),
267 step_(step)
268{
269 LASS_ASSERT(step != 0);
270 value_type m = (last_ - first_) % step_;
271 if (m != 0)
272 {
273 last_ += step_ - m;
274 }
275 LASS_ASSERT((last_ - first_) % step_ == 0);
276}
277
278
279
280template <typename I> inline
281typename integral_range_t<I>::const_iterator
282integral_range_t<I>::begin() const
283{
284 return const_iterator(first_, step_);
285}
286
287
288
289template <typename I> inline
290typename integral_range_t<I>::const_iterator
291integral_range_t<I>::end() const
292{
293 return const_iterator(last_, step_);
294}
295
296
297
298template <typename I> inline
299typename integral_range_t<I>::const_reverse_iterator
300integral_range_t<I>::rbegin() const
301{
302 return const_reverse_iterator(end());
303}
304
305
306
307template <typename I> inline
308typename integral_range_t<I>::const_reverse_iterator
309integral_range_t<I>::rend() const
310{
311 return const_reverse_iterator(begin());
312}
313
314
315
316template <typename I> inline
317typename integral_range_t<I>::self_type&
318integral_range_t<I>::operator++()
319{
320 LASS_ASSERT(first_ != last_);
321 first_ += step_;
322}
323
324
325
326template <typename I> inline
327typename integral_range_t<I>::self_type&
328integral_range_t<I>::operator--()
329{
330 LASS_ASSERT(first_ != last_);
331 first_ -= step_;
332}
333
334
335
336template <typename I> inline
337typename integral_range_t<I>::self_type&
338integral_range_t<I>::operator+=(difference_type n)
339{
340 LASS_ASSERT((step_ > 0) ? (first_ + n * step_ <= last_) : (first_ + n * step_ >= last_));
341 first_ += n * step_;
342}
343
344
345
346template <typename I> inline
347typename integral_range_t<I>::self_type&
348integral_range_t<I>::operator-=(difference_type n)
349{
350 first_ -= n * step_;
351}
352
353
354
355template <typename I> inline
356typename integral_range_t<I>::self_type
357integral_range_t<I>::operator+(difference_type n) const
358{
359 self_type temp(*this);
360 temp += n;
361 return temp;
362}
363
364
365
366template <typename I> inline
367typename integral_range_t<I>::self_type
368integral_range_t<I>::operator-(difference_type n) const
369{
370 self_type temp(*this);
371 temp -= n;
372 return temp;
373}
374
375
376
377template <typename I> inline
378const typename integral_range_t<I>::size_type
379integral_range_t<I>::size() const
380{
381 LASS_ASSERT((last_ - first_) % step_ == 0);
382 return static_cast<size_type>((last_ - first_) / step_);
383}
384
385
386
387template <typename I> inline
388const bool integral_range_t<I>::empty() const
389{
390 return first_ == last_;
391}
392
393
394
395template <typename I> inline
396const bool integral_range_t<I>::operator!() const
397{
398 return empty();
399}
400
401
402
403template <typename I> inline
404integral_range_t<I>::operator bool() const
405{
406 return !empty();
407}
408
409
410
411template <typename I>
412void integral_range_t<I>::swap(self_type& other)
413{
414 std::swap(first_, other.first_);
415 std::swap(last_, other.last_);
416 std::swap(step_, other.step_);
417}
418
419
420
421template <typename I> inline
422integral_range_t<I> integral_range(const I& last)
423{
424 return integral_range_t<I>(last);
425}
426
427
428
429template <typename I> inline
430integral_range_t<I> integral_range(const I& first, const I& last)
431{
432 return integral_range_t<I>(first, last);
433}
434
435
436
437template <typename I> inline
438integral_range_t<I> integral_range(const I& first, const I& last, const I& step)
439{
440 return integral_range_t<I>(first, last, step);
441}
442
443
444
445template <typename I> inline
446bool operator==(const integral_range_t<I>& a, const integral_range_t<I>& b)
447{
448 LASS_ASSERT(a.step_ == b.step_ && a.last_ == b.last_);
449 return a.first_ == b.first_;
450}
451
452
453
454template <typename I> inline
455bool operator!=(const integral_range_t<I>& a, const integral_range_t<I>& b)
456{
457 return !(a == b);
458}
459
460
461
462template <typename I> inline
463bool operator<(const integral_range_t<I>& a, const integral_range_t<I>& b)
464{
465 LASS_ASSERT(a.step_ == b.step_ && a.last_ == b.last_);
466 return a.first_ < b.first_;
467}
468
469
470
471template <typename I> inline
472bool operator>(const integral_range_t<I>& a, const integral_range_t<I>& b)
473{
474 return b < a;
475}
476
477
478
479template <typename I> inline
480bool operator<=(const integral_range_t<I>& a, const integral_range_t<I>& b)
481{
482 return !(b < a);
483}
484
485
486
487template <typename I> inline
488bool operator>=(const integral_range_t<I>& a, const integral_range_t<I>& b)
489{
490 return !(a < b);
491}
492
493}
494
495}
496
497// EOF
integral range.
lass extensions to the standard library
Library for Assembled Shared Sources.
Definition config.h:53