-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmatrix_io.hpp
68 lines (57 loc) · 1.49 KB
/
matrix_io.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
// This file is covered by the LICENSE file in the root of this project.
#pragma once
#include "io.hpp"
#include <cstddef>
#include <type_traits>
template<typename T = void, class Matrix, class Fn>
bool read_matrix(Matrix& mat, Fn fn)
{
using S = typename std::conditional<std::is_void<T>::value, typename Matrix::Value, T>::type;
for (std::size_t row = 0; row < mat.rows(); ++row)
for (std::size_t col = 0; col < mat.cols(); ++col)
{
S s;
if (!read(s))
return false;
mat(row, col) = fn(s);
}
return true;
}
template<class Matrix>
bool read_matrix(Matrix& mat)
{
return read_matrix<typename Matrix::Value>(
mat, [](const typename Matrix::Value& x) { return x; });
}
template<typename T = void, class Matrix, class Fn>
bool read_transposed_matrix(Matrix& mat, Fn fn)
{
using S = typename std::conditional<std::is_void<T>::value, typename Matrix::Value, T>::type;
for (std::size_t row = 0; row < mat.rows(); ++row)
for (std::size_t col = 0; col < mat.cols(); ++col)
{
S s;
if (!read(s))
return false;
mat(col, row) = fn(s);
}
return true;
}
template<class Matrix>
bool read_transposed_matrix(Matrix& mat)
{
return read_transposed_matrix<typename Matrix::Value>(
mat, [](const typename Matrix::Value& x) { return x; });
}
template<class Matrix>
bool write_matrix(Matrix& mat)
{
for (std::size_t row = 0; row < mat.rows(); ++row)
{
write(mat(row, 0));
for (std::size_t col = 1; col < mat.cols(); ++col)
write(' ', mat(row, col));
write_ln();
}
return true;
}