xoptional_assembly_base

Defined in xtensor/xoptional_assembly_base.hpp

template<class D>
class xoptional_assembly_base : private xt::xiterable<D>

Base class for dense multidimensional optional assemblies.

The xoptional_assembly_base class defines the interface for dense multidimensional optional assembly classes. Optional assembly classes hold optional values and are optimized for tensor operations. xoptional_assembly_base does not embed any data container, this responsibility is delegated to the inheriting classes.

Template Parameters:

D – The derived type, i.e. the inheriting class for which xoptional_assembly_base provides the interface.

Size and shape

inline size_type size() const noexcept

Returns the number of element in the optional assembly.

inline constexpr size_type dimension() const noexcept

Returns the number of dimensions of the optional assembly.

inline const inner_shape_type &shape() const noexcept

Returns the shape of the optional assembly.

inline size_type shape(size_type index) const

Returns the i-th dimension of the expression.

inline const inner_strides_type &strides() const noexcept

Returns the strides of the optional assembly.

inline const inner_backstrides_type &backstrides() const noexcept

Returns the backstrides of the optional assembly.

template<class S = shape_type>
inline void resize(const S &shape, bool force = false)

Resizes the optional assembly.

Parameters:
  • shape – the new shape

  • force – force reshaping, even if the shape stays the same (default: false)

template<class S = shape_type>
inline void resize(const S &shape, layout_type l)

Resizes the optional assembly.

Parameters:
  • shape – the new shape

  • l – the new layout_type

template<class S = shape_type>
inline void resize(const S &shape, const strides_type &strides)

Resizes the optional assembly.

Parameters:
  • shape – the new shape

  • strides – the new strides

inline layout_type layout() const noexcept

Return the layout_type of the container.

Returns:

layout_type of the container

template<class T>
inline void fill(const T &value)

Fills the data with the given value.

Parameters:

value – the value to fill the data with.

template<class S>
inline auto &reshape(const S &shape, layout_type layout) &

Reshapes the optional assembly.

Parameters:
  • shape – the new shape

  • layout – the new layout

Data

inline reference front()

Returns a reference to the first element of the optional assembly.

inline const_reference front() const

Returns a constant reference to the first element of the optional assembly.

inline reference back()

Returns a reference to the last element of the optional assembly.

inline const_reference back() const

Returns a constant reference to the last element of the optional assembly.

inline reference flat(size_type args)

Returns a reference to the element at the specified position of the underlying storage in the optional assembly.

Parameters:

index – index to underlying flat storage.

inline const_reference flat(size_type args) const

Returns a constant reference to the element at the specified position of the underlying storage in the optional assembly.

Parameters:

index – index to underlying flat storage.

template<class ...Args>
inline bool in_bounds(Args... args) const

Returns true only if the the specified position is a valid entry in the expression.

Parameters:

args – a list of indices specifying the position in the expression.

Returns:

bool

template<class ...Args>
inline auto operator()(Args... args) -> reference

Returns a reference to the element at the specified position in the optional assembly.

Parameters:

args – a list of indices specifying the position in the optional assembly. Indices must be unsigned integers, the number of indices should be equal or greater than the number of dimensions of the optional assembly.

template<class ...Args>
inline auto operator()(Args... args) const -> const_reference

Returns a constant reference to the element at the specified position in the optional assembly.

Parameters:

args – a list of indices specifying the position in the optional assembly. Indices must be unsigned integers, the number of indices should be equal or greater than the number of dimensions of the optional assembly.

template<class ...Args>
inline auto at(Args... args) -> reference

Returns a reference to the element at the specified position in the optional assembly, after dimension and bounds checking.

Parameters:

args – a list of indices specifying the position in the optional assembly. Indices must be unsigned integers, the number of indices should be equal to the number of dimensions of the optional assembly.

Throws:

std::out_of_range – if the number of argument is greater than the number of dimensions or if indices are out of bounds.

template<class ...Args>
inline auto at(Args... args) const -> const_reference

Returns a constant reference to the element at the specified position in the optional assembly, after dimension and bounds checking.

Parameters:

args – a list of indices specifying the position in the optional assembly. Indices must be unsigned integers, the number of indices should be equal to the number of dimensions of the optional assembly.

Throws:

std::out_of_range – if the number of argument is greater than the number of dimensions or if indices are out of bounds.

template<class ...Args>
inline auto unchecked(Args... args) -> reference

Returns a reference to the element at the specified position in the optional assembly.

Warning

This method is meant for performance, for expressions with a dynamic number of dimensions (i.e. not known at compile time). Since it may have undefined behavior (see parameters), operator() should be preferred whenever it is possible.

Warning

This method is NOT compatible with broadcasting, meaning the following code has undefined behavior:

xt::xarray<double> a = {{0, 1}, {2, 3}};
xt::xarray<double> b = {0, 1};
auto fd = a + b;
double res = fd.uncheked(0, 1);

Parameters:

args – a list of indices specifying the position in the optional assembly. Indices must be unsigned integers, the number of indices must be equal to the number of dimensions of the optional assembly, else the behavior is undefined.

template<class ...Args>
inline auto unchecked(Args... args) const -> const_reference

Returns a constant reference to the element at the specified position in the optional assembly.

Warning

This method is meant for performance, for expressions with a dynamic number of dimensions (i.e. not known at compile time). Since it may have undefined behavior (see parameters), operator() should be preferred whenever it is possible.

Warning

This method is NOT compatible with broadcasting, meaning the following code has undefined behavior:

xt::xarray<double> a = {{0, 1}, {2, 3}};
xt::xarray<double> b = {0, 1};
auto fd = a + b;
double res = fd.uncheked(0, 1);

Parameters:

args – a list of indices specifying the position in the optional assembly. Indices must be unsigned integers, the number of indices must be equal to the number of dimensions of the optional assembly, else the behavior is undefined.

template<class S>
inline auto operator[](const S &index) -> disable_integral_t<S, reference>

Returns a reference to the element at the specified position in the optional assembly.

Parameters:

index – a sequence of indices specifying the position in the optional assembly. Indices must be unsigned integers, the number of indices in the list should be equal or greater than the number of dimensions of the optional assembly.

template<class S>
inline auto operator[](const S &index) const -> disable_integral_t<S, const_reference>

Returns a constant reference to the element at the specified position in the optional assembly.

Parameters:

index – a sequence of indices specifying the position in the optional assembly. Indices must be unsigned integers, the number of indices in the list should be equal or greater than the number of dimensions of the optional assembly.

template<class ...Args>
inline auto periodic(Args... args) -> reference

Returns a reference to the element at the specified position in the optional assembly, after applying periodicity to the indices (negative and ‘overflowing’ indices are changed).

Parameters:

args – a list of indices specifying the position in the optional assembly. Indices must be unsigned integers, the number of indices should be equal to the number of dimensions of the optional assembly.

template<class ...Args>
inline auto periodic(Args... args) const -> const_reference

Returns a constant reference to the element at the specified position in the optional assembly, after applying periodicity to the indices (negative and ‘overflowing’ indices are changed).

Parameters:

args – a list of indices specifying the position in the optional assembly. Indices must be unsigned integers, the number of indices should be equal to the number of dimensions of the optional assembly.

template<class It>
inline auto element(It first, It last) -> reference

Returns a reference to the element at the specified position in the optional assembly.

Parameters:
  • first – iterator starting the sequence of indices

  • last – iterator ending the sequence of indices The number of indices in the sequence should be equal to or greater than the number of dimensions of the optional assembly.

template<class It>
inline auto element(It first, It last) const -> const_reference

Returns a constant reference to the element at the specified position in the optional assembly.

Parameters:
  • first – iterator starting the sequence of indices

  • last – iterator ending the sequence of indices The number of indices in the sequence should be equal to or greater than the number of dimensions of the optional assembly.

Broadcasting

template<class S>
inline bool broadcast_shape(S &shape, bool reuse_cache = false) const

Broadcast the shape of the optional assembly to the specified parameter.

Parameters:
  • shape – the result shape

  • reuse_cache – parameter for internal optimization

Returns:

a boolean indicating whether the broadcasting is trivial

template<class S>
inline bool has_linear_assign(const S &strides) const noexcept

Checks whether the xoptional_assembly_base can be linearly assigned to an expression with the specified strides.

Returns:

a boolean indicating whether a linear assign is possible

inline value_expression value() noexcept

Return an expression for the values of the optional assembly.

inline const_value_expression value() const noexcept

Return a constant expression for the values of the optional assembly.

inline flag_expression has_value() noexcept

Return an expression for the missing mask of the optional assembly.

inline const_flag_expression has_value() const noexcept

Return a constant expression for the missing mask of the optional assembly.

Public Functions

template<layout_type L>
inline auto begin() noexcept -> layout_iterator<L>

Returns an iterator to the first element of the expression.

Template Parameters:

L – order used for the traversal. Default value is XTENSOR_DEFAULT_TRAVERSAL.

template<layout_type L, class S>
inline auto begin(const S &shape) noexcept -> broadcast_iterator<S, L>

Returns an iterator to the first element of the expression.

The iteration is broadcasted to the specified shape.

Parameters:

shape – the shape used for broadcasting

Template Parameters:
  • S – type of the shape parameter.

  • L – order used for the traversal. Default value is XTENSOR_DEFAULT_TRAVERSAL.

template<layout_type L>
inline auto begin() const noexcept -> const_layout_iterator<L>

Returns a constant iterator to the first element of the expression.

Template Parameters:

L – order used for the traversal. Default value is XTENSOR_DEFAULT_TRAVERSAL.

template<layout_type L, class S>
inline auto begin(const S &shape) const noexcept -> const_broadcast_iterator<S, L>

Returns a constant iterator to the first element of the expression.

The iteration is broadcasted to the specified shape.

Parameters:

shape – the shape used for broadcasting

Template Parameters:
  • S – type of the shape parameter.

  • L – order used for the traversal. Default value is XTENSOR_DEFAULT_TRAVERSAL.

template<layout_type L>
inline auto end() noexcept -> layout_iterator<L>

Returns an iterator to the element following the last element of the expression.

Template Parameters:

L – order used for the traversal. Default value is XTENSOR_DEFAULT_TRAVERSAL.

template<layout_type L, class S>
inline auto end(const S &shape) noexcept -> broadcast_iterator<S, L>

Returns an iterator to the element following the last element of the expression.

The iteration is broadcasted to the specified shape.

Parameters:

shape – the shape used for broadcasting

Template Parameters:
  • S – type of the shape parameter.

  • L – order used for the traversal. Default value is XTENSOR_DEFAULT_TRAVERSAL.

template<layout_type L>
inline auto end() const noexcept -> const_layout_iterator<L>

Returns a constant iterator to the element following the last element of the expression.

Template Parameters:

L – order used for the traversal. Default value is XTENSOR_DEFAULT_TRAVERSAL.

template<layout_type L, class S>
inline auto end(const S &shape) const noexcept -> const_broadcast_iterator<S, L>

Returns a constant iterator to the element following the last element of the expression.

The iteration is broadcasted to the specified shape.

Parameters:

shape – the shape used for broadcasting

Template Parameters:
  • S – type of the shape parameter.

  • L – order used for the traversal. Default value is XTENSOR_DEFAULT_TRAVERSAL.

template<layout_type L>
inline auto rbegin() noexcept -> reverse_layout_iterator<L>

Returns an iterator to the first element of the reversed expression.

Template Parameters:

L – order used for the traversal. Default value is XTENSOR_DEFAULT_TRAVERSAL.

template<layout_type L, class S>
inline auto rbegin(const S &shape) noexcept -> reverse_broadcast_iterator<S, L>

Returns an iterator to the first element of the reversed expression.

The iteration is broadcasted to the specified shape.

Parameters:

shape – the shape used for broadcasting

Template Parameters:
  • S – type of the shape parameter.

  • L – order used for the traversal. Default value is XTENSOR_DEFAULT_TRAVERSAL.

template<layout_type L>
inline auto rbegin() const noexcept -> const_reverse_layout_iterator<L>

Returns a constant iterator to the first element of the reversed expression.

Template Parameters:

L – order used for the traversal. Default value is XTENSOR_DEFAULT_TRAVERSAL.

template<layout_type L, class S>
inline auto rbegin(const S &shape) const noexcept -> const_reverse_broadcast_iterator<S, L>

Returns a constant iterator to the first element of the reversed expression.

The iteration is broadcasted to the specified shape.

Parameters:

shape – the shape used for broadcasting

Template Parameters:
  • S – type of the shape parameter.

  • L – order used for the traversal. Default value is XTENSOR_DEFAULT_TRAVERSAL.

template<layout_type L>
inline auto rend() noexcept -> reverse_layout_iterator<L>

Returns an iterator to the element following the last element of the reversed expression.

Template Parameters:

L – order used for the traversal. Default value is XTENSOR_DEFAULT_TRAVERSAL.

template<layout_type L, class S>
inline auto rend(const S &shape) noexcept -> reverse_broadcast_iterator<S, L>

Returns an iterator to the element following the last element of the reversed expression.

The iteration is broadcasted to the specified shape.

Parameters:

shape – the shape used for broadcasting

Template Parameters:
  • S – type of the shape parameter.

  • L – order used for the traversal. Default value is XTENSOR_DEFAULT_TRAVERSAL.

template<layout_type L>
inline auto rend() const noexcept -> const_reverse_layout_iterator<L>

Returns a constant iterator to the element following the last element of the reversed expression.

Template Parameters:

L – order used for the traversal. Default value is XTENSOR_DEFAULT_TRAVERSAL.

template<layout_type L, class S>
inline auto rend(const S &shape) const noexcept -> const_reverse_broadcast_iterator<S, L>

Returns a constant iterator to the element following the last element of the reversed expression.

The iteration is broadcasted to the specified shape.

Parameters:

shape – the shape used for broadcasting

Template Parameters:
  • S – type of the shape parameter.

  • L – order used for the traversal. Default value is XTENSOR_DEFAULT_TRAVERSAL.