sources/range.h

changeset 69
eb4c25284a19
parent 5
146825d63b9a
child 73
07dda51a7a8e
equal deleted inserted replaced
68:202e74157de5 69:eb4c25284a19
46 public: 46 public:
47 struct Iterator 47 struct Iterator
48 { 48 {
49 T value; 49 T value;
50 T step; 50 T step;
51 inline METHOD operator*() -> T&; 51
52 inline METHOD operator== (const Iterator& other) const -> bool; 52 Iterator (T value, T step) :
53 inline METHOD operator!= (const Iterator& other) const -> bool; 53 value (value),
54 inline METHOD operator++() -> Iterator&; 54 step (step) {}
55
56 T& operator*()
57 {
58 return value;
59 }
60
61 bool operator== (const Iterator& other) const
62 {
63 return value == other.value;
64 }
65
66 bool operator!= (const Iterator& other) const
67 {
68 return value != other.value;
69 }
70
71 Iterator& operator++()
72 {
73 value += step; return *this;
74 }
55 }; 75 };
56 76
57 Range (const T& a, const T& b, const T& step = 1); 77 Range (T a, T b, T step = 1) :
58 Range(); 78 m_a (a),
79 m_b (b),
80 m_step (step)
81 {
82 check_bounds();
83 }
59 84
60 METHOD begin() const -> Iterator; 85 Range() :
61 METHOD end() const -> Iterator; 86 m_a (T()),
62 METHOD min() const -> T; 87 m_b (T()) {}
63 METHOD max() const -> T; 88
64 METHOD check_bounds() -> void; 89 Iterator begin() const
65 METHOD contains (const T& c) const -> bool; 90 {
66 METHOD contains_exclusively (const T& c) const -> bool; 91 Iterator it;
67 METHOD overlaps (Range<T> const& other) const -> bool; 92 it.value = min();
68 METHOD operator== (Range<T> const& other) const -> bool; 93 it.step = m_step;
69 METHOD operator!= (Range<T> const& other) const -> bool; 94 return it;
95 }
96
97 Iterator end() const
98 {
99 Iterator it;
100 it.value = max() + 1;
101 it.step = m_step;
102 return it;
103 }
104
105 T min() const
106 {
107 return m_a;
108 }
109
110 T max() const
111 {
112 return m_b;
113 }
114
115 void check_bounds()
116 {
117 if (m_b < m_a)
118 std::swap (m_a, m_b);
119 }
120
121 bool contains (T c) const
122 {
123 return c >= m_a
124 and c <= m_b;
125 }
126
127 bool contains_exclusively (T c) const
128 {
129 return c > m_a
130 and c < m_b;
131 }
132
133 bool overlaps (Range<T> const& other) const
134 {
135 return contains (other.m_a)
136 or contains (other.m_b);
137 }
138
139 bool operator== (Range<T> const& other) const
140 {
141 return m_a == other.m_a
142 and m_b == other.m_b;
143 }
144
145 bool operator!= (Range<T> const& other) const
146 {
147 return not operator== (other);
148 }
70 }; 149 };
71
72 // -------------------------------------------------------------------------------------------------
73 //
74 template<typename T>
75 Range<T>::Range (const T& a, const T& b, const T& step) :
76 m_a (a),
77 m_b (b),
78 m_step (step)
79 {
80 check_bounds();
81 }
82
83 // -------------------------------------------------------------------------------------------------
84 //
85 template<typename T>
86 Range<T>::Range() :
87 m_a (T()),
88 m_b (T()) {}
89
90 // -------------------------------------------------------------------------------------------------
91 //
92 template<typename T> inline METHOD
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 //
125 template<typename T> METHOD
126 Range<T>::check_bounds() -> void
127 {
128 if (m_b < m_a)
129 std::swap (m_a, m_b);
130 }
131
132 // -------------------------------------------------------------------------------------------------
133 //
134 template<typename T> METHOD
135 Range<T>::contains (const T& c) const -> bool
136 {
137 return (c >= m_a) and (c <= m_b);
138 }
139
140 // -------------------------------------------------------------------------------------------------
141 //
142 template<typename T> METHOD
143 Range<T>::contains_exclusively (const T& c) const -> bool
144 {
145 return (c > m_a) and (c < m_b);
146 }
147
148 // -------------------------------------------------------------------------------------------------
149 //
150 template<typename T> METHOD
151 Range<T>::overlaps (Range<T> const& other) const -> bool
152 {
153 return contains (other.m_a) or contains (other.m_b);
154 }
155
156 // -------------------------------------------------------------------------------------------------
157 //
158 template<typename T> METHOD
159 Range<T>::operator== (Range<T> const& other) const -> bool
160 {
161 return m_a == other.m_a and m_b == other.m_b;
162 }
163
164 // -------------------------------------------------------------------------------------------------
165 //
166 template<typename T> METHOD
167 Range<T>::operator!= (Range<T> const& other) const -> bool
168 {
169 return not operator== (other);
170 }
171
172 // -------------------------------------------------------------------------------------------------
173 //
174 template<typename T> METHOD
175 Range<T>::min() const -> T
176 {
177 return m_a;
178 }
179
180 // -------------------------------------------------------------------------------------------------
181 //
182 template<typename T> METHOD
183 Range<T>::max() const -> T
184 {
185 return m_b;
186 }
187
188 // -------------------------------------------------------------------------------------------------
189 //
190 template<typename T> METHOD
191 Range<T>::begin() const -> Iterator
192 {
193 Iterator it;
194 it.value = min();
195 it.step = m_step;
196 return it;
197 }
198
199 // -------------------------------------------------------------------------------------------------
200 //
201 template<typename T> METHOD
202 Range<T>::end() const -> Iterator
203 {
204 Iterator it;
205 it.value = max() + 1;
206 it.step = m_step;
207 return it;
208 }

mercurial