yat  0.10.4pre
Range.h
1 #ifndef _theplu_yat_utility_range_
2 #define _theplu_yat_utility_range_
3 
4 // $Id: Range.h 2119 2009-12-12 23:11:43Z peter $
5 
6 /*
7  Copyright (C) 2008 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2009 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 <algorithm>
27 
28 namespace theplu {
29 namespace yat {
30 namespace utility {
31 
41  template<typename T>
42  class Range
43  {
44  public:
48  typedef T iterator_type;
49 
53  // For STL container usage
54  Range(void);
55 
59  Range(T first, T last);
60 
68  T begin(void) const;
69 
73  T end(void) const;
74 
75 
83  Range& operator=(const Range&);
84 
85  private:
86  // Using compiler generated copy constructor
87  // Range(const Range&);
88 
89  T first_;
90  T last_;
91  };
92 
101  template<typename T1, typename T2>
102  bool operator==(const Range<T1>&, const Range<T2>&);
103 
111  template<typename T1, typename T2>
112  bool operator!=(const Range<T1>&, const Range<T2>&);
113 
125  template<typename T1, typename T2>
126  bool operator<(const Range<T1>& lhs, const Range<T2>& rhs);
127 
135  template<typename T1, typename T2>
136  bool operator<=(const Range<T1>& lhs, const Range<T2>& rhs);
137 
145  template<typename T1, typename T2>
146  bool operator>(const Range<T1>&, const Range<T2>&);
147 
155  template<typename T1, typename T2>
156  bool operator>=(const Range<T1>&, const Range<T2>&);
157 
158 
159  // implementations
160  template<typename T>
162 
163  template<typename T>
164  Range<T>::Range(T first, T last)
165  : first_(first), last_(last)
166  {}
167 
168  template<typename T>
169  T Range<T>::begin(void) const
170  { return first_; }
171 
172 
173  template<typename T>
174  T Range<T>::end(void) const
175  { return last_; }
176 
177 
178  template<typename T>
180  {
181  first_ = rhs.begin();
182  last_ = rhs.end();
183  return *this;
184  }
185 
186 
187  template<typename T1, typename T2>
188  bool operator==(const Range<T1>& lhs, const Range<T2>& rhs)
189  {
190  // we are not using std::equal because we want to handle ranges of
191  // different length
192  T1 first1(lhs.begin());
193  T1 last1(lhs.end());
194  T2 first2(rhs.begin());
195  T2 last2(rhs.end());
196  while (first1 != last1 && first2 != last2) {
197  if (*first1 != *first2)
198  return false;
199  ++first1;
200  ++first2;
201  }
202  // check that ranges are equally long
203  return first1==last1 && first2==last2;
204  }
205 
206 
207  template<typename T1, typename T2>
208  bool operator!=(const Range<T1>& lhs, const Range<T2>& rhs)
209  {
210  return ! (lhs==rhs);
211  }
212 
213 
214  template<typename T1, typename T2>
215  bool operator<(const Range<T1>& lhs, const Range<T2>& rhs)
216  {
217  return std::lexicographical_compare(lhs.begin(), lhs.end(),
218  rhs.begin(), rhs.end());
219  }
220 
221 
222  template<typename T1, typename T2>
223  bool operator>(const Range<T1>& lhs, const Range<T2>& rhs)
224  {
225  return rhs < lhs;
226  }
227 
228 
229  template<typename T1, typename T2>
230  bool operator<=(const Range<T1>& lhs, const Range<T2>& rhs)
231  {
232  return ! (rhs<lhs);
233  }
234 
235 
236  template<typename T1, typename T2>
237  bool operator>=(const Range<T1>& lhs, const Range<T2>& rhs)
238  {
239  return ! (lhs<rhs);
240  }
241 
242 }}} // of namespace utility, yat, and theplu
243 
244 #endif

Generated on Mon Nov 11 2013 09:41:44 for yat by  doxygen 1.8.1