10#include "serializer.h"
12#include "definitions.h"
16#include <bsplinebasis.h>
17#include <bsplinebasis1d.h>
22Serializer::Serializer()
24 stream = StreamType(0);
27Serializer::Serializer(
const std::string& fileName)
29 stream = StreamType(0);
30 loadFromFile(fileName);
33void Serializer::saveToFile(
const std::string& fileName)
35 std::fstream fs(fileName, std::fstream::out | std::fstream::binary);
37 for (
const auto&
byte : stream)
43void Serializer::loadFromFile(
const std::string& fileName)
46 std::ifstream ifs(fileName, std::ios::binary | std::ios::ate);
50 std::string error_message(
"Serializer::loadFromFile: Unable to open file \"");
51 error_message.append(fileName);
52 error_message.append(
"\" for deserializing.");
53 throw Exception(error_message);
57 std::ifstream::pos_type pos = ifs.tellg();
58 std::vector<char> result(pos);
59 ifs.seekg(0, std::ios::beg);
63 ifs.read(result.data(), pos);
68 for (
const char&
byte : result)
70 stream.push_back((uint8_t)
byte);
73 read = stream.cbegin();
80size_t Serializer::get_size(
const DataPoint& obj)
82 return get_size(obj.x) + get_size(obj.y);
85size_t Serializer::get_size(
const DataTable& obj)
87 return get_size(obj.allowDuplicates)
88 + get_size(obj.allowIncompleteGrid)
89 + get_size(obj.numDuplicates)
90 + get_size(obj.numVariables)
91 + get_size(obj.samples)
95size_t Serializer::get_size(
const BSpline& obj)
97 return get_size(obj.basis)
98 + get_size(obj.knotaverages)
99 + get_size(obj.coefficients)
100 + get_size(obj.numVariables);
103size_t Serializer::get_size(
const BSplineBasis& obj)
105 return get_size(obj.bases)
106 + get_size(obj.numVariables);
109size_t Serializer::get_size(
const BSplineBasis1D& obj)
111 return get_size(obj.degree)
112 + get_size(obj.knots)
113 + get_size(obj.targetNumBasisfunctions);
116size_t Serializer::get_size(
const DenseMatrix& obj)
118 size_t size =
sizeof(obj.rows());
119 size +=
sizeof(obj.cols());
120 size_t numElements = obj.rows() * obj.cols();
124 size += numElements *
sizeof(obj(0, 0));
130size_t Serializer::get_size(
const DenseVector& obj)
132 size_t size =
sizeof(obj.rows());
133 size_t numElements = obj.rows();
137 size += numElements *
sizeof(obj(0));
143size_t Serializer::get_size(
const SparseMatrix& obj)
145 DenseMatrix temp(obj);
146 return get_size(temp);
149size_t Serializer::get_size(
const SparseVector& obj)
151 DenseVector temp(obj);
152 return get_size(temp);
159void Serializer::_serialize(
const DataPoint& obj)
165void Serializer::_serialize(
const DataTable& obj)
167 _serialize(obj.allowDuplicates);
168 _serialize(obj.allowIncompleteGrid);
169 _serialize(obj.numDuplicates);
170 _serialize(obj.numVariables);
171 _serialize(obj.samples);
172 _serialize(obj.grid);
175void Serializer::_serialize(
const BSpline& obj)
177 _serialize(obj.basis);
178 _serialize(obj.knotaverages);
179 _serialize(obj.coefficients);
180 _serialize(obj.numVariables);
183void Serializer::_serialize(
const BSplineBasis& obj)
185 _serialize(obj.bases);
186 _serialize(obj.numVariables);
189void Serializer::_serialize(
const BSplineBasis1D& obj)
191 _serialize(obj.degree);
192 _serialize(obj.knots);
193 _serialize(obj.targetNumBasisfunctions);
196void Serializer::_serialize(
const DenseMatrix& obj)
199 _serialize(obj.rows());
200 _serialize(obj.cols());
203 for (
size_t i = 0;
i < (size_t) obj.rows(); ++
i)
205 for (
size_t j = 0; j < (size_t) obj.cols(); ++j)
207 _serialize(obj(
i, j));
212void Serializer::_serialize(
const DenseVector& obj)
215 _serialize(obj.rows());
218 for (
size_t i = 0;
i < (size_t) obj.rows(); ++
i)
224void Serializer::_serialize(
const SparseMatrix& obj)
226 DenseMatrix temp(obj);
230void Serializer::_serialize(
const SparseVector& obj)
232 DenseVector temp(obj);
240void Serializer::deserialize(DataPoint& obj)
246void Serializer::deserialize(DataTable& obj)
248 deserialize(obj.allowDuplicates);
249 deserialize(obj.allowIncompleteGrid);
250 deserialize(obj.numDuplicates);
251 deserialize(obj.numVariables);
252 deserialize(obj.samples);
253 deserialize(obj.grid);
256void Serializer::deserialize(BSpline& obj)
258 deserialize(obj.basis);
259 deserialize(obj.knotaverages);
260 deserialize(obj.coefficients);
261 deserialize(obj.numVariables);
264void Serializer::deserialize(BSplineBasis& obj)
266 deserialize(obj.bases);
267 deserialize(obj.numVariables);
270void Serializer::deserialize(BSplineBasis1D& obj)
272 deserialize(obj.degree);
273 deserialize(obj.knots);
274 deserialize(obj.targetNumBasisfunctions);
277void Serializer::deserialize(DenseMatrix& obj)
284 obj.resize(rows, cols);
286 for (
size_t i = 0;
i < rows; ++
i)
288 for (
size_t j = 0; j < cols; ++j)
290 deserialize(obj(
i, j));
295void Serializer::deserialize(DenseVector& obj)
302 for (
size_t i = 0;
i < rows; ++
i)
308void Serializer::deserialize(SparseMatrix& obj)
310 DenseMatrix temp(obj);
312 obj = temp.sparseView();
315void Serializer::deserialize(SparseVector& obj)
317 DenseVector temp(obj);
319 obj = temp.sparseView();