20 #include <tensor/sdf.h>
26 template <
class number>
27 void write_raw_with_endian(std::ofstream &s,
const number *data,
size_t n)
29 s.write((
char *)data, n *
sizeof(number));
31 std::cerr <<
"I/O error when reading from stream " << s;
38 template <
class number>
39 void write_raw_with_endian(std::ofstream &s,
const number *data,
size_t n)
41 const int size =
sizeof(number);
43 s.write((
char *)data, n *
sizeof(number));
45 std::cerr <<
"I/O error when writing to stream " << s;
52 const int buffer_size = 1024;
53 const char *alias = (
char *)data;
54 char buffer[buffer_size];
57 size_t now = std::min<size_t>(n, buffer_size/size)*size;
58 for (
size_t i = 0; i < now; i+=size) {
60 buffer[i+3] = *(alias++);
61 buffer[i+2] = *(alias++);
62 buffer[i+1] = *(alias++);
63 buffer[i] = *(alias++);
64 }
else if (size == 8) {
65 buffer[i+7] = *(alias++);
66 buffer[i+6] = *(alias++);
67 buffer[i+5] = *(alias++);
68 buffer[i+4] = *(alias++);
69 buffer[i+3] = *(alias++);
70 buffer[i+2] = *(alias++);
71 buffer[i+1] = *(alias++);
72 buffer[i] = *(alias++);
74 for (
size_t j = size; j--; ) {
75 buffer[i+j] = *(alias++);
82 std::cerr <<
"I/O error when reading from stream " << s;
95 OutDataFile::OutDataFile(
const std::string &a_filename,
int flags) :
96 DataFile(a_filename, flags)
98 bool existed = file_exists(actual_filename());
99 _stream.open(actual_filename().c_str(), std::ofstream::app | std::ofstream::binary);
105 OutDataFile::~OutDataFile()
120 OutDataFile::write_raw(
const char *data,
size_t n)
123 write_raw_with_endian(_stream, data, n);
127 OutDataFile::write_raw(
const int *data,
size_t n)
130 write_raw_with_endian(_stream, data, n);
134 OutDataFile::write_raw(
const long *data,
size_t n)
137 write_raw_with_endian(_stream, data, n);
141 OutDataFile::write_raw(
const size_t *data,
size_t n)
144 write_raw_with_endian(_stream, data, n);
148 OutDataFile::write_raw(
const double *data,
size_t n)
151 write_raw_with_endian(_stream, data, n);
155 OutDataFile::write_raw(
const cdouble *data,
size_t n)
158 write_raw_with_endian(_stream, (
double*)data, 2*n);
162 OutDataFile::write_variable_name(
const std::string &name)
164 char buffer[var_name_size];
165 memset(buffer, 0, var_name_size);
166 strncpy(buffer, name.c_str(), std::min<size_t>(var_name_size - 1, name.size()));
167 write_raw(buffer, var_name_size);
171 OutDataFile::write_tag(
const std::string &name,
size_t type)
173 write_variable_name(name);
177 template<
class Vector>
178 void OutDataFile::dump_vector(
const Vector &v)
181 write_raw(v.begin(), v.size());
185 OutDataFile::dump(
const RTensor &t,
const std::string &name)
187 write_tag(name, TAG_RTENSOR);
193 OutDataFile::dump(
const CTensor &t,
const std::string &name)
195 write_tag(name, TAG_CTENSOR);
201 OutDataFile::dump(
const std::vector<RTensor> &m,
const std::string &name)
204 write_tag(name, TAG_RTENSOR_VECTOR);
206 for (
size_t k = 0; k < l; k++) {
212 OutDataFile::dump(
const std::vector<CTensor> &m,
const std::string &name)
215 write_tag(name, TAG_CTENSOR_VECTOR);
217 for (
size_t k = 0; k < l; k++) {
223 OutDataFile::dump(
const double v,
const std::string &name)
231 OutDataFile::dump(
const cdouble v,
const std::string &name)
239 OutDataFile::dump(
size_t v,
const std::string &name)
241 dump((
double)v, name);
245 OutDataFile::dump(
int v,
const std::string &name)
247 dump((
double)v, name);
251 OutDataFile::write_header()
253 char tag[7] =
"sdf ";
254 tag[3] =
sizeof(int) +
'0';
255 tag[4] =
sizeof(long) +
'0';
256 tag[5] = endian +
'0';
258 write_variable_name(tag);
Real Tensor with elements of type "double".
Complex Tensor with elements of type "cdouble".
const Indices & dimensions() const
Return Tensor dimensions.
elt_t & at(index i)
Return a mutable reference to an element of a 1D Tensor.