yat
0.8.3pre
|
00001 #ifndef _theplu_yat_utility_range_ 00002 #define _theplu_yat_utility_range_ 00003 00004 // $Id: Range.h 2119 2009-12-12 23:11:43Z peter $ 00005 00006 /* 00007 Copyright (C) 2008 Jari Häkkinen, Peter Johansson 00008 Copyright (C) 2009 Peter Johansson 00009 00010 This file is part of the yat library, http://dev.thep.lu.se/yat 00011 00012 The yat library is free software; you can redistribute it and/or 00013 modify it under the terms of the GNU General Public License as 00014 published by the Free Software Foundation; either version 3 of the 00015 License, or (at your option) any later version. 00016 00017 The yat library is distributed in the hope that it will be useful, 00018 but WITHOUT ANY WARRANTY; without even the implied warranty of 00019 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00020 General Public License for more details. 00021 00022 You should have received a copy of the GNU General Public License 00023 along with yat. If not, see <http://www.gnu.org/licenses/>. 00024 */ 00025 00026 #include <algorithm> 00027 00028 namespace theplu { 00029 namespace yat { 00030 namespace utility { 00031 00041 template<typename T> 00042 class Range 00043 { 00044 public: 00048 typedef T iterator_type; 00049 00053 // For STL container usage 00054 Range(void); 00055 00059 Range(T first, T last); 00060 00068 T begin(void) const; 00069 00073 T end(void) const; 00074 00075 00083 Range& operator=(const Range&); 00084 00085 private: 00086 // Using compiler generated copy constructor 00087 // Range(const Range&); 00088 00089 T first_; 00090 T last_; 00091 }; 00092 00101 template<typename T1, typename T2> 00102 bool operator==(const Range<T1>&, const Range<T2>&); 00103 00111 template<typename T1, typename T2> 00112 bool operator!=(const Range<T1>&, const Range<T2>&); 00113 00125 template<typename T1, typename T2> 00126 bool operator<(const Range<T1>& lhs, const Range<T2>& rhs); 00127 00135 template<typename T1, typename T2> 00136 bool operator<=(const Range<T1>& lhs, const Range<T2>& rhs); 00137 00145 template<typename T1, typename T2> 00146 bool operator>(const Range<T1>&, const Range<T2>&); 00147 00155 template<typename T1, typename T2> 00156 bool operator>=(const Range<T1>&, const Range<T2>&); 00157 00158 00159 // implementations 00160 template<typename T> 00161 Range<T>::Range(void){} 00162 00163 template<typename T> 00164 Range<T>::Range(T first, T last) 00165 : first_(first), last_(last) 00166 {} 00167 00168 template<typename T> 00169 T Range<T>::begin(void) const 00170 { return first_; } 00171 00172 00173 template<typename T> 00174 T Range<T>::end(void) const 00175 { return last_; } 00176 00177 00178 template<typename T> 00179 Range<T>& Range<T>::operator=(const Range<T>& rhs) 00180 { 00181 first_ = rhs.begin(); 00182 last_ = rhs.end(); 00183 return *this; 00184 } 00185 00186 00187 template<typename T1, typename T2> 00188 bool operator==(const Range<T1>& lhs, const Range<T2>& rhs) 00189 { 00190 // we are not using std::equal because we want to handle ranges of 00191 // different length 00192 T1 first1(lhs.begin()); 00193 T1 last1(lhs.end()); 00194 T2 first2(rhs.begin()); 00195 T2 last2(rhs.end()); 00196 while (first1 != last1 && first2 != last2) { 00197 if (*first1 != *first2) 00198 return false; 00199 ++first1; 00200 ++first2; 00201 } 00202 // check that ranges are equally long 00203 return first1==last1 && first2==last2; 00204 } 00205 00206 00207 template<typename T1, typename T2> 00208 bool operator!=(const Range<T1>& lhs, const Range<T2>& rhs) 00209 { 00210 return ! (lhs==rhs); 00211 } 00212 00213 00214 template<typename T1, typename T2> 00215 bool operator<(const Range<T1>& lhs, const Range<T2>& rhs) 00216 { 00217 return std::lexicographical_compare(lhs.begin(), lhs.end(), 00218 rhs.begin(), rhs.end()); 00219 } 00220 00221 00222 template<typename T1, typename T2> 00223 bool operator>(const Range<T1>& lhs, const Range<T2>& rhs) 00224 { 00225 return rhs < lhs; 00226 } 00227 00228 00229 template<typename T1, typename T2> 00230 bool operator<=(const Range<T1>& lhs, const Range<T2>& rhs) 00231 { 00232 return ! (rhs<lhs); 00233 } 00234 00235 00236 template<typename T1, typename T2> 00237 bool operator>=(const Range<T1>& lhs, const Range<T2>& rhs) 00238 { 00239 return ! (lhs<rhs); 00240 } 00241 00242 }}} // of namespace utility, yat, and theplu 00243 00244 #endif