libpnicore
index_maps.hpp
1 
25 #pragma once
26 #include <sstream>
27 #include <boost/lexical_cast.hpp>
28 #include "index_map.hpp"
29 #include "static_index_map.hpp"
30 #include "c_index_map_imp.hpp"
31 #include "../../utilities/container_utils.hpp"
32 
33 namespace pni{
34 namespace core{
35 
36  //-------------------------------------------------------------------------
50  template<size_t... DIMS>
51  using static_cindex_map = static_index_map<c_index_map_imp,DIMS...>;
52 
53  //-------------------------------------------------------------------------
61  typedef index_map<std::vector<size_t>,c_index_map_imp> dynamic_cindex_map;
62 
63  //-------------------------------------------------------------------------
80  template<size_t NDIMS>
81  using fixed_dim_cindex_map = index_map<std::array<size_t,NDIMS>,c_index_map_imp>;
82 
83  //=================define some convienance function========================
84 
100  template<typename MAPT> struct map_utils
101  {
103  typedef MAPT map_type;
105  typedef typename map_type::storage_type storage_type;
106 
129  template<typename CTYPE> static MAPT create(const CTYPE &c)
130  {
131  storage_type storage;
132 
133  //try to create a storage type
134  try
135  {
136  storage = container_utils<storage_type>::create(c.size());
137  }
138  catch(size_mismatch_error &error)
139  {
140  std::stringstream ss;
141  ss<<"The map supports only a fixed number of dimensions ("
142  <<storage.size()<<")! However, the container you are "
143  <<"passing has ("<<c.size()<<") elements!"<<std::endl;
144  throw shape_mismatch_error(EXCEPTION_RECORD,ss.str());
145  }
146  std::copy(c.begin(),c.end(),storage.begin());
147  return MAPT(std::move(storage));
148  }
149 
150  //---------------------------------------------------------------------
159  template<typename IT>
160  static MAPT create(std::initializer_list<IT> shape)
161  {
162  storage_type storage;
163 
164  try
165  {
166  storage = container_utils<storage_type>::create(shape);
167  }
168  catch(size_mismatch_error &error)
169  {
171  "Rank of user shape ("
172  +boost::lexical_cast<string>(shape.size())+
173  ") does not match the map rank ("
174  +boost::lexical_cast<string>(map_type().rank())+")!");
175  }
176  std::copy(shape.begin(),shape.end(),storage.begin());
177  return MAPT(std::move(storage));
178  }
179  };
180 
181  //-------------------------------------------------------------------------
193  template<typename POLTYPE,size_t...DIMS>
194  struct map_utils<static_index_map<POLTYPE,DIMS...> >
195  {
197  typedef static_index_map<POLTYPE,DIMS...> map_type;
198 
209  template<typename CTYPE> static map_type create(const CTYPE &c)
210  {
211  map_type map;
212  if(c.size() != map.rank())
214  "rank does not match");
215 
216  if(!std::equal(map.begin(),map.end(),c.begin()))
218  "Elements counts do not match!");
219 
220  return map;
221  }
222 
223  //---------------------------------------------------------------------
233  template<typename IT>
234  static map_type create(std::initializer_list<IT> shape)
235  {
236  map_type map;
237  if(shape.size() != map.rank())
239  "rank does not match");
240 
241  if(!std::equal(map.begin(),map.end(),shape.begin()))
243  "Elements counts do not match!");
244 
245  return map;
246  }
247 
248  };
249 
250 //end of namespace
251 }
252 }
static container_type create(size_t n, value_type default_value=value_type())
create container of given size
Definition: container_utils.hpp:85
Size mismatch error.
Definition: exceptions.hpp:399
#define EXCEPTION_RECORD
macro creating an instance of ExceptionRecord
Definition: exceptions.hpp:48
Shape mismatch error.
Definition: exceptions.hpp:360
static map_type create(const CTYPE &c)
creat map from container
Definition: index_maps.hpp:209
static map_type create(std::initializer_list< IT > shape)
create index map from initializer list
Definition: index_maps.hpp:234
utility class for index maps
Definition: index_maps.hpp:100
static MAPT create(const CTYPE &c)
create map from container
Definition: index_maps.hpp:129
map_type::storage_type storage_type
storage type for MAPT
Definition: index_maps.hpp:105
Definition: add_op.hpp:29
the static general index map template
Definition: static_index_map.hpp:63
MAPT map_type
index map type
Definition: index_maps.hpp:103
static MAPT create(std::initializer_list< IT > shape)
create map from initializer list
Definition: index_maps.hpp:160
static_index_map< POLTYPE, DIMS...> map_type
index map type
Definition: index_maps.hpp:197
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