SHOGUN  v3.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SerializableHdf5File.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2010 Soeren Sonnenburg
8  * Copyright (C) 2010 Berlin Institute of Technology
9  */
10 #ifndef __SERIALIZABLE_HDF5_FILE_H__
11 #define __SERIALIZABLE_HDF5_FILE_H__
12 
13 #include <shogun/lib/config.h>
14 #ifdef HAVE_HDF5
15 
16 #include <hdf5.h>
17 
19 #include <shogun/base/DynArray.h>
20 
21 #define TYPE_INDEX H5T_NATIVE_INT32
22 
23 #define STR_IS_SGSERIALIZABLE "is_sgserializable"
24 #define STR_IS_SPARSE "is_sparse"
25 #define STR_IS_CONT "is_container"
26 #define STR_IS_NULL "is_null"
27 #define STR_INSTANCE_NAME "instance_name"
28 #define STR_GENERIC_NAME "generic_name"
29 #define STR_CTYPE_NAME "container_type"
30 #define STR_LENGTH_X "length_x"
31 #define STR_LENGTH_Y "length_y"
32 
33 #define STR_GROUP_PREFIX "$"
34 
35 #define STR_SPARSE_FPTR "features_ptr"
36 #define STR_SPARSEENTRY_FINDEX "feat_index"
37 #define STR_SPARSEENTRY_ENTRY "entry"
38 
39 namespace shogun
40 {
41 #define IGNORE_IN_CLASSLIST
42 IGNORE_IN_CLASSLIST class CSerializableHdf5File
43  :public CSerializableFile
44 {
45  friend class SerializableHdf5Reader00;
46 
47  struct type_item_t {
48  explicit type_item_t(const char* name_);
49  ~type_item_t();
50 
51  int rank;
52  hsize_t dims[2];
53  hid_t dspace, dtype, dset;
54  hvl_t* vltype;
55  index_t y, x, sub_y;
56  SGSparseVectorEntry<char>* sparse_ptr;
57  const char* name;
58  };
59 
60  DynArray<type_item_t*> m_stack_type;
61  DynArray<hid_t> m_stack_h5stream;
62 
63  static hid_t sizeof_sparsetype();
64  static hid_t new_sparsetype();
65  static hobj_ref_t* get_ref_sparstype(void* sparse_buf);
66  static hid_t new_sparseentrytype(EPrimitiveType ptype);
67  static hid_t ptype2hdf5(EPrimitiveType ptype);
68  static hid_t new_stype2hdf5(EStructType stype,
69  EPrimitiveType ptype);
70  static bool isequal_stype2hdf5(EStructType stype,
71  EPrimitiveType ptype, hid_t htype);
72  static bool index2string(char* dest, size_t n, EContainerType ctype,
73  index_t y, index_t x);
74 
75  void init(const char* fname);
76  bool dspace_select(EContainerType ctype, index_t y, index_t x);
77 
78  bool attr_write_scalar(hid_t datatype, const char* name,
79  const void* val);
80  bool attr_write_string(const char* name, const char* val);
81  bool attr_exists(const char* name);
82  size_t attr_get_size(const char* name);
83  bool attr_read_scalar(hid_t datatype, const char* name, void* val);
84  bool attr_read_string(const char* name, char* val, size_t n);
85 
86  bool group_create(const char* name, const char* prefix);
87  bool group_open(const char* name, const char* prefix);
88  bool group_close();
89 
90 protected:
91  virtual TSerializableReader* new_reader(
92  char* dest_version, size_t n);
93 
94  virtual bool write_scalar_wrapped(
95  const TSGDataType* type, const void* param);
96 
97  virtual bool write_cont_begin_wrapped(
98  const TSGDataType* type, index_t len_real_y,
99  index_t len_real_x);
100  virtual bool write_cont_end_wrapped(
101  const TSGDataType* type, index_t len_real_y,
102  index_t len_real_x);
103 
104  virtual bool write_string_begin_wrapped(
105  const TSGDataType* type, index_t length);
106  virtual bool write_string_end_wrapped(
107  const TSGDataType* type, index_t length);
108 
109  virtual bool write_stringentry_begin_wrapped(
110  const TSGDataType* type, index_t y);
111  virtual bool write_stringentry_end_wrapped(
112  const TSGDataType* type, index_t y);
113 
114  virtual bool write_sparse_begin_wrapped(
115  const TSGDataType* type, index_t length);
116  virtual bool write_sparse_end_wrapped(
117  const TSGDataType* type, index_t length);
118 
119  virtual bool write_sparseentry_begin_wrapped(
120  const TSGDataType* type, const SGSparseVectorEntry<char>* first_entry,
121  index_t feat_index, index_t y);
122  virtual bool write_sparseentry_end_wrapped(
123  const TSGDataType* type, const SGSparseVectorEntry<char>* first_entry,
124  index_t feat_index, index_t y);
125 
126  virtual bool write_item_begin_wrapped(
127  const TSGDataType* type, index_t y, index_t x);
128  virtual bool write_item_end_wrapped(
129  const TSGDataType* type, index_t y, index_t x);
130 
131  virtual bool write_sgserializable_begin_wrapped(
132  const TSGDataType* type, const char* sgserializable_name,
133  EPrimitiveType generic);
134  virtual bool write_sgserializable_end_wrapped(
135  const TSGDataType* type, const char* sgserializable_name,
136  EPrimitiveType generic);
137 
138  virtual bool write_type_begin_wrapped(
139  const TSGDataType* type, const char* name,
140  const char* prefix);
141  virtual bool write_type_end_wrapped(
142  const TSGDataType* type, const char* name,
143  const char* prefix);
144 
145 public:
147  explicit CSerializableHdf5File();
148 
154  explicit CSerializableHdf5File(const char* fname, char rw='r');
155 
157  virtual ~CSerializableHdf5File();
158 
160  virtual const char* get_name() const {
161  return "SerializableHdf5File";
162  }
163 
164  virtual void close();
165  virtual bool is_opened();
166 };
167 }
168 #endif /* HAVE_HDF5 */
169 #endif /* __SERIALIZABLE_HDF5_FILE_H__ */

SHOGUN Machine Learning Toolbox - Documentation