echelon  0.8.0
multi_array_view.hpp
1 // Copyright (c) 2014 Christopher Hinz
2 //
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 
6 #ifndef ECHELON_MULTI_ARRAY_VIEW_HPP
7 #define ECHELON_MULTI_ARRAY_VIEW_HPP
8 
9 #include <echelon/range.hpp>
10 
11 #include <echelon/detail/map_indices.hpp>
12 #include <echelon/detail/all_integral.hpp>
13 
14 #include <algorithm>
15 #include <functional>
16 #include <vector>
17 #include <utility>
18 #include <type_traits>
19 
20 namespace echelon
21 {
22 
27 template <typename T>
29 {
30 public:
32  using value_type = T;
33 
43  multi_array_view(T* data_, std::vector<std::size_t> shape_)
44  : data_(data_), shape_(std::move(shape_))
45  {
46  }
47 
63  template <typename... Indices, typename Enabler = typename std::enable_if<
64  detail::all_integral<Indices...>::value>::type>
65  const value_type& operator()(Indices... indices) const
66  {
67  static_assert(detail::all_integral<Indices...>::value,
68  "All indices must be of integral type.");
69 
70  return data_[detail::map_indices(shape_, indices...)];
71  }
72 
87  template <typename... Indices, typename Enabler = typename std::enable_if<
88  detail::all_integral<Indices...>::value>::type>
89  value_type& operator()(Indices... indices)
90  {
91  static_assert(detail::all_integral<Indices...>::value,
92  "All indices must be of integral type.");
93 
94  return data_[detail::map_indices(shape_, indices...)];
95  }
96 
105  template <typename... Args, typename Enabler = typename std::enable_if<
106  !detail::all_integral<Args...>::value>::type>
107  echelon::hdf5::array_slice<T> operator()(Args... args)
108  {
109  return echelon::make_slice(*this, std::forward<Args>(args)...);
110  }
111 
116  const value_type* data() const
117  {
118  return data_;
119  }
120 
126  {
127  return data_;
128  }
129 
132  const std::vector<std::size_t>& shape() const
133  {
134  return shape_;
135  }
136 
137 private:
138  T* data_;
139  std::vector<std::size_t> shape_;
140 };
141 }
142 
143 #endif
echelon&#39;s core namespace
Definition: attribute.cpp:10
value_type & operator()(Indices...indices)
Accesses a specified element.
Definition: multi_array_view.hpp:89
const value_type * data() const
Direct access to the underlying array.
Definition: multi_array_view.hpp:116
A handle to an HDF5 type.
Definition: type.hpp:21
A multidimensional view onto an array.
Definition: multi_array_view.hpp:28
T value_type
value type of the array
Definition: multi_array_view.hpp:32
echelon::hdf5::array_slice< T > operator()(Args...args)
Slice the array.
Definition: multi_array_view.hpp:107
const std::vector< std::size_t > & shape() const
The shape of the array.
Definition: multi_array_view.hpp:132
value_type * data()
Direct access to the underlying array.
Definition: multi_array_view.hpp:125
multi_array_view(T *data_, std::vector< std::size_t > shape_)
Constructs a new view for a given array using a given shape.
Definition: multi_array_view.hpp:43
const value_type & operator()(Indices...indices) const
Accesses a specified element.
Definition: multi_array_view.hpp:65