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