mf
Media Framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ndarray.h
Go to the documentation of this file.
1 #ifndef MF_NDARRAY_H_
2 #define MF_NDARRAY_H_
3 
4 #include "ndarray_view.h"
5 #include "ndcoord.h"
6 #include "../os/memory.h"
7 #include <memory>
8 #include <utility>
9 
10 #define MF_NDARRAY_VIEW_FUNC_(func) \
11  template<typename... Args> decltype(auto) func(Args&&... args) { \
12  return view().func(std::forward<Args>(args)...); \
13  } \
14  template<typename... Args> decltype(auto) func(Args&&... args) const { \
15  return cview().func(std::forward<Args>(args)...); \
16  }
17 
18 
19 namespace mf {
20 
22 
25 template<std::size_t Dim, typename T, typename Allocator = raw_allocator>
26 class ndarray {
27 public:
30 
35  using span_type = typename view_type::span_type;
36 
37  using value_type = T;
38  using pointer = T*;
39  using const_pointer = const T*;
40  using reference = T&;
41  using const_reference = const T&;
42 
43  using iterator = typename view_type::iterator;
45 
46  constexpr static std::size_t dimension = Dim;
47 
48 protected:
49  Allocator allocator_;
50  std::size_t stride_;
51  std::size_t padding_;
52  std::size_t allocated_size_ = 0;
54 
55  void allocate_();
56  void deallocate_();
57 
59 
65  ndarray(const shape_type& shape, std::size_t padding, std::size_t stride, const Allocator& allocator);
66 
68 
69 public:
70  explicit ndarray(const shape_type& shp, const Allocator& allocator = Allocator()) :
71  ndarray(shp, 0, sizeof(T), allocator) { }
72 
73  explicit ndarray(const const_view_type&);
74  ndarray(const ndarray& arr) : ndarray(arr.cview()) { }
75 
76  ~ndarray();
77 
78  ndarray& operator=(const const_view_type& arr);
79  ndarray& operator=(const ndarray& arr) { return operator=(arr.cview()); }
80 
81  view_type view() { return view_; }
82  const_view_type view() const { return cview(); }
84 
85  operator view_type () noexcept { return view(); }
86  operator const_view_type () const noexcept { return cview(); }
87 
89  { return view_.index_to_coordinates(index); }
91  { return view_.coordinates_to_index(coord); }
93  { return view_.coordinates_to_pointer(coord); }
95  { return view_.coordinates_to_pointer(coord); }
96 
97  std::size_t size() const noexcept { return view_.size(); }
98 
99  pointer start() noexcept { return view_.start(); }
100  const_pointer start() const noexcept { return view_.start(); }
101  const shape_type& shape() const noexcept { return view_.shape(); }
102  const strides_type& strides() const noexcept { return view_.strides(); }
103  std::size_t contiguous_length() const noexcept { return view_.contiguous_length(); }
104  std::size_t padding() const noexcept { return padding_; }
105 
111 
112  iterator begin() { return view_.begin(); }
113  const_iterator begin() const { return cview().begin(); }
114  const_iterator cbegin() const { return cview().begin(); }
115  iterator end() { return view_.end(); }
116  const_iterator end() const { return cview().end(); }
117  const_iterator cend() const { return cview().end(); }
118 };
119 
120 
121 
122 template<std::size_t Dim, typename T>
125 }
126 
127 
128 }
129 
130 #include "ndarray.tcc"
131 
132 #endif
typename view_type::iterator iterator
Definition: ndarray.h:43
#define MF_NDARRAY_VIEW_FUNC_(func)
Definition: ndarray.h:10
ndarray_iterator< ndarray_view > iterator
Definition: ndarray_view.h:79
std::size_t padding() const noexcept
Definition: ndarray.h:104
index_type coordinates_to_index(const coordinates_type &coord) const
Definition: ndarray.h:90
static constexpr std::size_t dimension
Definition: ndarray.h:46
ndsize< Dim > shape_type
Definition: ndarray_view.h:75
const_view_type cview() const
Definition: ndarray.h:83
Multidimensional array container.
Definition: ndarray.h:26
ndarray & operator=(const ndarray &arr)
Definition: ndarray.h:79
strides_type strides_with_padding_(const shape_type &shape, std::size_t padding)
Definition: ndarray.tcc:8
typename view_type::index_type index_type
Definition: ndarray.h:31
const shape_type & shape() const noexcept
Definition: ndarray_view.h:200
view_type view()
Definition: ndarray.h:81
const byte & const_reference
Definition: ndarray.h:41
coordinates_type index_to_coordinates(const index_type &index) const
Definition: ndarray.h:88
byte & reference
Definition: ndarray.h:40
typename view_type::shape_type shape_type
Definition: ndarray.h:33
ndarray_view< Dim, T > view_type
Definition: ndarray.h:28
ndarray_view< Dim, const T > const_view_type
Definition: ndarray.h:29
std::ptrdiff_t contiguous_length() const noexcept
Definition: ndarray_view.h:202
~ndarray()
Definition: ndarray.tcc:70
auto make_ndarray(const ndarray_view< Dim, T > &vw)
Definition: ndarray.h:123
ndarray(const ndarray &arr)
Definition: ndarray.h:74
pointer start() noexcept
Definition: ndarray.h:99
std::size_t size() const noexcept
Definition: ndarray.h:97
decltype(auto) slice(Args &&...args)
Definition: ndarray.h:107
ndarray & operator=(const const_view_type &arr)
Definition: ndarray.tcc:76
std::size_t padding_
Padding between frames, in bytes.
Definition: ndarray.h:51
const_pointer coordinates_to_pointer(const coordinates_type &coord) const
Definition: ndarray.h:94
ndptrdiff< Dim > strides_type
Definition: ndarray_view.h:76
decltype(auto) at(Args &&...args)
Definition: ndarray.h:110
ndptrdiff< Dim > coordinates_type
Definition: ndarray_view.h:74
iterator begin()
Definition: ndarray.h:112
typename view_type::strides_type strides_type
Definition: ndarray.h:34
pointer coordinates_to_pointer(const coordinates_type &) const
Definition: ndarray_view.tcc:153
decltype(auto) section(Args &&...args)
Definition: ndarray.h:106
std::size_t size() const
Definition: ndarray_view.h:197
pointer coordinates_to_pointer(const coordinates_type &coord)
Definition: ndarray.h:92
const_pointer start() const noexcept
Definition: ndarray.h:100
void deallocate_()
Definition: ndarray.tcc:42
std::ptrdiff_t index_type
Definition: ndarray_view.h:73
const shape_type & shape() const noexcept
Definition: ndarray.h:101
coordinates_type index_to_coordinates(const index_type &) const
Definition: ndarray_view.tcc:101
ndarray(const shape_type &shp, const Allocator &allocator=Allocator())
Definition: ndarray.h:70
ndarray(const shape_type &shape, std::size_t padding, std::size_t stride, const Allocator &allocator)
Constructor for use by derived classes.
Definition: ndarray.tcc:50
iterator end()
Definition: ndarray.h:115
std::size_t allocated_size_
Allocated memory size, in bytes.
Definition: ndarray.h:52
const_iterator begin() const
Definition: ndarray.h:113
std::size_t stride_
Stride of elements, in bytes.
Definition: ndarray.h:50
const_iterator cbegin() const
Definition: ndarray.h:114
const_iterator end() const
Definition: ndarray.h:116
const byte * const_pointer
Definition: ndarray.h:39
const_view_type view() const
Definition: ndarray.h:82
typename view_type::coordinates_type coordinates_type
Definition: ndarray.h:32
typename const_view_type::iterator const_iterator
Definition: ndarray.h:44
iterator begin() const
Definition: ndarray_view.tcc:170
void allocate_()
Definition: ndarray.tcc:17
byte * pointer
Definition: ndarray.h:38
ndspan< Dim > span_type
Definition: ndarray_view.h:77
std::size_t contiguous_length() const noexcept
Definition: ndarray.h:103
index_type coordinates_to_index(const coordinates_type &) const
Definition: ndarray_view.tcc:117
view_type view_
View used to access items.
Definition: ndarray.h:53
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
const_iterator cend() const
Definition: ndarray.h:117
byte value_type
Definition: ndarray.h:37
typename view_type::span_type span_type
Definition: ndarray.h:35
const strides_type & strides() const noexcept
Definition: ndarray.h:102
Allocator allocator_
Raw allocator used to allocate memory, in bytes.
Definition: ndarray.h:49