libpnicore
static_index_map.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: Oct 25, 2013
22 // Author: Eugen Wintersberger <eugen.wintersberger@desy.de>
23 //
24 
25 #pragma once
26 #include <algorithm>
27 #include <numeric>
28 #include "../../utilities/container_utils.hpp"
29 
30 namespace pni{
31 namespace core{
32 
46 
51  template<
60  typename MAP_IMP,
61  size_t... DIMS
62  >
64  {
65  public:
66  //=================public types====================================
68  typedef std::array<size_t,sizeof...(DIMS)> storage_type;
70  typedef MAP_IMP implementation_type;
74  typedef typename storage_type::const_iterator const_iterator;
75  private:
77  constexpr static storage_type _shape{{DIMS...}};
78  public:
79 
80  //-----------------------------------------------------------------
90  size_t max_elements() const
91  {
92  return std::accumulate(_shape.begin(),_shape.end(),size_t(1),
93  std::multiplies<size_t>());
94  }
95 
96  //-----------------------------------------------------------------
105  constexpr size_t rank() const { return sizeof...(DIMS); }
106 
107  //-----------------------------------------------------------------
115  constexpr size_t size() const { return rank(); }
116 
117 
118  //-----------------------------------------------------------------
124 
138  template<typename CTYPE,
143  typename = typename std::enable_if<
144  std::is_compound<
145  typename std::remove_const<
146  typename std::remove_reference<CTYPE>::type
147  >::type
148  >::value
149  >::type
150  >
151  size_t offset(const CTYPE &index) const
152  {
153  return implementation_type::template offset(_shape,index);
154  }
155 
156  //-----------------------------------------------------------------
165  template<typename CTYPE,
166  typename = typename std::enable_if<
167  std::is_compound<
168  typename std::remove_const<
169  typename std::remove_reference<CTYPE>::type
170  >::type
171  >::value
172  >::type
173  >
174  size_t offset(const array_selection &s,const CTYPE &index)
175  const
176  {
177  return implementation_type::template offset(s,_shape,index);
178  }
179 
180  //-----------------------------------------------------------------
187 
196  template<typename CTYPE> CTYPE index(size_t offset) const
201  {
203  implementation_type::template index(_shape,index,offset);
204  return index;
205  }
206 
207  //-----------------------------------------------------------------
211  const_iterator begin() const { return _shape.begin(); }
212 
213  //-----------------------------------------------------------------
217  const_iterator end() const { return _shape.end(); }
218 
219  };
220 
221 template<typename MAP_IMP,size_t... DIMS>
222  constexpr typename static_index_map<MAP_IMP,DIMS...>::storage_type
223  static_index_map<MAP_IMP,DIMS...>::_shape;
224 //end of namespace
225 }
226 }
storage_type::const_iterator const_iterator
constant iterator over the map
Definition: static_index_map.hpp:74
static container_type create(size_t n, value_type default_value=value_type())
create container of given size
Definition: container_utils.hpp:85
std::array< size_t, sizeof...(DIMS)> storage_type
storage type
Definition: static_index_map.hpp:68
MAP_IMP implementation_type
policy type
Definition: static_index_map.hpp:70
CTYPE index(size_t offset) const
compute index
Definition: static_index_map.hpp:200
selection from a multidimensional array
Definition: array_selection.hpp:72
storage_type index_type
index type
Definition: static_index_map.hpp:72
Definition: add_op.hpp:29
the static general index map template
Definition: static_index_map.hpp:63
invoke< std::enable_if< C::value >> enable_if
shortcut for std::enable_if
Definition: sfinae_macros.hpp:108
size_t offset(const CTYPE &index) const
compute the offset
Definition: static_index_map.hpp:151
size_t offset(const array_selection &s, const CTYPE &index) const
compute offset with selection
Definition: static_index_map.hpp:174
size_t max_elements() const
get number of elements
Definition: static_index_map.hpp:90
type erasure for POD data
Definition: value.hpp:46
constexpr size_t size() const
get number of elements
Definition: static_index_map.hpp:115
static constexpr storage_type _shape
storage for shape information
Definition: static_index_map.hpp:77
const_iterator begin() const
get a const iterator to the first element
Definition: static_index_map.hpp:211
constexpr size_t rank() const
get number of dimensions
Definition: static_index_map.hpp:105
const_iterator end() const
get a const iterator to the last+1 element
Definition: static_index_map.hpp:217