Lunchbox  1.15.0
Multi-threaded C++ toolbox library for all application developers creating high-performance multi-threaded programs.
indexIterator.h
1 
2 /* Copyright (c) 2011-2014, Stefan.Eilemann@epfl.ch
3  *
4  * This file is part of Lunchbox <https://github.com/Eyescale/Lunchbox>
5  *
6  * This library is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Lesser General Public License version 3.0 as published
8  * by the Free Software Foundation.
9  *
10  * This library is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifndef LUNCHBOX_INDEXITERATOR_H
21 #define LUNCHBOX_INDEXITERATOR_H
22 
23 namespace lunchbox
24 {
25 
38 template< class S, class C, class T > class IndexIterator
39  : public std::iterator< std::random_access_iterator_tag, T >
40 {
41 public:
43  IndexIterator( C* container, const size_t i )
44  : container_( container ), i_(i) {}
45 
47  explicit IndexIterator( const S& from )
48  : container_( from.container_ ), i_( from.i_ ) {}
49 
51  template< class U, class V, class W >
53  : container_( from.container_ ), i_( from.i_ ) {}
54 
56  // cppcheck-suppress operatorEq
57  S& operator = ( const IndexIterator& rhs )
58  {
59  container_ = rhs.container_;
60  i_ = rhs.i_;
61  return *static_cast< S* >( this );
62  }
63 
65  template< class U, class W >
67  {
68  container_ = rhs.container_;
69  i_ = rhs.i_;
70  return *static_cast< S* >( this );
71  }
72 
74  S& operator ++() { ++i_; return *static_cast< S* >( this ); }
75 
77  S& operator --() { --i_; return *static_cast< S* >( this ); }
78 
80  S operator ++ (int) { return S( container_, i_++ ); }
81 
83  S operator -- (int) { return S( container_, i_-- ); }
84 
86  S operator + ( const size_t& n ) const
87  { return S( container_, i_+n ); }
88 
90  S& operator += ( const size_t& n )
91  { i_ += n; return *static_cast< S* >( this ); }
92 
94  S operator - ( const size_t& n ) const
95  { return S( container_, i_-n ); }
96 
98  S& operator -= ( const size_t& n )
99  { i_ -= n; return *static_cast< S* >( this ); }
100 
102  ssize_t operator- ( const S& n ) const { return i_ - n.i_; }
103 
105  bool operator == ( const S& rhs ) const
106  { return container_ == rhs.container_ && i_ == rhs.i_; }
107 
109  bool operator != ( const S& rhs ) const
110  { return container_ != rhs.container_ || i_ != rhs.i_; }
111 
113  bool operator < ( const S& rhs ) const
114  { return container_ <= rhs.container_ && i_ < rhs.i_; }
115 
117  bool operator > ( const S& rhs ) const
118  { return container_ >= rhs.container_ && i_ > rhs.i_; }
119 
124  bool operator <= ( const S& rhs ) const
125  { return container_ <= rhs.container_ && i_ <= rhs.i_; }
126 
131  bool operator >= ( const S& rhs ) const
132  { return container_ >= rhs.container_ && i_ >= rhs.i_; }
133 
135  size_t getPosition() const { return i_; }
136 
137 protected:
138  C* container_;
139  size_t i_;
140 
141  // template copy ctor
142  template< class, class, class > friend class IndexIterator;
143 
144 private:
145  IndexIterator();
146 };
147 
148 }
149 
150 #endif // LUNCHBOX_INDEXITERATOR_H
IndexIterator(C *container, const size_t i)
Construct an iterator for a given container and position.
Definition: indexIterator.h:43
Counter-based, as opposed to pointer-based, iterator for any container.
Definition: indexIterator.h:38
bool operator==(const S &rhs) const
S operator-(const size_t &n) const
Decrement the iterator position by a given amount.
Definition: indexIterator.h:94
bool operator>(const S &rhs) const
bool operator>=(const S &rhs) const
size_t getPosition() const
S & operator=(const IndexIterator &rhs)
Assign the container and position from another iterator.
Definition: indexIterator.h:57
bool operator<=(const S &rhs) const
IndexIterator(const S &from)
Copy-construct an iterator.
Definition: indexIterator.h:47
S & operator++()
Increment the iterator position.
Definition: indexIterator.h:74
S & operator+=(const size_t &n)
Increment the iterator position by a given amount.
Definition: indexIterator.h:90
Abstraction layer and common utilities for multi-threaded programming.
Definition: algorithm.h:32
bool operator<(const S &rhs) const
S & operator-=(const size_t &n)
Decrement the iterator position by a given amount.
Definition: indexIterator.h:98
S & operator--()
Decrement the iterator position.
Definition: indexIterator.h:77
bool operator!=(const S &rhs) const
S operator+(const size_t &n) const
Increment the iterator position by a given amount.
Definition: indexIterator.h:86
IndexIterator(const IndexIterator< U, V, W > &from)
Copy-construct an iterator.
Definition: indexIterator.h:52