mf
Media Framework
|
Mapping between coordinates, indices, and addresses of multi-dimensional data. More...
#include <ndarray_view.h>
Public Types | |
using | value_type = T |
using | pointer = T * |
using | reference = T & |
using | index_type = std::ptrdiff_t |
using | coordinates_type = ndptrdiff< Dim > |
using | shape_type = ndsize< Dim > |
using | strides_type = ndptrdiff< Dim > |
using | span_type = ndspan< Dim > |
using | iterator = ndarray_iterator< ndarray_view > |
using | fcall_result = detail::ndarray_view_fcall< Dim, T, 1 > |
Public Member Functions | |
bool | has_default_strides (std::size_t minimal_dimension=0) const noexcept |
Check if view has default strides. More... | |
std::size_t | default_strides_padding (std::size_t minimal_dimension=0) const |
Returns padding of the view which has default strides. More... | |
ndarray_view () | |
Create null view. More... | |
ndarray_view (pointer start, const shape_type &, const strides_type &) | |
Create view with explicitly specified start, shape and strides. More... | |
ndarray_view (pointer start, const shape_type &shape) | |
Create view with explicitly specified start and shape, with default strides (without padding). More... | |
ndarray_view (const ndarray_view< Dim, std::remove_const_t< T >> &arr) | |
Copy-construct view. More... | |
bool | is_null () const noexcept |
operator bool () const noexcept | |
void | reset (const ndarray_view &other) noexcept |
void | reset () noexcept |
void | reset (pointer start, const shape_type &shape, const strides_type &strides) |
void | reset (pointer start, const shape_type &shape) |
template<typename Arg > | |
const ndarray_view & | operator= (Arg &&arg) const |
const ndarray_view & | operator= (const ndarray_view &other) const |
coordinates_type | index_to_coordinates (const index_type &) const |
index_type | coordinates_to_index (const coordinates_type &) const |
pointer | coordinates_to_pointer (const coordinates_type &) const |
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. More... | |
ndarray_view | section (const span_type &span, const strides_type &steps=strides_type(1)) const |
Cuboid section of view, defined using ndspan object. More... | |
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 . More... | |
decltype(auto) | operator[] (std::ptrdiff_t c) const |
Subscript operator, creates slice on first dimension. More... | |
fcall_result | operator() (std::ptrdiff_t start, std::ptrdiff_t end, std::ptrdiff_t step=1) const |
fcall_result | operator() (std::ptrdiff_t c) const |
fcall_result | operator() () const |
reference | at (const coordinates_type &) const |
iterator | begin () const |
iterator | end () const |
template<typename T2 > | |
void | assign (const ndarray_view< Dim, T2 > &) const |
void | assign (const ndarray_view< Dim, const T > &other) const |
template<typename T2 > | |
bool | compare (const ndarray_view< Dim, T2 > &) const |
bool | compare (const ndarray_view< Dim, const T > &other) const |
template<typename Arg > | |
bool | operator== (Arg &&arg) const |
template<typename Arg > | |
bool | operator!= (Arg &&arg) const |
std::size_t | size () const |
pointer | start () const noexcept |
const shape_type & | shape () const noexcept |
const strides_type & | strides () const noexcept |
std::ptrdiff_t | contiguous_length () const noexcept |
span_type | full_span () const noexcept |
template<std::size_t New_dim> | |
ndarray_view< New_dim, T > | reshape (const ndsize< New_dim > &) const |
ndarray_view< 1+Dim, T > | add_front_axis () const |
ndarray_view | swapaxis (std::size_t axis1, std::size_t axis2) const |
Static Public Member Functions | |
static strides_type | default_strides (const shape_type &, std::size_t padding=0) |
Default strides which correspond to row-major order for specified shape. More... | |
static ndarray_view | null () |
Static Public Attributes | |
static constexpr std::size_t | dimension = Dim |
Protected Member Functions | |
ndarray_view | section_ (std::ptrdiff_t dim, std::ptrdiff_t start, std::ptrdiff_t end, std::ptrdiff_t step) const |
std::ptrdiff_t | fix_coordinate_ (std::ptrdiff_t c, std::ptrdiff_t dim) const |
Protected Attributes | |
pointer | start_ |
shape_type | shape_ |
strides_type | strides_ |
std::ptrdiff_t | contiguous_length_ |
Friends | |
bool | same (const ndarray_view &a, const ndarray_view &b) noexcept |
Mapping between coordinates, indices, and addresses of multi-dimensional data.
Templated for dimension and element type. T
must be elem
type, i.e. elem_traits<T>
must be defined. The ndarray_view
is defined by the three values (start, shape, strides)
.
start
is the pointer to the array element at indices (0, 0, ..., 0)
.shape
is the vector of size Dim
which defined length of array in each axis.strides
defined memory layout of elements: strides[i]
defined distance in bytes between arr[][k][]
and arr[][k+1][]
(the i
-th coordinate changes). ndarray_view
is non-owning view to data. T
can be a const
type. Always gives full access to elements, no matter if this
const. Subscript operator operator[]
and functions section()
, etc., return another ndarray_view
to given region.Stride values may be negative to form reverse-order view on that axis (then start
is no longer the element with lowest absolute address.) If strides are in non-descending order, coordinates to address mapping is no longer row-major. Strides need to be set to at least sizeof(T)
and multiple of alignof(T)
. Default strides produce row-major mapping, optionally with padding between elements. The term default strides is still used here if there is inter-element padding. Coordinates map to address, but not the other way.
Coordinates map one-to-one to index, and index to coordinates. Index always advances in row-major order with coordinates, independently of strides. Index of coordinates (0, 0, ..., 0)
is 0
. Random-access iterator ndarray_iterator
always traverses ndarray in index order. As an optimization, iterator incrementation and decrementation is more efficient when all strides, or tail of strides, is default.
Important: Assignment and comparison operators perform deep assignment/comparison in the elements that the view points to, and not of the ndarray_view
itself. Shallow assignment and comparison is done with same()
and reset()
. This simplifies interface: assigning a single element arr[0][2] = 3
works the same as assigning an entire region arr[0] = make_frame(...)
. (Because operator[]
returns an ndarray_view
.) Copy-constructing a view does not copy data. Semantics are similar to C++ references.
Default constructor, or null()
returns null view. All null views compare equal (with same()
), and is_null()
or explicit bool
conversion operator test for null view. Zero-length views (where shape().product() == 0
) are possible, and are not equal to null views.
using mf::ndarray_view< Dim, T >::coordinates_type = ndptrdiff<Dim> |
using mf::ndarray_view< Dim, T >::fcall_result = detail::ndarray_view_fcall<Dim, T, 1> |
using mf::ndarray_view< Dim, T >::index_type = std::ptrdiff_t |
using mf::ndarray_view< Dim, T >::iterator = ndarray_iterator<ndarray_view> |
using mf::ndarray_view< Dim, T >::pointer = T* |
using mf::ndarray_view< Dim, T >::reference = T& |
using mf::ndarray_view< Dim, T >::shape_type = ndsize<Dim> |
using mf::ndarray_view< Dim, T >::span_type = ndspan<Dim> |
using mf::ndarray_view< Dim, T >::strides_type = ndptrdiff<Dim> |
using mf::ndarray_view< Dim, T >::value_type = T |
|
inline |
Create null view.
mf::ndarray_view< Dim, T >::ndarray_view | ( | pointer | start, |
const shape_type & | shape, | ||
const strides_type & | strides | ||
) |
Create view with explicitly specified start, shape and strides.
mf::ndarray_view< Dim, T >::ndarray_view | ( | pointer | start, |
const shape_type & | shape | ||
) |
Create view with explicitly specified start and shape, with default strides (without padding).
|
inline |
Copy-construct view.
Does not copy data. Can create ndarray_view<const T>
from ndarray_view<T>
. (But not the other way.)
ndarray_view< 1+Dim, T > mf::ndarray_view< Dim, T >::add_front_axis | ( | ) | const |
void mf::ndarray_view< Dim, T >::assign | ( | const ndarray_view< Dim, T2 > & | other | ) | const |
|
inline |
auto mf::ndarray_view< Dim, T >::at | ( | const coordinates_type & | coord | ) | const |
|
inline |
bool mf::ndarray_view< Dim, T >::compare | ( | const ndarray_view< Dim, T2 > & | other | ) | const |
|
inline |
|
inlinenoexcept |
auto mf::ndarray_view< Dim, T >::coordinates_to_index | ( | const coordinates_type & | coord | ) | const |
auto mf::ndarray_view< Dim, T >::coordinates_to_pointer | ( | const coordinates_type & | coord | ) | const |
|
static |
Default strides which correspond to row-major order for specified shape.
Optionally with padding
between elements.
std::size_t mf::ndarray_view< Dim, T >::default_strides_padding | ( | std::size_t | minimal_dimension = 0 | ) | const |
Returns padding of the view which has default strides.
If view does not have default strides, throws exception.
minimal_dimension | Like in has_default_strides(). |
auto mf::ndarray_view< Dim, T >::end | ( | ) | const |
|
protected |
|
inlinenoexcept |
|
noexcept |
Check if view has default strides.
If minimal_dimension
is specified, checks if view has default strides in dimensions from Dim - 1
down to minimal_dimension
. Strides from minimal_dimension + 1
down to 0
may be non-default.
auto mf::ndarray_view< Dim, T >::index_to_coordinates | ( | const index_type & | index | ) | const |
|
inlinenoexcept |
|
inlinestatic |
|
inlineexplicitnoexcept |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
Subscript operator, creates slice on first dimension.
If Dim > 1
, equivalent to slide(c, 0)
. If Dim == 1
, returns reference to c
-th element in view. Can access elements in multi-dimensional array like arr[i][j][k] = value
.
|
noexcept |
|
inlinenoexcept |
|
inline |
|
inline |
ndarray_view< New_dim, T > mf::ndarray_view< Dim, T >::reshape | ( | const ndsize< New_dim > & | new_shape | ) | const |
auto mf::ndarray_view< Dim, T >::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.
Can also specify step for each axis: Stride of the new view are stride of this view multiplied by step. Step 1
does not change stride, step 2
skips every second element on that axis, negative step also reverses direction. It is not necessary that coordinate of last element on an axis coincides with end - 1
.
|
inline |
Cuboid section of view, defined using ndspan
object.
|
protected |
|
inlinenoexcept |
|
inline |
auto mf::ndarray_view< Dim, 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
.
|
inlinenoexcept |
|
inlinenoexcept |
auto mf::ndarray_view< Dim, T >::swapaxis | ( | std::size_t | axis1, |
std::size_t | axis2 | ||
) | const |
|
friend |
|
protected |
|
static |
|
protected |
|
protected |
|
protected |