-
Notifications
You must be signed in to change notification settings - Fork 3
/
boost_serialize_armadillo.h
131 lines (102 loc) · 3.27 KB
/
boost_serialize_armadillo.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. See the enclosed file LICENSE for a copy or if
* that was not distributed with this file, You can obtain one at
* http://mozilla.org/MPL/2.0/.
*
* Copyright 2017 Max H. Gerlach
*
* */
/*
* boost_serialize_armadillo.h
*
* Created on: Apr 17, 2013
* Author: gerlach
*/
#ifndef BOOST_SERIALIZE_ARMADILLO_H_
#define BOOST_SERIALIZE_ARMADILLO_H_
//code to serialize Armadillo vectors / matrices / cubes with Boost
#include <armadillo>
#include <sstream>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
#pragma GCC diagnostic ignored "-Wshadow"
#include "boost/serialization/split_free.hpp"
#pragma GCC diagnostic pop
namespace boost { namespace serialization {
// Vectors
template<class Archive, class T>
inline void save(Archive& ar,
const arma::Col<T>& vec,
const uint32_t /*version*/) {
std::ostringstream outStream;
vec.save(outStream, arma::arma_binary);
std::string outString = outStream.str();
ar << outString;
}
template<class Archive, class T>
inline void load(Archive& ar,
arma::Col<T>& vec,
const uint32_t /*version*/) {
std::string inString;
ar >> inString;
std::istringstream inStream(inString);
vec.load(inStream, arma::arma_binary);
}
template<class Archive, class T>
inline void serialize(Archive& ar,
arma::Col<T>& vec,
const uint32_t file_version) {
boost::serialization::split_free(ar, vec, file_version);
}
// Matrices
template<class Archive, class T>
inline void save(Archive& ar,
const arma::Mat<T>& mat,
const uint32_t /*version*/) {
std::ostringstream outStream;
mat.save(outStream, arma::arma_binary);
std::string outString = outStream.str();
ar << outString;
}
template<class Archive, class T>
inline void load(Archive& ar,
arma::Mat<T>& mat,
const uint32_t /*version*/) {
std::string inString;
ar >> inString;
std::istringstream inStream(inString);
mat.load(inStream, arma::arma_binary);
}
template<class Archive, class T>
inline void serialize(Archive& ar,
arma::Mat<T>& mat,
const uint32_t file_version) {
boost::serialization::split_free(ar, mat, file_version);
}
// Cubes
template<class Archive, class T>
inline void save(Archive& ar,
const arma::Cube<T>& cube,
const uint32_t /*version*/) {
std::ostringstream outStream;
cube.save(outStream, arma::arma_binary);
std::string outString = outStream.str();
ar << outString;
}
template<class Archive, class T>
inline void load(Archive& ar,
arma::Cube<T>& cube,
const uint32_t /*version*/) {
std::string inString;
ar >> inString;
std::istringstream inStream(inString);
cube.load(inStream, arma::arma_binary);
}
template<class Archive, class T>
inline void serialize(Archive& ar,
arma::Cube<T>& cube,
const uint32_t file_version) {
boost::serialization::split_free(ar, cube, file_version);
}
} } // namespace boost::serialization
#endif /* BOOST_SERIALIZE_ARMADILLO_H_ */