1 | #ifndef _theplu_yat_utility_range_ |
---|
2 | #define _theplu_yat_utility_range_ |
---|
3 | |
---|
4 | // $Id: Range.h 1337 2008-06-06 19:00:46Z peter $ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2008 Peter Johansson |
---|
8 | |
---|
9 | This file is part of the yat library, http://trac.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 | template<typename T> |
---|
41 | class Range |
---|
42 | { |
---|
43 | public: |
---|
44 | /** |
---|
45 | @brief Default Constructor |
---|
46 | */ |
---|
47 | // For STL container usage |
---|
48 | Range(void); |
---|
49 | |
---|
50 | /** |
---|
51 | @brief Constructor |
---|
52 | */ |
---|
53 | Range(T first, T last); |
---|
54 | |
---|
55 | /** |
---|
56 | \note If T is a mutable iterator, it is possible to use |
---|
57 | returned iterator to modify underlying range although this |
---|
58 | function is const. |
---|
59 | |
---|
60 | \return iterator indicating beginning of Range. |
---|
61 | */ |
---|
62 | T begin(void) const; |
---|
63 | |
---|
64 | /** |
---|
65 | \return iterator after the Range. |
---|
66 | */ |
---|
67 | T end(void) const; |
---|
68 | |
---|
69 | |
---|
70 | /** |
---|
71 | @brief Does not modify underlying data. |
---|
72 | |
---|
73 | This only changes this object by reassigning two iterators, |
---|
74 | begin() and end(). If you would like to modify the underlying |
---|
75 | data, use std::copy instead. |
---|
76 | */ |
---|
77 | Range& operator=(const Range&); |
---|
78 | |
---|
79 | private: |
---|
80 | // Using compiler generated copy constructor |
---|
81 | // Range(const Range&); |
---|
82 | |
---|
83 | T first_; |
---|
84 | T last_; |
---|
85 | }; |
---|
86 | |
---|
87 | /** |
---|
88 | \brief Equality comparison |
---|
89 | \return true iff underlying elements are equal |
---|
90 | */ |
---|
91 | template<typename T1, typename T2> |
---|
92 | bool operator==(const Range<T1>&, const Range<T2>&); |
---|
93 | |
---|
94 | /** |
---|
95 | \brief Based on operator== |
---|
96 | */ |
---|
97 | template<typename T1, typename T2> |
---|
98 | bool operator!=(const Range<T1>&, const Range<T2>&); |
---|
99 | |
---|
100 | /** |
---|
101 | \brief Ordering relation |
---|
102 | |
---|
103 | Using std::lexicographical_compare |
---|
104 | |
---|
105 | \return true if \a lhs < \a rhs |
---|
106 | */ |
---|
107 | template<typename T1, typename T2> |
---|
108 | bool operator<(const Range<T1>& lhs, const Range<T2>& rhs); |
---|
109 | |
---|
110 | /** |
---|
111 | \return ! (\a rhs < \a lhs ) |
---|
112 | */ |
---|
113 | template<typename T1, typename T2> |
---|
114 | bool operator<=(const Range<T1>& lhs, const Range<T2>& rhs); |
---|
115 | |
---|
116 | /** |
---|
117 | \return \a rhs < \a lhs |
---|
118 | */ |
---|
119 | template<typename T1, typename T2> |
---|
120 | bool operator>(const Range<T1>&, const Range<T2>&); |
---|
121 | |
---|
122 | /** |
---|
123 | \return ! (\a lhs < \a rhs ) |
---|
124 | */ |
---|
125 | template<typename T1, typename T2> |
---|
126 | bool operator>=(const Range<T1>&, const Range<T2>&); |
---|
127 | |
---|
128 | |
---|
129 | // implementations |
---|
130 | template<typename T> |
---|
131 | Range<T>::Range(void){} |
---|
132 | |
---|
133 | template<typename T> |
---|
134 | Range<T>::Range(T first, T last) |
---|
135 | : first_(first), last_(last) |
---|
136 | {} |
---|
137 | |
---|
138 | template<typename T> |
---|
139 | T Range<T>::begin(void) const |
---|
140 | { return first_; } |
---|
141 | |
---|
142 | |
---|
143 | template<typename T> |
---|
144 | T Range<T>::end(void) const |
---|
145 | { return last_; } |
---|
146 | |
---|
147 | |
---|
148 | template<typename T> |
---|
149 | Range<T>& Range<T>::operator=(const Range<T>& rhs) |
---|
150 | { |
---|
151 | first_ = rhs.begin(); |
---|
152 | last_ = rhs.end(); |
---|
153 | return *this; |
---|
154 | } |
---|
155 | |
---|
156 | |
---|
157 | template<typename T> |
---|
158 | bool operator==(const Range<T>& lhs, const Range<T>& rhs) |
---|
159 | { |
---|
160 | // we are not using std::equal because we want to handle ranges of |
---|
161 | // different length |
---|
162 | T first1(lhs.begin()); |
---|
163 | T last1(lhs.end()); |
---|
164 | T first2(rhs.begin()); |
---|
165 | T last2(rhs.end()); |
---|
166 | while (first1 != last1 && first2 != last2) { |
---|
167 | if (*first1 != *first2) |
---|
168 | return false; |
---|
169 | ++first1; |
---|
170 | ++first2; |
---|
171 | } |
---|
172 | // check that ranges are equally long |
---|
173 | return first1==last1 && first2==last2; |
---|
174 | } |
---|
175 | |
---|
176 | |
---|
177 | template<typename T> |
---|
178 | bool operator!=(const Range<T>& lhs, const Range<T>& rhs) |
---|
179 | { |
---|
180 | return ! (lhs==rhs); |
---|
181 | } |
---|
182 | |
---|
183 | |
---|
184 | template<typename T> |
---|
185 | bool operator<(const Range<T>& lhs, const Range<T>& rhs) |
---|
186 | { |
---|
187 | return std::lexicographical_compare(lhs.begin(), lhs.end(), |
---|
188 | rhs.begin(), rhs.end()); |
---|
189 | } |
---|
190 | |
---|
191 | |
---|
192 | template<typename T> |
---|
193 | bool operator>(const Range<T>& lhs, const Range<T>& rhs) |
---|
194 | { |
---|
195 | return rhs < lhs; |
---|
196 | } |
---|
197 | |
---|
198 | |
---|
199 | template<typename T> |
---|
200 | bool operator<=(const Range<T>& lhs, const Range<T>& rhs) |
---|
201 | { |
---|
202 | return ! (rhs<lhs); |
---|
203 | } |
---|
204 | |
---|
205 | |
---|
206 | template<typename T> |
---|
207 | bool operator>=(const Range<T>& lhs, const Range<T>& rhs) |
---|
208 | { |
---|
209 | return ! (lhs<rhs); |
---|
210 | } |
---|
211 | |
---|
212 | }}} // of namespace utility, yat, and theplu |
---|
213 | |
---|
214 | #endif |
---|