15 using namespace shogun;
34 #define GET_VECTOR(fname, sg_type, datatype) \
35 void CBinaryFile::fname(sg_type*& vec, int32_t& len) \
38 SG_ERROR("File invalid.\n"); \
39 TSGDataType dtype(CT_SCALAR, ST_NONE, PT_BOOL); read_header(&dtype); \
40 if (dtype!=datatype) \
41 SG_ERROR("Datatype mismatch\n"); \
43 if (fread(&len, sizeof(int32_t), 1, file)!=1) \
44 SG_ERROR("Failed to read vector length\n"); \
45 vec=SG_MALLOC(sg_type, len); \
46 if (fread(vec, sizeof(sg_type), len, file)!=(size_t) len) \
47 SG_ERROR("Failed to read Matrix\n"); \
59 #define GET_MATRIX(fname, sg_type, datatype) \
60 void CBinaryFile::fname(sg_type*& matrix, int32_t& num_feat, int32_t& num_vec) \
63 SG_ERROR("File invalid.\n"); \
64 TSGDataType dtype(CT_SCALAR, ST_NONE, PT_BOOL); read_header(&dtype); \
65 if (dtype!=datatype) \
66 SG_ERROR("Datatype mismatch\n"); \
68 if (fread(&num_feat, sizeof(int32_t), 1, file)!=1 || \
69 fread(&num_vec, sizeof(int32_t), 1, file)!=1) \
70 SG_ERROR("Failed to read Matrix dimensions\n"); \
71 matrix=SG_MALLOC(sg_type, int64_t(num_feat)*num_vec); \
72 if (fread(matrix, sizeof(sg_type)*num_feat, num_vec, file)!=(size_t) num_vec) \
73 SG_ERROR("Failed to read Matrix\n"); \
90 #define GET_NDARRAY(fname,sg_type,datatype) \
91 void CBinaryFile::fname(sg_type *& array, int32_t *& dims,int32_t & num_dims)\
96 SG_ERROR("File invalid.\n"); \
98 TSGDataType dtype(CT_SCALAR, ST_NONE, PT_BOOL); \
99 read_header(&dtype); \
101 if (dtype!=datatype) \
102 SG_ERROR("Datatype mismatch\n"); \
104 if (fread(&num_dims,sizeof(int32_t),1,file) != 1) \
105 SG_ERROR("Failed to read number of dimensions"); \
107 dims = SG_MALLOC(int32_t, num_dims); \
108 if (fread(dims,sizeof(int32_t),num_dims,file) != (size_t)num_dims) \
109 SG_ERROR("Failed to read sizes of dimensions!"); \
111 for (int32_t i = 0;i < num_dims;i++) \
114 array = SG_MALLOC(sg_type, total); \
115 if (fread(array,sizeof(sg_type),total,file) != (size_t)total) \
116 SG_ERROR("Failed to read array data!"); \
128 #define GET_SPARSEMATRIX(fname, sg_type, datatype) \
129 void CBinaryFile::fname(SGSparseVector<sg_type>*& matrix, int32_t& num_feat, int32_t& num_vec) \
132 SG_ERROR("File invalid.\n"); \
134 TSGDataType dtype(CT_SCALAR, ST_NONE, PT_BOOL); read_header(&dtype); \
135 if (dtype!=datatype) \
136 SG_ERROR("Datatype mismatch\n"); \
138 if (fread(&num_vec, sizeof(int32_t), 1, file)!=1) \
139 SG_ERROR("Failed to read number of vectors\n"); \
141 matrix=SG_MALLOC(SGSparseVector<sg_type>, num_vec); \
143 for (int32_t i=0; i<num_vec; i++) \
146 if (fread(&len, sizeof(int32_t), 1, file)!=1) \
147 SG_ERROR("Failed to read sparse vector length of vector idx=%d\n", i); \
148 matrix[i].num_feat_entries=len; \
149 SGSparseVectorEntry<sg_type>* vec = SG_MALLOC(SGSparseVectorEntry<sg_type>, len); \
150 if (fread(vec, sizeof(SGSparseVectorEntry<sg_type>), len, file)!= (size_t) len) \
151 SG_ERROR("Failed to read sparse vector %d\n", i); \
152 matrix[i].features=vec; \
168 #undef GET_SPARSEMATRIX
171 #define GET_STRING_LIST(fname, sg_type, datatype) \
172 void CBinaryFile::fname(SGString<sg_type>*& strings, int32_t& num_str, int32_t& max_string_len) \
179 SG_ERROR("File invalid.\n"); \
181 TSGDataType dtype(CT_SCALAR, ST_NONE, PT_BOOL); read_header(&dtype); \
182 if (dtype!=datatype) \
183 SG_ERROR("Datatype mismatch\n"); \
185 if (fread(&num_str, sizeof(int32_t), 1, file)!=1) \
186 SG_ERROR("Failed to read number of strings\n"); \
188 strings=SG_MALLOC(SGString<sg_type>, num_str); \
190 for (int32_t i=0; i<num_str; i++) \
193 if (fread(&len, sizeof(int32_t), 1, file)!=1) \
194 SG_ERROR("Failed to read string length of string with idx=%d\n", i); \
195 strings[i].slen=len; \
196 sg_type* str = SG_MALLOC(sg_type, len); \
197 if (fread(str, sizeof(sg_type), len, file)!= (size_t) len) \
198 SG_ERROR("Failed to read string %d\n", i); \
199 strings[i].string=str; \
215 #undef GET_STRING_LIST
219 #define SET_VECTOR(fname, sg_type, dtype) \
220 void CBinaryFile::fname(const sg_type* vec, int32_t len) \
222 if (!(file && vec)) \
223 SG_ERROR("File or vector invalid.\n"); \
225 TSGDataType t dtype; write_header(&t); \
227 if (fwrite(&len, sizeof(int32_t), 1, file)!=1 || \
228 fwrite(vec, sizeof(sg_type), len, file)!=(size_t) len) \
229 SG_ERROR("Failed to write vector\n"); \
231 SET_VECTOR(set_vector, uint8_t, (CT_VECTOR, ST_NONE, PT_UINT8))
232 SET_VECTOR(set_vector,
char, (CT_VECTOR, ST_NONE, PT_CHAR))
233 SET_VECTOR(set_vector, int32_t, (CT_VECTOR, ST_NONE, PT_INT32))
234 SET_VECTOR(set_vector, float32_t, (CT_VECTOR, ST_NONE, PT_FLOAT32))
235 SET_VECTOR(set_vector, float64_t, (CT_VECTOR, ST_NONE, PT_FLOAT64))
236 SET_VECTOR(set_vector, int16_t, (CT_VECTOR, ST_NONE, PT_INT16))
237 SET_VECTOR(set_vector, uint16_t, (CT_VECTOR, ST_NONE, PT_INT16))
240 #define SET_MATRIX(fname, sg_type, dtype) \
241 void CBinaryFile::fname(const sg_type* matrix, int32_t num_feat, int32_t num_vec) \
243 if (!(file && matrix)) \
244 SG_ERROR("File or matrix invalid.\n"); \
246 TSGDataType t dtype; write_header(&t); \
248 if (fwrite(&num_feat, sizeof(int32_t), 1, file)!=1 || \
249 fwrite(&num_vec, sizeof(int32_t), 1, file)!=1 || \
250 fwrite(matrix, sizeof(sg_type)*num_feat, num_vec, file)!=(size_t) num_vec) \
251 SG_ERROR("Failed to write Matrix\n"); \
253 SET_MATRIX(set_matrix,
char, (CT_MATRIX, ST_NONE, PT_CHAR))
254 SET_MATRIX(set_matrix, uint8_t, (CT_MATRIX, ST_NONE, PT_UINT8))
255 SET_MATRIX(set_int8_matrix, int8_t, (CT_MATRIX, ST_NONE, PT_INT8))
256 SET_MATRIX(set_matrix, int32_t, (CT_MATRIX, ST_NONE, PT_INT32))
257 SET_MATRIX(set_uint_matrix, uint32_t, (CT_MATRIX, ST_NONE, PT_INT32))
258 SET_MATRIX(set_long_matrix, int64_t, (CT_MATRIX, ST_NONE, PT_INT64))
259 SET_MATRIX(set_ulong_matrix, uint64_t, (CT_MATRIX, ST_NONE, PT_INT64))
260 SET_MATRIX(set_matrix, int16_t, (CT_MATRIX, ST_NONE, PT_INT16))
261 SET_MATRIX(set_matrix, uint16_t, (CT_MATRIX, ST_NONE, PT_INT16))
262 SET_MATRIX(set_matrix, float32_t, (CT_MATRIX, ST_NONE, PT_FLOAT32))
263 SET_MATRIX(set_matrix, float64_t, (CT_MATRIX, ST_NONE, PT_FLOAT64))
264 SET_MATRIX(set_longreal_matrix, floatmax_t, (CT_MATRIX, ST_NONE, PT_FLOATMAX))
267 #define SET_NDARRAY(fname,sg_type,datatype) \
268 void CBinaryFile::fname(const sg_type * array, int32_t * dims,int32_t num_dims) \
273 SG_ERROR("File invalid.\n"); \
276 SG_ERROR("Invalid array!\n"); \
278 TSGDataType t datatype; \
281 if (fwrite(&num_dims,sizeof(int32_t),1,file) != 1) \
282 SG_ERROR("Failed to write number of dimensions!\n"); \
284 if (fwrite(dims,sizeof(int32_t),num_dims,file) != (size_t)num_dims) \
285 SG_ERROR("Failed to write sizes of dimensions!\n"); \
287 for (int32_t i = 0;i < num_dims;i++) \
290 if (fwrite(array,sizeof(sg_type),total,file) != (size_t)total) \
291 SG_ERROR("Failed to write array data!\n"); \
294 SET_NDARRAY(set_ndarray,uint8_t,(CT_NDARRAY, ST_NONE, PT_UINT8));
295 SET_NDARRAY(set_ndarray,
char,(CT_NDARRAY, ST_NONE, PT_CHAR));
296 SET_NDARRAY(set_ndarray,int32_t,(CT_NDARRAY, ST_NONE, PT_INT32));
297 SET_NDARRAY(set_ndarray,int16_t,(CT_NDARRAY, ST_NONE, PT_INT16));
298 SET_NDARRAY(set_ndarray,uint16_t,(CT_NDARRAY, ST_NONE, PT_UINT16));
299 SET_NDARRAY(set_ndarray,float32_t,(CT_NDARRAY, ST_NONE, PT_FLOAT32));
300 SET_NDARRAY(set_ndarray,float64_t,(CT_NDARRAY, ST_NONE, PT_FLOAT64));
303 #define SET_SPARSEMATRIX(fname, sg_type, dtype) \
304 void CBinaryFile::fname(const SGSparseVector<sg_type>* matrix, \
305 int32_t num_feat, int32_t num_vec) \
307 if (!(file && matrix)) \
308 SG_ERROR("File or matrix invalid.\n"); \
310 TSGDataType t dtype; write_header(&t); \
312 if (fwrite(&num_vec, sizeof(int32_t), 1, file)!=1) \
313 SG_ERROR("Failed to write Sparse Matrix\n"); \
315 for (int32_t i=0; i<num_vec; i++) \
317 SGSparseVectorEntry<sg_type>* vec = matrix[i].features; \
318 int32_t len=matrix[i].num_feat_entries; \
319 if ((fwrite(&len, sizeof(int32_t), 1, file)!=1) || \
320 (fwrite(vec, sizeof(SGSparseVectorEntry<sg_type>), len, file)!= (size_t) len)) \
321 SG_ERROR("Failed to write Sparse Matrix\n"); \
326 SET_SPARSEMATRIX(set_sparse_matrix, uint8_t, (CT_MATRIX, ST_NONE, PT_UINT8))
327 SET_SPARSEMATRIX(set_int8_sparsematrix, int8_t, (CT_MATRIX, ST_NONE, PT_INT8))
328 SET_SPARSEMATRIX(set_sparse_matrix, int32_t, (CT_MATRIX, ST_NONE, PT_INT32))
329 SET_SPARSEMATRIX(set_uint_sparsematrix, uint32_t, (CT_MATRIX, ST_NONE, PT_INT32))
330 SET_SPARSEMATRIX(set_long_sparsematrix, int64_t, (CT_MATRIX, ST_NONE, PT_INT64))
331 SET_SPARSEMATRIX(set_ulong_sparsematrix, uint64_t, (CT_MATRIX, ST_NONE, PT_INT64))
332 SET_SPARSEMATRIX(set_sparse_matrix, int16_t, (CT_MATRIX, ST_NONE, PT_INT16))
333 SET_SPARSEMATRIX(set_sparse_matrix, uint16_t, (CT_MATRIX, ST_NONE, PT_INT16))
334 SET_SPARSEMATRIX(set_sparse_matrix, float32_t, (CT_MATRIX, ST_NONE, PT_FLOAT32))
335 SET_SPARSEMATRIX(set_sparse_matrix, float64_t, (CT_MATRIX, ST_NONE, PT_FLOAT64))
336 SET_SPARSEMATRIX(set_longreal_sparsematrix, floatmax_t, (CT_MATRIX, ST_NONE, PT_FLOATMAX))
337 #undef SET_SPARSEMATRIX
339 #define SET_STRING_LIST(fname, sg_type, dtype) \
340 void CBinaryFile::fname(const SGString<sg_type>* strings, int32_t num_str) \
342 if (!(file && strings)) \
343 SG_ERROR("File or strings invalid.\n"); \
345 TSGDataType t dtype; write_header(&t); \
346 for (int32_t i=0; i<num_str; i++) \
348 int32_t len = strings[i].slen; \
349 if ((fwrite(&len, sizeof(int32_t), 1, file)!=1) || \
350 (fwrite(strings[i].string, sizeof(sg_type), len, file)!= (size_t) len)) \
351 SG_ERROR("Failed to write Sparse Matrix\n"); \
355 SET_STRING_LIST(set_string_list, uint8_t, (CT_VECTOR, ST_NONE, PT_UINT8))
356 SET_STRING_LIST(set_int8_string_list, int8_t, (CT_VECTOR, ST_NONE, PT_INT8))
357 SET_STRING_LIST(set_string_list, int32_t, (CT_VECTOR, ST_NONE, PT_INT32))
358 SET_STRING_LIST(set_uint_string_list, uint32_t, (CT_VECTOR, ST_NONE, PT_INT32))
359 SET_STRING_LIST(set_long_string_list, int64_t, (CT_VECTOR, ST_NONE, PT_INT64))
360 SET_STRING_LIST(set_ulong_string_list, uint64_t, (CT_VECTOR, ST_NONE, PT_INT64))
361 SET_STRING_LIST(set_string_list, int16_t, (CT_VECTOR, ST_NONE, PT_INT16))
362 SET_STRING_LIST(set_string_list, uint16_t, (CT_VECTOR, ST_NONE, PT_INT16))
363 SET_STRING_LIST(set_string_list, float32_t, (CT_VECTOR, ST_NONE, PT_FLOAT32))
364 SET_STRING_LIST(set_string_list, float64_t, (CT_VECTOR, ST_NONE, PT_FLOAT64))
365 SET_STRING_LIST(set_longreal_string_list, floatmax_t, (CT_VECTOR, ST_NONE, PT_FLOATMAX))
366 #undef SET_STRING_LIST
385 if (fseek(
file, 0L, SEEK_SET)!=0)
391 if (fread(&fourcc,
sizeof(
char), 4,
file)!=4)
394 if (fread(&endian,
sizeof(uint16_t), 1,
file)!=1)
401 if (strncmp(fourcc,
"SG01", 4))
410 const char* fourcc=
"SG01";
411 uint16_t endian=0x1234;
413 if (!((fwrite(fourcc,
sizeof(
char), 4,
file)==4) &&
414 (fwrite(&endian,
sizeof(uint16_t), 1,
file)==1) &&