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") \
64 #define GET_MATRIX(fname, sg_type, datatype) \
65 void CBinaryFile::fname(sg_type*& matrix, int32_t& num_feat, int32_t& num_vec) \
68 SG_ERROR("File invalid.\n") \
69 TSGDataType dtype(CT_SCALAR, ST_NONE, PT_BOOL); read_header(&dtype); \
70 if (dtype!=datatype) \
71 SG_ERROR("Datatype mismatch\n") \
73 if (fread(&num_feat, sizeof(int32_t), 1, file)!=1 || \
74 fread(&num_vec, sizeof(int32_t), 1, file)!=1) \
75 SG_ERROR("Failed to read Matrix dimensions\n") \
76 matrix=SG_MALLOC(sg_type, int64_t(num_feat)*num_vec); \
77 if (fread(matrix, sizeof(sg_type)*num_feat, num_vec, file)!=(size_t) num_vec) \
78 SG_ERROR("Failed to read Matrix\n") \
95 #define GET_NDARRAY(fname,sg_type,datatype) \
96 void CBinaryFile::fname(sg_type *& array, int32_t *& dims,int32_t & num_dims)\
101 SG_ERROR("File invalid.\n") \
103 TSGDataType dtype(CT_SCALAR, ST_NONE, PT_BOOL); \
104 read_header(&dtype); \
106 if (dtype!=datatype) \
107 SG_ERROR("Datatype mismatch\n") \
109 if (fread(&num_dims,sizeof(int32_t),1,file) != 1) \
110 SG_ERROR("Failed to read number of dimensions") \
112 dims = SG_MALLOC(int32_t, num_dims); \
113 if (fread(dims,sizeof(int32_t),num_dims,file) != (size_t)num_dims) \
114 SG_ERROR("Failed to read sizes of dimensions!") \
116 for (int32_t i = 0;i < num_dims;i++) \
119 array = SG_MALLOC(sg_type, total); \
120 if (fread(array,sizeof(sg_type),total,file) != (size_t)total) \
121 SG_ERROR("Failed to read array data!") \
133 #define GET_SPARSEMATRIX(fname, sg_type, datatype) \
134 void CBinaryFile::fname(SGSparseVector<sg_type>*& matrix, int32_t& num_feat, int32_t& num_vec) \
137 SG_ERROR("File invalid.\n") \
139 TSGDataType dtype(CT_SCALAR, ST_NONE, PT_BOOL); read_header(&dtype); \
140 if (dtype!=datatype) \
141 SG_ERROR("Datatype mismatch\n") \
143 if (fread(&num_vec, sizeof(int32_t), 1, file)!=1) \
144 SG_ERROR("Failed to read number of vectors\n") \
146 matrix=SG_MALLOC(SGSparseVector<sg_type>, num_vec); \
148 for (int32_t i=0; i<num_vec; i++) \
150 new (&matrix[i]) SGSparseVector<sg_type>(); \
152 if (fread(&len, sizeof(int32_t), 1, file)!=1) \
153 SG_ERROR("Failed to read sparse vector length of vector idx=%d\n", i) \
154 matrix[i].num_feat_entries=len; \
155 SGSparseVectorEntry<sg_type>* vec = SG_MALLOC(SGSparseVectorEntry<sg_type>, len); \
156 if (fread(vec, sizeof(SGSparseVectorEntry<sg_type>), len, file)!= (size_t) len) \
157 SG_ERROR("Failed to read sparse vector %d\n", i) \
158 matrix[i].features=vec; \
159 num_feat = CMath::max(num_feat, matrix[i].get_num_dimensions()); \
175 #undef GET_SPARSEMATRIX
178 #define GET_STRING_LIST(fname, sg_type, datatype) \
179 void CBinaryFile::fname(SGString<sg_type>*& strings, int32_t& num_str, int32_t& max_string_len) \
186 SG_ERROR("File invalid.\n") \
188 TSGDataType dtype(CT_SCALAR, ST_NONE, PT_BOOL); read_header(&dtype); \
189 if (dtype!=datatype) \
190 SG_ERROR("Datatype mismatch\n") \
192 if (fread(&num_str, sizeof(int32_t), 1, file)!=1) \
193 SG_ERROR("Failed to read number of strings\n") \
195 strings=SG_MALLOC(SGString<sg_type>, num_str); \
197 for (int32_t i=0; i<num_str; i++) \
200 if (fread(&len, sizeof(int32_t), 1, file)!=1) \
201 SG_ERROR("Failed to read string length of string with idx=%d\n", i) \
202 strings[i].slen=len; \
203 sg_type* str = SG_MALLOC(sg_type, len); \
204 if (fread(str, sizeof(sg_type), len, file)!= (size_t) len) \
205 SG_ERROR("Failed to read string %d\n", i) \
206 strings[i].string=str; \
222 #undef GET_STRING_LIST
226 #define SET_VECTOR(fname, sg_type, dtype) \
227 void CBinaryFile::fname(const sg_type* vec, int32_t len) \
229 if (!(file && vec)) \
230 SG_ERROR("File or vector invalid.\n") \
232 TSGDataType t dtype; write_header(&t); \
234 if (fwrite(&len, sizeof(int32_t), 1, file)!=1 || \
235 fwrite(vec, sizeof(sg_type), len, file)!=(size_t) len) \
236 SG_ERROR("Failed to write vector\n") \
238 SET_VECTOR(set_vector, int8_t, (CT_VECTOR, ST_NONE, PT_INT8))
239 SET_VECTOR(set_vector, uint8_t, (CT_VECTOR, ST_NONE, PT_UINT8))
240 SET_VECTOR(set_vector,
char, (CT_VECTOR, ST_NONE, PT_CHAR))
241 SET_VECTOR(set_vector, int32_t, (CT_VECTOR, ST_NONE, PT_INT32))
242 SET_VECTOR(set_vector, uint32_t, (CT_VECTOR, ST_NONE, PT_UINT32))
243 SET_VECTOR(set_vector, float32_t, (CT_VECTOR, ST_NONE, PT_FLOAT32))
244 SET_VECTOR(set_vector, float64_t, (CT_VECTOR, ST_NONE, PT_FLOAT64))
245 SET_VECTOR(set_vector, floatmax_t, (CT_VECTOR, ST_NONE, PT_FLOATMAX))
246 SET_VECTOR(set_vector, int16_t, (CT_VECTOR, ST_NONE, PT_INT16))
247 SET_VECTOR(set_vector, uint16_t, (CT_VECTOR, ST_NONE, PT_INT16))
248 SET_VECTOR(set_vector, int64_t, (CT_VECTOR, ST_NONE, PT_INT64))
249 SET_VECTOR(set_vector, uint64_t, (CT_VECTOR, ST_NONE, PT_UINT64))
252 #define SET_MATRIX(fname, sg_type, dtype) \
253 void CBinaryFile::fname(const sg_type* matrix, int32_t num_feat, int32_t num_vec) \
255 if (!(file && matrix)) \
256 SG_ERROR("File or matrix invalid.\n") \
258 TSGDataType t dtype; write_header(&t); \
260 if (fwrite(&num_feat, sizeof(int32_t), 1, file)!=1 || \
261 fwrite(&num_vec, sizeof(int32_t), 1, file)!=1 || \
262 fwrite(matrix, sizeof(sg_type)*num_feat, num_vec, file)!=(size_t) num_vec) \
263 SG_ERROR("Failed to write Matrix\n") \
265 SET_MATRIX(set_matrix,
char, (CT_MATRIX, ST_NONE, PT_CHAR))
266 SET_MATRIX(set_matrix, uint8_t, (CT_MATRIX, ST_NONE, PT_UINT8))
267 SET_MATRIX(set_matrix, int8_t, (CT_MATRIX, ST_NONE, PT_INT8))
268 SET_MATRIX(set_matrix, int32_t, (CT_MATRIX, ST_NONE, PT_INT32))
269 SET_MATRIX(set_matrix, uint32_t, (CT_MATRIX, ST_NONE, PT_INT32))
270 SET_MATRIX(set_matrix, int64_t, (CT_MATRIX, ST_NONE, PT_INT64))
271 SET_MATRIX(set_matrix, uint64_t, (CT_MATRIX, ST_NONE, PT_INT64))
272 SET_MATRIX(set_matrix, int16_t, (CT_MATRIX, ST_NONE, PT_INT16))
273 SET_MATRIX(set_matrix, uint16_t, (CT_MATRIX, ST_NONE, PT_INT16))
274 SET_MATRIX(set_matrix, float32_t, (CT_MATRIX, ST_NONE, PT_FLOAT32))
275 SET_MATRIX(set_matrix, float64_t, (CT_MATRIX, ST_NONE, PT_FLOAT64))
276 SET_MATRIX(set_matrix, floatmax_t, (CT_MATRIX, ST_NONE, PT_FLOATMAX))
279 #define SET_NDARRAY(fname,sg_type,datatype) \
280 void CBinaryFile::fname(const sg_type * array, int32_t * dims,int32_t num_dims) \
285 SG_ERROR("File invalid.\n") \
288 SG_ERROR("Invalid array!\n") \
290 TSGDataType t datatype; \
293 if (fwrite(&num_dims,sizeof(int32_t),1,file) != 1) \
294 SG_ERROR("Failed to write number of dimensions!\n") \
296 if (fwrite(dims,sizeof(int32_t),num_dims,file) != (size_t)num_dims) \
297 SG_ERROR("Failed to write sizes of dimensions!\n") \
299 for (int32_t i = 0;i < num_dims;i++) \
302 if (fwrite(array,sizeof(sg_type),total,file) != (size_t)total) \
303 SG_ERROR("Failed to write array data!\n") \
306 SET_NDARRAY(set_ndarray,uint8_t,(CT_NDARRAY, ST_NONE, PT_UINT8));
307 SET_NDARRAY(set_ndarray,
char,(CT_NDARRAY, ST_NONE, PT_CHAR));
308 SET_NDARRAY(set_ndarray,int32_t,(CT_NDARRAY, ST_NONE, PT_INT32));
309 SET_NDARRAY(set_ndarray,int16_t,(CT_NDARRAY, ST_NONE, PT_INT16));
310 SET_NDARRAY(set_ndarray,uint16_t,(CT_NDARRAY, ST_NONE, PT_UINT16));
311 SET_NDARRAY(set_ndarray,float32_t,(CT_NDARRAY, ST_NONE, PT_FLOAT32));
312 SET_NDARRAY(set_ndarray,float64_t,(CT_NDARRAY, ST_NONE, PT_FLOAT64));
315 #define SET_SPARSEMATRIX(fname, sg_type, dtype) \
316 void CBinaryFile::fname(const SGSparseVector<sg_type>* matrix, \
317 int32_t num_feat, int32_t num_vec) \
319 if (!(file && matrix)) \
320 SG_ERROR("File or matrix invalid.\n") \
322 TSGDataType t dtype; write_header(&t); \
324 if (fwrite(&num_vec, sizeof(int32_t), 1, file)!=1) \
325 SG_ERROR("Failed to write Sparse Matrix\n") \
327 for (int32_t i=0; i<num_vec; i++) \
329 SGSparseVectorEntry<sg_type>* vec = matrix[i].features; \
330 int32_t len=matrix[i].num_feat_entries; \
331 if ((fwrite(&len, sizeof(int32_t), 1, file)!=1) || \
332 (fwrite(vec, sizeof(SGSparseVectorEntry<sg_type>), len, file)!= (size_t) len)) \
333 SG_ERROR("Failed to write Sparse Matrix\n") \
338 SET_SPARSEMATRIX(set_sparse_matrix, uint8_t, (CT_MATRIX, ST_NONE, PT_UINT8))
340 SET_SPARSEMATRIX(set_sparse_matrix, int32_t, (CT_MATRIX, ST_NONE, PT_INT32))
341 SET_SPARSEMATRIX(set_sparse_matrix, uint32_t, (CT_MATRIX, ST_NONE, PT_INT32))
342 SET_SPARSEMATRIX(set_sparse_matrix, int64_t, (CT_MATRIX, ST_NONE, PT_INT64))
343 SET_SPARSEMATRIX(set_sparse_matrix, uint64_t, (CT_MATRIX, ST_NONE, PT_INT64))
344 SET_SPARSEMATRIX(set_sparse_matrix, int16_t, (CT_MATRIX, ST_NONE, PT_INT16))
345 SET_SPARSEMATRIX(set_sparse_matrix, uint16_t, (CT_MATRIX, ST_NONE, PT_INT16))
346 SET_SPARSEMATRIX(set_sparse_matrix, float32_t, (CT_MATRIX, ST_NONE, PT_FLOAT32))
347 SET_SPARSEMATRIX(set_sparse_matrix, float64_t, (CT_MATRIX, ST_NONE, PT_FLOAT64))
348 SET_SPARSEMATRIX(set_sparse_matrix, floatmax_t, (CT_MATRIX, ST_NONE, PT_FLOATMAX))
349 #undef SET_SPARSEMATRIX
351 #define SET_STRING_LIST(fname, sg_type, dtype) \
352 void CBinaryFile::fname(const SGString<sg_type>* strings, int32_t num_str) \
354 if (!(file && strings)) \
355 SG_ERROR("File or strings invalid.\n") \
357 TSGDataType t dtype; write_header(&t); \
358 for (int32_t i=0; i<num_str; i++) \
360 int32_t len = strings[i].slen; \
361 if ((fwrite(&len, sizeof(int32_t), 1, file)!=1) || \
362 (fwrite(strings[i].string, sizeof(sg_type), len, file)!= (size_t) len)) \
363 SG_ERROR("Failed to write Sparse Matrix\n") \
367 SET_STRING_LIST(set_string_list, uint8_t, (CT_VECTOR, ST_NONE, PT_UINT8))
368 SET_STRING_LIST(set_string_list, int8_t, (CT_VECTOR, ST_NONE, PT_INT8))
369 SET_STRING_LIST(set_string_list, int32_t, (CT_VECTOR, ST_NONE, PT_INT32))
370 SET_STRING_LIST(set_string_list, uint32_t, (CT_VECTOR, ST_NONE, PT_INT32))
371 SET_STRING_LIST(set_string_list, int64_t, (CT_VECTOR, ST_NONE, PT_INT64))
372 SET_STRING_LIST(set_string_list, uint64_t, (CT_VECTOR, ST_NONE, PT_INT64))
373 SET_STRING_LIST(set_string_list, int16_t, (CT_VECTOR, ST_NONE, PT_INT16))
374 SET_STRING_LIST(set_string_list, uint16_t, (CT_VECTOR, ST_NONE, PT_INT16))
375 SET_STRING_LIST(set_string_list, float32_t, (CT_VECTOR, ST_NONE, PT_FLOAT32))
376 SET_STRING_LIST(set_string_list, float64_t, (CT_VECTOR, ST_NONE, PT_FLOAT64))
377 SET_STRING_LIST(set_string_list, floatmax_t, (CT_VECTOR, ST_NONE, PT_FLOATMAX))
378 #undef SET_STRING_LIST
397 if (fseek(
file, 0L, SEEK_SET)!=0)
403 if (fread(&fourcc,
sizeof(
char), 4,
file)!=4)
406 if (fread(&endian,
sizeof(uint16_t), 1,
file)!=1)
413 if (strncmp(fourcc,
"SG01", 4))
422 const char* fourcc=
"SG01";
423 uint16_t endian=0x1234;
425 if (!((fwrite(fourcc,
sizeof(
char), 4,
file)==4) &&
426 (fwrite(&endian,
sizeof(uint16_t), 1,
file)==1) &&