-
Notifications
You must be signed in to change notification settings - Fork 13
/
hpc_matrix.hpp
187 lines (181 loc) · 4.56 KB
/
hpc_matrix.hpp
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#pragma once
#include <hpc_execution.hpp>
#include <hpc_range.hpp>
#include <hpc_vector.hpp>
namespace hpc {
template <
class T,
layout L = layout::right,
class Allocator = std::allocator<T>,
class ExecutionPolicy = ::hpc::serial_policy,
class RowIndex = std::ptrdiff_t,
class ColumnIndex = std::ptrdiff_t>
class matrix
{
public:
using row_type = RowIndex;
using column_type = ColumnIndex;
using nonzero_type = decltype(std::declval<row_type>() * std::declval<column_type>());
using value_type = T;
private:
using data_type = ::hpc::vector<value_type, Allocator, ExecutionPolicy, nonzero_type>;
using data_iterator = typename data_type::iterator;
using const_data_iterator = typename data_type::const_iterator;
using range_type = range_product<data_iterator, L, row_type, column_type>;
using const_range_type = range_product<const_data_iterator, L, row_type, column_type>;
vector<value_type, Allocator, ExecutionPolicy, nonzero_type> m_data;
row_type m_rows;
column_type m_columns;
range_type
get_range() noexcept
{
return range_type(m_data.begin(), rows(), columns());
}
const_range_type
get_const_range() const noexcept
{
return const_range_type(m_data.begin(), rows(), columns());
}
public:
using allocator_type = Allocator;
using execution_policy = ExecutionPolicy;
using size_type = row_type;
using difference_type = decltype(m_rows - m_rows);
using reference = value_type&;
using const_reference = value_type const&;
using pointer = T*;
using const_pointer = T const*;
using iterator = typename range_type::iterator;
using const_iterator = typename const_range_type::const_iterator;
constexpr matrix() noexcept : m_rows(0), m_columns(0)
{
}
matrix(row_type row_count, column_type column_count)
: m_data(row_count * column_count), m_rows(row_count), m_columns(column_count)
{
}
constexpr matrix(allocator_type const& allocator_in, execution_policy const& exec_in) noexcept
: m_data(allocator_in, exec_in), m_rows(0), m_columns(0)
{
}
matrix(
row_type row_count,
column_type column_count,
allocator_type const& allocator_in,
execution_policy const& exec_in)
: m_data(row_count * column_count, allocator_in, exec_in), m_rows(row_count), m_columns(column_count)
{
}
matrix(matrix&& other) noexcept = default;
matrix(matrix const&) = delete;
matrix&
operator=(matrix const&) = delete;
matrix&
operator=(matrix&& other) = default;
~matrix() = default;
T*
data() noexcept
{
return m_data.data();
}
T const*
data() const noexcept
{
return m_data.data();
}
iterator
begin() noexcept
{
return get_range().begin();
}
const_iterator
begin() const noexcept
{
return get_const_range().begin();
}
const_iterator
cbegin() const noexcept
{
return get_const_range().begin();
}
iterator
end() noexcept
{
return get_range().end();
}
const_iterator
end() const noexcept
{
return get_const_range().end();
}
const_iterator
cend() const noexcept
{
return get_const_range().end();
}
bool
empty() const noexcept
{
return m_data.empty();
}
size_type
size() const noexcept
{
return m_rows;
}
row_type
rows() const noexcept
{
return m_rows;
}
column_type
columns() const noexcept
{
return m_columns;
}
void
clear()
{
m_data.clear();
}
void
resize(row_type row_count, column_type column_count)
{
if (row_count == rows() && column_count == columns()) return;
m_data.clear();
m_data.resize(row_count * column_count);
m_rows = row_count;
m_columns = column_count;
}
constexpr allocator_type
get_allocator() const noexcept
{
return m_data.get_allocator();
}
constexpr execution_policy
get_execution_policy() const noexcept
{
return m_data.get_execution_policy();
}
constexpr typename range_type::reference
operator[](size_type i) noexcept
{
return begin()[i];
}
constexpr typename range_type::const_reference
operator[](size_type i) const noexcept
{
return begin()[i];
}
constexpr reference
operator()(row_type i, column_type j) noexcept
{
return begin()[i][j];
}
constexpr const_reference
operator()(row_type i, column_type j) const noexcept
{
return begin()[i][j];
}
};
} // namespace hpc