"...source/release/git@code.ornl.gov:mantidproject/mantid.git" did not exist on "c9a5d7972aed017503de0b492d6b54c6f5391dcb"
Newer
Older
//------------------------------------------------------------------------------
// Includes
//------------------------------------------------------------------------------
#include "MantidKernel/BinaryStreamReader.h"
#include "MantidKernel/Matrix.h"
namespace Mantid {
namespace Kernel {
//------------------------------------------------------------------------------
// Anonymous functions
//------------------------------------------------------------------------------
namespace {
/**
* Read a value from the stream based on the template type
* @param stream The open stream on which to perform the read
* @param value An object of type T to fill with the value from the file
*/
template <typename T>
inline void readFromStream(std::istream &stream, T &value) {
stream.read(reinterpret_cast<char *>(&value), sizeof(T));
}
/**
* Overload to read an array of values from the stream based on the template
* type for the element of the array.
* @param stream The open stream on which to perform the read
* @param value An object of type std::vector<T> to fill with the value from
* the file
* @param nvals The number of values to read
*/
template <typename T>
inline void readFromStream(std::istream &stream, std::vector<T> &value,
size_t nvals) {
if (value.size() < nvals)
value.resize(nvals);
stream.read(reinterpret_cast<char *>(value.data()), nvals * sizeof(T));
}
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
/**
* Read a stream of numbers and interpret them as a 2D matrix of specified
* shape & order
* @param stream The open stream on which to perform the read
* @param value The Matrix to fill. Its size is increased if necessary
* @param shape 2D-vector defined as (nrows, ncols)
* @param order Defines whether the stream of bytes is interpreted as moving
* across the rows first (RowMajor) or down columns first (ColumnMajor)
*/
template <typename T>
inline void readFromStream(std::istream &stream, Matrix<T> &value,
const std::vector<int32_t> &shape,
BinaryStreamReader::MatrixOrdering order) {
assert(2 <= shape.size());
const size_t s0(shape[0]), s1(shape[1]), totalLength(s0 * s1);
std::vector<T> buffer;
readFromStream(stream, buffer, totalLength);
value = Matrix<T>(s0, s1);
if (order == BinaryStreamReader::MatrixOrdering::RowMajor) {
for (size_t i = 0; i < s0; ++i) {
auto row = value[i];
const size_t offset = i * s1;
for (size_t j = 0; j < s1; ++j) {
row[j] = buffer[offset + j];
}
}
} else {
for (size_t i = 0; i < s0; ++i) {
auto row = value[i];
for (size_t j = 0; j < s1; ++j) {
row[j] = buffer[j * s0 + i];
}
}
}
}
}
//------------------------------------------------------------------------------
// Public members
//------------------------------------------------------------------------------
/**
* Constructor taking the stream to read.
* @param istrm An open stream from which data will be read. The object does
* not take ownership of the stream. The caller is responsible for closing
* it.
*/
BinaryStreamReader::BinaryStreamReader(std::istream &istrm)
: m_istrm(istrm), m_strLengthSize(static_cast<uint64_t>(sizeof(int32_t))) {
if (!istrm) {
throw std::runtime_error(
"BinaryStreamReader: Input stream is in a bad state. Cannot continue.");
}
}
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
/**
* Read a int32_t from the stream
* @param value The value is stored in the given stream
* @return A reference to the BinaryStreamReader object
*/
BinaryStreamReader &BinaryStreamReader::operator>>(int32_t &value) {
readFromStream(m_istrm, value);
return *this;
}
/**
* Read a int64_t from the stream
* @param value The value is stored in the given stream
* @return A reference to the BinaryStreamReader object
*/
BinaryStreamReader &BinaryStreamReader::operator>>(int64_t &value) {
readFromStream(m_istrm, value);
return *this;
}
/**
* Read a float (4-bytes) from the stream
* @param value The value is stored in the given stream
* @return A reference to the BinaryStreamReader object
*/
BinaryStreamReader &BinaryStreamReader::operator>>(float &value) {
readFromStream(m_istrm, value);
return *this;
}
/**
* Read a double (8-bytes) from the stream
* @param value The value is stored in this object
* @return A reference to the BinaryStreamReader object
*/
BinaryStreamReader &BinaryStreamReader::operator>>(double &value) {
readFromStream(m_istrm, value);
return *this;
}
/**
* Read a string of characters into given object. This method assumes that
* the stream currently points at a type specifying the length followed directly
* by the string itself.
* @param value The string value is stored in the given object. It is resized
* to the appropriate length
* @return A reference to the BinaryStreamReader object
*/
BinaryStreamReader &BinaryStreamReader::operator>>(std::string &value) {
// First read the size.
decltype(m_strLengthSize) length(0);
m_istrm.read(reinterpret_cast<char *>(&length), m_strLengthSize);
// Now the value
value.resize(static_cast<std::string::size_type>(length));
m_istrm.read(const_cast<char *>(value.data()), static_cast<size_t>(length));
return *this;
}
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/**
* Read an array of int32_t into the given vector.
* @param value The array to fill. Its size is increased if necessary
* @param nvals The number values to attempt to read from the stream
* @return A reference to the BinaryStreamReader object
*/
BinaryStreamReader &BinaryStreamReader::read(std::vector<int32_t> &value,
const size_t nvals) {
readFromStream(m_istrm, value, nvals);
return *this;
}
/**
* Read an array of int64_t into the given vector.
* @param value The array to fill. Its size is increased if necessary
* @param nvals The number values to attempt to read from the stream
* @return A reference to the BinaryStreamReader object
*/
BinaryStreamReader &BinaryStreamReader::read(std::vector<int64_t> &value,
const size_t nvals) {
readFromStream(m_istrm, value, nvals);
return *this;
}
/**
* Read an array of float balues into the given vector.
* @param value The array to fill. Its size is increased if necessary
* @param nvals The number values to attempt to read from the stream
* @return A reference to the BinaryStreamReader object
*/
BinaryStreamReader &BinaryStreamReader::read(std::vector<float> &value,
const size_t nvals) {
readFromStream(m_istrm, value, nvals);
return *this;
}
/**
* Read an array of double values into the given vector.
* @param value The array to fill. Its size is increased if necessary
* @param nvals The number values to attempt to read from the stream
* @return A reference to the BinaryStreamReader object
*/
BinaryStreamReader &BinaryStreamReader::read(std::vector<double> &value,
const size_t nvals) {
readFromStream(m_istrm, value, nvals);
return *this;
}
/**
* Read a series of characters into a string object.
* @param value The string to fill. Its size is increased if necessary
* @param length The number characters to attempt to read from the stream
* @return A reference to the BinaryStreamReader object
*/
BinaryStreamReader &BinaryStreamReader::read(std::string &value,
const size_t length) {
value.resize(length);
m_istrm.read(const_cast<char *>(value.data()), length);
return *this;
}
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/**
* Read a stream of characters and interpret them as a 2D matrix of specified
* shape & order
* @param value The array to fill. Its size is increased if necessary
* @param shape 2D-vector defined as (nstrs, strLength)
* @param order Defines whether the stream of bytes is interpreted as moving
* across the rows first (RowMajor) or down columns first (ColumnMajor)
* @return A reference to the BinaryStreamReader object
*/
BinaryStreamReader &
BinaryStreamReader::read(std::vector<std::string> &value,
const std::vector<int32_t> &shape,
BinaryStreamReader::MatrixOrdering order) {
assert(2 <= shape.size());
const size_t s0(shape[0]), s1(shape[1]), totalLength(s0 * s1);
std::string buffer;
this->read(buffer, totalLength);
value.resize(s0);
if (order == MatrixOrdering::RowMajor) {
size_t pos(0);
for (auto &str : value) {
str = buffer.substr(pos, s1);
pos += s1;
}
} else {
for (size_t i = 0; i < s0; ++i) {
auto &str = value[i];
str.resize(s1);
for (size_t j = 0; j < s1; ++j) {
str[j] = buffer[j * s0 + i];
}
}
}
return *this;
}
/**
* Read a stream of floats and interpret them as a 2D matrix of specified
* shape & order
* @param value The Matrix to fill. Its size is increased if necessary
* @param shape 2D-vector defined as (nrows, ncols)
* @param order Defines whether the stream of bytes is interpreted as moving
* across the rows first (RowMajor) or down columns first (ColumnMajor)
* @return A reference to the BinaryStreamReader object
*/
BinaryStreamReader &
BinaryStreamReader::read(Kernel::Matrix<float> &value,
const std::vector<int32_t> &shape,
BinaryStreamReader::MatrixOrdering order) {
readFromStream(m_istrm, value, shape, order);
return *this;
}
/**
* Read a stream of doubles and interpret them as a 2D matrix of specified
* shape & order
* @param value The Matrix to fill. Its size is increased if necessary
* @param shape 2D-vector defined as (nrows, ncols)
* @param order Defines whether the stream of bytes is interpreted as moving
* across the rows first (RowMajor) or down columns first (ColumnMajor)
* @return A reference to the BinaryStreamReader object
*/
BinaryStreamReader &
BinaryStreamReader::read(Kernel::Matrix<double> &value,
const std::vector<int32_t> &shape,
BinaryStreamReader::MatrixOrdering order) {
readFromStream(m_istrm, value, shape, order);
return *this;
}
} // namespace Kernel
} // namespace Mantid