libpnicore
index_map.hpp
1 
25 #pragma once
26 #include <algorithm>
27 #include <numeric>
28 
29 #include "../../utilities/container_utils.hpp"
30 
31 namespace pni{
32 namespace core{
33 
34  class array_selection;
35 
50 
63  template<
77  typename SHAPE_STORE,
78  typename MAP_IMP
79  >
80  class index_map
81  {
82  public:
83  //=================public types====================================
85  typedef SHAPE_STORE storage_type;
87  typedef typename storage_type::value_type value_type;
89  typedef MAP_IMP implementation_type;
91  typedef typename storage_type::iterator iterator;
93  typedef typename storage_type::const_iterator const_iterator;
96  private:
98  storage_type _shape;
99  public:
100  //-----------------------------------------------------------------
104  index_map():_shape() {}
105 
106  //-----------------------------------------------------------------
112  index_map(const map_type &m):_shape(m._shape) {}
113 
114  //-----------------------------------------------------------------
120  index_map(map_type &&m):_shape(std::move(m._shape))
121  {
122  std::fill(m._shape.begin(),m._shape.end(),0);
123  }
124 
125  //-----------------------------------------------------------------
132 
141  explicit index_map(const storage_type &s):_shape(s) {}
144 
145  //-----------------------------------------------------------------
152 
160  explicit index_map(storage_type &&s):_shape(std::move(s)) {}
163 
164  //-----------------------------------------------------------------
171  map_type &operator=(const map_type &m)
172  {
173  if(this == &m) return *this;
174  _shape = m._shape;
175 
176  return *this;
177  }
178 
179  //-----------------------------------------------------------------
186  map_type &operator=(map_type &&m)
187  {
188  if(this == &m) return *this;
189  _shape = std::move(m._shape);
190  std::fill(m._shape.begin(),m._shape.end(),0);
191 
192  return *this;
193  }
194 
195  //-----------------------------------------------------------------
205  size_t max_elements() const
206  {
207  if(_shape.size() == 0) return 0;
208  else
209  return std::accumulate(_shape.begin(),_shape.end(),1,
210  std::multiplies<size_t>());
211  }
212 
213  //-----------------------------------------------------------------
222  size_t rank() const { return _shape.size(); }
223 
224  //----------------------------------------------------------------
233  size_t size() const { return rank(); }
234 
235  //-----------------------------------------------------------------
241 
247  template<
252  typename CTYPE,
253  typename = typename std::enable_if<
254  std::is_compound<
255  typename std::remove_reference<CTYPE>::type
256  >::value
257  >::type
258  >
259  size_t offset(const CTYPE &index) const
260  {
261  return implementation_type::template offset(_shape,index);
262  }
263 
264  //-----------------------------------------------------------------
270 
280  template<typename CTYPE,
286  typename = typename std::enable_if<
287  std::is_compound<
288  typename std::remove_reference<CTYPE>::type
289  >::value
290  >::type
291  >
292  size_t offset(const array_selection &s,const CTYPE &index)
293  {
294  return implementation_type::template offset(s,_shape,index);
295  }
296 
297  //-----------------------------------------------------------------
303 
310  template<typename CTYPE> CTYPE index(size_t offset) const
315  {
317  implementation_type::template index(_shape,index,offset);
318  return index;
319 
320  }
321 
322  //-----------------------------------------------------------------
326  iterator begin() { return _shape.begin(); }
327 
328  //-----------------------------------------------------------------
332  iterator end() { return _shape.end(); }
333 
334  //-----------------------------------------------------------------
338  const_iterator begin() const { return _shape.begin(); }
339 
340  //-----------------------------------------------------------------
344  const_iterator end() const { return _shape.end(); }
345 
346  };
347 //end of namespace
348 }
349 }
static container_type create(size_t n, value_type default_value=value_type())
create container of given size
Definition: container_utils.hpp:85
storage_type::const_iterator const_iterator
constant iterator over the map
Definition: index_map.hpp:93
index_map(map_type &&m)
move constructor
Definition: index_map.hpp:120
size_t rank() const
get number of dimensions
Definition: index_map.hpp:222
dynamic general index map template
Definition: index_map.hpp:80
STL namespace.
storage_type::iterator iterator
read write iterator
Definition: index_map.hpp:91
selection from a multidimensional array
Definition: array_selection.hpp:72
map_type & operator=(const map_type &m)
copy assignment operator
Definition: index_map.hpp:171
index_map< storage_type, implementation_type > map_type
map type
Definition: index_map.hpp:95
storage_type _shape
storage for shape information
Definition: index_map.hpp:98
Definition: add_op.hpp:29
MAP_IMP implementation_type
policy type
Definition: index_map.hpp:89
invoke< std::enable_if< C::value >> enable_if
shortcut for std::enable_if
Definition: sfinae_macros.hpp:108
CTYPE index(size_t offset) const
compute index
Definition: index_map.hpp:314
map_type & operator=(map_type &&m)
move assignment operator
Definition: index_map.hpp:186
iterator end()
return iterator to last+1 element
Definition: index_map.hpp:332
size_t size() const
get number of dimensions
Definition: index_map.hpp:233
SHAPE_STORE storage_type
storage type
Definition: index_map.hpp:85
size_t max_elements() const
get number of elements
Definition: index_map.hpp:205
storage_type::value_type value_type
index type
Definition: index_map.hpp:87
index_map(const map_type &m)
copy constructor
Definition: index_map.hpp:112
index_map()
default constructor
Definition: index_map.hpp:104
type erasure for POD data
Definition: value.hpp:46
const_iterator end() const
return const iterator to last+1 element
Definition: index_map.hpp:344
const_iterator begin() const
return const iterator to first element
Definition: index_map.hpp:338
size_t offset(const CTYPE &index) const
compute the offset
Definition: index_map.hpp:259
iterator begin()
return iterator to first element
Definition: index_map.hpp:326
size_t offset(const array_selection &s, const CTYPE &index)
compute offset with selection
Definition: index_map.hpp:292