1 | #ifndef _theplu_yat_utility_range_ |
---|
2 | #define _theplu_yat_utility_range_ |
---|
3 | |
---|
4 | // $Id: Range.h 3178 2014-03-23 02:27:44Z peter $ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2008 Jari Häkkinen, Peter Johansson |
---|
8 | Copyright (C) 2009 Peter Johansson |
---|
9 | |
---|
10 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
11 | |
---|
12 | The yat library is free software; you can redistribute it and/or |
---|
13 | modify it under the terms of the GNU General Public License as |
---|
14 | published by the Free Software Foundation; either version 3 of the |
---|
15 | License, or (at your option) any later version. |
---|
16 | |
---|
17 | The yat library is distributed in the hope that it will be useful, |
---|
18 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
20 | General Public License for more details. |
---|
21 | |
---|
22 | You should have received a copy of the GNU General Public License |
---|
23 | along with yat. If not, see <http://www.gnu.org/licenses/>. |
---|
24 | */ |
---|
25 | |
---|
26 | #include <algorithm> |
---|
27 | |
---|
28 | namespace theplu { |
---|
29 | namespace yat { |
---|
30 | namespace utility { |
---|
31 | |
---|
32 | /** |
---|
33 | @brief A class for storing a shallow copy of a Range |
---|
34 | |
---|
35 | This class can be used to create a shallow copy of range, [first, |
---|
36 | end), defined by two iterators of type T. This can be useful, for |
---|
37 | example, when creating numerous sub-ranges of a larger container. |
---|
38 | |
---|
39 | \since New in yat 0.5 |
---|
40 | */ |
---|
41 | template<typename T> |
---|
42 | class Range |
---|
43 | { |
---|
44 | public: |
---|
45 | /** |
---|
46 | Iterator type T. |
---|
47 | */ |
---|
48 | typedef T iterator_type; |
---|
49 | |
---|
50 | /** |
---|
51 | @brief Default Constructor |
---|
52 | */ |
---|
53 | // For STL container usage |
---|
54 | Range(void); |
---|
55 | |
---|
56 | /** |
---|
57 | \brief Constructor |
---|
58 | */ |
---|
59 | Range(T first, T last); |
---|
60 | |
---|
61 | /** |
---|
62 | \note If T is a mutable iterator, it is possible to use |
---|
63 | returned iterator to modify underlying range although this |
---|
64 | function is const. |
---|
65 | |
---|
66 | \return iterator indicating beginning of Range. |
---|
67 | */ |
---|
68 | T begin(void) const; |
---|
69 | |
---|
70 | /** |
---|
71 | \return iterator after the Range. |
---|
72 | */ |
---|
73 | T end(void) const; |
---|
74 | |
---|
75 | |
---|
76 | /** |
---|
77 | @brief Does not modify underlying data. |
---|
78 | |
---|
79 | This only changes this object by reassigning two iterators, |
---|
80 | begin() and end(). If you would like to modify the underlying |
---|
81 | data, use std::copy instead. |
---|
82 | */ |
---|
83 | Range& operator=(const Range&); |
---|
84 | |
---|
85 | private: |
---|
86 | // Using compiler generated copy constructor |
---|
87 | // Range(const Range&); |
---|
88 | |
---|
89 | T first_; |
---|
90 | T last_; |
---|
91 | }; |
---|
92 | |
---|
93 | /** |
---|
94 | \brief Equality comparison |
---|
95 | \return true iff underlying elements are equal |
---|
96 | |
---|
97 | \since New in yat 0.5 |
---|
98 | |
---|
99 | \relatesalso Range |
---|
100 | */ |
---|
101 | template<typename T1, typename T2> |
---|
102 | bool operator==(const Range<T1>&, const Range<T2>&); |
---|
103 | |
---|
104 | /** |
---|
105 | \brief Based on operator== |
---|
106 | |
---|
107 | \since New in yat 0.5 |
---|
108 | |
---|
109 | \relatesalso Range |
---|
110 | */ |
---|
111 | template<typename T1, typename T2> |
---|
112 | bool operator!=(const Range<T1>&, const Range<T2>&); |
---|
113 | |
---|
114 | /** |
---|
115 | \brief Ordering relation |
---|
116 | |
---|
117 | Using std::lexicographical_compare |
---|
118 | |
---|
119 | \return true if \a lhs < \a rhs |
---|
120 | |
---|
121 | \since New in yat 0.5 |
---|
122 | |
---|
123 | \relatesalso Range |
---|
124 | */ |
---|
125 | template<typename T1, typename T2> |
---|
126 | bool operator<(const Range<T1>& lhs, const Range<T2>& rhs); |
---|
127 | |
---|
128 | /** |
---|
129 | \return ! (\a rhs < \a lhs ) |
---|
130 | |
---|
131 | \since New in yat 0.5 |
---|
132 | |
---|
133 | \relatesalso Range |
---|
134 | */ |
---|
135 | template<typename T1, typename T2> |
---|
136 | bool operator<=(const Range<T1>& lhs, const Range<T2>& rhs); |
---|
137 | |
---|
138 | /** |
---|
139 | \return \a rhs < \a lhs |
---|
140 | |
---|
141 | \since New in yat 0.5 |
---|
142 | |
---|
143 | \relatesalso Range |
---|
144 | */ |
---|
145 | template<typename T1, typename T2> |
---|
146 | bool operator>(const Range<T1>&, const Range<T2>&); |
---|
147 | |
---|
148 | /** |
---|
149 | \return ! (\a lhs < \a rhs ) |
---|
150 | |
---|
151 | \since New in yat 0.5 |
---|
152 | |
---|
153 | \relatesalso Range |
---|
154 | */ |
---|
155 | template<typename T1, typename T2> |
---|
156 | bool operator>=(const Range<T1>&, const Range<T2>&); |
---|
157 | |
---|
158 | |
---|
159 | // implementations |
---|
160 | template<typename T> |
---|
161 | Range<T>::Range(void){} |
---|
162 | |
---|
163 | template<typename T> |
---|
164 | Range<T>::Range(T first, T last) |
---|
165 | : first_(first), last_(last) |
---|
166 | {} |
---|
167 | |
---|
168 | template<typename T> |
---|
169 | T Range<T>::begin(void) const |
---|
170 | { return first_; } |
---|
171 | |
---|
172 | |
---|
173 | template<typename T> |
---|
174 | T Range<T>::end(void) const |
---|
175 | { return last_; } |
---|
176 | |
---|
177 | |
---|
178 | template<typename T> |
---|
179 | Range<T>& Range<T>::operator=(const Range<T>& rhs) |
---|
180 | { |
---|
181 | first_ = rhs.begin(); |
---|
182 | last_ = rhs.end(); |
---|
183 | return *this; |
---|
184 | } |
---|
185 | |
---|
186 | |
---|
187 | template<typename T1, typename T2> |
---|
188 | bool operator==(const Range<T1>& lhs, const Range<T2>& rhs) |
---|
189 | { |
---|
190 | // we are not using std::equal because we want to handle ranges of |
---|
191 | // different length |
---|
192 | T1 first1(lhs.begin()); |
---|
193 | T1 last1(lhs.end()); |
---|
194 | T2 first2(rhs.begin()); |
---|
195 | T2 last2(rhs.end()); |
---|
196 | while (first1 != last1 && first2 != last2) { |
---|
197 | if (*first1 != *first2) |
---|
198 | return false; |
---|
199 | ++first1; |
---|
200 | ++first2; |
---|
201 | } |
---|
202 | // check that ranges are equally long |
---|
203 | return first1==last1 && first2==last2; |
---|
204 | } |
---|
205 | |
---|
206 | |
---|
207 | template<typename T1, typename T2> |
---|
208 | bool operator!=(const Range<T1>& lhs, const Range<T2>& rhs) |
---|
209 | { |
---|
210 | return ! (lhs==rhs); |
---|
211 | } |
---|
212 | |
---|
213 | |
---|
214 | template<typename T1, typename T2> |
---|
215 | bool operator<(const Range<T1>& lhs, const Range<T2>& rhs) |
---|
216 | { |
---|
217 | return std::lexicographical_compare(lhs.begin(), lhs.end(), |
---|
218 | rhs.begin(), rhs.end()); |
---|
219 | } |
---|
220 | |
---|
221 | |
---|
222 | template<typename T1, typename T2> |
---|
223 | bool operator>(const Range<T1>& lhs, const Range<T2>& rhs) |
---|
224 | { |
---|
225 | return rhs < lhs; |
---|
226 | } |
---|
227 | |
---|
228 | |
---|
229 | template<typename T1, typename T2> |
---|
230 | bool operator<=(const Range<T1>& lhs, const Range<T2>& rhs) |
---|
231 | { |
---|
232 | return ! (rhs<lhs); |
---|
233 | } |
---|
234 | |
---|
235 | |
---|
236 | template<typename T1, typename T2> |
---|
237 | bool operator>=(const Range<T1>& lhs, const Range<T2>& rhs) |
---|
238 | { |
---|
239 | return ! (lhs<rhs); |
---|
240 | } |
---|
241 | |
---|
242 | }}} // of namespace utility, yat, and theplu |
---|
243 | |
---|
244 | #endif |
---|