Lunchbox  1.16.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 {
37 template <class S, class C, class T>
38 class IndexIterator : public std::iterator<std::random_access_iterator_tag, T>
39 {
40 public:
42  IndexIterator(C* container, const size_t i)
43  : container_(container)
44  , i_(i)
45  {
46  }
47 
49  explicit IndexIterator(const S& from)
50  : container_(from.container_)
51  , i_(from.i_)
52  {
53  }
54 
56  template <class U, class V, class W>
58  : container_(from.container_)
59  , i_(from.i_)
60  {
61  }
62 
64  // cppcheck-suppress operatorEq
65  S& operator=(const IndexIterator& rhs)
66  {
67  container_ = rhs.container_;
68  i_ = rhs.i_;
69  return *static_cast<S*>(this);
70  }
71 
73  template <class U, class W>
75  {
76  container_ = rhs.container_;
77  i_ = rhs.i_;
78  return *static_cast<S*>(this);
79  }
80 
83  {
84  ++i_;
85  return *static_cast<S*>(this);
86  }
87 
90  {
91  --i_;
92  return *static_cast<S*>(this);
93  }
94 
96  S operator++(int) { return S(container_, i_++); }
98  S operator--(int) { return S(container_, i_--); }
100  S operator+(const size_t& n) const { return S(container_, i_ + n); }
102  S& operator+=(const size_t& n)
103  {
104  i_ += n;
105  return *static_cast<S*>(this);
106  }
107 
109  S operator-(const size_t& n) const { return S(container_, i_ - n); }
111  S& operator-=(const size_t& n)
112  {
113  i_ -= n;
114  return *static_cast<S*>(this);
115  }
116 
118  ssize_t operator-(const S& n) const { return i_ - n.i_; }
120  bool operator==(const S& rhs) const
121  {
122  return container_ == rhs.container_ && i_ == rhs.i_;
123  }
124 
126  bool operator!=(const S& rhs) const
127  {
128  return container_ != rhs.container_ || i_ != rhs.i_;
129  }
130 
132  bool operator<(const S& rhs) const
133  {
134  return container_ <= rhs.container_ && i_ < rhs.i_;
135  }
136 
138  bool operator>(const S& rhs) const
139  {
140  return container_ >= rhs.container_ && i_ > rhs.i_;
141  }
142 
147  bool operator<=(const S& rhs) const
148  {
149  return container_ <= rhs.container_ && i_ <= rhs.i_;
150  }
151 
156  bool operator>=(const S& rhs) const
157  {
158  return container_ >= rhs.container_ && i_ >= rhs.i_;
159  }
160 
162  size_t getPosition() const { return i_; }
163 protected:
164  C* container_;
165  size_t i_;
166 
167  // template copy ctor
168  template <class, class, class>
169  friend class IndexIterator;
170 
171 private:
172  IndexIterator();
173 };
174 }
175 
176 #endif // LUNCHBOX_INDEXITERATOR_H
IndexIterator(C *container, const size_t i)
Construct an iterator for a given container and position.
Definition: indexIterator.h:42
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.
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:65
bool operator<=(const S &rhs) const
ssize_t operator-(const S &n) const
Compute the distance between this and another iterator.
S operator++(int)
Increment the iterator position.
Definition: indexIterator.h:96
IndexIterator(const S &from)
Copy-construct an iterator.
Definition: indexIterator.h:49
S & operator++()
Increment the iterator position.
Definition: indexIterator.h:82
S & operator+=(const size_t &n)
Increment the iterator position by a given amount.
Abstraction layer and common utilities for multi-threaded programming.
Definition: algorithm.h:29
bool operator<(const S &rhs) const
S & operator-=(const size_t &n)
Decrement the iterator position by a given amount.
S & operator--()
Decrement the iterator position.
Definition: indexIterator.h:89
S operator--(int)
Decrement the iterator position.
Definition: indexIterator.h:98
bool operator!=(const S &rhs) const
S operator+(const size_t &n) const
Increment the iterator position by a given amount.
IndexIterator(const IndexIterator< U, V, W > &from)
Copy-construct an iterator.
Definition: indexIterator.h:57
S & operator=(const IndexIterator< S, U, W > &rhs)
Assign the container and position from another iterator.
Definition: indexIterator.h:74