echelon  0.8.0
multi_array.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_HPP
7 #define ECHELON_MULTI_ARRAY_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 <vector>
15 #include <algorithm>
16 #include <functional>
17 #include <utility>
18 #include <type_traits>
19 
20 namespace echelon
21 {
22 
27 template <typename T>
29 {
30 public:
32  typedef T value_type;
33 
36  multi_array() = default;
37 
44  explicit multi_array(const std::vector<std::size_t>& shape_, const T& value_ = T())
45  : data_(std::accumulate(std::begin(shape_), std::end(shape_), std::size_t(1),
46  std::multiplies<std::size_t>()),
47  value_),
48  shape_(shape_)
49  {
50  }
51 
65  template <typename... Indices, typename Enabler = typename std::enable_if<
66  detail::all_integral<Indices...>::value>::type>
67  const T& operator()(Indices... indices) const
68  {
69  return data_[detail::map_indices(shape_, indices...)];
70  }
71 
86  template <typename... Indices, typename Enabler = typename std::enable_if<
87  detail::all_integral<Indices...>::value>::type>
88  T& operator()(Indices... indices)
89  {
90  return data_[detail::map_indices(shape_, indices...)];
91  }
92 
101  template <typename... Args, typename Enabler = typename std::enable_if<
102  !detail::all_integral<Args...>::value>::type>
103  echelon::hdf5::array_slice<T> operator()(Args... args)
104  {
105  return echelon::make_slice(*this, std::forward<Args>(args)...);
106  }
107 
112  const T* data() const
113  {
114  return data_.data();
115  }
116 
121  T* data()
122  {
123  return data_.data();
124  }
125 
132  typename std::vector<T>::iterator begin()
133  {
134  return data_.begin();
135  }
136 
143  typename std::vector<T>::iterator end()
144  {
145  return data_.end();
146  }
147 
154  typename std::vector<T>::const_iterator begin() const
155  {
156  return data_.begin();
157  }
158 
165  typename std::vector<T>::const_iterator end() const
166  {
167  return data_.end();
168  }
169 
172  const std::vector<std::size_t>& shape() const
173  {
174  return shape_;
175  }
176 
181  void reshape(const std::vector<std::size_t>& new_shape)
182  {
183  data_.resize(std::accumulate(std::begin(new_shape), std::end(new_shape), std::size_t(1),
184  std::multiplies<std::size_t>()));
185 
186  this->shape_ = new_shape;
187  }
188 
189 private:
190  std::vector<T> data_;
191  std::vector<std::size_t> shape_;
192 };
193 }
194 
195 #endif
echelon&#39;s core namespace
Definition: attribute.cpp:10
T & operator()(Indices...indices)
Accesses a specified element.
Definition: multi_array.hpp:88
std::vector< T >::iterator end()
Returns an iterator, which points to the last element of the flattened array.
Definition: multi_array.hpp:143
A handle to an HDF5 type.
Definition: type.hpp:21
T * data()
Direct access to the underlying array.
Definition: multi_array.hpp:121
T value_type
value type of the array
Definition: multi_array.hpp:32
echelon::hdf5::array_slice< T > operator()(Args...args)
Slice the array.
Definition: multi_array.hpp:103
multi_array()=default
Creates an empty array.
const std::vector< std::size_t > & shape() const
The shape of the array.
Definition: multi_array.hpp:172
void reshape(const std::vector< std::size_t > &new_shape)
Reshapes the array.
Definition: multi_array.hpp:181
const T & operator()(Indices...indices) const
Accesses a specified element.
Definition: multi_array.hpp:67
std::vector< T >::const_iterator begin() const
Returns an iterator, which points to the first element of the flattened array.
Definition: multi_array.hpp:154
Multidimensional array with runtime rank and shape.
Definition: multi_array.hpp:28
multi_array(const std::vector< std::size_t > &shape_, const T &value_=T())
Creates an array with a given shape.
Definition: multi_array.hpp:44
const T * data() const
Direct access to the underlying array.
Definition: multi_array.hpp:112
std::vector< T >::iterator begin()
Returns an iterator, which points to the first element of the flattened array.
Definition: multi_array.hpp:132
std::vector< T >::const_iterator end() const
Returns an iterator, which points to the last element of the flattened array.
Definition: multi_array.hpp:165