Xped
Loading...
Searching...
No Matches
TMatrix.hpp
Go to the documentation of this file.
1#ifndef XPED_TMATRIX_H_
2#define XPED_TMATRIX_H_
3
4#include "yas/serialize.hpp"
5#include "yas/std_types.hpp"
6
8
9namespace Xped {
10
11template <typename Ttype>
12struct TMatrix
13{
14 TMatrix() = default;
15
16 explicit TMatrix(const Pattern& pat, const std::string name = "")
17 : pat(pat)
18 , name(name)
19 {
20 tensors.resize(pat.uniqueSize());
21 is_changed.resize(pat.uniqueSize());
22 std::fill(is_changed.begin(), is_changed.end(), false);
23 }
24
25 template <typename OtherTtype>
27 {
28 pat = other.pat;
29 is_changed.resize(pat.uniqueSize());
30 std::fill(is_changed.begin(), is_changed.end(), false);
31 tensors.resize(other.size());
32 for(auto it = other.cbegin(); it != other.cend(); ++it) { tensors[std::distance(other.cbegin(), it)] = *it; }
33 }
34
35 inline std::size_t rows() const { return pat.Lx; }
36 inline std::size_t cols() const { return pat.Ly; }
37
38 inline std::size_t size() const { return tensors.size(); }
39
40 inline Ttype& operator()(const int row, const int col)
41 {
42 is_changed[pat.uniqueIndex(row, col)] = true;
43 return tensors[pat.uniqueIndex(row, col)];
44 }
45 inline const Ttype& operator()(const int row, const int col) const { return tensors[pat.uniqueIndex(row, col)]; }
46
47 inline Ttype& operator[](const std::size_t index)
48 {
49 is_changed[index] = true;
50 return tensors[index];
51 }
52 inline const Ttype& operator[](const std::size_t index) const { return tensors[index]; }
53 inline const Ttype& at(const std::size_t index) const { return tensors.at(index); }
54
55 inline bool isChanged(const int row, const int col) const { return is_changed[pat.uniqueIndex(row, col)]; }
56 inline void resetChange() { std::fill(is_changed.begin(), is_changed.end(), false); }
57
58 inline void resize(const Pattern& pattern)
59 {
60 pat = pattern;
61 tensors.clear();
62 tensors.resize(pat.uniqueSize());
63 is_changed.resize(pat.uniqueSize());
64 std::fill(is_changed.begin(), is_changed.end(), false);
65 }
66
67 auto begin() { return tensors.begin(); }
68 auto end() { return tensors.end(); }
69
70 auto cbegin() const { return tensors.cbegin(); }
71 auto cend() const { return tensors.cend(); }
72
73 void fill(const std::vector<Ttype>& tensors_in) { tensors = tensors_in; }
74
75 void setConstant(const Ttype& val) { std::fill(tensors.begin(), tensors.end(), val); }
76
77 Ttype sum() const { return std::accumulate(tensors.begin(), tensors.end(), Ttype(0.)); }
78
79 template <typename Ar>
80 void serialize(Ar& ar)
81 {
82 ar& YAS_OBJECT_NVP("TMatrix", ("pattern", pat), ("tensors", tensors), ("is_changed", is_changed), ("name", name));
83 }
84
85 std::vector<Ttype> uncompressedVector() const
86 {
87 std::vector<Ttype> out(pat.size());
88 for(int x = 0; x < pat.Lx; ++x) {
89 for(int y = 0; y < pat.Ly; ++y) { out[x + y * pat.Lx] = tensors[pat.uniqueIndex(x, y)]; }
90 }
91 return out;
92 }
94
95private:
96 std::vector<Ttype> tensors;
97 std::vector<bool> is_changed;
98 std::string name = "";
99};
100
101} // namespace Xped
102#endif
Definition: bench.cpp:62
Definition: Pattern.hpp:18
std::size_t Ly
Definition: Pattern.hpp:72
std::size_t Lx
Definition: Pattern.hpp:72
std::size_t uniqueSize() const
Definition: Pattern.hpp:43
std::size_t size() const
Definition: Pattern.hpp:44
std::size_t uniqueIndex(const int x, const int y) const
Definition: Pattern.cpp:66
Definition: TMatrix.hpp:13
Ttype sum() const
Definition: TMatrix.hpp:77
auto cbegin() const
Definition: TMatrix.hpp:70
std::size_t size() const
Definition: TMatrix.hpp:38
TMatrix(const Pattern &pat, const std::string name="")
Definition: TMatrix.hpp:16
const Ttype & at(const std::size_t index) const
Definition: TMatrix.hpp:53
auto end()
Definition: TMatrix.hpp:68
void serialize(Ar &ar)
Definition: TMatrix.hpp:80
const Ttype & operator()(const int row, const int col) const
Definition: TMatrix.hpp:45
TMatrix(const TMatrix< OtherTtype > &other)
Definition: TMatrix.hpp:26
std::vector< Ttype > uncompressedVector() const
Definition: TMatrix.hpp:85
void resize(const Pattern &pattern)
Definition: TMatrix.hpp:58
TMatrix()=default
Ttype & operator[](const std::size_t index)
Definition: TMatrix.hpp:47
auto begin()
Definition: TMatrix.hpp:67
std::size_t cols() const
Definition: TMatrix.hpp:36
Ttype & operator()(const int row, const int col)
Definition: TMatrix.hpp:40
void resetChange()
Definition: TMatrix.hpp:56
void setConstant(const Ttype &val)
Definition: TMatrix.hpp:75
Pattern pat
Definition: TMatrix.hpp:93
bool isChanged(const int row, const int col) const
Definition: TMatrix.hpp:55
std::size_t rows() const
Definition: TMatrix.hpp:35
void fill(const std::vector< Ttype > &tensors_in)
Definition: TMatrix.hpp:73
auto cend() const
Definition: TMatrix.hpp:71
const Ttype & operator[](const std::size_t index) const
Definition: TMatrix.hpp:52