libpnicore
array_view_utils.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: Nov 20, 2013
22 // Author: Eugen Wintersberger <eugen.wintersberger@desy.de>
23 //
24 
25 #pragma once
26 
27 #include <vector>
28 #include <array>
29 
30 #include <functional>
31 #include <boost/mpl/contains.hpp>
32 #include <boost/mpl/vector.hpp>
33 #include <boost/mpl/placeholders.hpp>
34 #include <boost/mpl/count_if.hpp>
35 #include <boost/mpl/size.hpp>
36 
37 namespace pni{
38 namespace core{
39 
40  template<typename ATYPE> class array_view;
41 
42  //==============================Define some useful macros===================
43 #define IDX_ARRAY(IT,i)\
44  std::array<size_t,sizeof...(IT)>{{size_t(i)...}}
45 
54  template<typename T>
56  {
58  static const bool value = std::is_integral<T>::value ||
59  std::is_same<T,slice>::value;
60  };
61 
62  //-------------------------------------------------------------------------
73  template<typename ...ITYPES>
75  {
84  template<typename T>
86  {
89  };
90 
92  using _ = boost::mpl::placeholders::_;
94  typedef typename boost::mpl::vector<ITYPES...> types;
95 
97  typedef boost::mpl::count_if<types,is_index_type_pred<_>> n_index_types;
98 
101  static const bool value = n_index_types::value ==
102  int(boost::mpl::size<types>::value);
103  };
104 
105  //-------------------------------------------------------------------------
116 
122  template<typename ...ITYPES>
127  {
129  typedef typename boost::mpl::contains<
130  typename boost::mpl::vector<ITYPES...>::type,
131  slice
133 
135  static const bool value = type::value;
136  };
137 
138  //-------------------------------------------------------------------------
147 
155  template<typename ...ITYPES>
159  {
161  typedef typename boost::mpl::contains<
162  typename boost::mpl::vector<ITYPES...>::type,slice>::type has_slice;
164  typedef typename boost::mpl::contains<
165  typename boost::mpl::vector<ITYPES...>::type,size_t>::type
167 
169  static const bool value = has_slice::value || has_size_t::value;
170  };
171 
172 
173  //-------------------------------------------------------------------------
181  template<typename CTYPE> struct is_view_cont
182  {
184  static const bool value = false;
185  };
186 
187  //-------------------------------------------------------------------------
196  template<typename T> struct is_view_cont<std::vector<T>>
197  {
199  static const bool value = std::is_same<T,slice>::value;
200  };
201 
202  //------------------------------------------------------------------------
212  template<typename T,size_t N> struct is_view_cont<std::array<T,N>>
213  {
215  static const bool value = std::is_same<T,slice>::value;
216  };
217 
218  //-------------------------------------------------------------------------
234  template<
235  typename ATYPE,
236  bool is_view
237  >
239 
240  //-------------------------------------------------------------------------
251  template<typename ATYPE>
252  struct array_view_trait<ATYPE,true>
253  {
258  };
259 
260  //-------------------------------------------------------------------------
268  template<typename ATYPE>
269  struct array_view_trait<ATYPE,false>
270  {
272  typedef typename ATYPE::value_type& type;
274  typedef typename ATYPE::value_type const_type;
275 
276  };
277 
278 template<typename CTYPE>
279  using enable_element_cont = std::enable_if<!is_index_type<CTYPE>::value &&
281 
282 template<typename CTYPE>
283  using enable_view_cont = std::enable_if<!is_index_type<CTYPE>::value &&
285 
286  //-------------------------------------------------------------------------
295  template<
296  typename RTYPE,
297  bool is_view
298  >
300 
301  //-------------------------------------------------------------------------
312  template<typename ATYPE>
313  struct view_provider<ATYPE,false>
314  {
319 
320  //---------------------------------------------------------------------
336  template<
337  typename CTYPE,
338  typename MAP,
339  typename... ITYPES
340  >
341  static ref_type get_reference(CTYPE &c,MAP &map,ITYPES ...indexes)
342  {
343  typedef std::array<size_t,sizeof...(ITYPES)> array_type;
344 #ifdef DEBUG
345  array_type buffer{{size_t(indexes)...}};
346  check_indexes(buffer,map,EXCEPTION_RECORD);
347 #endif
348 
349  size_t offset = map.offset(array_type{{size_t(indexes)...}});
350  return c[offset];
351  }
352 
353  //---------------------------------------------------------------------
369  template<
370  typename CTYPE,
371  typename MAP,
372  typename ...ITYPES
373  >
374  static type get_value(const CTYPE &c,MAP &map,ITYPES ...indexes)
375  {
376  typedef std::array<size_t,sizeof...(ITYPES)> array_type;
377 #ifdef DEBUG
378  array_type buffer{{size_t(indexes)...}};
379  check_indexes(buffer,map,EXCEPTION_RECORD);
380 #endif
381 
382  size_t offset = map.offset(array_type{{size_t(indexes)...}});
383  return c[offset];
384  }
385 
386  };
387 
388  //-------------------------------------------------------------------------
398  template<typename ATYPE>
399  struct view_provider<ATYPE,true>
400  {
405 
406  //---------------------------------------------------------------------
420  template<
421  typename CTYPE,
422  typename MAP,
423  typename... ITYPES
424  >
425  static ref_type get_reference(CTYPE &c,MAP &,ITYPES ...indexes)
426  {
427  typedef std::array<slice,sizeof...(ITYPES)> array_type;
428 
429  return ref_type(c,array_selection::create(
430  array_type{{slice(indexes)...}}));
431  }
432 
433 
434  //---------------------------------------------------------------------
448  template<
449  typename CTYPE,
450  typename MAP,
451  typename ...ITYPES
452  >
453  static type get_value(const CTYPE &c,MAP &,ITYPES ...indexes)
454  {
455  typedef std::array<slice,sizeof...(ITYPES)> array_type;
456 
457  return type(c,array_selection::create(
458  array_type{{slice(indexes)...}}));
459  }
460 
461  };
462 
463 template<typename ATYPE,
464  typename ...ITYPES
465  >
466  using view_type_trait = array_view_trait<ATYPE,
467  is_view_index<ITYPES...>::value>;
468 
469 template<typename ...ITYPES>
470  using enable_valid_index = std::enable_if<is_index_types<ITYPES...>::value>;
471 //end of namespace
472 }
473 }
array_view_trait< ATYPE, false >::const_type type
const type (value type)
Definition: array_view_utils.hpp:318
static type get_value(const CTYPE &c, MAP &, ITYPES...indexes)
get const view
Definition: array_view_utils.hpp:453
boost::mpl::contains< typename boost::mpl::vector< ITYPES...>::type, slice >::type has_slice
result type of MPL exrepssion
Definition: array_view_utils.hpp:162
#define EXCEPTION_RECORD
macro creating an instance of ExceptionRecord
Definition: exceptions.hpp:48
check if an index type is a valid index type
Definition: array_view_utils.hpp:55
type erasure array types
Definition: array.hpp:74
array_view_trait< ATYPE, true >::type ref_type
reference type
Definition: array_view_utils.hpp:402
ATYPE::value_type const_type
single element value
Definition: array_view_utils.hpp:274
boost::mpl::contains< typename boost::mpl::vector< ITYPES...>::type, size_t >::type has_size_t
result type of MPL expression
Definition: array_view_utils.hpp:166
STL namespace.
is_index_type< T > type
result type
Definition: array_view_utils.hpp:88
check if valid index
Definition: array_view_utils.hpp:158
boost::mpl::contains< typename boost::mpl::vector< ITYPES...>::type, slice >::type type
result type of MPL expression
Definition: array_view_utils.hpp:132
array_view_trait< ATYPE, false >::type ref_type
reference type
Definition: array_view_utils.hpp:316
boost::mpl::vector< ITYPES...> types
list of types passed by the user
Definition: array_view_utils.hpp:94
Definition: array_view_utils.hpp:299
Definition: add_op.hpp:29
check if view index
Definition: array_view_utils.hpp:181
view trait
Definition: array_view_utils.hpp:238
invoke< std::enable_if< C::value >> enable_if
shortcut for std::enable_if
Definition: sfinae_macros.hpp:108
array_view_trait< ATYPE, true >::const_type type
const reference type
Definition: array_view_utils.hpp:404
array_view< const ATYPE > const_type
const view type
Definition: array_view_utils.hpp:257
index slice
Definition: slice.hpp:49
bool check_indexes(const ITYPE &index, const STYPE &shape)
check indexes
Definition: exception_utils.hpp:167
predicate type for count_if
Definition: array_view_utils.hpp:85
boost::mpl::placeholders::_ _
load the _ placeholder from boost::mpl
Definition: array_view_utils.hpp:92
check a set of types for index
Definition: array_view_utils.hpp:74
boost::mpl::count_if< types, is_index_type_pred< _ > > n_index_types
count all index types
Definition: array_view_utils.hpp:97
static ref_type get_reference(CTYPE &c, MAP &map, ITYPES...indexes)
get reference
Definition: array_view_utils.hpp:341
ATYPE::value_type & type
single element reference
Definition: array_view_utils.hpp:272
array_view< ATYPE > type
non-const view type
Definition: array_view_utils.hpp:255
type erasure for POD data
Definition: value.hpp:46
checks for view index
Definition: array_view_utils.hpp:126
static ref_type get_reference(CTYPE &c, MAP &, ITYPES...indexes)
get view object
Definition: array_view_utils.hpp:425
size_t offset(const MAPT &map, const array_selection &s, const CTYPE &index)
compute offset
Definition: array_selection.hpp:510
static type get_value(const CTYPE &c, MAP &map, ITYPES...indexes)
get value
Definition: array_view_utils.hpp:374
static array_selection create(const CTYPE &s)
static creation function
Definition: array_selection.hpp:215
provides a view on a part of an array
Definition: add_op.hpp:32