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++) \
145 new (&matrix[i]) SGSparseVector<sg_type>(); \
147 if (fread(&len, sizeof(int32_t), 1, file)!=1) \
148 SG_ERROR("Failed to read sparse vector length of vector idx=%d\n", i); \
149 matrix[i].num_feat_entries=len; \
150 SGSparseVectorEntry<sg_type>* vec = SG_MALLOC(SGSparseVectorEntry<sg_type>, len); \
151 if (fread(vec, sizeof(SGSparseVectorEntry<sg_type>), len, file)!= (size_t) len) \
152 SG_ERROR("Failed to read sparse vector %d\n", i); \
153 matrix[i].features=vec; \
169 #undef GET_SPARSEMATRIX
172 #define GET_STRING_LIST(fname, sg_type, datatype) \
173 void CBinaryFile::fname(SGString<sg_type>*& strings, int32_t& num_str, int32_t& max_string_len) \
180 SG_ERROR("File invalid.\n"); \
182 TSGDataType dtype(CT_SCALAR, ST_NONE, PT_BOOL); read_header(&dtype); \
183 if (dtype!=datatype) \
184 SG_ERROR("Datatype mismatch\n"); \
186 if (fread(&num_str, sizeof(int32_t), 1, file)!=1) \
187 SG_ERROR("Failed to read number of strings\n"); \
189 strings=SG_MALLOC(SGString<sg_type>, num_str); \
191 for (int32_t i=0; i<num_str; i++) \
194 if (fread(&len, sizeof(int32_t), 1, file)!=1) \
195 SG_ERROR("Failed to read string length of string with idx=%d\n", i); \
196 strings[i].slen=len; \
197 sg_type* str = SG_MALLOC(sg_type, len); \
198 if (fread(str, sizeof(sg_type), len, file)!= (size_t) len) \
199 SG_ERROR("Failed to read string %d\n", i); \
200 strings[i].string=str; \
216 #undef GET_STRING_LIST
220 #define SET_VECTOR(fname, sg_type, dtype) \
221 void CBinaryFile::fname(const sg_type* vec, int32_t len) \
223 if (!(file && vec)) \
224 SG_ERROR("File or vector invalid.\n"); \
226 TSGDataType t dtype; write_header(&t); \
228 if (fwrite(&len, sizeof(int32_t), 1, file)!=1 || \
229 fwrite(vec, sizeof(sg_type), len, file)!=(size_t) len) \
230 SG_ERROR("Failed to write vector\n"); \
232 SET_VECTOR(set_vector, uint8_t, (CT_VECTOR, ST_NONE, PT_UINT8))
233 SET_VECTOR(set_vector,
char, (CT_VECTOR, ST_NONE, PT_CHAR))
234 SET_VECTOR(set_vector, int32_t, (CT_VECTOR, ST_NONE, PT_INT32))
235 SET_VECTOR(set_vector, float32_t, (CT_VECTOR, ST_NONE, PT_FLOAT32))
236 SET_VECTOR(set_vector, float64_t, (CT_VECTOR, ST_NONE, PT_FLOAT64))
237 SET_VECTOR(set_vector, int16_t, (CT_VECTOR, ST_NONE, PT_INT16))
238 SET_VECTOR(set_vector, uint16_t, (CT_VECTOR, ST_NONE, PT_INT16))
241 #define SET_MATRIX(fname, sg_type, dtype) \
242 void CBinaryFile::fname(const sg_type* matrix, int32_t num_feat, int32_t num_vec) \
244 if (!(file && matrix)) \
245 SG_ERROR("File or matrix invalid.\n"); \
247 TSGDataType t dtype; write_header(&t); \
249 if (fwrite(&num_feat, sizeof(int32_t), 1, file)!=1 || \
250 fwrite(&num_vec, sizeof(int32_t), 1, file)!=1 || \
251 fwrite(matrix, sizeof(sg_type)*num_feat, num_vec, file)!=(size_t) num_vec) \
252 SG_ERROR("Failed to write Matrix\n"); \
254 SET_MATRIX(set_matrix,
char, (CT_MATRIX, ST_NONE, PT_CHAR))
255 SET_MATRIX(set_matrix, uint8_t, (CT_MATRIX, ST_NONE, PT_UINT8))
256 SET_MATRIX(set_int8_matrix, int8_t, (CT_MATRIX, ST_NONE, PT_INT8))
257 SET_MATRIX(set_matrix, int32_t, (CT_MATRIX, ST_NONE, PT_INT32))
258 SET_MATRIX(set_uint_matrix, uint32_t, (CT_MATRIX, ST_NONE, PT_INT32))
259 SET_MATRIX(set_long_matrix, int64_t, (CT_MATRIX, ST_NONE, PT_INT64))
260 SET_MATRIX(set_ulong_matrix, uint64_t, (CT_MATRIX, ST_NONE, PT_INT64))
261 SET_MATRIX(set_matrix, int16_t, (CT_MATRIX, ST_NONE, PT_INT16))
262 SET_MATRIX(set_matrix, uint16_t, (CT_MATRIX, ST_NONE, PT_INT16))
263 SET_MATRIX(set_matrix, float32_t, (CT_MATRIX, ST_NONE, PT_FLOAT32))
264 SET_MATRIX(set_matrix, float64_t, (CT_MATRIX, ST_NONE, PT_FLOAT64))
265 SET_MATRIX(set_longreal_matrix, floatmax_t, (CT_MATRIX, ST_NONE, PT_FLOATMAX))
268 #define SET_NDARRAY(fname,sg_type,datatype) \
269 void CBinaryFile::fname(const sg_type * array, int32_t * dims,int32_t num_dims) \
274 SG_ERROR("File invalid.\n"); \
277 SG_ERROR("Invalid array!\n"); \
279 TSGDataType t datatype; \
282 if (fwrite(&num_dims,sizeof(int32_t),1,file) != 1) \
283 SG_ERROR("Failed to write number of dimensions!\n"); \
285 if (fwrite(dims,sizeof(int32_t),num_dims,file) != (size_t)num_dims) \
286 SG_ERROR("Failed to write sizes of dimensions!\n"); \
288 for (int32_t i = 0;i < num_dims;i++) \
291 if (fwrite(array,sizeof(sg_type),total,file) != (size_t)total) \
292 SG_ERROR("Failed to write array data!\n"); \
295 SET_NDARRAY(set_ndarray,uint8_t,(CT_NDARRAY, ST_NONE, PT_UINT8));
296 SET_NDARRAY(set_ndarray,
char,(CT_NDARRAY, ST_NONE, PT_CHAR));
297 SET_NDARRAY(set_ndarray,int32_t,(CT_NDARRAY, ST_NONE, PT_INT32));
298 SET_NDARRAY(set_ndarray,int16_t,(CT_NDARRAY, ST_NONE, PT_INT16));
299 SET_NDARRAY(set_ndarray,uint16_t,(CT_NDARRAY, ST_NONE, PT_UINT16));
300 SET_NDARRAY(set_ndarray,float32_t,(CT_NDARRAY, ST_NONE, PT_FLOAT32));
301 SET_NDARRAY(set_ndarray,float64_t,(CT_NDARRAY, ST_NONE, PT_FLOAT64));
304 #define SET_SPARSEMATRIX(fname, sg_type, dtype) \
305 void CBinaryFile::fname(const SGSparseVector<sg_type>* matrix, \
306 int32_t num_feat, int32_t num_vec) \
308 if (!(file && matrix)) \
309 SG_ERROR("File or matrix invalid.\n"); \
311 TSGDataType t dtype; write_header(&t); \
313 if (fwrite(&num_vec, sizeof(int32_t), 1, file)!=1) \
314 SG_ERROR("Failed to write Sparse Matrix\n"); \
316 for (int32_t i=0; i<num_vec; i++) \
318 SGSparseVectorEntry<sg_type>* vec = matrix[i].features; \
319 int32_t len=matrix[i].num_feat_entries; \
320 if ((fwrite(&len, sizeof(int32_t), 1, file)!=1) || \
321 (fwrite(vec, sizeof(SGSparseVectorEntry<sg_type>), len, file)!= (size_t) len)) \
322 SG_ERROR("Failed to write Sparse Matrix\n"); \
327 SET_SPARSEMATRIX(set_sparse_matrix, uint8_t, (CT_MATRIX, ST_NONE, PT_UINT8))
328 SET_SPARSEMATRIX(set_int8_sparsematrix, int8_t, (CT_MATRIX, ST_NONE, PT_INT8))
329 SET_SPARSEMATRIX(set_sparse_matrix, int32_t, (CT_MATRIX, ST_NONE, PT_INT32))
330 SET_SPARSEMATRIX(set_uint_sparsematrix, uint32_t, (CT_MATRIX, ST_NONE, PT_INT32))
331 SET_SPARSEMATRIX(set_long_sparsematrix, int64_t, (CT_MATRIX, ST_NONE, PT_INT64))
332 SET_SPARSEMATRIX(set_ulong_sparsematrix, uint64_t, (CT_MATRIX, ST_NONE, PT_INT64))
333 SET_SPARSEMATRIX(set_sparse_matrix, int16_t, (CT_MATRIX, ST_NONE, PT_INT16))
334 SET_SPARSEMATRIX(set_sparse_matrix, uint16_t, (CT_MATRIX, ST_NONE, PT_INT16))
335 SET_SPARSEMATRIX(set_sparse_matrix, float32_t, (CT_MATRIX, ST_NONE, PT_FLOAT32))
336 SET_SPARSEMATRIX(set_sparse_matrix, float64_t, (CT_MATRIX, ST_NONE, PT_FLOAT64))
337 SET_SPARSEMATRIX(set_longreal_sparsematrix, floatmax_t, (CT_MATRIX, ST_NONE, PT_FLOATMAX))
338 #undef SET_SPARSEMATRIX
340 #define SET_STRING_LIST(fname, sg_type, dtype) \
341 void CBinaryFile::fname(const SGString<sg_type>* strings, int32_t num_str) \
343 if (!(file && strings)) \
344 SG_ERROR("File or strings invalid.\n"); \
346 TSGDataType t dtype; write_header(&t); \
347 for (int32_t i=0; i<num_str; i++) \
349 int32_t len = strings[i].slen; \
350 if ((fwrite(&len, sizeof(int32_t), 1, file)!=1) || \
351 (fwrite(strings[i].string, sizeof(sg_type), len, file)!= (size_t) len)) \
352 SG_ERROR("Failed to write Sparse Matrix\n"); \
356 SET_STRING_LIST(set_string_list, uint8_t, (CT_VECTOR, ST_NONE, PT_UINT8))
357 SET_STRING_LIST(set_int8_string_list, int8_t, (CT_VECTOR, ST_NONE, PT_INT8))
358 SET_STRING_LIST(set_string_list, int32_t, (CT_VECTOR, ST_NONE, PT_INT32))
359 SET_STRING_LIST(set_uint_string_list, uint32_t, (CT_VECTOR, ST_NONE, PT_INT32))
360 SET_STRING_LIST(set_long_string_list, int64_t, (CT_VECTOR, ST_NONE, PT_INT64))
361 SET_STRING_LIST(set_ulong_string_list, uint64_t, (CT_VECTOR, ST_NONE, PT_INT64))
362 SET_STRING_LIST(set_string_list, int16_t, (CT_VECTOR, ST_NONE, PT_INT16))
363 SET_STRING_LIST(set_string_list, uint16_t, (CT_VECTOR, ST_NONE, PT_INT16))
364 SET_STRING_LIST(set_string_list, float32_t, (CT_VECTOR, ST_NONE, PT_FLOAT32))
365 SET_STRING_LIST(set_string_list, float64_t, (CT_VECTOR, ST_NONE, PT_FLOAT64))
366 SET_STRING_LIST(set_longreal_string_list, floatmax_t, (CT_VECTOR, ST_NONE, PT_FLOATMAX))
367 #undef SET_STRING_LIST
386 if (fseek(
file, 0L, SEEK_SET)!=0)
392 if (fread(&fourcc,
sizeof(
char), 4,
file)!=4)
395 if (fread(&endian,
sizeof(uint16_t), 1,
file)!=1)
402 if (strncmp(fourcc,
"SG01", 4))
411 const char* fourcc=
"SG01";
412 uint16_t endian=0x1234;
414 if (!((fwrite(fourcc,
sizeof(
char), 4,
file)==4) &&
415 (fwrite(&endian,
sizeof(uint16_t), 1,
file)==1) &&