echelon  0.8.0
multi_array_adapter.hpp
1 // Copyright (c) 2012-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_ADAPTER_HPP
7 #define ECHELON_MULTI_ARRAY_ADAPTER_HPP
8 
9 #include <echelon/range.hpp>
10 
11 #include <echelon/hdf5/container_adaption.hpp>
12 #include <echelon/dataset.hpp>
13 #include <echelon/detail/map_indices.hpp>
14 #include <echelon/detail/all_integral.hpp>
15 
16 #include <algorithm>
17 #include <functional>
18 #include <vector>
19 #include <utility>
20 #include <type_traits>
21 
22 namespace echelon
23 {
24 
31 template <typename Container>
33 {
34 public:
36  typedef typename Container::value_type value_type;
37 
48  multi_array_adapter(Container& container_, std::vector<std::size_t> shape_)
49  : container_(container_), shape_(std::move(shape_))
50  {
51  }
52 
68  template <typename... Indices, typename Enabler = typename std::enable_if<
69  detail::all_integral<Indices...>::value>::type>
70  const value_type& operator()(Indices... indices) const
71  {
72  static_assert(detail::all_integral<Indices...>::value,
73  "All indices must be of integral type.");
74 
75  return container_[detail::map_indices(shape_, indices...)];
76  }
77 
92  template <typename... Indices, typename Enabler = typename std::enable_if<
93  detail::all_integral<Indices...>::value>::type>
94  value_type& operator()(Indices... indices)
95  {
96  static_assert(detail::all_integral<Indices...>::value,
97  "All indices must be of integral type.");
98 
99  return container_[detail::map_indices(shape_, indices...)];
100  }
101 
110  template <typename... Args, typename Enabler = typename std::enable_if<
111  !detail::all_integral<Args...>::value>::type>
112  echelon::hdf5::array_slice<value_type> operator()(Args... args)
113  {
114  return echelon::make_slice(*this, std::forward<Args>(args)...);
115  }
116 
123  typename Container::iterator begin()
124  {
125  return container_.begin();
126  }
127 
134  typename Container::iterator end()
135  {
136  return container_.end();
137  }
138 
145  typename Container::const_iterator begin() const
146  {
147  return container_.begin();
148  }
149 
156  typename Container::const_iterator end() const
157  {
158  return container_.end();
159  }
160 
165  const value_type* data() const
166  {
167  using ::echelon::hdf5::data;
168 
169  return data(container_);
170  }
171 
176  value_type* data()
177  {
178  using ::echelon::hdf5::data;
179 
180  return data(container_);
181  }
182 
185  const std::vector<std::size_t>& shape() const
186  {
187  return shape_;
188  }
189 
190 private:
191  Container& container_;
192  std::vector<std::size_t> shape_;
193 };
194 }
195 
196 #endif
echelon&#39;s core namespace
Definition: attribute.cpp:10
Container::iterator begin()
Returns an iterator, which points to the first element of the flattened array.
Definition: multi_array_adapter.hpp:123
Container::value_type value_type
value type of the array
Definition: multi_array_adapter.hpp:36
echelon::hdf5::array_slice< value_type > operator()(Args...args)
Slice the array.
Definition: multi_array_adapter.hpp:112
const std::vector< std::size_t > & shape() const
The shape of the array.
Definition: multi_array_adapter.hpp:185
A handle to an HDF5 type.
Definition: type.hpp:21
Container::const_iterator end() const
Returns an iterator, which points to the last element of the flattened array.
Definition: multi_array_adapter.hpp:156
Container::const_iterator begin() const
Returns an iterator, which points to the first element of the flattened array.
Definition: multi_array_adapter.hpp:145
const value_type & operator()(Indices...indices) const
Accesses a specified element.
Definition: multi_array_adapter.hpp:70
value_type * data()
Direct access to the underlying array.
Definition: multi_array_adapter.hpp:176
An adapter, around a random-access sequence container, which behaves like a multidimensional array...
Definition: multi_array_adapter.hpp:32
value_type & operator()(Indices...indices)
Accesses a specified element.
Definition: multi_array_adapter.hpp:94
const value_type * data() const
Direct access to the underlying array.
Definition: multi_array_adapter.hpp:165
multi_array_adapter(Container &container_, std::vector< std::size_t > shape_)
Constructs a new adapter around a given container using a given shape.
Definition: multi_array_adapter.hpp:48
Container::iterator end()
Returns an iterator, which points to the last element of the flattened array.
Definition: multi_array_adapter.hpp:134