-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmatrix_storage.h
171 lines (144 loc) · 6.27 KB
/
matrix_storage.h
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#if !defined MATRIX_STORAGE_2018_08_24_12_32_44
#define MATRIX_STORAGE_2018_08_24_12_32_44
#include <initializer_list>
namespace std::experimental::la {
struct fixed_size_matrix_t{};
struct dynamic_size_matrix_t{};
template<class T>
using is_fixed_size = typename enable_if<std::is_base_of<fixed_size_matrix_t, T>::value>::type;
template<class T>
using is_dynamic_size = typename enable_if<std::is_base_of<dynamic_size_matrix_t, T>::value>::type;
template<class Scalar, size_t RowCount, size_t ColCount>
struct fixed_size_matrix : public fixed_size_matrix_t
{
using scalar_t = Scalar;
using matrix_t = fixed_size_matrix<Scalar, RowCount, ColCount>;
template<class Other>
using multiply_t = fixed_size_matrix<Scalar, RowCount, Other::col>;
using transpose_t = fixed_size_matrix<Scalar, ColCount, RowCount>;
using submatrix_t = fixed_size_matrix<Scalar, RowCount - 1, ColCount - 1>;
constexpr static size_t row = RowCount;
constexpr static size_t col = ColCount;
constexpr fixed_size_matrix() = default;
constexpr fixed_size_matrix(std::initializer_list<Scalar>) noexcept; // Pass by value or rref?
constexpr Scalar operator()(size_t, size_t) const;
constexpr Scalar& operator()(size_t, size_t);
constexpr Scalar* begin() noexcept;
constexpr const Scalar* cbegin() const noexcept;
constexpr Scalar* end() noexcept;
constexpr const Scalar* cend() const noexcept;
Scalar _Data[RowCount * ColCount];
};
template<class Scalar, class Alloc = std::allocator<Scalar>>
struct dynamic_size_matrix : public dynamic_size_matrix_t
{
using scalar_t = Scalar;
using matrix_t = dynamic_size_matrix<Scalar, Alloc>;
template<class Other>
using multiply_t = dynamic_size_matrix<Scalar, Alloc>;
using transpose_t = dynamic_size_matrix<Scalar, Alloc>;
using submatrix_t = dynamic_size_matrix<Scalar, Alloc>;
constexpr dynamic_size_matrix() = default;
constexpr dynamic_size_matrix(dynamic_size_matrix const&);
constexpr dynamic_size_matrix(std::pair<size_t, size_t>);
constexpr Scalar operator()(size_t, size_t) const;
constexpr Scalar& operator()(size_t, size_t);
constexpr Scalar* begin() noexcept;
constexpr const Scalar* cbegin() const noexcept;
constexpr Scalar* end() noexcept;
constexpr const Scalar* cend() const noexcept;
size_t _RowCount = 0;
size_t _ColCount = 0;
std::unique_ptr<Scalar[]> _Data = nullptr;
};
}
////////////////////////////////////////////////////////
// fixed_size_matrix implementation
////////////////////////////////////////////////////////
template<class Scalar, size_t RowCount, size_t ColCount>
inline constexpr std::experimental::la::fixed_size_matrix<Scalar, RowCount, ColCount>::fixed_size_matrix(std::initializer_list<Scalar> il) noexcept
{
std::copy(il.begin(), il.end(), _Data);
}
template<class Scalar, size_t RowCount, size_t ColCount>
inline constexpr Scalar std::experimental::la::fixed_size_matrix<Scalar, RowCount, ColCount>::operator()(size_t i, size_t j) const
{
return _Data[i * RowCount + j];
}
template<class Scalar, size_t RowCount, size_t ColCount>
inline constexpr Scalar& std::experimental::la::fixed_size_matrix<Scalar, RowCount, ColCount>::operator()(size_t i, size_t j)
{
return _Data[i * RowCount + j];
}
template<class Scalar, size_t RowCount, size_t ColCount>
inline constexpr Scalar* std::experimental::la::fixed_size_matrix<Scalar, RowCount, ColCount>::begin() noexcept
{
return _Data;
}
template<class Scalar, size_t RowCount, size_t ColCount>
inline constexpr const Scalar* std::experimental::la::fixed_size_matrix<Scalar, RowCount, ColCount>::cbegin() const noexcept
{
return _Data;
}
template<class Scalar, size_t RowCount, size_t ColCount>
inline constexpr Scalar* std::experimental::la::fixed_size_matrix<Scalar, RowCount, ColCount>::end() noexcept
{
return _Data + RowCount * ColCount;
}
template<class Scalar, size_t RowCount, size_t ColCount>
inline constexpr const Scalar* std::experimental::la::fixed_size_matrix<Scalar, RowCount, ColCount>::cend() const noexcept
{
return _Data + RowCount * ColCount;
}
////////////////////////////////////////////////////////
// dynamic_size_matrix implementation
////////////////////////////////////////////////////////
template<class Scalar, class Alloc>
inline constexpr std::experimental::la::dynamic_size_matrix<Scalar, Alloc>::dynamic_size_matrix(dynamic_size_matrix const& rhs)
: _RowCount(rhs._RowCount)
, _ColCount(rhs._ColCount)
, _Data(new Scalar[_RowCount * _ColCount])
{
if (rhs._Data)
{
std::copy(rhs._Data.get(), rhs._Data.get() + _RowCount * _ColCount, _Data.get());
}
}
template<class Scalar, class Alloc>
inline constexpr std::experimental::la::dynamic_size_matrix<Scalar, Alloc>::dynamic_size_matrix(std::pair<size_t, size_t> size)
: _RowCount(size.first)
, _ColCount(size.first)
, _Data(new Scalar[_RowCount * _ColCount])
{
}
template<class Scalar, class Alloc>
inline constexpr Scalar std::experimental::la::dynamic_size_matrix<Scalar, Alloc>::operator()(size_t i, size_t j) const
{
return _Data.get()[i * _RowCount + j];
}
template<class Scalar, class Alloc>
inline constexpr Scalar& std::experimental::la::dynamic_size_matrix<Scalar, Alloc>::operator()(size_t i, size_t j)
{
return _Data.get()[i * _RowCount + j];
}
template<class Scalar, class Alloc>
inline constexpr Scalar* std::experimental::la::dynamic_size_matrix<Scalar, Alloc>::begin() noexcept
{
return _Data.get();
}
template<class Scalar, class Alloc>
inline constexpr const Scalar* std::experimental::la::dynamic_size_matrix<Scalar, Alloc>::cbegin() const noexcept
{
return _Data.get();
}
template<class Scalar, class Alloc>
inline constexpr Scalar* std::experimental::la::dynamic_size_matrix<Scalar, Alloc>::end() noexcept
{
return _Data.get() + _RowCount * _ColCount;
}
template<class Scalar, class Alloc>
inline constexpr const Scalar* std::experimental::la::dynamic_size_matrix<Scalar, Alloc>::cend() const noexcept
{
return _Data.get() + _RowCount * _ColCount;
}
#endif //MATRIX_STORAGE_2018_08_24_12_32_44