1# Vector {#dsppp_vector}
2
3The use of vectors has been explained in @ref dsppp_vector_example "example with vector operations" and focusing on `float32_t`.
4
5The vector template is defined as:
6
7```cpp
8template<typename P,
9         int L=DYNAMIC,
10         template<int> typename Allocator = TMP_ALLOC>
11struct Vector:Vector_Base<P>
12```
13
14* `P` is the datatype of vector elements
15* `L` is the static length of the vector (length known at build time). `L<0` when the length is dynamic and not known at build time. It is the default value.
16* `Allocator` is the memory allocator. By default it is `TMP_ALLOC` that you can redefine since it is a macro
17* `Vector_Base<P>` is providing the storage. A vector owns its storage buffer.
18
19## Q15 example
20
21Example with `Q15` is very similar:
22
23The vectors are defined:
24
25```cpp
26Vector<Q15,NB> aQ15;
27Vector<Q15,NB> bQ15;
28Vector<Q15,NB> cQ15;
29```
30
31They are initialized:
32
33```cpp
34for(int i = 0;i< NB;i++)
35{
36   aQ15[i] = bQ15[i] = cQ15[i] = Q15(i);
37}
38```
39
40Here, the `Q15` value is initialized from the int value `i` and thus represents \f$ i/2^{15} \f$
41
42Some computation is done
43
44```cpp
45Vector<Q15,NB> dQ15 = aQ15 + bQ15 * cQ15;
46```
47
48The result is displayed:
49
50```cpp
51std::cout << "Result = " << dQ15 ;
52```
53
54## VectorView
55
56A vector view is a virtual vector : a view of a vector.
57
58One can define a `VectorView` with:
59
60```cpp
61auto subD = d.sub(2);
62```
63
64This is creating a virtual vector starting at index `2` (3rd element) of vector `d`.
65
66You can then operate with this virtual vector:
67
68```cpp
69subD = subD + 2.0f;
70```
71
72If you display the vector `d`, you'll see that `2.0f` has been added to all elements starting from the 2rd one.
73
74`VectorView` do not own their memory. It is owned by the original vector.
75
76If you write:
77
78```cpp
79x = y
80```
81
82and `x` and `y` are `VectorView`, no copy will occur. `x` will just reference the same data as `y`. If you want to copy you have to be explicit and write:
83
84```cpp
85x = copy(y)
86```
87
88It is advised to always use the `copy` operator (even with normal vectors).
89
90Virtual vectors can have a stride:
91
92```cpp
93d.sub<2>(1) = 0.0f;
94```
95
96This line sets the odd elements of the vector to `0.0f`. It is creating a virtual vector with stride `2` and starting at index `1` of first vector.
97
98Then, all elements of this virtual vector are set to `0.0f`.
99
100The `sub` API is:
101
102```cpp
103template<int S=1>
104VectorView<P,S> sub(const index_t start=0,const index_t stop=L)
105```
106
107You can define:
108
109* The stride `S` : statically known and by default `1`.
110* The start of the view (`0` by default)
111* The end of the view (`L` by default : the length known at build time). Note that it is the first index **after** the end of the vector.
112
113