yat  0.21pre
Range.h
1 #ifndef _theplu_yat_utility_range_
2 #define _theplu_yat_utility_range_
3 
4 // $Id: Range.h 4245 2022-09-21 05:41:41Z peter $
5 
6 /*
7  Copyright (C) 2008 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2009, 2014 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 "deprecate.h"
27 
28 #include <algorithm>
29 
30 namespace theplu {
31 namespace yat {
32 namespace utility {
33 
47  template<typename T>
48  class Range
49  {
50  public:
54  typedef T iterator_type;
55 
59  // For STL container usage
61 
65  Range(T first, T last) YAT_DEPRECATE_GCC_PRE4_3;
66 
74  T begin(void) const;
75 
79  T end(void) const;
80 
81 
82  private:
83  // Using compiler generated copy constructor
84  // Range(const Range&);
85 
86  T first_;
87  T last_;
88  } YAT_DEPRECATE;
89 
98  template<typename T1, typename T2>
99  bool operator==(const Range<T1>&, const Range<T2>&);
100 
108  template<typename T1, typename T2>
109  bool operator!=(const Range<T1>&, const Range<T2>&);
110 
122  template<typename T1, typename T2>
123  bool operator<(const Range<T1>& lhs, const Range<T2>& rhs);
124 
132  template<typename T1, typename T2>
133  bool operator<=(const Range<T1>& lhs, const Range<T2>& rhs);
134 
142  template<typename T1, typename T2>
143  bool operator>(const Range<T1>&, const Range<T2>&);
144 
152  template<typename T1, typename T2>
153  bool operator>=(const Range<T1>&, const Range<T2>&);
154 
155 
156  // implementations
157  template<typename T>
159 
160  template<typename T>
161  Range<T>::Range(T first, T last)
162  : first_(first), last_(last)
163  {}
164 
165  template<typename T>
166  T Range<T>::begin(void) const
167  { return first_; }
168 
169 
170  template<typename T>
171  T Range<T>::end(void) const
172  { return last_; }
173 
174 
175  template<typename T1, typename T2>
176  bool operator==(const Range<T1>& lhs, const Range<T2>& rhs)
177  {
178  // we are not using std::equal because we want to handle ranges of
179  // different length
180  T1 first1(lhs.begin());
181  T1 last1(lhs.end());
182  T2 first2(rhs.begin());
183  T2 last2(rhs.end());
184  while (first1 != last1 && first2 != last2) {
185  if (*first1 != *first2)
186  return false;
187  ++first1;
188  ++first2;
189  }
190  // check that ranges are equally long
191  return first1==last1 && first2==last2;
192  }
193 
194 
195  template<typename T1, typename T2>
196  bool operator!=(const Range<T1>& lhs, const Range<T2>& rhs)
197  {
198  return ! (lhs==rhs);
199  }
200 
201 
202  template<typename T1, typename T2>
203  bool operator<(const Range<T1>& lhs, const Range<T2>& rhs)
204  {
205  return std::lexicographical_compare(lhs.begin(), lhs.end(),
206  rhs.begin(), rhs.end());
207  }
208 
209 
210  template<typename T1, typename T2>
211  bool operator>(const Range<T1>& lhs, const Range<T2>& rhs)
212  {
213  return rhs < lhs;
214  }
215 
216 
217  template<typename T1, typename T2>
218  bool operator<=(const Range<T1>& lhs, const Range<T2>& rhs)
219  {
220  return ! (rhs<lhs);
221  }
222 
223 
224  template<typename T1, typename T2>
225  bool operator>=(const Range<T1>& lhs, const Range<T2>& rhs)
226  {
227  return ! (lhs<rhs);
228  }
229 
230 }}} // of namespace utility, yat, and theplu
231 
232 #endif
The Department of Theoretical Physics namespace as we define it.
Range(void) YAT_DEPRECATE_GCC_PRE4_3
Default Constructor.
Definition: Range.h:158
#define YAT_DEPRECATE_GCC_PRE4_3
with GCC 4.3 and newer (or other compilers) define it as empty
Definition: deprecate.h:53
A class for storing a shallow copy of a Range.
Definition: Range.h:48
bool operator!=(const Range< T1 > &, const Range< T2 > &)
Based on operator==.
Definition: Range.h:196
bool operator>(const Range< T1 > &, const Range< T2 > &)
Definition: Range.h:211
T begin(void) const
Definition: Range.h:166
bool operator==(const Range< T1 > &, const Range< T2 > &)
Equality comparison.
Definition: Range.h:176
T end(void) const
Definition: Range.h:171
T iterator_type
Definition: Range.h:54
bool operator>=(const Range< T1 > &, const Range< T2 > &)
Definition: Range.h:225

Generated on Wed Jan 25 2023 03:34:29 for yat by  doxygen 1.8.14