libpnicore
scalar_iterator.hpp
1 //
2 // (c) Copyright 2013 DESY, Eugen Wintersberger <eugen.wintersberger@desy.de>
3 //
4 // This file is part of libpnicore.
5 //
6 // libpnicore is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // libpnicore is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with libpnicore. If not, see <http://www.gnu.org/licenses/>.
18 //
19 // ============================================================================
20 //
21 // Created on: Dec 2, 2013
22 // Author: Eugen Wintersberger <eugen.wintersberger@desy.de>
23 //
24 #pragma once
25 
26 #include <type_traits>
27 #include "../error/exceptions.hpp"
28 
29 namespace pni{
30 namespace core{
31 
39 
50  template<typename ITERABLE>
58  {
59  private:
61  ITERABLE* _container;
62 
64  ssize_t _state;
65 
67  //removing a possible const-ness
68  typedef typename std::remove_const<ITERABLE>::type container_type;
69  public:
70  //====================public types==================================
74  typedef typename container_type::value_type value_type;
76  typedef typename std::conditional<std::is_const<ITERABLE>::value,
77  const value_type*,value_type *>::type pointer;
79  typedef typename std::conditional<std::is_const<ITERABLE>::value,
80  const value_type&,value_type&>::type reference;
82  typedef ssize_t difference_type;
84  typedef std::random_access_iterator_tag iterator_category;
85  //================constructor and destructor========================
95  scalar_iterator(ITERABLE *container,size_t state=0):
96  _container(container),
97  _state(state)
98  { }
99 
100  //-----------------------------------------------------------------
106  scalar_iterator(const iterator_type &i):
107  _container(i._container),
108  _state(i._state)
109  {}
110 
111  //====================public methods and operators==================
125  explicit operator bool() const { return true; }
126  //------------------------------------------------------------------
138  reference operator*() { return (*_container)[_state]; }
139 
140  //------------------------------------------------------------------
150  pointer operator->() { return &((*_container)[_state]); }
151 
152  //------------------------------------------------------------------
154  iterator_type &operator++()
155  {
156  this->_state++;
157  return *this;
158  }
159 
160  //------------------------------------------------------------------
162  iterator_type operator++(int )
163  {
164  iterator_type temp(*this);
165  ++(*this);
166  return temp;
167  }
168 
169  //------------------------------------------------------------------
171  iterator_type &operator--()
172  {
173  this->_state--;
174  return *this;
175  }
176 
177  //------------------------------------------------------------------
179  iterator_type operator--(int )
180  {
181  iterator_type tmp = *this;
182  --(*this);
183  return tmp;
184  }
185 
186  //------------------------------------------------------------------
188  iterator_type &operator+=(ssize_t i)
189  {
190  this->_state += i;
191  return *this;
192  }
193 
194  //------------------------------------------------------------------
196  iterator_type &operator-=(ssize_t i)
197  {
198  this->_state -= i;
199  return *this;
200  }
201  //------------------------------------------------------------------
203  bool operator==(const iterator_type &a)
204  {
205  //return true only if this is the same iterator instance
206  if(this == &a) return true;
207  return false;
208  }
209 
210  //------------------------------------------------------------------
212  bool operator!=(const iterator_type &a)
213  {
214  if(this==&a) return false;
215  return true;
216  }
217 
218  //===============comparison operators==============================
220  bool operator<(const iterator_type &b)
221  {
222  return this->_state < b._state;
223  }
224 
225  //-----------------------------------------------------------------
227  bool operator<=(const iterator_type &b)
228  {
229  return this->_state <= b._state;
230  }
231 
232  //-----------------------------------------------------------------
234  bool operator>(const iterator_type &b)
235  {
236  return this->_state > b._state;
237  }
238 
239  //-----------------------------------------------------------------
241  bool operator>=(const iterator_type &b)
242  {
243  return this->_state >= b._state;
244  }
245 
247  ssize_t state() const { return this->_state; }
248 
249  };
250 
251  //================binary arithmetic operators===============================
259 
265  template<typename ITERABLE>
271  scalar_iterator<ITERABLE>
273  {
274  scalar_iterator<ITERABLE> iter = a;
275  iter += b;
276  return iter;
277  }
278 
279  //--------------------------------------------------------------------------
291  template<typename ITERABLE>
292  scalar_iterator<ITERABLE>
294  {
295  return b+a;
296  }
297 
298  //--------------------------------------------------------------------------
310  template<typename ITERABLE>
311  scalar_iterator<ITERABLE>
313  {
314  scalar_iterator<ITERABLE> iter = a;
315  iter -= b;
316  return iter;
317  }
318 
319  //--------------------------------------------------------------------------
330  template<typename ITERABLE>
332  const scalar_iterator<ITERABLE> &b)
333  {
334  return a.state() - b.state();
335  }
336 
337 //end of namespace
338 }
339 }
std::random_access_iterator_tag iterator_category
type of iterator
Definition: scalar_iterator.hpp:84
std::remove_const< ITERABLE >::type container_type
extract the container type from the template argument (by
Definition: scalar_iterator.hpp:68
scalar_iterator< ITERABLE > iterator_type
type of the iterator
Definition: scalar_iterator.hpp:72
iterator_type operator--(int)
decrement operators
Definition: scalar_iterator.hpp:179
iterator_type & operator+=(ssize_t i)
compound assignment with +=
Definition: scalar_iterator.hpp:188
scalar_iterator< ITERABLE > operator+(ssize_t a, const scalar_iterator< ITERABLE > &b)
add offset to iterator
Definition: scalar_iterator.hpp:293
ssize_t operator-(const scalar_iterator< ITERABLE > &a, const scalar_iterator< ITERABLE > &b)
subtract two iterators
Definition: scalar_iterator.hpp:331
std::conditional< std::is_const< ITERABLE >::value, const value_type &, value_type & >::type reference
reference type the iterator provides
Definition: scalar_iterator.hpp:80
bool operator<=(const iterator_type &b)
lesser than equal operator
Definition: scalar_iterator.hpp:227
bool operator==(const iterator_type &a)
comparsion operator - equality
Definition: scalar_iterator.hpp:203
Definition: add_op.hpp:29
ssize_t difference_type
difference type of the iterator
Definition: scalar_iterator.hpp:82
container_type::value_type value_type
value type of the container
Definition: scalar_iterator.hpp:74
bool operator>=(const iterator_type &b)
greater equal than operator
Definition: scalar_iterator.hpp:241
ITERABLE * _container
pointer to the container object
Definition: scalar_iterator.hpp:61
ssize_t _state
the internal state of the iterator
Definition: scalar_iterator.hpp:64
bool operator<(const iterator_type &b)
lesser than operator
Definition: scalar_iterator.hpp:220
scalar_iterator(const iterator_type &i)
copy constructor
Definition: scalar_iterator.hpp:106
ssize_t state() const
get state of the iterator
Definition: scalar_iterator.hpp:247
pointer operator->()
pointer access operator
Definition: scalar_iterator.hpp:150
bool operator>(const iterator_type &b)
greater than operator
Definition: scalar_iterator.hpp:234
iterator_type & operator-=(ssize_t i)
compound assignment with -=
Definition: scalar_iterator.hpp:196
scalar iterator
Definition: scalar_iterator.hpp:57
iterator_type & operator--()
decrement operators
Definition: scalar_iterator.hpp:171
bool operator!=(const iterator_type &a)
comparison operator - inequality
Definition: scalar_iterator.hpp:212
std::conditional< std::is_const< ITERABLE >::value, const value_type *, value_type * >::type pointer
pointer type the iterator provides
Definition: scalar_iterator.hpp:77
reference operator*()
dereferencing operator
Definition: scalar_iterator.hpp:138
iterator_type operator++(int)
increment iterator position
Definition: scalar_iterator.hpp:162
scalar_iterator(ITERABLE *container, size_t state=0)
standard constructor
Definition: scalar_iterator.hpp:95
iterator_type & operator++()
increment iterator position
Definition: scalar_iterator.hpp:154