yat  0.13.2pre
Range.h
1 #ifndef _theplu_yat_utility_range_
2 #define _theplu_yat_utility_range_
3 
4 // $Id: Range.h 3259 2014-06-09 14:52:49Z 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 
89  Range& operator=(const Range&);
90 
91  private:
92  // Using compiler generated copy constructor
93  // Range(const Range&);
94 
95  T first_;
96  T last_;
97  } YAT_DEPRECATE;
98 
107  template<typename T1, typename T2>
108  bool operator==(const Range<T1>&, const Range<T2>&);
109 
117  template<typename T1, typename T2>
118  bool operator!=(const Range<T1>&, const Range<T2>&);
119 
131  template<typename T1, typename T2>
132  bool operator<(const Range<T1>& lhs, const Range<T2>& rhs);
133 
141  template<typename T1, typename T2>
142  bool operator<=(const Range<T1>& lhs, const Range<T2>& rhs);
143 
151  template<typename T1, typename T2>
152  bool operator>(const Range<T1>&, const Range<T2>&);
153 
161  template<typename T1, typename T2>
162  bool operator>=(const Range<T1>&, const Range<T2>&);
163 
164 
165  // implementations
166  template<typename T>
167  Range<T>::Range(void){}
168 
169  template<typename T>
170  Range<T>::Range(T first, T last)
171  : first_(first), last_(last)
172  {}
173 
174  template<typename T>
175  T Range<T>::begin(void) const
176  { return first_; }
177 
178 
179  template<typename T>
180  T Range<T>::end(void) const
181  { return last_; }
182 
183 
184  template<typename T>
186  {
187  first_ = rhs.begin();
188  last_ = rhs.end();
189  return *this;
190  }
191 
192 
193  template<typename T1, typename T2>
194  bool operator==(const Range<T1>& lhs, const Range<T2>& rhs)
195  {
196  // we are not using std::equal because we want to handle ranges of
197  // different length
198  T1 first1(lhs.begin());
199  T1 last1(lhs.end());
200  T2 first2(rhs.begin());
201  T2 last2(rhs.end());
202  while (first1 != last1 && first2 != last2) {
203  if (*first1 != *first2)
204  return false;
205  ++first1;
206  ++first2;
207  }
208  // check that ranges are equally long
209  return first1==last1 && first2==last2;
210  }
211 
212 
213  template<typename T1, typename T2>
214  bool operator!=(const Range<T1>& lhs, const Range<T2>& rhs)
215  {
216  return ! (lhs==rhs);
217  }
218 
219 
220  template<typename T1, typename T2>
221  bool operator<(const Range<T1>& lhs, const Range<T2>& rhs)
222  {
223  return std::lexicographical_compare(lhs.begin(), lhs.end(),
224  rhs.begin(), rhs.end());
225  }
226 
227 
228  template<typename T1, typename T2>
229  bool operator>(const Range<T1>& lhs, const Range<T2>& rhs)
230  {
231  return rhs < lhs;
232  }
233 
234 
235  template<typename T1, typename T2>
236  bool operator<=(const Range<T1>& lhs, const Range<T2>& rhs)
237  {
238  return ! (rhs<lhs);
239  }
240 
241 
242  template<typename T1, typename T2>
243  bool operator>=(const Range<T1>& lhs, const Range<T2>& rhs)
244  {
245  return ! (lhs<rhs);
246  }
247 
248 }}} // of namespace utility, yat, and theplu
249 
250 #endif
Range(void) YAT_DEPRECATE_GCC_PRE4_3
Default Constructor.
Definition: Range.h:167
#define YAT_DEPRECATE_GCC_PRE4_3
with GCC 4.3 and newer (or other compilers) define it as empty
Definition: deprecate.h:53
T end(void) const
Definition: Range.h:180
Range & operator=(const Range &)
Does not modify underlying data.
Definition: Range.h:185
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:214
bool operator>(const Range< T1 > &, const Range< T2 > &)
Definition: Range.h:229
bool operator==(const Range< T1 > &, const Range< T2 > &)
Equality comparison.
Definition: Range.h:194
T begin(void) const
Definition: Range.h:175
T iterator_type
Definition: Range.h:54
bool operator>=(const Range< T1 > &, const Range< T2 > &)
Definition: Range.h:243

Generated on Wed Jan 4 2017 02:23:07 for yat by  doxygen 1.8.5