Loading...
Searching...
No Matches
serializer.C
Go to the documentation of this file.
1/*
2 * This file is part of the SPLINTER library.
3 * Copyright (C) 2012 Bjarne Grimstad (bjarne.grimstad@gmail.com).
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8*/
9
10#include "serializer.h"
11#include <fstream>
12#include "definitions.h"
13#include "datapoint.h"
14#include <datatable.h>
15#include <bspline.h>
16#include <bsplinebasis.h>
17#include <bsplinebasis1d.h>
18
19namespace SPLINTER
20{
21
22Serializer::Serializer()
23{
24 stream = StreamType(0);
25}
26
27Serializer::Serializer(const std::string& fileName)
28{
29 stream = StreamType(0);
30 loadFromFile(fileName);
31}
32
33void Serializer::saveToFile(const std::string& fileName)
34{
35 std::fstream fs(fileName, std::fstream::out | std::fstream::binary);
36
37 for (const auto& byte : stream)
38 {
39 fs << byte;
40 }
41}
42
43void Serializer::loadFromFile(const std::string& fileName)
44{
45 // Open the file in binary mode at the end
46 std::ifstream ifs(fileName, std::ios::binary | std::ios::ate);
47
48 if (!ifs.is_open())
49 {
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);
54 }
55
56 // Because we opened the file at the end, tellg() will give us the size of the file
57 std::ifstream::pos_type pos = ifs.tellg();
58 std::vector<char> result(pos);
59 ifs.seekg(0, std::ios::beg);
60 // http://www.cplusplus.com/reference/vector/vector/data/
61 // Elements of the vector are guaranteed to be stored in a contiguous array,
62 // *result.data() can therefore be treated as an array of the same type as the vector
63 ifs.read(result.data(), pos);
64 //assert(ifs);
65 stream.clear();
66
67 // Convert from char to uint_8 vector
68 for (const char& byte : result)
69 {
70 stream.push_back((uint8_t)byte);
71 }
72
73 read = stream.cbegin();
74}
75
76/*
77 * get_size implementations
78 */
79
80size_t Serializer::get_size(const DataPoint& obj)
81{
82 return get_size(obj.x) + get_size(obj.y);
83}
84
85size_t Serializer::get_size(const DataTable& obj)
86{
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)
92 + get_size(obj.grid);
93}
94
95size_t Serializer::get_size(const BSpline& obj)
96{
97 return get_size(obj.basis)
98 + get_size(obj.knotaverages)
99 + get_size(obj.coefficients)
100 + get_size(obj.numVariables);
101}
102
103size_t Serializer::get_size(const BSplineBasis& obj)
104{
105 return get_size(obj.bases)
106 + get_size(obj.numVariables);
107}
108
109size_t Serializer::get_size(const BSplineBasis1D& obj)
110{
111 return get_size(obj.degree)
112 + get_size(obj.knots)
113 + get_size(obj.targetNumBasisfunctions);
114}
115
116size_t Serializer::get_size(const DenseMatrix& obj)
117{
118 size_t size = sizeof(obj.rows());
119 size += sizeof(obj.cols());
120 size_t numElements = obj.rows() * obj.cols();
121
122 if (numElements > 0)
123 {
124 size += numElements * sizeof(obj(0, 0));
125 }
126
127 return size;
128}
129
130size_t Serializer::get_size(const DenseVector& obj)
131{
132 size_t size = sizeof(obj.rows());
133 size_t numElements = obj.rows();
134
135 if (numElements > 0)
136 {
137 size += numElements * sizeof(obj(0));
138 }
139
140 return size;
141}
142
143size_t Serializer::get_size(const SparseMatrix& obj)
144{
145 DenseMatrix temp(obj);
146 return get_size(temp);
147}
148
149size_t Serializer::get_size(const SparseVector& obj)
150{
151 DenseVector temp(obj);
152 return get_size(temp);
153}
154
155/*
156 * _serialize implementations
157 */
158
159void Serializer::_serialize(const DataPoint& obj)
160{
161 _serialize(obj.x);
162 _serialize(obj.y);
163}
164
165void Serializer::_serialize(const DataTable& obj)
166{
167 _serialize(obj.allowDuplicates);
168 _serialize(obj.allowIncompleteGrid);
169 _serialize(obj.numDuplicates);
170 _serialize(obj.numVariables);
171 _serialize(obj.samples);
172 _serialize(obj.grid);
173}
174
175void Serializer::_serialize(const BSpline& obj)
176{
177 _serialize(obj.basis);
178 _serialize(obj.knotaverages);
179 _serialize(obj.coefficients);
180 _serialize(obj.numVariables);
181}
182
183void Serializer::_serialize(const BSplineBasis& obj)
184{
185 _serialize(obj.bases);
186 _serialize(obj.numVariables);
187}
188
189void Serializer::_serialize(const BSplineBasis1D& obj)
190{
191 _serialize(obj.degree);
192 _serialize(obj.knots);
193 _serialize(obj.targetNumBasisfunctions);
194}
195
196void Serializer::_serialize(const DenseMatrix& obj)
197{
198 // Store the number of matrix rows and columns first
199 _serialize(obj.rows());
200 _serialize(obj.cols());
201
202 // Store the matrix elements
203 for (size_t i = 0; i < (size_t) obj.rows(); ++i)
204 {
205 for (size_t j = 0; j < (size_t) obj.cols(); ++j)
206 {
207 _serialize(obj(i, j));
208 }
209 }
210}
211
212void Serializer::_serialize(const DenseVector& obj)
213{
214 // Store the number of vector rows
215 _serialize(obj.rows());
216
217 // Store the vector elements
218 for (size_t i = 0; i < (size_t) obj.rows(); ++i)
219 {
220 _serialize(obj(i));
221 }
222}
223
224void Serializer::_serialize(const SparseMatrix& obj)
225{
226 DenseMatrix temp(obj);
227 _serialize(temp);
228}
229
230void Serializer::_serialize(const SparseVector& obj)
231{
232 DenseVector temp(obj);
233 _serialize(temp);
234}
235
236/*
237 * deserialize implementations
238 */
239
240void Serializer::deserialize(DataPoint& obj)
241{
242 deserialize(obj.x);
243 deserialize(obj.y);
244}
245
246void Serializer::deserialize(DataTable& obj)
247{
248 deserialize(obj.allowDuplicates);
249 deserialize(obj.allowIncompleteGrid);
250 deserialize(obj.numDuplicates);
251 deserialize(obj.numVariables);
252 deserialize(obj.samples);
253 deserialize(obj.grid);
254}
255
256void Serializer::deserialize(BSpline& obj)
257{
258 deserialize(obj.basis);
259 deserialize(obj.knotaverages);
260 deserialize(obj.coefficients);
261 deserialize(obj.numVariables);
262}
263
264void Serializer::deserialize(BSplineBasis& obj)
265{
266 deserialize(obj.bases);
267 deserialize(obj.numVariables);
268}
269
270void Serializer::deserialize(BSplineBasis1D& obj)
271{
272 deserialize(obj.degree);
273 deserialize(obj.knots);
274 deserialize(obj.targetNumBasisfunctions);
275}
276
277void Serializer::deserialize(DenseMatrix& obj)
278{
279 // Retrieve the number of rows
280 size_t rows;
281 deserialize(rows);
282 size_t cols;
283 deserialize(cols);
284 obj.resize(rows, cols);
285
286 for (size_t i = 0; i < rows; ++i)
287 {
288 for (size_t j = 0; j < cols; ++j)
289 {
290 deserialize(obj(i, j));
291 }
292 }
293}
294
295void Serializer::deserialize(DenseVector& obj)
296{
297 // Retrieve the number of rows
298 size_t rows;
299 deserialize(rows);
300 obj.resize(rows);
301
302 for (size_t i = 0; i < rows; ++i)
303 {
304 deserialize(obj(i));
305 }
306}
307
308void Serializer::deserialize(SparseMatrix& obj)
309{
310 DenseMatrix temp(obj);
311 deserialize(temp);
312 obj = temp.sparseView();
313}
314
315void Serializer::deserialize(SparseVector& obj)
316{
317 DenseVector temp(obj);
318 deserialize(temp);
319 obj = temp.sparseView();
320}
321
322} // namespace SPLINTER
turbulence read()
label i
Definition pEqn.H:46