libpnicore
index_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: Jan 8, 2013
22 // Author: Eugen Wintersberger <eugen.wintersberger@desy.de>
23 //
24 #pragma once
25 
26 #include <algorithm>
27 #include <vector>
28 #include "index_map/index_maps.hpp"
29 
30 namespace pni{
31 namespace core{
32 
51  template<
52  typename INDEXT,
53  typename IMT
54  >
56  {
57  public:
58  //===================public types==================================
60  typedef IMT map_type;
62  typedef INDEXT index_type;
64  typedef index_type value_type;
66  typedef const value_type* pointer;
68  typedef const value_type& reference;
70  typedef const value_type* cptr_type;
72  typedef ssize_t difference_type;
75  private:
77  map_type _index_map;
79  ssize_t _state;
81  index_type _index;
82 
83  public:
84  //===================constructors and destructor===================
87 
88  //-----------------------------------------------------------------
98  template<typename CTYPE>
99  index_iterator(const CTYPE &shape,size_t state=0):
100  _index_map(map_utils<map_type>::create(shape)),
101  _state(state),
102  _index(shape.size())
103  {}
104 
105  //====================factory to create an index iterator==========
117  template<typename CTYPE>
118  static iterator_type begin(const CTYPE &shape)
119  {
120  return index_iterator(shape);
121  }
122 
123  //-----------------------------------------------------------------
133  template<typename CTYPE>
134  static iterator_type end(const CTYPE &shape)
135  {
136  size_t size=std::accumulate(shape.begin(),shape.end(),size_t(1),
137  std::multiplies<size_t>());;
138 
139  return index_iterator(shape,size);
140  }
141 
142  //===================public member functions=======================
149  value_type operator*()
150  {
151  return _index;
152  }
153 
154  //-----------------------------------------------------------------
156  pointer operator->()
157  {
158  return &_index;
159  }
160 
161  //-----------------------------------------------------------------
163  iterator_type &operator++()
164  {
165  _state++;
166  if(_state < ssize_t(_index_map.max_elements()))
167  _index = _index_map.template index<index_type>(_state);
168  return *this;
169  }
170 
171  //-----------------------------------------------------------------
173  iterator_type operator++(int )
174  {
175  iterator_type temp = *this;
176  ++(*this);
177  return temp;
178  }
179 
180  //-----------------------------------------------------------------
182  iterator_type &operator--()
183  {
184  _state--;
185  if(this->_state>=0)
186  _index = _index_map.template index<index_type>(_state);
187 
188  return *this;
189  }
190 
191  //-----------------------------------------------------------------
193  iterator_type operator--(int )
194  {
195  iterator_type temp = *this;
196  --(*this);
197  return temp;
198  }
199 
200  //-----------------------------------------------------------------
202  bool operator==(const iterator_type &a)
203  {
204  return this->_state==a._state;
205  }
206 
207  //-----------------------------------------------------------------
209  bool operator!=(const iterator_type &a)
210  {
211  if(*this == a) return false;
212  return true;
213  }
214  };
215 
216 
217 //end of namespace
218 }
219 }
index iterator
Definition: index_iterator.hpp:55
const value_type * pointer
pointer type to an index
Definition: index_iterator.hpp:66
index_type value_type
type to store the index
Definition: index_iterator.hpp:64
index_iterator< index_type, map_type > iterator_type
type of the iterator
Definition: index_iterator.hpp:74
iterator_type operator++(int)
postfix increment operator
Definition: index_iterator.hpp:173
const value_type & reference
reference type to an index
Definition: index_iterator.hpp:68
index_type _index
index buffer
Definition: index_iterator.hpp:81
index_iterator(const CTYPE &shape, size_t state=0)
constructor
Definition: index_iterator.hpp:99
utility class for index maps
Definition: index_maps.hpp:100
map_type _index_map
the index map used to compute the indices
Definition: index_iterator.hpp:77
Definition: add_op.hpp:29
ssize_t _state
the actual state of the iterator
Definition: index_iterator.hpp:79
static iterator_type end(const CTYPE &shape)
static creation function
Definition: index_iterator.hpp:134
value_type operator*()
dereferencing operator
Definition: index_iterator.hpp:149
INDEXT index_type
local index type
Definition: index_iterator.hpp:62
bool operator==(const iterator_type &a)
equality operator
Definition: index_iterator.hpp:202
const value_type * cptr_type
container pointer type
Definition: index_iterator.hpp:70
iterator_type operator--(int)
postfix decrement operator
Definition: index_iterator.hpp:193
IMT map_type
local index map type
Definition: index_iterator.hpp:60
ssize_t difference_type
iterator difference type
Definition: index_iterator.hpp:72
bool operator!=(const iterator_type &a)
inequality operator
Definition: index_iterator.hpp:209
iterator_type & operator++()
increment operator
Definition: index_iterator.hpp:163
size_t size(const slice &s)
compute slice size
index_iterator()
default constructor
pointer operator->()
pointer access operator
Definition: index_iterator.hpp:156
iterator_type & operator--()
decrement operator
Definition: index_iterator.hpp:182
static iterator_type begin(const CTYPE &shape)
static creation function
Definition: index_iterator.hpp:118