-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStridedVector.hpp
More file actions
48 lines (41 loc) · 1.49 KB
/
Copy pathStridedVector.hpp
File metadata and controls
48 lines (41 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <vector>
#include <iostream>
// class for strided access with an offset of the underlying vector
template <typename T>
class StridedVector
{
private:
std::vector<T>& v;
int const stride;
int const offset;
size_t const strided_size;
public:
T& operator[](int i)
{
assert( 0 <= i and i < static_cast<int>(strided_size) );
return v[offset + i * stride];
};
T const& operator[](int i) const
{
assert( 0 <= i and i < static_cast<int>(strided_size) );
return v[offset + i * stride];
};
size_t size() const
{
return strided_size;
};
explicit StridedVector(std::vector<T>& v) : v{v}, stride{1}, offset{0}, strided_size{v.size()} {};
explicit StridedVector(std::vector<T>& v, int const stride, int const offset, int const size) : v{v}, stride{stride}, offset{offset}, strided_size(size)
{
assert(0 <= offset and offset < v.size() and stride > 0);
};
explicit StridedVector(StridedVector<T>& sv, int const stride, int const offset, int const size) : v{sv.v}, stride{sv.stride * stride}, offset{sv.offset + offset * sv.stride}, strided_size(size)
{
assert( 0 <= offset and offset < static_cast<int>(v.size()) and stride > 0 );
};
StridedVector(StridedVector const &sv) = default;
StridedVector(StridedVector &&) = default;
StridedVector &operator=(StridedVector const &) = default;
StridedVector &operator=(StridedVector &&) = default;
~StridedVector() = default;
};