SerializableHdf5File.h

Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 3 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * Written (W) 2010 Soeren Sonnenburg
00008  * Copyright (C) 2010 Berlin Institute of Technology
00009  */
00010 #ifndef __SERIALIZABLE_HDF5_FILE_H__
00011 #define __SERIALIZABLE_HDF5_FILE_H__
00012 
00013 #include <shogun/lib/config.h>
00014 #ifdef HAVE_HDF5
00015 
00016 #include <hdf5.h>
00017 
00018 #include <shogun/io/SerializableFile.h>
00019 #include <shogun/base/DynArray.h>
00020 
00021 #define TYPE_INDEX                 H5T_NATIVE_INT32
00022 
00023 #define STR_IS_SGSERIALIZABLE      "is_sgserializable"
00024 #define STR_IS_SPARSE              "is_sparse"
00025 #define STR_IS_CONT                "is_container"
00026 #define STR_IS_NULL                "is_null"
00027 #define STR_INSTANCE_NAME          "instance_name"
00028 #define STR_GENERIC_NAME           "generic_name"
00029 #define STR_CTYPE_NAME             "container_type"
00030 #define STR_LENGTH_X               "length_x"
00031 #define STR_LENGTH_Y               "length_y"
00032 
00033 #define STR_GROUP_PREFIX           "$"
00034 
00035 #define STR_SPARSE_VINDEX          "vec_index"
00036 #define STR_SPARSE_FPTR            "features_ptr"
00037 #define STR_SPARSEENTRY_FINDEX     "feat_index"
00038 #define STR_SPARSEENTRY_ENTRY      "entry"
00039 
00040 namespace shogun
00041 {
00042 #define IGNORE_IN_CLASSLIST
00043 IGNORE_IN_CLASSLIST class CSerializableHdf5File
00044     :public CSerializableFile
00045 {
00046     friend class SerializableHdf5Reader00;
00047 
00048     struct type_item_t {
00049         explicit type_item_t(const char* name_);
00050         ~type_item_t();
00051 
00052         int rank;
00053         hsize_t dims[2];
00054         hid_t dspace, dtype, dset;
00055         hvl_t* vltype;
00056         index_t y, x, sub_y;
00057         SGSparseVectorEntry<char>* sparse_ptr;
00058         const char* name;
00059     };
00060 
00061     DynArray<type_item_t*> m_stack_type;
00062     DynArray<hid_t> m_stack_h5stream;
00063 
00064     static hid_t sizeof_sparsetype();
00065     static hid_t new_sparsetype();
00066     static hobj_ref_t* get_ref_sparstype(void* sparse_buf);
00067     static hid_t new_sparseentrytype(EPrimitiveType ptype);
00068     static hid_t ptype2hdf5(EPrimitiveType ptype);
00069     static hid_t new_stype2hdf5(EStructType stype,
00070                                 EPrimitiveType ptype);
00071     static bool isequal_stype2hdf5(EStructType stype,
00072                                    EPrimitiveType ptype, hid_t htype);
00073     static bool index2string(char* dest, size_t n, EContainerType ctype,
00074                              index_t y, index_t x);
00075 
00076     void init(const char* fname);
00077     bool dspace_select(EContainerType ctype, index_t y, index_t x);
00078 
00079     bool attr_write_scalar(hid_t datatype, const char* name,
00080                            const void* val);
00081     bool attr_write_string(const char* name, const char* val);
00082     bool attr_exists(const char* name);
00083     size_t attr_get_size(const char* name);
00084     bool attr_read_scalar(hid_t datatype, const char* name, void* val);
00085     bool attr_read_string(const char* name, char* val, size_t n);
00086 
00087     bool group_create(const char* name, const char* prefix);
00088     bool group_open(const char* name, const char* prefix);
00089     bool group_close();
00090 
00091 protected:
00092     virtual TSerializableReader* new_reader(
00093         char* dest_version, size_t n);
00094 
00095     virtual bool write_scalar_wrapped(
00096         const TSGDataType* type, const void* param);
00097 
00098     virtual bool write_cont_begin_wrapped(
00099         const TSGDataType* type, index_t len_real_y,
00100         index_t len_real_x);
00101     virtual bool write_cont_end_wrapped(
00102         const TSGDataType* type, index_t len_real_y,
00103         index_t len_real_x);
00104 
00105     virtual bool write_string_begin_wrapped(
00106         const TSGDataType* type, index_t length);
00107     virtual bool write_string_end_wrapped(
00108         const TSGDataType* type, index_t length);
00109 
00110     virtual bool write_stringentry_begin_wrapped(
00111         const TSGDataType* type, index_t y);
00112     virtual bool write_stringentry_end_wrapped(
00113         const TSGDataType* type, index_t y);
00114 
00115     virtual bool write_sparse_begin_wrapped(
00116         const TSGDataType* type, index_t vec_index,
00117         index_t length);
00118     virtual bool write_sparse_end_wrapped(
00119         const TSGDataType* type, index_t vec_index,
00120         index_t length);
00121 
00122     virtual bool write_sparseentry_begin_wrapped(
00123         const TSGDataType* type, const SGSparseVectorEntry<char>* first_entry,
00124         index_t feat_index, index_t y);
00125     virtual bool write_sparseentry_end_wrapped(
00126         const TSGDataType* type, const SGSparseVectorEntry<char>* first_entry,
00127         index_t feat_index, index_t y);
00128 
00129     virtual bool write_item_begin_wrapped(
00130         const TSGDataType* type, index_t y, index_t x);
00131     virtual bool write_item_end_wrapped(
00132         const TSGDataType* type, index_t y, index_t x);
00133 
00134     virtual bool write_sgserializable_begin_wrapped(
00135         const TSGDataType* type, const char* sgserializable_name,
00136         EPrimitiveType generic);
00137     virtual bool write_sgserializable_end_wrapped(
00138         const TSGDataType* type, const char* sgserializable_name,
00139         EPrimitiveType generic);
00140 
00141     virtual bool write_type_begin_wrapped(
00142         const TSGDataType* type, const char* name,
00143         const char* prefix);
00144     virtual bool write_type_end_wrapped(
00145         const TSGDataType* type, const char* name,
00146         const char* prefix);
00147 
00148 public:
00150     explicit CSerializableHdf5File();
00151 
00157     explicit CSerializableHdf5File(const char* fname, char rw='r');
00158 
00160     virtual ~CSerializableHdf5File();
00161 
00163     inline virtual const char* get_name() const {
00164         return "SerializableHdf5File";
00165     }
00166 
00167     virtual void close();
00168     virtual bool is_opened();
00169 };
00170 }
00171 #endif /* HAVE_HDF5  */
00172 #endif /* __SERIALIZABLE_HDF5_FILE_H__  */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation