AMDiS 2.10
The Adaptive Multi-Dimensional Simulation Toolbox
FlatMatrix.hpp
1#pragma once
2
3#include <type_traits>
4#include <vector>
5
6namespace AMDiS
7{
9 template <class T, class Allocator = std::allocator<T>>
11 : public std::vector<T,Allocator>
12 {
13 using Super = std::vector<T,Allocator>;
14
15 public:
16 using value_type = T;
17 using size_type = typename Super::size_type;
18 using reference = typename Super::reference;
19 using const_reference = typename Super::const_reference;
20
21 private:
23 template <class Vector>
24 struct AccessProxy
25 {
28 template <class V = Vector, std::enable_if_t<not std::is_const_v<V>,int> = 0>
29 reference operator[](size_type col)
30 {
31 return (*vector_)[row_*cols_ + col];
32 }
33
34 const_reference operator[](size_type col) const
35 {
36 return (*vector_)[row_*cols_ + col];
37 }
38 // @}
39
40 Vector* vector_;
41 size_type row_;
42 size_type cols_;
43 };
44
45 public:
47 FlatMatrix() = default;
48
51 FlatMatrix(size_type r, size_type c, value_type v = {})
52 : Super(r*c, v)
53 , rows_(r)
54 , cols_(c)
55 {}
56
58 FlatMatrix& operator=(value_type s)
59 {
60 Super::assign(Super::size(), s);
61 return *this;
62 }
63
65
70 void resize(size_type r, size_type c, value_type v = {})
71 {
72 Super::resize(r*c, v);
73 rows_ = r;
74 cols_ = c;
75 }
76
78 size_type rows() const noexcept
79 {
80 return rows_;
81 }
82
84 size_type cols() const noexcept
85 {
86 return cols_;
87 }
88
90 using Super::size;
91
94 AccessProxy<Super> operator[](size_type row)
95 {
96 return AccessProxy<Super>{static_cast<Super*>(this), row, cols_};
97 }
98
99 AccessProxy<Super const> operator[](size_type row) const
100 {
101 return AccessProxy<Super const>{static_cast<Super const*>(this), row, cols_};
102 }
104
105 private:
106 size_type rows_ = 0;
107 size_type cols_ = 0;
108 };
109
110} // end namespace AMDiS
Dense matrix with row-wise storage in flat data vector.
Definition: FlatMatrix.hpp:12
FlatMatrix(size_type r, size_type c, value_type v={})
Definition: FlatMatrix.hpp:51
FlatMatrix()=default
Default constructor, creates an empty 0x0 matrix.
size_type cols() const noexcept
Return the number of columns of the matrix.
Definition: FlatMatrix.hpp:84
AccessProxy< Super > operator[](size_type row)
Definition: FlatMatrix.hpp:94
void resize(size_type r, size_type c, value_type v={})
Resizes the container to contain r x c elements.
Definition: FlatMatrix.hpp:70
FlatMatrix & operator=(value_type s)
Assign value s to all entries of the matrix.
Definition: FlatMatrix.hpp:58
size_type rows() const noexcept
Return the number of rows of the matrix.
Definition: FlatMatrix.hpp:78