mf
Media Framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ndarray_view.h
Go to the documentation of this file.
1 #ifndef MF_NDARRAY_VIEW_H_
2 #define MF_NDARRAY_VIEW_H_
3 
4 #include "../common.h"
5 #include "ndcoord.h"
6 #include "ndarray_iterator.h"
7 #include "ndspan.h"
8 #include "../utility/misc.h"
9 #include "../elem.h"
10 #include <type_traits>
11 #include <utility>
12 #include <memory>
13 
14 namespace mf {
15 
16 template<std::size_t Dim, typename T> class ndarray_view;
17 
18 namespace detail {
19  template<std::size_t Dim, typename T>
20  ndarray_view<Dim - 1, T> get_subscript(const ndarray_view<Dim, T>& array, std::ptrdiff_t c) {
21  return array.slice(c, 0);
22  }
23 
24  template<typename T>
25  T& get_subscript(const ndarray_view<1, T>& array, std::ptrdiff_t c) {
26  return array.at({c});
27  }
28 
29  template<std::size_t Dim, typename T, std::ptrdiff_t I>
30  class ndarray_view_fcall;
31 }
32 
33 
35 
65 template<std::size_t Dim, typename T>
66 class ndarray_view {
67  static_assert(Dim >= 1, "ndarray_view dimension must be >= 1");
68 
69 public:
70  using value_type = T;
71  using pointer = T*;
72  using reference = T&;
73  using index_type = std::ptrdiff_t;
78 
80 
81  constexpr static std::size_t dimension = Dim;
82 
83 protected:
87 
88  std::ptrdiff_t contiguous_length_;
89 
90  ndarray_view section_(std::ptrdiff_t dim, std::ptrdiff_t start, std::ptrdiff_t end, std::ptrdiff_t step) const;
91  std::ptrdiff_t fix_coordinate_(std::ptrdiff_t c, std::ptrdiff_t dim) const;
92 
93 public:
95 
96  static strides_type default_strides(const shape_type&, std::size_t padding = 0);
97 
99 
101  bool has_default_strides(std::size_t minimal_dimension = 0) const noexcept;
102 
104 
106  std::size_t default_strides_padding(std::size_t minimal_dimension = 0) const;
107 
110 
113 
116 
118 
119  ndarray_view(const ndarray_view<Dim, std::remove_const_t<T>>& arr) :
120  ndarray_view(arr.start(), arr.shape(), arr.strides()) { }
121 
122  static ndarray_view null() { return ndarray_view(); }
123  bool is_null() const noexcept { return (start_ == nullptr); }
124  explicit operator bool () const noexcept { return ! is_null(); }
125 
126  void reset(const ndarray_view& other) noexcept;
127  void reset() noexcept { reset(ndarray_view()); }
129  { reset(ndarray_view(start, shape, strides)); }
131  { reset(ndarray_view(start, shape)); }
132 
133  template<typename Arg> const ndarray_view& operator=(Arg&& arg) const
134  { assign(std::forward<Arg>(arg)); return *this; }
135  const ndarray_view& operator=(const ndarray_view& other) const
136  { assign(other); return *this; }
137 
141 
143 
147  const coordinates_type& start,
148  const coordinates_type& end,
149  const strides_type& steps = strides_type(1)
150  ) const;
151 
153  ndarray_view section(const span_type& span, const strides_type& steps = strides_type(1)) const {
154  return section(span.start_pos(), span.end_pos(), steps);
155  }
156 
158  ndarray_view<Dim - 1, T> slice(std::ptrdiff_t c, std::ptrdiff_t dimension) const;
159 
161 
163  decltype(auto) operator[](std::ptrdiff_t c) const {
164  return detail::get_subscript(*this, c);
165  }
166 
167  using fcall_result = detail::ndarray_view_fcall<Dim, T, 1>;
168  fcall_result operator()(std::ptrdiff_t start, std::ptrdiff_t end, std::ptrdiff_t step = 1) const {
169  return section_(0, start, end, step);
170  }
171  fcall_result operator()(std::ptrdiff_t c) const {
172  return section_(0, c, c + 1, 1);
173  }
175  return *this;
176  }
177 
178  reference at(const coordinates_type&) const;
179 
180  iterator begin() const;
181  iterator end() const;
182 
183  template<typename T2> void assign(const ndarray_view<Dim, T2>&) const;
184  void assign(const ndarray_view<Dim, const T>& other) const { assign<const T>(other); }
185 
186  template<typename T2> bool compare(const ndarray_view<Dim, T2>&) const;
187  bool compare(const ndarray_view<Dim, const T>& other) const { return compare<const T>(other); }
188 
189  template<typename Arg> bool operator==(Arg&& arg) const { return compare(std::forward<Arg>(arg)); }
190  template<typename Arg> bool operator!=(Arg&& arg) const { return ! compare(std::forward<Arg>(arg)); }
191 
192  friend bool same(const ndarray_view& a, const ndarray_view& b) noexcept {
193  if(a.is_null() && b.is_null()) return true;
194  else return (a.start_ == b.start_) && (a.shape_ == b.shape_) && (a.strides_ == b.strides_);
195  }
196 
197  std::size_t size() const { return shape().product(); }
198 
199  pointer start() const noexcept { return start_; }
200  const shape_type& shape() const noexcept { return shape_; }
201  const strides_type& strides() const noexcept { return strides_; }
202  std::ptrdiff_t contiguous_length() const noexcept { return contiguous_length_; }
203 
204  span_type full_span() const noexcept { return span_type(0, shape_); }
205 
206  template<std::size_t New_dim>
208 
210 
211  ndarray_view swapaxis(std::size_t axis1, std::size_t axis2) const;
212 };
213 
214 
215 template<typename T>
217  return vw.swapaxis(0, 1);
218 }
219 
220 
221 }
222 
223 #include "ndarray_view.tcc"
224 
225 #endif
strides_type strides_
Definition: ndarray_view.h:86
bool has_default_strides(std::size_t minimal_dimension=0) const noexcept
Check if view has default strides.
Definition: ndarray_view.tcc:51
ndarray_view(const ndarray_view< Dim, std::remove_const_t< T >> &arr)
Copy-construct view.
Definition: ndarray_view.h:119
std::ptrdiff_t contiguous_length_
Definition: ndarray_view.h:88
Mapping between coordinates, indices, and addresses of multi-dimensional data.
Definition: ndarray_view.h:16
void assign(const ndarray_view< Dim, T2 > &) const
Definition: ndarray_view.tcc:138
pointer start_
Definition: ndarray_view.h:84
ndarray_iterator< ndarray_view > iterator
Definition: ndarray_view.h:79
ndarray_view swapaxis(std::size_t axis1, std::size_t axis2) const
Definition: ndarray_view.tcc:250
ndsize< Dim > shape_type
Definition: ndarray_view.h:75
void reset(pointer start, const shape_type &shape)
Definition: ndarray_view.h:130
detail::ndarray_view_fcall< Dim, Elem, 1 > fcall_result
Definition: ndarray_view.h:167
const shape_type & shape() const noexcept
Definition: ndarray_view.h:200
fcall_result operator()(std::ptrdiff_t c) const
Definition: ndarray_view.h:171
static strides_type default_strides(const shape_type &, std::size_t padding=0)
Default strides which correspond to row-major order for specified shape.
Definition: ndarray_view.tcc:39
bool operator==(Arg &&arg) const
Definition: ndarray_view.h:189
static ndarray_view null()
Definition: ndarray_view.h:122
bool operator!=(Arg &&arg) const
Definition: ndarray_view.h:190
std::ptrdiff_t contiguous_length() const noexcept
Definition: ndarray_view.h:202
Elem value_type
Definition: ndarray_view.h:70
ndarray_view()
Create null view.
Definition: ndarray_view.h:109
fcall_result operator()() const
Definition: ndarray_view.h:174
ndarray_view< Dim-1, T > slice(std::ptrdiff_t c, std::ptrdiff_t dimension) const
Create ndarray_view with one less dimension, by fixing coordinate of axis dimension to c...
Definition: ndarray_view.tcc:225
const ndarray_view & operator=(const ndarray_view &other) const
Definition: ndarray_view.h:135
const coordinates_type & end_pos() const noexcept
Definition: ndspan.h:32
ndarray_view section(const span_type &span, const strides_type &steps=strides_type(1)) const
Cuboid section of view, defined using ndspan object.
Definition: ndarray_view.h:153
bool is_null() const noexcept
Definition: ndarray_view.h:123
ndptrdiff< Dim > strides_type
Definition: ndarray_view.h:76
ndarray_view section(const coordinates_type &start, const coordinates_type &end, const strides_type &steps=strides_type(1)) const
Cuboid section of view, with interval in each axis.
Definition: ndarray_view.tcc:214
ndptrdiff< Dim > coordinates_type
Definition: ndarray_view.h:74
Elem * pointer
Definition: ndarray_view.h:71
ndarray_view< New_dim, T > reshape(const ndsize< New_dim > &) const
Definition: ndarray_view.tcc:235
bool compare(const ndarray_view< Dim, T2 > &) const
Definition: ndarray_view.tcc:145
Vector of n-dimensional coordinates.
Definition: ndcoord.h:18
pointer coordinates_to_pointer(const coordinates_type &) const
Definition: ndarray_view.tcc:153
std::size_t size() const
Definition: ndarray_view.h:197
T product() const noexcept
Definition: ndcoord.h:111
std::ptrdiff_t index_type
Definition: ndarray_view.h:73
fcall_result operator()(std::ptrdiff_t start, std::ptrdiff_t end, std::ptrdiff_t step=1) const
Definition: ndarray_view.h:168
Cuboid n-dimensional span delimited by two ndcoord vectors.
Definition: ndspan.h:15
ndarray_view< 1+Dim, T > add_front_axis() const
Definition: ndarray_view.tcc:243
bool compare(const ndarray_view< Dim, const T > &other) const
Definition: ndarray_view.h:187
void reset(pointer start, const shape_type &shape, const strides_type &strides)
Definition: ndarray_view.h:128
shape_type shape_
Definition: ndarray_view.h:85
span_type full_span() const noexcept
Definition: ndarray_view.h:204
coordinates_type index_to_coordinates(const index_type &) const
Definition: ndarray_view.tcc:101
const ndarray_view & operator=(Arg &&arg) const
Definition: ndarray_view.h:133
reference at(const coordinates_type &) const
Definition: ndarray_view.tcc:162
const coordinates_type & start_pos() const noexcept
Definition: ndspan.h:31
ndarray_view< 2, T > flip(const ndarray_view< 2, T > &vw)
Definition: ndarray_view.h:216
iterator begin() const
Definition: ndarray_view.tcc:170
void assign(const ndarray_view< Dim, const T > &other) const
Definition: ndarray_view.h:184
std::size_t default_strides_padding(std::size_t minimal_dimension=0) const
Returns padding of the view which has default strides.
Definition: ndarray_view.tcc:64
ndspan< Dim > span_type
Definition: ndarray_view.h:77
friend bool same(const ndarray_view &a, const ndarray_view &b) noexcept
Definition: ndarray_view.h:192
static constexpr std::size_t dimension
Definition: ndarray_view.h:81
ndarray_view section_(std::ptrdiff_t dim, std::ptrdiff_t start, std::ptrdiff_t end, std::ptrdiff_t step) const
Definition: ndarray_view.tcc:184
index_type coordinates_to_index(const coordinates_type &) const
Definition: ndarray_view.tcc:117
const strides_type & strides() const noexcept
Definition: ndarray_view.h:201
iterator end() const
Definition: ndarray_view.tcc:176
pointer start() const noexcept
Definition: ndarray_view.h:199
void reset() noexcept
Definition: ndarray_view.h:127
Elem & reference
Definition: ndarray_view.h:72
Random access iterator which traverses an ndarray_view.
Definition: ndarray_iterator.h:16
std::ptrdiff_t fix_coordinate_(std::ptrdiff_t c, std::ptrdiff_t dim) const
Definition: ndarray_view.tcc:92