source: trunk/yat/utility/Range.h

Last change on this file was 3259, checked in by Peter, 7 years ago

deprecated class Range. closes #786

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.3 KB
Line 
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
30namespace theplu {
31namespace yat {
32namespace utility {
33
34  /**
35     @brief A class for storing a shallow copy of a Range
36
37     This class can be used to create a shallow copy of range, [first,
38     end), defined by two iterators of type T. This can be useful, for
39     example, when creating numerous sub-ranges of a larger container.
40
41     \since New in yat 0.5
42
43     \deprecated Provided for backward compatibility with the 0.12 API.
44     Use <a href="\boost_url/range/index.html">Boost Range class</a>
45     instead, which provides a more extensive interface.
46  */
47  template<typename T>
48  class Range
49  {
50  public:
51    /**
52       Iterator type T.
53     */
54    typedef T iterator_type;
55
56    /**
57       @brief Default Constructor
58    */
59    // For STL container usage
60    Range(void) YAT_DEPRECATE_GCC_PRE4_3;
61
62    /**
63       \brief Constructor
64    */
65    Range(T first, T last) YAT_DEPRECATE_GCC_PRE4_3;
66
67    /**
68       \note If T is a mutable iterator, it is possible to use
69       returned iterator to modify underlying range although this
70       function is const.
71
72       \return iterator indicating beginning of Range.
73     */
74    T begin(void) const;
75
76    /**
77       \return iterator after the Range.
78     */
79    T end(void) const;
80
81
82    /**
83       @brief Does not modify underlying data.
84
85       This only changes this object by reassigning two iterators,
86       begin() and end(). If you would like to modify the underlying
87       data, use std::copy instead.
88    */
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
99  /**
100     \brief Equality comparison
101     \return true iff underlying elements are equal
102
103     \since New in yat 0.5
104
105     \relatesalso Range
106   */
107  template<typename T1, typename T2>
108  bool operator==(const Range<T1>&, const Range<T2>&);
109
110  /**
111     \brief Based on operator==
112
113     \since New in yat 0.5
114
115     \relatesalso Range
116   */
117  template<typename T1, typename T2>
118  bool operator!=(const Range<T1>&, const Range<T2>&);
119
120  /**
121     \brief Ordering relation
122
123     Using std::lexicographical_compare
124
125     \return true if \a lhs < \a rhs
126
127     \since New in yat 0.5
128
129     \relatesalso Range
130   */
131  template<typename T1, typename T2>
132  bool operator<(const Range<T1>& lhs, const Range<T2>& rhs);
133
134  /**
135     \return ! (\a rhs < \a lhs )
136
137     \since New in yat 0.5
138
139     \relatesalso Range
140   */
141  template<typename T1, typename T2>
142  bool operator<=(const Range<T1>& lhs, const Range<T2>& rhs);
143
144  /**
145     \return \a rhs < \a lhs
146
147     \since New in yat 0.5
148
149     \relatesalso Range
150   */
151  template<typename T1, typename T2>
152  bool operator>(const Range<T1>&, const Range<T2>&);
153
154  /**
155     \return ! (\a lhs < \a rhs )
156
157     \since New in yat 0.5
158
159     \relatesalso Range
160   */
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>
185  Range<T>& Range<T>::operator=(const Range<T>& rhs)
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
Note: See TracBrowser for help on using the repository browser.