![]() |
View on a specific subvector of a dense or sparse vector.The Subvector class template represents a view on a specific subvector of a dense or sparse vector primitive. The type of the vector is specified via the first template parameter: More...
#include <BaseTemplate.h>
View on a specific subvector of a dense or sparse vector.
The Subvector class template represents a view on a specific subvector of a dense or sparse vector primitive. The type of the vector is specified via the first template parameter:
A view on a dense or sparse subvector can be created very conveniently via the subvector()
function:
This 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. The view can also be used on both sides of an assignment: The subvector can either be used as an alias to grant write access to a specific subvector of a vector primitive on the left-hand side of an assignment or to grant read-access to a specific subvector of a vector primitive or expression on the right-hand side of an assignment. The following example demonstrates this in detail:
A subvector can be used like any other dense or sparse vector. For instance, the elements of the subvector can be directly accessed with the subscript operator.
The numbering of the subvector elements is
where N is the specified size of the subvector. Alternatively, the elements of a subvector can be traversed via iterators. Just as with vectors, in case of non-const subvectors, begin()
and end()
return an Iterator, which allows a manipulation of the non-zero values, in case of constant subvectors a ConstIterator is returned:
Inserting/accessing elements in a sparse subvector can be done by several alternative functions. The following example demonstrates all options:
The current number of subvector 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 subvector are views on a specific subvector of a vector, several operations are not possible on views, such as resizing and swapping:
The following example gives an impression of the use of Subvector within arithmetic operations. All operations (addition, subtraction, multiplication, scaling, ...) can be performed on all possible combinations of dense and sparse vectors with fitting element types:
Usually subvectors can be defined anywhere within a vector. They may start at any position and may have an arbitrary size (only restricted by the size of the underlying vector). However, in contrast to vectors themselves, which are always properly aligned in memory and therefore can provide maximum performance, this means that subvectors in general have to be considered to be unaligned. This can be made explicit by the blaze::unaligned flag:
All of these calls to the subvector()
function are identical. Whether the alignment flag is explicitly specified or not, it always returns an unaligned subvector. Whereas this may provide full flexibility in the creation of subvectors, this might result in performance restrictions (even in case the specified subvector could be aligned). However, it is also possible to create aligned subvectors. Aligned subvectors are identical to unaligned subvectors in all aspects, except that they may pose additional alignment restrictions and therefore have less flexibility during creation, but don't suffer from performance penalties and provide the same performance as the underlying vector. Aligned subvectors are created by explicitly specifying the blaze::aligned flag:
The alignment restrictions refer to system dependent address restrictions for the used element type and the available vectorization mode (SSE, AVX, ...). The following source code gives some examples for a double precision dense vector, assuming that AVX is available, which packs 4 double
values into a SIMD vector:
Note that the discussed alignment restrictions are only valid for aligned dense subvectors. In contrast, aligned sparse subvectors at this time don't pose any additional restrictions. Therefore aligned and unaligned sparse subvectors are truly fully identical. Still, in case the blaze::aligned flag is specified during setup, an aligned subvector is created:
It is also possible to create a subvector view on another subvector. In this context it is important to remember that the type returned by the subvector()
function is the same type as the type of the given subvector, since the view on a subvector is just another view on the underlying vector: