1 | #ifndef _theplu_yat_utility_range_ |
---|
2 | #define _theplu_yat_utility_range_ |
---|
3 | |
---|
4 | // $Id: Range.h 1334 2008-06-06 17:41:02Z 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 std::type_info 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 | \return true iff underlying lhs.get()<rhs.get() |
---|
103 | */ |
---|
104 | template<typename T1, typename T2> |
---|
105 | bool operator<(const Range<T1>& lhs, const Range<T2>& rhs); |
---|
106 | |
---|
107 | /** |
---|
108 | \brief Based on operators == and < |
---|
109 | */ |
---|
110 | template<typename T1, typename T2> |
---|
111 | bool operator<=(const Range<T1>&, const Range<T2>&); |
---|
112 | |
---|
113 | /** |
---|
114 | \brief Based on operators == and < |
---|
115 | */ |
---|
116 | template<typename T1, typename T2> |
---|
117 | bool operator>(const Range<T1>&, const Range<T2>&); |
---|
118 | |
---|
119 | /** |
---|
120 | \brief Based on operators == and < |
---|
121 | */ |
---|
122 | template<typename T1, typename T2> |
---|
123 | bool operator>=(const Range<T1>&, const Range<T2>&); |
---|
124 | |
---|
125 | |
---|
126 | // implementations |
---|
127 | template<typename T> |
---|
128 | Range<T>::Range(void){} |
---|
129 | |
---|
130 | template<typename T> |
---|
131 | Range<T>::Range(T first, T last) |
---|
132 | : first_(first), last_(last) |
---|
133 | {} |
---|
134 | |
---|
135 | template<typename T> |
---|
136 | T Range<T>::begin(void) const |
---|
137 | { return first_; } |
---|
138 | |
---|
139 | |
---|
140 | template<typename T> |
---|
141 | T Range<T>::end(void) const |
---|
142 | { return last_; } |
---|
143 | |
---|
144 | |
---|
145 | template<typename T> |
---|
146 | Range<T>& Range<T>::operator=(const Range<T>& rhs) |
---|
147 | { |
---|
148 | first_ = rhs.begin(); |
---|
149 | last_ = rhs.end(); |
---|
150 | return *this; |
---|
151 | } |
---|
152 | |
---|
153 | |
---|
154 | template<typename T> |
---|
155 | bool operator==(const Range<T>& lhs, const Range<T>& rhs) |
---|
156 | { |
---|
157 | // we are not using std::equal in order to handle ranges of equal length |
---|
158 | T first1(lhs.begin()); |
---|
159 | T last1(lhs.end()); |
---|
160 | T first2(rhs.begin()); |
---|
161 | T last2(rhs.end()); |
---|
162 | while (first1 != last1 && first2 != last2) { |
---|
163 | if (*first1 != *svnnfirst2) |
---|
164 | return false; |
---|
165 | ++first1; |
---|
166 | ++first2; |
---|
167 | } |
---|
168 | return first1==last1 && first2==last2; |
---|
169 | } |
---|
170 | |
---|
171 | |
---|
172 | template<typename T> |
---|
173 | bool operator!=(const Range<T>& lhs, const Range<T>& rhs) |
---|
174 | { |
---|
175 | return ! (lhs==rhs); |
---|
176 | } |
---|
177 | |
---|
178 | |
---|
179 | template<typename T> |
---|
180 | bool operator<(const Range<T>& lhs, const Range<T>& rhs) |
---|
181 | { |
---|
182 | return std::lexicographical_compare(lhs.begin(), lhs.end(), |
---|
183 | rhs.begin(), rhs.end()); |
---|
184 | } |
---|
185 | |
---|
186 | |
---|
187 | template<typename T> |
---|
188 | bool operator>(const Range<T>& lhs, const Range<T>& rhs) |
---|
189 | { |
---|
190 | return rhs < lhs; |
---|
191 | } |
---|
192 | |
---|
193 | |
---|
194 | template<typename T> |
---|
195 | bool operator<=(const Range<T>& lhs, const Range<T>& rhs) |
---|
196 | { |
---|
197 | return ! (rhs<lhs); |
---|
198 | } |
---|
199 | |
---|
200 | |
---|
201 | template<typename T> |
---|
202 | bool operator>=(const Range<T>& lhs, const Range<T>& rhs) |
---|
203 | { |
---|
204 | return ! (lhs<rhs); |
---|
205 | } |
---|
206 | |
---|
207 | }}} // of namespace utility, yat, and theplu |
---|
208 | |
---|
209 | #endif |
---|