SHOGUN  3.2.1
 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 
15 #ifdef HAVE_HDF5
16 
17 #include <hdf5.h>
18 
20 #include <shogun/base/DynArray.h>
21 
22 #define TYPE_INDEX H5T_NATIVE_INT32
23 
24 #define STR_IS_SGSERIALIZABLE "is_sgserializable"
25 #define STR_IS_SPARSE "is_sparse"
26 #define STR_IS_CONT "is_container"
27 #define STR_IS_NULL "is_null"
28 #define STR_INSTANCE_NAME "instance_name"
29 #define STR_GENERIC_NAME "generic_name"
30 #define STR_CTYPE_NAME "container_type"
31 #define STR_LENGTH_X "length_x"
32 #define STR_LENGTH_Y "length_y"
33 
34 #define STR_GROUP_PREFIX "$"
35 
36 #define STR_SPARSE_FPTR "features_ptr"
37 #define STR_SPARSEENTRY_FINDEX "feat_index"
38 #define STR_SPARSEENTRY_ENTRY "entry"
39 
40 namespace shogun
41 {
42 #define IGNORE_IN_CLASSLIST
43 IGNORE_IN_CLASSLIST class CSerializableHdf5File
44  :public CSerializableFile
45 {
46  friend class SerializableHdf5Reader00;
47 
48  struct type_item_t {
49  explicit type_item_t(const char* name_);
50  ~type_item_t();
51 
52  int rank;
53  hsize_t dims[2];
54  hid_t dspace, dtype, dset;
55  hvl_t* vltype;
56  index_t y, x, sub_y;
57  SGSparseVectorEntry<char>* sparse_ptr;
58  const char* name;
59  };
60 
61  DynArray<type_item_t*> m_stack_type;
62  DynArray<hid_t> m_stack_h5stream;
63 
64  static hid_t sizeof_sparsetype();
65  static hid_t new_sparsetype();
66  static hobj_ref_t* get_ref_sparstype(void* sparse_buf);
67  static hid_t new_sparseentrytype(EPrimitiveType ptype);
68  static hid_t ptype2hdf5(EPrimitiveType ptype);
69  static hid_t new_stype2hdf5(EStructType stype,
70  EPrimitiveType ptype);
71  static bool isequal_stype2hdf5(EStructType stype,
72  EPrimitiveType ptype, hid_t htype);
73  static bool index2string(char* dest, size_t n, EContainerType ctype,
74  index_t y, index_t x);
75 
76  void init(const char* fname);
77  bool dspace_select(EContainerType ctype, index_t y, index_t x);
78 
79  bool attr_write_scalar(hid_t datatype, const char* name,
80  const void* val);
81  bool attr_write_string(const char* name, const char* val);
82  bool attr_exists(const char* name);
83  size_t attr_get_size(const char* name);
84  bool attr_read_scalar(hid_t datatype, const char* name, void* val);
85  bool attr_read_string(const char* name, char* val, size_t n);
86 
87  bool group_create(const char* name, const char* prefix);
88  bool group_open(const char* name, const char* prefix);
89  bool group_close();
90 
91 protected:
92  virtual TSerializableReader* new_reader(
93  char* dest_version, size_t n);
94 
95  virtual bool write_scalar_wrapped(
96  const TSGDataType* type, const void* param);
97 
98  virtual bool write_cont_begin_wrapped(
99  const TSGDataType* type, index_t len_real_y,
100  index_t len_real_x);
101  virtual bool write_cont_end_wrapped(
102  const TSGDataType* type, index_t len_real_y,
103  index_t len_real_x);
104 
105  virtual bool write_string_begin_wrapped(
106  const TSGDataType* type, index_t length);
107  virtual bool write_string_end_wrapped(
108  const TSGDataType* type, index_t length);
109 
110  virtual bool write_stringentry_begin_wrapped(
111  const TSGDataType* type, index_t y);
112  virtual bool write_stringentry_end_wrapped(
113  const TSGDataType* type, index_t y);
114 
115  virtual bool write_sparse_begin_wrapped(
116  const TSGDataType* type, index_t length);
117  virtual bool write_sparse_end_wrapped(
118  const TSGDataType* type, index_t length);
119 
120  virtual bool write_sparseentry_begin_wrapped(
121  const TSGDataType* type, const SGSparseVectorEntry<char>* first_entry,
122  index_t feat_index, index_t y);
123  virtual bool write_sparseentry_end_wrapped(
124  const TSGDataType* type, const SGSparseVectorEntry<char>* first_entry,
125  index_t feat_index, index_t y);
126 
127  virtual bool write_item_begin_wrapped(
128  const TSGDataType* type, index_t y, index_t x);
129  virtual bool write_item_end_wrapped(
130  const TSGDataType* type, index_t y, index_t x);
131 
132  virtual bool write_sgserializable_begin_wrapped(
133  const TSGDataType* type, const char* sgserializable_name,
134  EPrimitiveType generic);
135  virtual bool write_sgserializable_end_wrapped(
136  const TSGDataType* type, const char* sgserializable_name,
137  EPrimitiveType generic);
138 
139  virtual bool write_type_begin_wrapped(
140  const TSGDataType* type, const char* name,
141  const char* prefix);
142  virtual bool write_type_end_wrapped(
143  const TSGDataType* type, const char* name,
144  const char* prefix);
145 
146 public:
148  explicit CSerializableHdf5File();
149 
155  explicit CSerializableHdf5File(const char* fname, char rw='r');
156 
158  virtual ~CSerializableHdf5File();
159 
161  virtual const char* get_name() const {
162  return "SerializableHdf5File";
163  }
164 
165  virtual void close();
166  virtual bool is_opened();
167 };
168 }
169 #endif /* HAVE_HDF5 */
170 #endif /* __SERIALIZABLE_HDF5_FILE_H__ */

SHOGUN Machine Learning Toolbox - Documentation