Lunchbox  1.8.0
indexIterator.h
1 
2 /* Copyright (c) 2011-2012, EPFL/Blue Brain Project
3  * Stefan Eilemann <stefan.eilemann@epfl.ch>
4  *
5  * This file is part of Lunchbox <https://github.com/Eyescale/Lunchbox>
6  *
7  * This library is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Lesser General Public License version 3.0 as published
9  * by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20 
21 #ifndef LUNCHBOX_INDEXITERATOR_H
22 #define LUNCHBOX_INDEXITERATOR_H
23 
24 namespace lunchbox
25 {
26 
39 template< class S, class C, class T > class IndexIterator
40  : public std::iterator< std::random_access_iterator_tag, T >
41 {
42 public:
43  IndexIterator( C* container, const size_t i )
44  : container_( container ), i_(i) {}
45  IndexIterator( const S& from )
46  : container_( from.container_ ), i_( from.i_ ) {}
47  template< class U, class V, class W >
49  : container_( from.container_ ), i_( from.i_ ) {}
50 
51  S& operator = ( const IndexIterator& rhs )
52  {
53  container_ = rhs.container_;
54  i_ = rhs.i_;
55  return *static_cast< S* >( this );
56  }
57 
58  template< class U, class W >
59  S& operator = ( const IndexIterator< S, U, W >& rhs )
60  {
61  container_ = rhs.container_;
62  i_ = rhs.i_;
63  return *static_cast< S* >( this );
64  }
65 
66  S& operator ++() { ++i_; return *static_cast< S* >( this ); }
67  S& operator --() { --i_; return *static_cast< S* >( this ); }
68  S operator ++ (int) { return S( container_, i_++ ); }
69  S operator -- (int) { return S( container_, i_-- ); }
70 
71  S operator + ( const size_t& n ) const
72  { return S( container_, i_+n ); }
73  S& operator += ( const size_t& n )
74  { i_ += n; return *static_cast< S* >( this ); }
75 
76  S operator - ( const size_t& n ) const
77  { return S( container_, i_-n ); }
78  S& operator -= ( const size_t& n )
79  { i_ -= n; return *static_cast< S* >( this ); }
80 
81  ssize_t operator- ( const S& n ) const { return i_ - n.i_; }
82 
83  bool operator == ( const S& rhs ) const
84  { return container_ == rhs.container_ && i_ == rhs.i_; }
85  bool operator != ( const S& rhs ) const
86  { return container_ != rhs.container_ || i_ != rhs.i_; }
87  bool operator < ( const S& rhs ) const
88  { return container_ <= rhs.container_ && i_ < rhs.i_; }
89  bool operator > ( const S& rhs ) const
90  { return container_ >= rhs.container_ && i_ > rhs.i_; }
91  bool operator <= ( const S& rhs ) const
92  { return container_ <= rhs.container_ && i_ <= rhs.i_; }
93  bool operator >= ( const S& rhs ) const
94  { return container_ >= rhs.container_ && i_ >= rhs.i_; }
95 
96  size_t getPosition() const { return i_; }
97 
98 protected:
99  C* container_;
100  size_t i_;
101 
102  // template copy ctor
103  template< class, class, class > friend class IndexIterator;
104 
105 private:
106  IndexIterator();
107 };
108 
109 }
110 
111 #endif // LUNCHBOX_INDEXITERATOR_H