TRIQS/nda 1.3.0
Multi-dimensional array library for C++
|
#include <nda/basic_array_view.hpp>
A generic view of a multi-dimensional array.
Together with nda::basic_array, this class forms the backbone of the nda library. It is templated with the following parameters:
ValueType
: This is the type of the elements in the view. Depending on how the view was constructed and on the underlying array, this can be a const or non-const type.Rank
: Integer specifying the number of dimensions of the view. This is a compile-time constant.LayoutPolicy
: The layout policy specifies how the view accesses its elements. It provides a mapping from multi-dimensional to linear indices and vice versa (see Layout policies).Algebra
: The algebra specifies how the view behaves when it is used in an expression. Possible values are 'A' (array), 'M' (matrix) and 'V' (vector) (see nda::get_algebra).AccessorPolicy
: The accessor policy specifies how the view accesses the data pointer. This can be useful for optimizations.OwningPolicy
: The owning policy specifies the ownership of the data pointer and who is responsible for handling the memory resources (see Handles).In contrast to regular arrays (see nda::basic_array), views do not own the data they point to. They are a fast and efficient way to access and manipulate already existing data:
Output:
Views are usually created by taking a slice of a regular nda::basic_array or another view. In the example above, arr(nda::range::all, 0)
creates a view of the first column of the regular array arr
, which is then set to zero. A view of the full array can be created with arr()
.
ValueType | Type stored in the array. |
Rank | Number of dimensions of the view. |
LayoutPolicy | Policy determining the memory layout. |
Algebra | Algebra of the view. |
AccessorPolicy | Policy determining how the data pointer is accessed. |
OwningPolicy | Policy determining the ownership of the data. |
Definition at line 127 of file basic_array_view.hpp.
Public Types | |
using | accessor_policy_t = AccessorPolicy |
Type of the accessor policy (see e.g. nda::default_accessor). | |
using | const_iterator = array_iterator<iterator_rank, ValueType const, typename AccessorPolicy::template accessor<ValueType>::pointer> |
Const iterator type of the view/array. | |
using | iterator = array_iterator<iterator_rank, ValueType, typename AccessorPolicy::template accessor<ValueType>::pointer> |
Iterator type of the view/array. | |
using | layout_policy_t = LayoutPolicy |
Type of the memory layout policy (see Layout policies). | |
using | layout_t = typename LayoutPolicy::template mapping<Rank> |
Type of the memory layout (an nda::idx_map). | |
using | owning_policy_t = OwningPolicy |
Type of the owning policy (see Memory policies). | |
using | regular_type = basic_array<std::remove_const_t<ValueType>, Rank, C_layout, Algebra, heap<mem::get_addr_space<storage_t>>> |
The associated regular (nda::basic_array) type. | |
using | storage_t = typename OwningPolicy::template handle<ValueType> |
Type of the memory handle (see Handles). | |
using | value_type = ValueType |
Type of the values in the view (might be const). | |
Public Member Functions | |
basic_array_view ()=default | |
Default constructor constructs an empty view with a default constructed memory handle and layout. | |
template<MemoryArrayOfRank< Rank > A> requires ((get_layout_info<A>.stride_order == layout_t::stride_order_encoded) and (std::is_same_v<std::remove_const_t<ValueType>, get_value_t<A>>) and (std::is_const_v<ValueType> or !std::is_const_v<typename std::decay_t<A>::value_type>)) | |
basic_array_view (A &&a) noexcept | |
Generic constructor from any nda::MemoryArray type. | |
basic_array_view (basic_array_view &&)=default | |
Default move constructor moves the memory handle and layout. | |
basic_array_view (basic_array_view const &)=default | |
Default copy constructor copies the memory handle and layout. | |
basic_array_view (layout_t const &idxm, storage_t st) | |
Construct a view from a given layout and memory handle. | |
basic_array_view (layout_t const &idxm, ValueType *p) noexcept | |
Construct a view from a bare pointer to some contiguous data and a memory layout. | |
template<std::ranges::contiguous_range R> requires (Rank == 1 and not MemoryArray<R> and (std::is_same_v<std::ranges::range_value_t<R>, ValueType> or std::is_same_v<const std::ranges::range_value_t<R>, ValueType>)) | |
basic_array_view (R &rg) noexcept | |
Construct a 1-dimensional view of a general contiguous range. | |
basic_array_view (std::array< long, Rank > const &shape, ValueType *p) noexcept | |
Construct a view from a bare pointer to some contiguous data and a shape. | |
template<size_t N> requires (Rank == 1 and std::is_const_v<ValueType>) | |
basic_array_view (std::array< std::remove_const_t< ValueType >, N > const &a) noexcept | |
Construct a 1-dimensional view of a std::array. | |
template<size_t N> requires (Rank == 1) | |
basic_array_view (std::array< ValueType, N > &a) noexcept | |
Construct a 1-dimensional view of a std::array. | |
auto | as_array_view () |
auto | as_array_view () const |
const_iterator | begin () const noexcept |
Get a const iterator to the beginning of the view/array. | |
iterator | begin () noexcept |
Get an iterator to the beginning of the view/array. | |
const_iterator | cbegin () const noexcept |
Get a const iterator to the beginning of the view/array. | |
const_iterator | cend () const noexcept |
Get a const iterator to the end of the view/array. | |
ValueType const * | data () const noexcept |
Get a pointer to the actual data (in general this is not the beginning of the memory block for a view). | |
ValueType * | data () noexcept |
Get a pointer to the actual data (in general this is not the beginning of thr memory block for a view). | |
bool | empty () const |
Is the view/array empty? | |
const_iterator | end () const noexcept |
Get a const iterator to the end of the view/array. | |
iterator | end () noexcept |
Get an iterator to the end of the view/array. | |
long | extent (int i) const noexcept |
Get the extent of the ith dimension. | |
constexpr auto const & | indexmap () const noexcept |
Get the memory layout of the view/array. | |
auto | indices () const noexcept |
Get a range that generates all valid index tuples. | |
long | is_contiguous () const noexcept |
Is the memory layout of the view/array contiguous? | |
bool | is_empty () const noexcept |
decltype(auto) | operator() (_linear_index_t idx) const noexcept |
Access the element of the view/array at the given nda::_linear_index_t. | |
decltype(auto) | operator() (_linear_index_t idx) noexcept |
Non-const overload of nda::basic_array_view::operator()(_linear_index_t) const. | |
template<typename... Ts> | |
__inline__ decltype(auto) | operator() (Ts const &...idxs) &&noexcept(has_no_boundcheck) |
Rvalue overload of nda::basic_array_view::operator()(Ts const &...) const & . | |
template<typename... Ts> | |
__inline__ decltype(auto) | operator() (Ts const &...idxs) &noexcept(has_no_boundcheck) |
Non-const overload of nda::basic_array_view::operator()(Ts const &...) const & . | |
template<typename... Ts> | |
__inline__ decltype(auto) | operator() (Ts const &...idxs) const &noexcept(has_no_boundcheck) |
Function call operator to access the view/array. | |
template<typename RHS > | |
auto & | operator*= (RHS const &rhs) noexcept |
Multiplication assignment operator. | |
template<typename RHS > | |
auto & | operator+= (RHS const &rhs) noexcept |
Addition assignment operator. | |
template<typename RHS > | |
auto & | operator-= (RHS const &rhs) noexcept |
Subtraction assignment operator. | |
template<typename RHS > | |
auto & | operator/= (RHS const &rhs) noexcept |
Division assignment operator. | |
basic_array_view & | operator= (basic_array_view const &rhs) noexcept |
Copy assignment operator makes a deep copy of the contents of the view. | |
template<ArrayInitializer< basic_array_view > Initializer> | |
basic_array_view & | operator= (Initializer const &initializer) noexcept |
Assignment operator uses an nda::ArrayInitializer to assign to the view. | |
template<std::ranges::contiguous_range R> requires (Rank == 1 and not MemoryArray<R>) | |
auto & | operator= (R const &rhs) noexcept |
Assignment operator makes a deep copy of a general contiguous range and assigns it to the 1-dimensional view/array. | |
template<ArrayOfRank< Rank > RHS> | |
basic_array_view & | operator= (RHS const &rhs) noexcept |
Assignment operator makes a deep copy of the contents of an nda::ArrayOfRank object. | |
template<typename RHS > requires (is_scalar_for_v<RHS, basic_array_view>) | |
basic_array_view & | operator= (RHS const &rhs) noexcept |
Assignment operator assigns a scalar to the view. | |
template<typename T > | |
decltype(auto) | operator[] (T const &idx) const &noexcept(has_no_boundcheck) |
Subscript operator to access the 1-dimensional view/array. | |
template<typename T > | |
decltype(auto) | operator[] (T const &x) &&noexcept(has_no_boundcheck) |
Rvalue overload of nda::basic_array_view::operator[](T const &) const & . | |
template<typename T > | |
decltype(auto) | operator[] (T const &x) &noexcept(has_no_boundcheck) |
Non-const overload of nda::basic_array_view::operator[](T const &) const & . | |
template<typename T , int R, typename LP , char A, typename AP , typename OP > | |
void | rebind (basic_array_view< T, R, LP, A, AP, OP > v) noexcept |
Rebind the current view to another view. | |
auto const & | shape () const noexcept |
Get the shape of the view/array. | |
long | shape (int i) const noexcept |
long | size () const noexcept |
Get the total size of the view/array. | |
storage_t | storage () &&noexcept |
Get the data storage of the view/array. | |
storage_t & | storage () &noexcept |
Get the data storage of the view/array. | |
storage_t const & | storage () const &noexcept |
Get the data storage of the view/array. | |
constexpr auto | stride_order () const noexcept |
Get the stride order of the memory layout of the view/array (see nda::idx_map for more details on how we define stride orders). | |
auto const & | strides () const noexcept |
Get the strides of the view/array (see nda::idx_map for more details on how we define strides). | |
Static Public Member Functions | |
template<char ResultAlgebra, bool SelfIsRvalue, typename Self , typename... Ts> | |
static __inline__ decltype(auto) | call (Self &&self, Ts const &...idxs) noexcept(has_no_boundcheck) |
Implementation of the function call operator. | |
static constexpr bool | is_stride_order_C () noexcept |
Is the stride order of the view/array in C-order? | |
static constexpr bool | is_stride_order_Fortran () noexcept |
Is the stride order of the view/array in Fortran-order? | |
Static Public Attributes | |
static constexpr int | iterator_rank = (has_strided_1d(layout_t::layout_prop) ? 1 : Rank) |
Rank of the nda::array_iterator for the view/array. | |
static constexpr int | rank = Rank |
Number of dimensions of the view. | |
Friends | |
void | deep_swap (basic_array_view a, basic_array_view b) noexcept |
Swap two views by swapping their data. | |
void | swap (basic_array_view &a, basic_array_view &b) noexcept |
Swap two views by swapping their memory handles and layouts. | |
|
inline |
Construct a view from a given layout and memory handle.
idxm | Layout of the view. |
st | Memory handle of the view. |
Definition at line 197 of file basic_array_view.hpp.
|
inlineexplicitnoexcept |
Generic constructor from any nda::MemoryArray type.
It simply copies the memory layout and initializes the memory handle with the handle of the given nda::MemoryArray object.
A | nda::MemoryArray type of the same rank as the view. |
a | nda::MemoryArray object. |
Definition at line 232 of file basic_array_view.hpp.
|
inlinenoexcept |
Construct a view from a bare pointer to some contiguous data and a shape.
shape | Shape of the view. |
p | Pointer to the data. |
Definition at line 244 of file basic_array_view.hpp.
|
inlinenoexcept |
Construct a view from a bare pointer to some contiguous data and a memory layout.
idxm | Layout of the view. |
p | Pointer to the data. |
Definition at line 255 of file basic_array_view.hpp.
|
inlineexplicitnoexcept |
Construct a 1-dimensional view of a std::array.
N | Size of the std::array. |
a | Reference to a std::array object. |
Definition at line 265 of file basic_array_view.hpp.
|
inlineexplicitnoexcept |
Construct a 1-dimensional view of a std::array.
N | Size of the std::array. |
a | Const reference to a std::array object. |
Definition at line 275 of file basic_array_view.hpp.
|
inlineexplicitnoexcept |
Construct a 1-dimensional view of a general contiguous range.
R | Type of the range. |
rg | Range object. |
Definition at line 286 of file basic_array_view.hpp.
|
inline |
Definition at line 202 of file basic_array_view.hpp.
|
inline |
Definition at line 205 of file basic_array_view.hpp.
|
inlinestaticnoexcept |
Implementation of the function call operator.
This function is an implementation detail an should be private. Since the Green's function library in TRIQS uses this function, it is kept public (for now).
ResultAlgebra | Algebra of the resulting view/array. |
SelfIsRvalue | True if the view/array is an rvalue. |
Self | Type of the calling view/array. |
T | Types of the arguments. |
self | Calling view. |
idxs | Multi-dimensional index consisting of long , nda::range , nda::range::all_t , nda::ellipsis or lazy arguments. |
Definition at line 594 of file basic_array_view.hpp.
|
inlinenodiscardnoexcept |
Get a pointer to the actual data (in general this is not the beginning of the memory block for a view).
Definition at line 464 of file basic_array_view.hpp.
|
inlinenodiscardnoexcept |
Get a pointer to the actual data (in general this is not the beginning of thr memory block for a view).
Definition at line 470 of file basic_array_view.hpp.
|
inlinenodiscard |
Is the view/array empty?
Definition at line 500 of file basic_array_view.hpp.
|
inlinenodiscardnoexcept |
Get the extent of the ith dimension.
Definition at line 509 of file basic_array_view.hpp.
|
inlinenodiscardconstexprnoexcept |
Get the memory layout of the view/array.
Definition at line 432 of file basic_array_view.hpp.
|
inlinenodiscardnoexcept |
Get a range that generates all valid index tuples.
itertools::multiplied
range that can be used to iterate over all valid index tuples. Definition at line 526 of file basic_array_view.hpp.
|
inlinenodiscardnoexcept |
Is the memory layout of the view/array contiguous?
Definition at line 494 of file basic_array_view.hpp.
|
inlinenodiscardnoexcept |
Definition at line 503 of file basic_array_view.hpp.
|
inlinestaticconstexprnoexcept |
Is the stride order of the view/array in C-order?
Definition at line 532 of file basic_array_view.hpp.
|
inlinestaticconstexprnoexcept |
Is the stride order of the view/array in Fortran-order?
Definition at line 538 of file basic_array_view.hpp.
|
inlinenoexcept |
Access the element of the view/array at the given nda::_linear_index_t.
The linear index specifies the position of the element in the view/array and not the position of the element w.r.t. to the data pointer (i.e. any possible strides should not be taken into account).
idx | nda::_linear_index_t object. |
Definition at line 549 of file basic_array_view.hpp.
|
inlinenoexcept |
Function call operator to access the view/array.
Depending on the type of the calling object and the given arguments, this function call does the following:
long
, a single element is accessed:Ts | Types of the function arguments. |
idxs | Multi-dimensional index consisting of long , nda::range , nda::range::all_t , nda::ellipsis or lazy arguments. |
Definition at line 659 of file basic_array_view.hpp.
|
inlinenoexcept |
Multiplication assignment operator.
It first performs the (lazy) multiplication with the right hand side operand and then assigns the result to the left hand side view/array.
See nda::operator*(L &&, R &&) and nda::operator*(A &&, S &&) for more details.
RHS | nda::Scalar or nda::Array type. |
rhs | Right hand side operand of the multiplication assignment operation. |
Definition at line 815 of file basic_array_view.hpp.
|
inlinenoexcept |
Addition assignment operator.
It first performs the (lazy) addition with the right hand side operand and then assigns the result to the left hand side view/array.
See nda::operator+(L &&, R &&) and nda::operator+(A &&, S &&) for more details.
RHS | nda::Scalar or nda::Array type. |
rhs | Right hand side operand of the addition assignment operation. |
Definition at line 779 of file basic_array_view.hpp.
|
inlinenoexcept |
Subtraction assignment operator.
It first performs the (lazy) subtraction with the right hand side operand and then assigns the result to the left hand side view/array.
See nda::operator-(L &&, R &&) and nda::operator-(A &&, S &&) for more details.
RHS | nda::Scalar or nda::Array type. |
rhs | Right hand side operand of the subtraction assignment operation. |
Definition at line 797 of file basic_array_view.hpp.
|
inlinenoexcept |
Division assignment operator.
It first performs the (lazy) division with the right hand side operand and then assigns the result to the left hand side view/array.
See nda::operator/(L &&, R &&) and nda::operator/(A &&, S &&) for more details.
RHS | nda::Scalar or nda::Array type. |
rhs | Right hand side operand of the division assignment operation. |
Definition at line 833 of file basic_array_view.hpp.
|
inlinenoexcept |
Copy assignment operator makes a deep copy of the contents of the view.
The dimension of the right hand side must be large enough or the behaviour is undefined. If NDA_ENFORCE_BOUNDCHECK
is defined, bounds checking is enabled.
rhs | Right hand side of the assignment operation. |
Definition at line 296 of file basic_array_view.hpp.
|
inlinenoexcept |
Assignment operator uses an nda::ArrayInitializer to assign to the view.
The shape of the view is expected to be the same as the shape of the initializer.
Initializer | nda::ArrayInitializer type. |
initializer | Initializer object. |
Definition at line 345 of file basic_array_view.hpp.
|
inlinenoexcept |
Assignment operator makes a deep copy of a general contiguous range and assigns it to the 1-dimensional view/array.
R | Range type. |
rhs | Right hand side range object. |
Definition at line 847 of file basic_array_view.hpp.
|
inlinenoexcept |
Assignment operator makes a deep copy of the contents of an nda::ArrayOfRank object.
The dimension of the right hand side must be large enough or the behaviour is undefined. If NDA_ENFORCE_BOUNDCHECK
is defined, bounds checking is enabled.
RHS | nda::ArrayOfRank type with the same rank as the view. |
rhs | Right hand side of the assignment operation. |
Definition at line 311 of file basic_array_view.hpp.
|
inlinenoexcept |
Assignment operator assigns a scalar to the view.
The behavior depends on the algebra of the view:
RHS | Type of the scalar. |
rhs | Right hand side of the assignment operation. |
Definition at line 328 of file basic_array_view.hpp.
|
inlinenoexcept |
Subscript operator to access the 1-dimensional view/array.
Depending on the type of the calling object and the given argument, this subscript operation does the following:
long
, a single element is accessed:T | Type of the argument. |
idx | 1-dimensional index that is either a long , nda::range , nda::range::all_t , nda::ellipsis or a lazy argument. |
Definition at line 699 of file basic_array_view.hpp.
|
inlinenoexcept |
Rebind the current view to another view.
It simply copies the layout and memory handle of the other view.
T | Value type of the other view. |
R | Rank of the other view. |
LP | Layout policy of the other view. |
A | Algebra of the other view. |
AP | Accessor policy of the other view. |
OP | Owning policy of the other view. |
v | Other view. |
Definition at line 366 of file basic_array_view.hpp.
|
inlinenodiscardnoexcept |
Get the shape of the view/array.
std::array<long, Rank>
object specifying the shape of the view/array. Definition at line 476 of file basic_array_view.hpp.
|
inlinenodiscardnoexcept |
extent(i)
or shape()[i]
instead. Definition at line 520 of file basic_array_view.hpp.
|
inlinenodiscardnoexcept |
Get the total size of the view/array.
Definition at line 488 of file basic_array_view.hpp.
|
inlinenodiscardnoexcept |
Get the data storage of the view/array.
Definition at line 450 of file basic_array_view.hpp.
|
inlinenodiscardnoexcept |
Get the data storage of the view/array.
Definition at line 444 of file basic_array_view.hpp.
|
inlinenodiscardnoexcept |
Get the data storage of the view/array.
Definition at line 438 of file basic_array_view.hpp.
|
inlinenodiscardconstexprnoexcept |
Get the stride order of the memory layout of the view/array (see nda::idx_map for more details on how we define stride orders).
std::array<int, Rank>
object specifying the stride order. Definition at line 458 of file basic_array_view.hpp.
|
inlinenodiscardnoexcept |
Get the strides of the view/array (see nda::idx_map for more details on how we define strides).
std::array<long, Rank>
object specifying the strides of the view/array. Definition at line 482 of file basic_array_view.hpp.
|
friend |
Swap two views by swapping their data.
This modifies the data the views point to.
a | First view. |
b | Second view. |
Definition at line 405 of file basic_array_view.hpp.
|
friend |
Swap two views by swapping their memory handles and layouts.
This does not modify the data the views point to.
a | First view. |
b | Second view. |
Definition at line 392 of file basic_array_view.hpp.