Lunchbox  1.6.0
indexIterator.h
00001 
00002 /* Copyright (c) 2011-2012, EPFL/Blue Brain Project
00003  *                          Stefan Eilemann <stefan.eilemann@epfl.ch>
00004  *
00005  * This file is part of Lunchbox <https://github.com/Eyescale/Lunchbox>
00006  *
00007  * This library is free software; you can redistribute it and/or modify it under
00008  * the terms of the GNU Lesser General Public License version 3.0 as published
00009  * by the Free Software Foundation.
00010  *
00011  * This library is distributed in the hope that it will be useful, but WITHOUT
00012  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00013  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00014  * details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public License
00017  * along with this library; if not, write to the Free Software Foundation, Inc.,
00018  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00019  */
00020 
00021 #ifndef LUNCHBOX_INDEXITERATOR_H
00022 #define LUNCHBOX_INDEXITERATOR_H
00023 
00024 namespace lunchbox
00025 {
00026 
00035 template< class S, class C, class T > class IndexIterator
00036     : public std::iterator< std::random_access_iterator_tag, T >
00037 {
00038 public:
00039     IndexIterator( C* container, const size_t i )
00040             : container_( container ), i_(i) {}
00041     IndexIterator( const S& from )
00042             : container_( from.container_ ), i_( from.i_ ) {}
00043     template< class U, class V, class W >
00044     IndexIterator( const IndexIterator< U, V, W >& from )
00045             : container_( from.container_ ), i_( from.i_ ) {}
00046 
00047     S& operator = ( const IndexIterator& rhs )
00048         {
00049             container_ = rhs.container_;
00050             i_ = rhs.i_;
00051             return *static_cast< S* >( this );
00052         }
00053 
00054     template< class U, class W >
00055     S& operator = ( const IndexIterator< S, U, W >& rhs )
00056         {
00057             container_ = rhs.container_;
00058             i_ = rhs.i_;
00059             return *static_cast< S* >( this );
00060         }
00061 
00062     S& operator ++() { ++i_; return *static_cast< S* >( this ); }
00063     S& operator --() { --i_; return *static_cast< S* >( this ); }
00064     S operator ++ (int) { return S( container_, i_++ ); }
00065     S operator -- (int) { return S( container_, i_-- ); }
00066 
00067     S operator + ( const size_t& n ) const
00068         { return S( container_, i_+n ); }
00069     S& operator += ( const size_t& n )
00070         { i_ += n; return *static_cast< S* >( this ); }
00071 
00072     S operator - ( const size_t& n ) const
00073         { return S( container_, i_-n ); }
00074     S& operator -= ( const size_t& n )
00075         { i_ -= n; return *static_cast< S* >( this ); }
00076 
00077     ssize_t operator- ( const S& n ) const { return i_ - n.i_; }
00078 
00079     bool operator == ( const S& rhs ) const
00080         { return container_ == rhs.container_ && i_ == rhs.i_; }
00081     bool operator != ( const S& rhs ) const
00082         { return container_ != rhs.container_ || i_ != rhs.i_; }
00083     bool operator < ( const S& rhs ) const
00084         { return container_ <= rhs.container_ && i_ < rhs.i_; }
00085     bool operator > ( const S& rhs ) const
00086         { return container_ >= rhs.container_ && i_ > rhs.i_; }
00087     bool operator <= ( const S& rhs ) const
00088         { return container_ <= rhs.container_ && i_ <= rhs.i_; }
00089     bool operator >= ( const S& rhs ) const
00090         { return container_ >= rhs.container_ && i_ >= rhs.i_; }
00091 
00092     size_t getPosition() const { return i_; }
00093 
00094 protected:
00095     C* container_;
00096     size_t i_;
00097 
00098     // template copy ctor
00099     template< class, class, class > friend class IndexIterator;
00100 
00101 private:
00102     IndexIterator();
00103 };
00104 
00105 }
00106 
00107 #endif // LUNCHBOX_INDEXITERATOR_H