source: trunk/yat/utility/Range.h @ 1340

Last change on this file since 1340 was 1340, checked in by Peter, 13 years ago

fixing so declarations match implementations

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