libpnicore
container_iterator.hpp
1 //
2 // (c) Copyright 2012 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: May 16, 2012
22 // Author: Eugen Wintersberger <eugen.wintersberger@desy.de>
23 //
24 #pragma once
25 
26 #include "../error/exceptions.hpp"
27 
28 namespace pni{
29 namespace core{
30 
31 
32 
41 
53  template<typename ITERABLE> class container_iterator
60  {
61  private:
63  typedef typename std::remove_const<ITERABLE>::type container_type;
65  typedef ITERABLE *cptr_type;
67  ITERABLE *_container;
68 
70  ssize_t _state;
71 
73  ssize_t _maxsize;
74 
75  public:
76  //====================public types==================================
78  typedef typename container_type::value_type value_type;
80  typedef typename std::conditional<std::is_const<ITERABLE>::value,
81  const value_type*,value_type*>::type pointer;
83  typedef typename std::conditional<std::is_const<ITERABLE>::value,
84  const value_type&,value_type&>::type reference;
86  typedef ssize_t difference_type;
88  typedef std::random_access_iterator_tag iterator_category;
91  //================constructor and destructor========================
93  container_iterator():_container(nullptr),_state(0),_maxsize(0) {}
94 
95  //------------------------------------------------------------------
104  explicit container_iterator(cptr_type container,size_t state=0):
105  _container(container),
106  _state(state),
107  _maxsize(container->size())
108  { }
109 
110  //------------------------------------------------------------------
112  container_iterator(const iterator_type &i):
113  _container(i._container),
114  _state(i._state),
115  _maxsize(i._maxsize)
116  { }
117 
118  //------------------------------------------------------------------
120  container_iterator(iterator_type &&i):
121  _container(i._container),
122  _state(i._state),
123  _maxsize(i._maxsize)
124  {
125  i._container = nullptr;
126  i._state = 0;
127  i._maxsize = 0;
128  }
129 
130  //------------------------------------------------------------------
133 
134  //=================assignment operator==============================
136  iterator_type &operator=(const iterator_type &i)
137  {
138  if(this == &i) return *this;
139  _container = i._container;
140  _state = i._state;
141  _maxsize = i._maxsize;
142  return *this;
143  }
144 
145  //------------------------------------------------------------------
147  iterator_type &operator=(iterator_type &&i)
148  {
149  if(this == &i) return *this;
150  _container = i._container;
151  i._container = nullptr;
152  _state = i._state;
153  i._state = 0;
154  _maxsize = i._maxsize;
155  i._maxsize = 0;
156  return *this;
157  }
158 
159  //====================public methods and operators==================
173  explicit operator bool() const
174  {
175  return !( (!_container)|| (_state >= _maxsize)|| (_state<0));
176  }
177 
178  //------------------------------------------------------------------
189  typename std::conditional<std::is_const<ITERABLE>::value,
190  value_type,reference>::type
192  {
193  if(!(*this))
194  throw iterator_error(EXCEPTION_RECORD,"Iterator invalid!");
195 
196  return (*(_container))[_state];
197  }
198 
199  //------------------------------------------------------------------
209  pointer operator->()
210  {
211  if(!(*this))
212  throw iterator_error(EXCEPTION_RECORD,"Iterator invalid!");
213 
214  return &(*(_container))[_state];
215  }
216 
217  //------------------------------------------------------------------
219  iterator_type &operator++()
220  {
221  _state++;
222  return *this;
223  }
224 
225  //------------------------------------------------------------------
227  iterator_type operator++(int)
228  {
229  iterator_type temp = *this;
230  ++(*this);
231  return temp;
232  }
233 
234  //------------------------------------------------------------------
236  iterator_type &operator--()
237  {
238  _state--;
239  return *this;
240  }
241 
242  //------------------------------------------------------------------
244  iterator_type operator--(int)
245  {
246  iterator_type tmp = *this;
247  --(*this);
248  return tmp;
249  }
250 
251  //------------------------------------------------------------------
253  iterator_type &operator+=(ssize_t i)
254  {
255  _state += i;
256  return *this;
257  }
258 
259  //------------------------------------------------------------------
261  iterator_type &operator-=(ssize_t i)
262  {
263  _state -= i;
264  return *this;
265  }
266  //------------------------------------------------------------------
268  bool operator==(const iterator_type &a)
269  {
270  //check if the iterators point to the same container
271  if(_container != a._container) return false;
272  //check if the iterators point to the same element
273  //within the container
274  if(_state != a._state) return false;
275 
276  return true;
277  }
278 
279  //------------------------------------------------------------------
281  bool operator!=(const iterator_type &a)
282  {
283  if((*this)==a) return false;
284  return true;
285  }
286 
287  //===============comparison operators==============================
289  bool operator<(const iterator_type &b)
290  {
291  return _state < b._state;
292  }
293 
294  //-----------------------------------------------------------------
296  bool operator<=(const iterator_type &b)
297  {
298  return _state <= b._state;
299  }
300 
301  //-----------------------------------------------------------------
303  bool operator>(const iterator_type &b)
304  {
305  return _state > b._state;
306  }
307 
308  //-----------------------------------------------------------------
310  bool operator>=(const iterator_type &b)
311  {
312  return _state >= b._state;
313  }
314 
316  ssize_t state() const { return _state; }
317 
318  };
319 
320  //================binary arithmetic operators===============================
337  template<typename ITER>
338  container_iterator<ITER> operator+(const container_iterator<ITER> &a,
339  ssize_t b)
340  {
341  container_iterator<ITER> iter = a;
342  iter += b;
343  return iter;
344  }
345 
346  //--------------------------------------------------------------------------
358  template<typename ITER>
359  container_iterator<ITER> operator+(ssize_t a,
360  const container_iterator<ITER> &b)
361  {
362  return b+a;
363  }
364 
365  //--------------------------------------------------------------------------
377  template<typename ITER>
378  container_iterator<ITER> operator-(const container_iterator<ITER> &a,
379  ssize_t b)
380  {
381  container_iterator<ITER> iter = a;
382  iter -= b;
383  return iter;
384  }
385 
386  //--------------------------------------------------------------------------
398  template<typename ITER>
399  ssize_t operator-(const container_iterator<ITER> &a,
400  const container_iterator<ITER> &b)
401  {
402  return a.state() - b.state();
403  }
404 
405 //end of namespace
406 }
407 }
iterator_type & operator=(const iterator_type &i)
copy assignment operator
Definition: container_iterator.hpp:136
ITERABLE * _container
pointer to the container object
Definition: container_iterator.hpp:67
#define EXCEPTION_RECORD
macro creating an instance of ExceptionRecord
Definition: exceptions.hpp:48
ITERABLE * cptr_type
pointer type of the container
Definition: container_iterator.hpp:65
container_iterator()
default constructor
Definition: container_iterator.hpp:93
bool operator>(const iterator_type &b)
greater than operator
Definition: container_iterator.hpp:303
bool operator!=(const iterator_type &a)
comparison operator - inequality
Definition: container_iterator.hpp:281
std::remove_const< ITERABLE >::type container_type
type of the container object
Definition: container_iterator.hpp:63
bool operator==(const iterator_type &a)
comparsion operator - equality
Definition: container_iterator.hpp:268
iterator type
Definition: container_iterator.hpp:59
iterator_type & operator--()
decrement operators
Definition: container_iterator.hpp:236
container_iterator(cptr_type container, size_t state=0)
constructor
Definition: container_iterator.hpp:104
container_iterator(iterator_type &&i)
move constructor
Definition: container_iterator.hpp:120
bool operator<=(const iterator_type &b)
lesser than equal operator
Definition: container_iterator.hpp:296
container_type::value_type value_type
value type of the container
Definition: container_iterator.hpp:78
Definition: add_op.hpp:29
ssize_t _state
actual position state of the iterator
Definition: container_iterator.hpp:70
scalar_iterator< ITERABLE > operator+(const scalar_iterator< ITERABLE > &a, ssize_t b)
add scalar to iterator
Definition: scalar_iterator.hpp:272
iterator_type & operator++()
increment iterator position
Definition: container_iterator.hpp:219
pointer operator->()
pointer access operator
Definition: container_iterator.hpp:209
iterator_type & operator=(iterator_type &&i)
move assignment operator
Definition: container_iterator.hpp:147
container_iterator< ITERABLE > iterator_type
iterator type
Definition: container_iterator.hpp:90
~container_iterator()
default constructor
Definition: container_iterator.hpp:132
iterator_type operator--(int)
decrement operators
Definition: container_iterator.hpp:244
iterator error
Definition: exceptions.hpp:692
std::random_access_iterator_tag iterator_category
type of iterator
Definition: container_iterator.hpp:88
bool operator>=(const iterator_type &b)
greater equal than operator
Definition: container_iterator.hpp:310
ssize_t difference_type
difference type of the iterator
Definition: container_iterator.hpp:86
bool operator<(const iterator_type &b)
lesser than operator
Definition: container_iterator.hpp:289
container_iterator(const iterator_type &i)
copy constructor
Definition: container_iterator.hpp:112
iterator_type & operator+=(ssize_t i)
compound assignment with +=
Definition: container_iterator.hpp:253
std::conditional< std::is_const< ITERABLE >::value, const value_type *, value_type * >::type pointer
pointer type the iterator provides
Definition: container_iterator.hpp:81
ssize_t state() const
get state of the iterator
Definition: container_iterator.hpp:316
iterator_type operator++(int)
increment iterator position
Definition: container_iterator.hpp:227
std::conditional< std::is_const< ITERABLE >::value, value_type, reference >::type operator*()
dereferencing operator
Definition: container_iterator.hpp:191
iterator_type & operator-=(ssize_t i)
compound assignment with -=
Definition: container_iterator.hpp:261
size_t size(const slice &s)
compute slice size
ssize_t _maxsize
maximum number of elements in the container
Definition: container_iterator.hpp:73
std::conditional< std::is_const< ITERABLE >::value, const value_type &, value_type & >::type reference
reference type the iterator provides
Definition: container_iterator.hpp:84
scalar_iterator< ITERABLE > operator-(const scalar_iterator< ITERABLE > &a, ssize_t b)
subtract offset from iterator
Definition: scalar_iterator.hpp:312