libpnicore
array_factory.hpp
1 //
2 // (c) Copyright 2014 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: Mar 12, 2014
22 // Author: Eugen Wintersberger <eugen.wintersberger@desy.de>
23 //
24 #pragma once
25 
26 #include <boost/mpl/arithmetic.hpp>
27 #include <boost/mpl/times.hpp>
28 #include <boost/mpl/vector.hpp>
29 #include <boost/mpl/size_t.hpp>
30 #include <boost/lexical_cast.hpp>
31 
32 #include "../utilities/container_utils.hpp"
33 
34 namespace pni{
35 namespace core{
36 
37 
38  //-------------------------------------------------------------------------
50  template<typename ATYPE> struct array_factory
51  {
53  typedef ATYPE array_type;
55  typedef typename array_type::value_type value_type;
57  typedef typename array_type::map_type map_type;
59  typedef typename array_type::storage_type storage_type;
60 
61 
62  //---------------------------------------------------------------------
68 
81  template<typename STYPE>
88  static array_type create(const STYPE &s,
89  value_type def_val = value_type())
90  {
91  //create the index map
92  auto map = map_utils<map_type>::create(s);
93  auto storage = container_utils<storage_type>::create(map.max_elements());
94  std::fill(storage.begin(),storage.end(),def_val);
95 
96  return array_type(std::move(map),std::move(storage));
97  }
98 
99  //---------------------------------------------------------------------
107 
120  template<typename STYPE,
130  typename DTYPE
131  >
132  static array_type create(const STYPE &s,const DTYPE &data)
133  {
134  auto map = map_utils<map_type>::create(s);
135  if(map.max_elements() != data.size())
137  "Total number of elements from map ("
138  +boost::lexical_cast<string>(map.max_elements())+
139  ") does not match data size ("
140  +boost::lexical_cast<string>(data.size())+")!");
141 
142  auto storage = container_utils<storage_type>::create(data.size());
143  std::copy(data.begin(),data.end(),storage.begin());
144  return array_type(std::move(map),std::move(storage));
145  }
146 
147  //---------------------------------------------------------------------
154 
163  template<
171  typename IT,
172  typename DT
173  >
174  static array_type create(std::initializer_list<IT> shape,
175  std::initializer_list<DT> data)
176  {
177  auto map = map_utils<map_type>::create(shape);
178  if(map.max_elements() != data.size())
180  "Total number of elements from map ("
181  +boost::lexical_cast<string>(map.max_elements())+
182  "does not match data size ("
183  +boost::lexical_cast<string>(data.size())+")!");
184 
185  auto storage = container_utils<storage_type>::create(data.size());
186  std::copy(data.begin(),data.end(),storage.begin());
187  return array_type(std::move(map),std::move(storage));
188  }
189 
190  };
191 
192 //end of namespace
193 }
194 }
195 
static container_type create(size_t n, value_type default_value=value_type())
create container of given size
Definition: container_utils.hpp:85
ATYPE array_type
shortcut for the array type
Definition: array_factory.hpp:53
Size mismatch error.
Definition: exceptions.hpp:399
#define EXCEPTION_RECORD
macro creating an instance of ExceptionRecord
Definition: exceptions.hpp:48
static array_type create(std::initializer_list< IT > shape, std::initializer_list< DT > data)
construct from initializer list
Definition: array_factory.hpp:174
array_type::map_type map_type
map type of the array
Definition: array_factory.hpp:57
array_type::value_type value_type
value type of the array
Definition: array_factory.hpp:55
static MAPT create(const CTYPE &c)
create map from container
Definition: index_maps.hpp:129
Definition: add_op.hpp:29
static array_type create(const STYPE &s, value_type def_val=value_type())
create array from shape
Definition: array_factory.hpp:88
array factory
Definition: array_factory.hpp:50
array_type::storage_type storage_type
storage type of the array
Definition: array_factory.hpp:59
static array_type create(const STYPE &s, const DTYPE &data)
create array from shape and data
Definition: array_factory.hpp:132