sources/range.h

changeset 185
e83ec58cc458
parent 184
afd63357a03d
child 186
9330b93d9946
equal deleted inserted replaced
184:afd63357a03d 185:e83ec58cc458
1 /*
2 Copyright 2014 - 2016 Teemu Piippo
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8
9 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14 3. Neither the name of the copyright holder nor the names of its
15 contributors may be used to endorse or promote products derived from
16 this software without specific prior written permission.
17
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
21 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
22 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #pragma once
32 #include <algorithm>
33 #include <memory>
34 #include "basics.h"
35 BEGIN_ZFC_NAMESPACE
36
37 //
38 // -------------------------------------------------------------------------------------------------
39 //
40
41 template<typename T>
42 class Range
43 {
44 T m_a;
45 T m_b;
46 T m_step;
47
48 public:
49 struct Iterator
50 {
51 T value;
52 T step;
53
54 Iterator (T value, T step) :
55 value (value),
56 step (step) {}
57
58 T& operator*()
59 {
60 return value;
61 }
62
63 bool operator== (const Iterator& other) const
64 {
65 return value == other.value;
66 }
67
68 bool operator!= (const Iterator& other) const
69 {
70 return value < other.value;
71 }
72
73 Iterator& operator++()
74 {
75 value += step; return *this;
76 }
77 };
78
79 Range (T a, T b, T step = 1) :
80 m_a (a),
81 m_b (b),
82 m_step (step)
83 {
84 check_bounds();
85 }
86
87 Range() :
88 m_a (T()),
89 m_b (T()) {}
90
91 Iterator begin() const
92 {
93 return Iterator(min(), m_step);
94 }
95
96 Iterator end() const
97 {
98 return Iterator(max(), m_step);
99 }
100
101 T min() const
102 {
103 return m_a;
104 }
105
106 T max() const
107 {
108 return m_b;
109 }
110
111 T step() const
112 {
113 return m_step;
114 }
115
116 void check_bounds()
117 {
118 if (m_b < m_a)
119 std::swap (m_a, m_b);
120 }
121
122 bool contains (T c) const
123 {
124 return c >= m_a
125 and c <= m_b;
126 }
127
128 bool contains_exclusively (T c) const
129 {
130 return c > m_a
131 and c < m_b;
132 }
133
134 bool overlaps (Range<T> const& other) const
135 {
136 return contains (other.m_a)
137 or contains (other.m_b);
138 }
139
140 bool operator== (Range<T> const& other) const
141 {
142 return m_a == other.m_a
143 and m_b == other.m_b;
144 }
145
146 bool operator!= (Range<T> const& other) const
147 {
148 return not operator== (other);
149 }
150 };
151
152 template<typename T>
153 Range<T> range(T a, T b, T step = 1)
154 {
155 return Range<T>(a, b, step);
156 }
157
158 template<typename T>
159 Range<T> range(T b)
160 {
161 return Range<T>(T(), b);
162 }
163
164 END_ZFC_NAMESPACE

mercurial