![]() |
Blaze 3.9
|
Element selections provide views on arbitrary compositions of elements of dense and sparse vectors. These views act as a reference to the selected elements and represent them as another dense or sparse vector. This reference is valid and can be used in every way any other dense or sparse vector can be used as long as the vector containing the elements is not resized or entirely destroyed. The element selection also acts as an alias to the vector elements in the specified range: Changes made to the elements (e.g. modifying values, inserting or erasing elements) are immediately visible in the vector and changes made via the vector are immediately visible in the elements.
An element selection can be created very conveniently via the elements()
function. It can be included via the header files
and forward declared via the header file
The indices of the elements to be selected can be specified either at compile time or at runtime (by means of an initializer list, array or vector):
Note that it is possible to alias the elements of the underlying vector in any order. Also note that it is possible to use the same index multiple times.
Alternatively it is possible to pass a callable such as a lambda or functor that produces the indices:
The elements()
function returns an expression representing the view on the selected elements. The type of this expression depends on the given arguments, primarily the type of the vector and the compile time arguments. If the type is required, it can be determined via the decltype
specifier:
The resulting view can be treated as any other dense or sparse vector, i.e. it can be assigned to, it can be copied from, and it can be used in arithmetic operations. An element selection created from a row vector can be used as any other row vector, an element selection created from a column vector can be used as any other column vector. The view can also be used on both sides of an assignment: It can either be used as an alias to grant write access to specific elements of a vector primitive on the left-hand side of an assignment or to grant read-access to specific elements of a vector primitive or expression on the right-hand side of an assignment. The following example demonstrates this in detail:
Please note that using an element selection, which refers to an index multiple times, on the left-hand side of an assignment leads to undefined behavior:
In this example both vectors have the same size, which results in a correct vector assignment, but the final value of the element at index 1 is unspecified.
The elements of an element selection can be directly accessed via the subscript operator:
The numbering of the selected elements is
\f[\left(\begin{array}{*{5}{c}} 0 & 1 & 2 & \cdots & N-1 \\ \end{array}\right),\f]
where N is the number of selected elements. Alternatively, the elements of an element selection can be traversed via iterators. Just as with vectors, in case of non-const element selections, begin()
and end()
return an iterator, which allows to manipulate the elements, in case of constant element selections an iterator to immutable elements is returned:
Inserting/accessing elements in a sparse element selection can be done by several alternative functions. The following example demonstrates all options:
An element selection can be used like any other dense or sparse vector. For instance, the number of selected elements can be obtained via the size()
function, the current capacity via the capacity()
function, and the number of non-zero elements via the nonZeros()
function. However, since element selections are references to a specific range of a vector, several operations are not possible, such as resizing and swapping. The following example shows this by means of an element selection on a dense vector:
Both dense and sparse element selections can be used in all arithmetic operations that any other dense or sparse vector can be used in. The following example gives an impression of the use of dense element selections within arithmetic operations. All operations (addition, subtraction, multiplication, scaling, ...) can be performed on all possible combinations of dense and sparse element selections with fitting element types:
Previous: Subvectors Next: Submatrices