1334 |
06 Jun 08 |
peter |
1 |
#ifndef _theplu_yat_utility_range_ |
1334 |
06 Jun 08 |
peter |
2 |
#define _theplu_yat_utility_range_ |
1334 |
06 Jun 08 |
peter |
3 |
|
1334 |
06 Jun 08 |
peter |
// $Id$ |
1334 |
06 Jun 08 |
peter |
5 |
|
1334 |
06 Jun 08 |
peter |
6 |
/* |
2119 |
12 Dec 09 |
peter |
Copyright (C) 2008 Jari Häkkinen, Peter Johansson |
3210 |
05 May 14 |
peter |
Copyright (C) 2009, 2014 Peter Johansson |
1334 |
06 Jun 08 |
peter |
9 |
|
1469 |
02 Sep 08 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/yat |
1334 |
06 Jun 08 |
peter |
11 |
|
1334 |
06 Jun 08 |
peter |
The yat library is free software; you can redistribute it and/or |
1334 |
06 Jun 08 |
peter |
modify it under the terms of the GNU General Public License as |
1486 |
09 Sep 08 |
jari |
published by the Free Software Foundation; either version 3 of the |
1334 |
06 Jun 08 |
peter |
License, or (at your option) any later version. |
1334 |
06 Jun 08 |
peter |
16 |
|
1334 |
06 Jun 08 |
peter |
The yat library is distributed in the hope that it will be useful, |
1334 |
06 Jun 08 |
peter |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
1334 |
06 Jun 08 |
peter |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
1334 |
06 Jun 08 |
peter |
General Public License for more details. |
1334 |
06 Jun 08 |
peter |
21 |
|
1334 |
06 Jun 08 |
peter |
You should have received a copy of the GNU General Public License |
1487 |
10 Sep 08 |
jari |
along with yat. If not, see <http://www.gnu.org/licenses/>. |
1334 |
06 Jun 08 |
peter |
24 |
*/ |
1334 |
06 Jun 08 |
peter |
25 |
|
3259 |
09 Jun 14 |
peter |
26 |
#include "deprecate.h" |
3259 |
09 Jun 14 |
peter |
27 |
|
1334 |
06 Jun 08 |
peter |
28 |
#include <algorithm> |
1334 |
06 Jun 08 |
peter |
29 |
|
1334 |
06 Jun 08 |
peter |
30 |
namespace theplu { |
1334 |
06 Jun 08 |
peter |
31 |
namespace yat { |
1334 |
06 Jun 08 |
peter |
32 |
namespace utility { |
1334 |
06 Jun 08 |
peter |
33 |
|
1334 |
06 Jun 08 |
peter |
34 |
/** |
1334 |
06 Jun 08 |
peter |
@brief A class for storing a shallow copy of a Range |
1334 |
06 Jun 08 |
peter |
36 |
|
1334 |
06 Jun 08 |
peter |
This class can be used to create a shallow copy of range, [first, |
1334 |
06 Jun 08 |
peter |
end), defined by two iterators of type T. This can be useful, for |
1334 |
06 Jun 08 |
peter |
example, when creating numerous sub-ranges of a larger container. |
1339 |
06 Jun 08 |
peter |
40 |
|
1339 |
06 Jun 08 |
peter |
\since New in yat 0.5 |
3179 |
23 Mar 14 |
peter |
42 |
|
3259 |
09 Jun 14 |
peter |
\deprecated Provided for backward compatibility with the 0.12 API. |
3259 |
09 Jun 14 |
peter |
Use <a href="\boost_url/range/index.html">Boost Range class</a> |
3259 |
09 Jun 14 |
peter |
instead, which provides a more extensive interface. |
1334 |
06 Jun 08 |
peter |
46 |
*/ |
1334 |
06 Jun 08 |
peter |
47 |
template<typename T> |
1334 |
06 Jun 08 |
peter |
48 |
class Range |
1334 |
06 Jun 08 |
peter |
49 |
{ |
1334 |
06 Jun 08 |
peter |
50 |
public: |
1334 |
06 Jun 08 |
peter |
51 |
/** |
1443 |
27 Aug 08 |
peter |
Iterator type T. |
1443 |
27 Aug 08 |
peter |
53 |
*/ |
1443 |
27 Aug 08 |
peter |
54 |
typedef T iterator_type; |
1443 |
27 Aug 08 |
peter |
55 |
|
1443 |
27 Aug 08 |
peter |
56 |
/** |
3178 |
23 Mar 14 |
peter |
@brief Default Constructor |
1334 |
06 Jun 08 |
peter |
58 |
*/ |
1334 |
06 Jun 08 |
peter |
// For STL container usage |
3259 |
09 Jun 14 |
peter |
60 |
Range(void) YAT_DEPRECATE_GCC_PRE4_3; |
1334 |
06 Jun 08 |
peter |
61 |
|
1334 |
06 Jun 08 |
peter |
62 |
/** |
3178 |
23 Mar 14 |
peter |
\brief Constructor |
1334 |
06 Jun 08 |
peter |
64 |
*/ |
3259 |
09 Jun 14 |
peter |
65 |
Range(T first, T last) YAT_DEPRECATE_GCC_PRE4_3; |
1334 |
06 Jun 08 |
peter |
66 |
|
1334 |
06 Jun 08 |
peter |
67 |
/** |
1334 |
06 Jun 08 |
peter |
\note If T is a mutable iterator, it is possible to use |
1334 |
06 Jun 08 |
peter |
returned iterator to modify underlying range although this |
1334 |
06 Jun 08 |
peter |
function is const. |
1334 |
06 Jun 08 |
peter |
71 |
|
1334 |
06 Jun 08 |
peter |
\return iterator indicating beginning of Range. |
1334 |
06 Jun 08 |
peter |
73 |
*/ |
1334 |
06 Jun 08 |
peter |
74 |
T begin(void) const; |
1334 |
06 Jun 08 |
peter |
75 |
|
1334 |
06 Jun 08 |
peter |
76 |
/** |
1334 |
06 Jun 08 |
peter |
\return iterator after the Range. |
1334 |
06 Jun 08 |
peter |
78 |
*/ |
1334 |
06 Jun 08 |
peter |
79 |
T end(void) const; |
1334 |
06 Jun 08 |
peter |
80 |
|
1334 |
06 Jun 08 |
peter |
81 |
|
1334 |
06 Jun 08 |
peter |
82 |
private: |
1334 |
06 Jun 08 |
peter |
// Using compiler generated copy constructor |
1334 |
06 Jun 08 |
peter |
// Range(const Range&); |
1334 |
06 Jun 08 |
peter |
85 |
|
1334 |
06 Jun 08 |
peter |
86 |
T first_; |
1334 |
06 Jun 08 |
peter |
87 |
T last_; |
3259 |
09 Jun 14 |
peter |
88 |
} YAT_DEPRECATE; |
1334 |
06 Jun 08 |
peter |
89 |
|
1334 |
06 Jun 08 |
peter |
90 |
/** |
1334 |
06 Jun 08 |
peter |
\brief Equality comparison |
1337 |
06 Jun 08 |
peter |
\return true iff underlying elements are equal |
1339 |
06 Jun 08 |
peter |
93 |
|
1339 |
06 Jun 08 |
peter |
\since New in yat 0.5 |
1887 |
31 Mar 09 |
peter |
95 |
|
1887 |
31 Mar 09 |
peter |
\relatesalso Range |
3178 |
23 Mar 14 |
peter |
97 |
*/ |
1334 |
06 Jun 08 |
peter |
98 |
template<typename T1, typename T2> |
1334 |
06 Jun 08 |
peter |
99 |
bool operator==(const Range<T1>&, const Range<T2>&); |
1334 |
06 Jun 08 |
peter |
100 |
|
1334 |
06 Jun 08 |
peter |
101 |
/** |
1334 |
06 Jun 08 |
peter |
\brief Based on operator== |
1339 |
06 Jun 08 |
peter |
103 |
|
1339 |
06 Jun 08 |
peter |
\since New in yat 0.5 |
1887 |
31 Mar 09 |
peter |
105 |
|
1887 |
31 Mar 09 |
peter |
\relatesalso Range |
3178 |
23 Mar 14 |
peter |
107 |
*/ |
1334 |
06 Jun 08 |
peter |
108 |
template<typename T1, typename T2> |
1334 |
06 Jun 08 |
peter |
109 |
bool operator!=(const Range<T1>&, const Range<T2>&); |
1334 |
06 Jun 08 |
peter |
110 |
|
1334 |
06 Jun 08 |
peter |
111 |
/** |
1334 |
06 Jun 08 |
peter |
\brief Ordering relation |
1337 |
06 Jun 08 |
peter |
113 |
|
1337 |
06 Jun 08 |
peter |
Using std::lexicographical_compare |
1337 |
06 Jun 08 |
peter |
115 |
|
1337 |
06 Jun 08 |
peter |
\return true if \a lhs < \a rhs |
1339 |
06 Jun 08 |
peter |
117 |
|
1339 |
06 Jun 08 |
peter |
\since New in yat 0.5 |
1887 |
31 Mar 09 |
peter |
119 |
|
1887 |
31 Mar 09 |
peter |
\relatesalso Range |
3178 |
23 Mar 14 |
peter |
121 |
*/ |
1334 |
06 Jun 08 |
peter |
122 |
template<typename T1, typename T2> |
1334 |
06 Jun 08 |
peter |
123 |
bool operator<(const Range<T1>& lhs, const Range<T2>& rhs); |
1334 |
06 Jun 08 |
peter |
124 |
|
1334 |
06 Jun 08 |
peter |
125 |
/** |
1337 |
06 Jun 08 |
peter |
\return ! (\a rhs < \a lhs ) |
1339 |
06 Jun 08 |
peter |
127 |
|
1339 |
06 Jun 08 |
peter |
\since New in yat 0.5 |
1887 |
31 Mar 09 |
peter |
129 |
|
1887 |
31 Mar 09 |
peter |
\relatesalso Range |
3178 |
23 Mar 14 |
peter |
131 |
*/ |
1334 |
06 Jun 08 |
peter |
132 |
template<typename T1, typename T2> |
1337 |
06 Jun 08 |
peter |
133 |
bool operator<=(const Range<T1>& lhs, const Range<T2>& rhs); |
1334 |
06 Jun 08 |
peter |
134 |
|
1334 |
06 Jun 08 |
peter |
135 |
/** |
1337 |
06 Jun 08 |
peter |
\return \a rhs < \a lhs |
1339 |
06 Jun 08 |
peter |
137 |
|
1339 |
06 Jun 08 |
peter |
\since New in yat 0.5 |
1887 |
31 Mar 09 |
peter |
139 |
|
1887 |
31 Mar 09 |
peter |
\relatesalso Range |
3178 |
23 Mar 14 |
peter |
141 |
*/ |
1334 |
06 Jun 08 |
peter |
142 |
template<typename T1, typename T2> |
1334 |
06 Jun 08 |
peter |
143 |
bool operator>(const Range<T1>&, const Range<T2>&); |
1334 |
06 Jun 08 |
peter |
144 |
|
1334 |
06 Jun 08 |
peter |
145 |
/** |
1337 |
06 Jun 08 |
peter |
\return ! (\a lhs < \a rhs ) |
1339 |
06 Jun 08 |
peter |
147 |
|
1339 |
06 Jun 08 |
peter |
\since New in yat 0.5 |
1887 |
31 Mar 09 |
peter |
149 |
|
1887 |
31 Mar 09 |
peter |
\relatesalso Range |
3178 |
23 Mar 14 |
peter |
151 |
*/ |
1334 |
06 Jun 08 |
peter |
152 |
template<typename T1, typename T2> |
1334 |
06 Jun 08 |
peter |
153 |
bool operator>=(const Range<T1>&, const Range<T2>&); |
1334 |
06 Jun 08 |
peter |
154 |
|
1334 |
06 Jun 08 |
peter |
155 |
|
1334 |
06 Jun 08 |
peter |
// implementations |
1334 |
06 Jun 08 |
peter |
157 |
template<typename T> |
1334 |
06 Jun 08 |
peter |
158 |
Range<T>::Range(void){} |
3178 |
23 Mar 14 |
peter |
159 |
|
1334 |
06 Jun 08 |
peter |
160 |
template<typename T> |
1334 |
06 Jun 08 |
peter |
161 |
Range<T>::Range(T first, T last) |
1334 |
06 Jun 08 |
peter |
162 |
: first_(first), last_(last) |
1334 |
06 Jun 08 |
peter |
163 |
{} |
3178 |
23 Mar 14 |
peter |
164 |
|
1334 |
06 Jun 08 |
peter |
165 |
template<typename T> |
1334 |
06 Jun 08 |
peter |
166 |
T Range<T>::begin(void) const |
1334 |
06 Jun 08 |
peter |
167 |
{ return first_; } |
1334 |
06 Jun 08 |
peter |
168 |
|
3178 |
23 Mar 14 |
peter |
169 |
|
1334 |
06 Jun 08 |
peter |
170 |
template<typename T> |
1334 |
06 Jun 08 |
peter |
171 |
T Range<T>::end(void) const |
1334 |
06 Jun 08 |
peter |
172 |
{ return last_; } |
1334 |
06 Jun 08 |
peter |
173 |
|
3178 |
23 Mar 14 |
peter |
174 |
|
1340 |
12 Jun 08 |
peter |
175 |
template<typename T1, typename T2> |
1340 |
12 Jun 08 |
peter |
176 |
bool operator==(const Range<T1>& lhs, const Range<T2>& rhs) |
1334 |
06 Jun 08 |
peter |
177 |
{ |
1337 |
06 Jun 08 |
peter |
// we are not using std::equal because we want to handle ranges of |
1337 |
06 Jun 08 |
peter |
// different length |
1340 |
12 Jun 08 |
peter |
180 |
T1 first1(lhs.begin()); |
1340 |
12 Jun 08 |
peter |
181 |
T1 last1(lhs.end()); |
1340 |
12 Jun 08 |
peter |
182 |
T2 first2(rhs.begin()); |
1340 |
12 Jun 08 |
peter |
183 |
T2 last2(rhs.end()); |
1334 |
06 Jun 08 |
peter |
184 |
while (first1 != last1 && first2 != last2) { |
1336 |
06 Jun 08 |
peter |
185 |
if (*first1 != *first2) |
1334 |
06 Jun 08 |
peter |
186 |
return false; |
1334 |
06 Jun 08 |
peter |
187 |
++first1; |
1334 |
06 Jun 08 |
peter |
188 |
++first2; |
1334 |
06 Jun 08 |
peter |
189 |
} |
1337 |
06 Jun 08 |
peter |
// check that ranges are equally long |
1334 |
06 Jun 08 |
peter |
191 |
return first1==last1 && first2==last2; |
1334 |
06 Jun 08 |
peter |
192 |
} |
1334 |
06 Jun 08 |
peter |
193 |
|
1334 |
06 Jun 08 |
peter |
194 |
|
1340 |
12 Jun 08 |
peter |
195 |
template<typename T1, typename T2> |
1340 |
12 Jun 08 |
peter |
196 |
bool operator!=(const Range<T1>& lhs, const Range<T2>& rhs) |
3178 |
23 Mar 14 |
peter |
197 |
{ |
3178 |
23 Mar 14 |
peter |
198 |
return ! (lhs==rhs); |
1334 |
06 Jun 08 |
peter |
199 |
} |
1334 |
06 Jun 08 |
peter |
200 |
|
1334 |
06 Jun 08 |
peter |
201 |
|
1340 |
12 Jun 08 |
peter |
202 |
template<typename T1, typename T2> |
1340 |
12 Jun 08 |
peter |
203 |
bool operator<(const Range<T1>& lhs, const Range<T2>& rhs) |
3178 |
23 Mar 14 |
peter |
204 |
{ |
1334 |
06 Jun 08 |
peter |
205 |
return std::lexicographical_compare(lhs.begin(), lhs.end(), |
3178 |
23 Mar 14 |
peter |
206 |
rhs.begin(), rhs.end()); |
1334 |
06 Jun 08 |
peter |
207 |
} |
1334 |
06 Jun 08 |
peter |
208 |
|
1334 |
06 Jun 08 |
peter |
209 |
|
1340 |
12 Jun 08 |
peter |
210 |
template<typename T1, typename T2> |
1340 |
12 Jun 08 |
peter |
211 |
bool operator>(const Range<T1>& lhs, const Range<T2>& rhs) |
3178 |
23 Mar 14 |
peter |
212 |
{ |
3178 |
23 Mar 14 |
peter |
213 |
return rhs < lhs; |
1334 |
06 Jun 08 |
peter |
214 |
} |
1334 |
06 Jun 08 |
peter |
215 |
|
1334 |
06 Jun 08 |
peter |
216 |
|
1340 |
12 Jun 08 |
peter |
217 |
template<typename T1, typename T2> |
1340 |
12 Jun 08 |
peter |
218 |
bool operator<=(const Range<T1>& lhs, const Range<T2>& rhs) |
3178 |
23 Mar 14 |
peter |
219 |
{ |
3178 |
23 Mar 14 |
peter |
220 |
return ! (rhs<lhs); |
1334 |
06 Jun 08 |
peter |
221 |
} |
1334 |
06 Jun 08 |
peter |
222 |
|
1334 |
06 Jun 08 |
peter |
223 |
|
1340 |
12 Jun 08 |
peter |
224 |
template<typename T1, typename T2> |
1340 |
12 Jun 08 |
peter |
225 |
bool operator>=(const Range<T1>& lhs, const Range<T2>& rhs) |
3178 |
23 Mar 14 |
peter |
226 |
{ |
3178 |
23 Mar 14 |
peter |
227 |
return ! (lhs<rhs); |
1334 |
06 Jun 08 |
peter |
228 |
} |
1334 |
06 Jun 08 |
peter |
229 |
|
1334 |
06 Jun 08 |
peter |
230 |
}}} // of namespace utility, yat, and theplu |
1334 |
06 Jun 08 |
peter |
231 |
|
1334 |
06 Jun 08 |
peter |
232 |
#endif |