Wiselib
wiselib.testing/util/pstl/iterator.h
Go to the documentation of this file.
00001 /***************************************************************************
00002  ** This file is part of the generic algorithm library Wiselib.           **
00003  ** Copyright (C) 2008,2009 by the Wisebed (www.wisebed.eu) project.      **
00004  **                                                                       **
00005  ** The Wiselib is free software: you can redistribute it and/or modify   **
00006  ** it under the terms of the GNU Lesser General Public License as        **
00007  ** published by the Free Software Foundation, either version 3 of the    **
00008  ** License, or (at your option) any later version.                       **
00009  **                                                                       **
00010  ** The Wiselib is distributed in the hope that it will be useful,        **
00011  ** but WITHOUT ANY WARRANTY; without even the implied warranty of        **
00012  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         **
00013  ** GNU Lesser General Public License for more details.                   **
00014  **                                                                       **
00015  ** You should have received a copy of the GNU Lesser General Public      **
00016  ** License along with the Wiselib.                                       **
00017  ** If not, see <http://www.gnu.org/licenses/>.                           **
00018  ***************************************************************************/
00019 #ifndef __WISELIB_INTERNAL_INTERFACE_STL_ITERATOR_H
00020 #define __WISELIB_INTERNAL_INTERFACE_STL_ITERATOR_H
00021 
00022 #include "util/pstl/iterator_base_types.h"
00023 
00024 namespace wiselib {
00025 
00026 template<typename OsModel_P, typename Iterator_P, typename Container_P>
00027 class normal_iterator {
00028 public:
00029    typedef OsModel_P OsModel;
00030 
00031    typedef Iterator_P iterator_type;
00032    typedef typename iterator_traits<Iterator_P>::iterator_category
00033          iterator_category;
00034    typedef typename iterator_traits<Iterator_P>::value_type value_type;
00035    typedef typename iterator_traits<Iterator_P>::difference_type
00036          difference_type;
00037    typedef typename iterator_traits<Iterator_P>::reference reference;
00038    typedef typename iterator_traits<Iterator_P>::pointer pointer;
00039    // --------------------------------------------------------------------
00042    normal_iterator() :
00043       current_(iterator_type()) {
00044    }
00045    ;
00046    // --------------------------------------------------------------------
00047    explicit normal_iterator(const iterator_type& it) :
00048       current_(it) {
00049    }
00051    // --------------------------------------------------------------------
00054    reference operator*() const {
00055       return *current_;
00056    }
00057    // --------------------------------------------------------------------
00058    pointer operator->() const {
00059       return current_;
00060    }
00061    // --------------------------------------------------------------------
00062    const iterator_type& base() const {
00063       return current_;
00064    }
00066    // --------------------------------------------------------------------
00069    normal_iterator&
00070    operator++() {
00071       ++current_;
00072       return *this;
00073    }
00074    // --------------------------------------------------------------------
00075    normal_iterator operator++(int) {
00076       return normal_iterator(current_++);
00077    }
00078    // --------------------------------------------------------------------
00079    normal_iterator&
00080    operator--() {
00081       --current_;
00082       return *this;
00083    }
00084    // --------------------------------------------------------------------
00085    normal_iterator operator--(int) {
00086       return normal_iterator(current_--);
00087    }
00088    // --------------------------------------------------------------------
00089    // Random access iterator requirements
00090    reference operator[](const difference_type& n) const {
00091       return current_[n];
00092    }
00093    // --------------------------------------------------------------------
00094    normal_iterator&
00095    operator+=(const difference_type& n) {
00096       current_ += n;
00097       return *this;
00098    }
00099    // --------------------------------------------------------------------
00100    normal_iterator operator+(const difference_type& n) const {
00101       return normal_iterator(current_ + n);
00102    }
00103    // --------------------------------------------------------------------
00104    normal_iterator&
00105    operator-=(const difference_type& n) {
00106       current_ -= n;
00107       return *this;
00108    }
00109    // --------------------------------------------------------------------
00110    normal_iterator operator-(const difference_type& n) const {
00111       return normal_iterator(current_ - n);
00112    }
00114 
00115 private:
00116    iterator_type current_;
00117 };
00118 
00121 template<typename _OsModel, typename _Iterator_L, typename _Iterator_R,
00122       typename _Container>
00123 inline bool operator==(
00124       const normal_iterator<_OsModel, _Iterator_L, _Container>& lhs,
00125       const normal_iterator<_OsModel, _Iterator_R, _Container>& rhs) {
00126    return lhs.base() == rhs.base();
00127 }
00128 // --------------------------------------------------------------------
00129 template<typename _OsModel, typename _Iterator, typename _Container>
00130 inline bool operator==(
00131       const normal_iterator<_OsModel, _Iterator, _Container>& lhs,
00132       const normal_iterator<_OsModel, _Iterator, _Container>& rhs) {
00133    return lhs.base() == rhs.base();
00134 }
00135 // --------------------------------------------------------------------
00136 template<typename _OsModel, typename _Iterator_L, typename _Iterator_R,
00137       typename _Container>
00138 inline bool operator!=(
00139       const normal_iterator<_OsModel, _Iterator_L, _Container>& lhs,
00140       const normal_iterator<_OsModel, _Iterator_R, _Container>& rhs) {
00141    return lhs.base() != rhs.base();
00142 }
00143 // --------------------------------------------------------------------
00144 template<typename _OsModel, typename _Iterator, typename _Container>
00145 inline bool operator!=(
00146       const normal_iterator<_OsModel, _Iterator, _Container>& lhs,
00147       const normal_iterator<_OsModel, _Iterator, _Container>& rhs) {
00148    return lhs.base() != rhs.base();
00149 }
00150 // --------------------------------------------------------------------
00151 template<typename _OsModel, typename _Iterator_L, typename _Iterator_R,
00152       typename _Container>
00153 inline typename normal_iterator<_OsModel, _Iterator_L, _Container>::difference_type operator-(
00154       const normal_iterator<_OsModel, _Iterator_L, _Container>& lhs,
00155       const normal_iterator<_OsModel, _Iterator_R, _Container>& rhs) {
00156    return lhs.base() - rhs.base();
00157 }
00158 // --------------------------------------------------------------------
00159 template<typename _OsModel, typename _Iterator, typename _Container>
00160 inline typename normal_iterator<_OsModel, _Iterator, _Container>::difference_type operator-(
00161       const normal_iterator<_OsModel, _Iterator, _Container>& lhs,
00162       const normal_iterator<_OsModel, _Iterator, _Container>& rhs) {
00163    return lhs.base() - rhs.base();
00164 }
00165 // --------------------------------------------------------------------
00166 template<typename _OsModel, typename _Iterator_L, typename _Iterator_R,
00167       typename _Container>
00168 inline typename normal_iterator<_OsModel, _Iterator_L, _Container>::difference_type operator+(
00169       const normal_iterator<_OsModel, _Iterator_L, _Container>& lhs,
00170       const normal_iterator<_OsModel, _Iterator_R, _Container>& rhs) {
00171    return lhs.base() + rhs.base();
00172 }
00173 // --------------------------------------------------------------------
00174 template<typename _OsModel, typename _Iterator, typename _Container>
00175 inline typename normal_iterator<_OsModel, _Iterator, _Container>::difference_type operator+(
00176       const normal_iterator<_OsModel, _Iterator, _Container>& lhs,
00177       const normal_iterator<_OsModel, _Iterator, _Container>& rhs) {
00178    return lhs.base() + rhs.base();
00179 }
00180 // --------------------------------------------------------------------
00181 template<typename _OsModel, typename _Iterator_L, typename _Iterator_R,
00182       typename _Container>
00183 inline bool operator<(
00184       const normal_iterator<_OsModel, _Iterator_L, _Container>& lhs,
00185       const normal_iterator<_OsModel, _Iterator_R, _Container>& rhs) {
00186    return rhs.base() < lhs.base();
00187 }
00188 // --------------------------------------------------------------------
00189 template<typename _OsModel, typename _Iterator, typename _Container>
00190 inline bool operator<(
00191       const normal_iterator<_OsModel, _Iterator, _Container>& lhs,
00192       const normal_iterator<_OsModel, _Iterator, _Container>& rhs) {
00193    return rhs.base() < lhs.base();
00194 }
00195 // --------------------------------------------------------------------
00196 template<typename _OsModel, typename _Iterator_L, typename _Iterator_R,
00197       typename _Container>
00198 inline bool operator>(
00199       const normal_iterator<_OsModel, _Iterator_L, _Container>& lhs,
00200       const normal_iterator<_OsModel, _Iterator_R, _Container>& rhs) {
00201    return rhs < lhs;
00202 }
00203 // --------------------------------------------------------------------
00204 template<typename _OsModel, typename _Iterator, typename _Container>
00205 inline bool operator>(
00206       const normal_iterator<_OsModel, _Iterator, _Container>& lhs,
00207       const normal_iterator<_OsModel, _Iterator, _Container>& rhs) {
00208    return rhs < lhs;
00209 }
00210 // --------------------------------------------------------------------
00211 template<typename _OsModel, typename _Iterator_L, typename _Iterator_R,
00212       typename _Container>
00213 inline bool operator<=(
00214       const normal_iterator<_OsModel, _Iterator_L, _Container>& lhs,
00215       const normal_iterator<_OsModel, _Iterator_R, _Container>& rhs) {
00216    return !(rhs < lhs);
00217 }
00218 // --------------------------------------------------------------------
00219 template<typename _OsModel, typename _Iterator, typename _Container>
00220 inline bool operator<=(
00221       const normal_iterator<_OsModel, _Iterator, _Container>& lhs,
00222       const normal_iterator<_OsModel, _Iterator, _Container>& rhs) {
00223    return !(rhs < lhs);
00224 }
00225 // --------------------------------------------------------------------
00226 template<typename _OsModel, typename _Iterator_L, typename _Iterator_R,
00227       typename _Container>
00228 inline bool operator>=(
00229       const normal_iterator<_OsModel, _Iterator_L, _Container>& lhs,
00230       const normal_iterator<_OsModel, _Iterator_R, _Container>& rhs) {
00231    return !(lhs < rhs);
00232 }
00233 // --------------------------------------------------------------------
00234 template<typename _OsModel, typename _Iterator, typename _Container>
00235 inline bool operator>=(
00236       const normal_iterator<_OsModel, _Iterator, _Container>& lhs,
00237       const normal_iterator<_OsModel, _Iterator, _Container>& rhs) {
00238    return !(lhs < rhs);
00239 }
00241 
00242 /*
00243  * Begin
00244  * Author: Juan Farré UPC
00245  */
00246 template<class InputIterator, class Distance>
00247 void __advance(InputIterator& i, Distance n, input_iterator_tag) {
00248    for (; n > 0; --n)
00249       ++i;
00250 }
00251 
00252 template<class RandomAccessIterator, class Distance>
00253 void __advance(RandomAccessIterator& i, Distance n, random_access_iterator_tag) {
00254    i += n;
00255 }
00256 
00257 template<class InputIterator, class Distance>
00258 void advance(InputIterator& i, Distance n) {
00259    __advance(i, n,
00260          typename iterator_traits<InputIterator>::iterator_category());
00261 }
00262 
00263 template<class InputIterator>
00264 typename iterator_traits<InputIterator>::difference_type distance(
00265       InputIterator first, InputIterator last, input_iterator_tag) {
00266    typename iterator_traits<InputIterator>::difference_type n = 0;
00267    for (; first != last; ++first)
00268       ++n;
00269    return n;
00270 }
00271 
00272 template<class RandomAccessIterator>
00273 typename iterator_traits<RandomAccessIterator>::difference_type distance(
00274       RandomAccessIterator first, RandomAccessIterator last,
00275       random_access_iterator_tag) {
00276    return last - first;
00277 }
00278 
00279 template<class InputIterator>
00280 typename iterator_traits<InputIterator>::difference_type distance(
00281       InputIterator first, InputIterator last) {
00282    __distance(first, last,
00283          typename iterator_traits<InputIterator>::iterator_category());
00284 }
00285 
00286 /*
00287  * End
00288  */
00289 
00290 }
00291 
00292 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines