AMDiS 2.10
The Adaptive Multi-Dimensional Simulation Toolbox
Transposed.hpp
1#pragma once
2
3#include <cassert>
4#include <type_traits>
5#include <amdis/common/TypeTraits.hpp>
6#include <dune/common/typeutilities.hh>
7
8namespace AMDiS
9{
11 template <class Matrix>
13 {
14 using RawMatrix = remove_cvref_t<Matrix>;
15
16 public:
17 using size_type = typename RawMatrix::size_type;
18 using value_type = typename RawMatrix::value_type;
19
20 private:
21 struct ConstRowProxy
22 {
23 RawMatrix const* mat;
24 size_type row;
25
26 value_type const& operator[](size_type col) const
27 {
28 return (*mat)[col][row];
29 }
30 };
31
32 struct MutableRowProxy
33 {
34 RawMatrix* mat;
35 size_type row;
36
37 value_type& operator[](size_type col)
38 {
39 return (*mat)[col][row];
40 }
41 };
42
43 public:
44 template <class M, Dune::disableCopyMove<TransposedMatrix,M> = 0>
45 TransposedMatrix(M&& matrix)
46 : matrix_(FWD(matrix))
47 {}
48
49 ConstRowProxy operator[](size_type row) const
50 {
51 return ConstRowProxy{&matrix_, row};
52 }
53
54 template <class M = Matrix,
55 std::enable_if_t<not std::is_const_v<M>, int> = 0>
56 MutableRowProxy operator[](size_type row)
57 {
58 return MutableRowProxy{&matrix_, row};
59 }
60
61 size_type N() const
62 {
63 return matrix_.M();
64 }
65
66 size_type M() const
67 {
68 return matrix_.N();
69 }
70
71 template <class Mat>
72 TransposedMatrix& operator+=(Mat const& mat)
73 {
74 assert(mat.N() == N());
75 assert(mat.M() == M());
76 for (size_type i = 0; i < N(); ++i)
77 for (size_type j = 0; j < M(); ++j)
78 (*this)[i][j] += mat[i][j];
79
80 return *this;
81 }
82
83 private:
84 Matrix& matrix_;
85 };
86
87 template <class Matrix>
88 auto transposed(Matrix&& matrix)
89 {
90 using M = std::remove_reference_t<Matrix>;
91 return TransposedMatrix<M>{FWD(matrix)};
92 }
93
94} // end namespace AMDiS
The transposed view onto a matrix.
Definition: Transposed.hpp:13