mf
Media Framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Member Functions | Protected Attributes | Friends | List of all members
mf::ndarray_view< Dim, T > Class Template Reference

Mapping between coordinates, indices, and addresses of multi-dimensional data. More...

#include <ndarray_view.h>

Inheritance diagram for mf::ndarray_view< Dim, T >:
mf::ndarray_timed_view< Dim, T >

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_viewoperator= (Arg &&arg) const
 
const ndarray_viewoperator= (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_typeshape () const noexcept
 
const strides_typestrides () 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
 

Detailed Description

template<std::size_t Dim, typename T>
class mf::ndarray_view< Dim, T >

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).

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.

Member Typedef Documentation

template<std::size_t Dim, typename T>
using mf::ndarray_view< Dim, T >::coordinates_type = ndptrdiff<Dim>
template<std::size_t Dim, typename T>
using mf::ndarray_view< Dim, T >::fcall_result = detail::ndarray_view_fcall<Dim, T, 1>
template<std::size_t Dim, typename T>
using mf::ndarray_view< Dim, T >::index_type = std::ptrdiff_t
template<std::size_t Dim, typename T>
using mf::ndarray_view< Dim, T >::iterator = ndarray_iterator<ndarray_view>
template<std::size_t Dim, typename T>
using mf::ndarray_view< Dim, T >::pointer = T*
template<std::size_t Dim, typename T>
using mf::ndarray_view< Dim, T >::reference = T&
template<std::size_t Dim, typename T>
using mf::ndarray_view< Dim, T >::shape_type = ndsize<Dim>
template<std::size_t Dim, typename T>
using mf::ndarray_view< Dim, T >::span_type = ndspan<Dim>
template<std::size_t Dim, typename T>
using mf::ndarray_view< Dim, T >::strides_type = ndptrdiff<Dim>
template<std::size_t Dim, typename T>
using mf::ndarray_view< Dim, T >::value_type = T

Constructor & Destructor Documentation

template<std::size_t Dim, typename T>
mf::ndarray_view< Dim, T >::ndarray_view ( )
inline

Create null view.

template<std::size_t Dim, typename T >
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.

template<std::size_t Dim, typename T >
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).

template<std::size_t Dim, typename T>
mf::ndarray_view< Dim, T >::ndarray_view ( const ndarray_view< Dim, std::remove_const_t< T >> &  arr)
inline

Copy-construct view.

Does not copy data. Can create ndarray_view<const T> from ndarray_view<T>. (But not the other way.)

Member Function Documentation

template<std::size_t Dim, typename T >
ndarray_view< 1+Dim, T > mf::ndarray_view< Dim, T >::add_front_axis ( ) const
template<std::size_t Dim, typename T >
template<typename T2 >
void mf::ndarray_view< Dim, T >::assign ( const ndarray_view< Dim, T2 > &  other) const
template<std::size_t Dim, typename T>
void mf::ndarray_view< Dim, T >::assign ( const ndarray_view< Dim, const T > &  other) const
inline
template<std::size_t Dim, typename T >
auto mf::ndarray_view< Dim, T >::at ( const coordinates_type coord) const
template<std::size_t Dim, typename T >
auto mf::ndarray_view< Dim, T >::begin ( ) const
inline
template<std::size_t Dim, typename T >
template<typename T2 >
bool mf::ndarray_view< Dim, T >::compare ( const ndarray_view< Dim, T2 > &  other) const
template<std::size_t Dim, typename T>
bool mf::ndarray_view< Dim, T >::compare ( const ndarray_view< Dim, const T > &  other) const
inline
template<std::size_t Dim, typename T>
std::ptrdiff_t mf::ndarray_view< Dim, T >::contiguous_length ( ) const
inlinenoexcept
template<std::size_t Dim, typename T >
auto mf::ndarray_view< Dim, T >::coordinates_to_index ( const coordinates_type coord) const
template<std::size_t Dim, typename T >
auto mf::ndarray_view< Dim, T >::coordinates_to_pointer ( const coordinates_type coord) const
template<std::size_t Dim, typename T >
auto mf::ndarray_view< Dim, T >::default_strides ( const shape_type shape,
std::size_t  padding = 0 
)
static

Default strides which correspond to row-major order for specified shape.

Optionally with padding between elements.

template<std::size_t Dim, typename T >
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.

Parameters
minimal_dimensionLike in has_default_strides().
template<std::size_t Dim, typename T >
auto mf::ndarray_view< Dim, T >::end ( ) const
template<std::size_t Dim, typename T >
std::ptrdiff_t mf::ndarray_view< Dim, T >::fix_coordinate_ ( std::ptrdiff_t  c,
std::ptrdiff_t  dim 
) const
protected
template<std::size_t Dim, typename T>
span_type mf::ndarray_view< Dim, T >::full_span ( ) const
inlinenoexcept
template<std::size_t Dim, typename T >
bool mf::ndarray_view< Dim, T >::has_default_strides ( std::size_t  minimal_dimension = 0) const
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.

template<std::size_t Dim, typename T >
auto mf::ndarray_view< Dim, T >::index_to_coordinates ( const index_type index) const
template<std::size_t Dim, typename T>
bool mf::ndarray_view< Dim, T >::is_null ( ) const
inlinenoexcept
template<std::size_t Dim, typename T>
static ndarray_view mf::ndarray_view< Dim, T >::null ( )
inlinestatic
template<std::size_t Dim, typename T>
mf::ndarray_view< Dim, T >::operator bool ( ) const
inlineexplicitnoexcept
template<std::size_t Dim, typename T>
template<typename Arg >
bool mf::ndarray_view< Dim, T >::operator!= ( Arg &&  arg) const
inline
template<std::size_t Dim, typename T>
fcall_result mf::ndarray_view< Dim, T >::operator() ( std::ptrdiff_t  start,
std::ptrdiff_t  end,
std::ptrdiff_t  step = 1 
) const
inline
template<std::size_t Dim, typename T>
fcall_result mf::ndarray_view< Dim, T >::operator() ( std::ptrdiff_t  c) const
inline
template<std::size_t Dim, typename T>
fcall_result mf::ndarray_view< Dim, T >::operator() ( ) const
inline
template<std::size_t Dim, typename T>
template<typename Arg >
const ndarray_view& mf::ndarray_view< Dim, T >::operator= ( Arg &&  arg) const
inline
template<std::size_t Dim, typename T>
const ndarray_view& mf::ndarray_view< Dim, T >::operator= ( const ndarray_view< Dim, T > &  other) const
inline
template<std::size_t Dim, typename T>
template<typename Arg >
bool mf::ndarray_view< Dim, T >::operator== ( Arg &&  arg) const
inline
template<std::size_t Dim, typename T>
decltype(auto) mf::ndarray_view< Dim, T >::operator[] ( std::ptrdiff_t  c) const
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.

template<std::size_t Dim, typename T >
void mf::ndarray_view< Dim, T >::reset ( const ndarray_view< Dim, T > &  other)
noexcept
template<std::size_t Dim, typename T>
void mf::ndarray_view< Dim, T >::reset ( )
inlinenoexcept
template<std::size_t Dim, typename T>
void mf::ndarray_view< Dim, T >::reset ( pointer  start,
const shape_type shape,
const strides_type strides 
)
inline
template<std::size_t Dim, typename T>
void mf::ndarray_view< Dim, T >::reset ( pointer  start,
const shape_type shape 
)
inline
template<std::size_t Dim, typename T >
template<std::size_t New_dim>
ndarray_view< New_dim, T > mf::ndarray_view< Dim, T >::reshape ( const ndsize< New_dim > &  new_shape) const
template<std::size_t Dim, typename T >
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.

template<std::size_t Dim, typename T>
ndarray_view mf::ndarray_view< Dim, T >::section ( const span_type span,
const strides_type steps = strides_type(1) 
) const
inline

Cuboid section of view, defined using ndspan object.

template<std::size_t Dim, typename T >
auto mf::ndarray_view< Dim, T >::section_ ( std::ptrdiff_t  dim,
std::ptrdiff_t  start,
std::ptrdiff_t  end,
std::ptrdiff_t  step 
) const
protected
template<std::size_t Dim, typename T>
const shape_type& mf::ndarray_view< Dim, T >::shape ( ) const
inlinenoexcept
template<std::size_t Dim, typename T>
std::size_t mf::ndarray_view< Dim, T >::size ( ) const
inline
template<std::size_t Dim, typename T >
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.

template<std::size_t Dim, typename T>
pointer mf::ndarray_view< Dim, T >::start ( ) const
inlinenoexcept
template<std::size_t Dim, typename T>
const strides_type& mf::ndarray_view< Dim, T >::strides ( ) const
inlinenoexcept
template<std::size_t Dim, typename T >
auto mf::ndarray_view< Dim, T >::swapaxis ( std::size_t  axis1,
std::size_t  axis2 
) const

Friends And Related Function Documentation

template<std::size_t Dim, typename T>
bool same ( const ndarray_view< Dim, T > &  a,
const ndarray_view< Dim, T > &  b 
)
friend

Member Data Documentation

template<std::size_t Dim, typename T>
std::ptrdiff_t mf::ndarray_view< Dim, T >::contiguous_length_
protected
template<std::size_t Dim, typename T>
constexpr std::size_t mf::ndarray_view< Dim, T >::dimension = Dim
static
template<std::size_t Dim, typename T>
shape_type mf::ndarray_view< Dim, T >::shape_
protected
template<std::size_t Dim, typename T>
pointer mf::ndarray_view< Dim, T >::start_
protected
template<std::size_t Dim, typename T>
strides_type mf::ndarray_view< Dim, T >::strides_
protected

The documentation for this class was generated from the following files: