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_FPTR            "features_ptr"
00036 #define STR_SPARSEENTRY_FINDEX     "feat_index"
00037 #define STR_SPARSEENTRY_ENTRY      "entry"
00038 
00039 namespace shogun
00040 {
00041 #define IGNORE_IN_CLASSLIST
00042 IGNORE_IN_CLASSLIST class CSerializableHdf5File
00043     :public CSerializableFile
00044 {
00045     friend class SerializableHdf5Reader00;
00046 
00047     struct type_item_t {
00048         explicit type_item_t(const char* name_);
00049         ~type_item_t();
00050 
00051         int rank;
00052         hsize_t dims[2];
00053         hid_t dspace, dtype, dset;
00054         hvl_t* vltype;
00055         index_t y, x, sub_y;
00056         SGSparseVectorEntry<char>* sparse_ptr;
00057         const char* name;
00058     };
00059 
00060     DynArray<type_item_t*> m_stack_type;
00061     DynArray<hid_t> m_stack_h5stream;
00062 
00063     static hid_t sizeof_sparsetype();
00064     static hid_t new_sparsetype();
00065     static hobj_ref_t* get_ref_sparstype(void* sparse_buf);
00066     static hid_t new_sparseentrytype(EPrimitiveType ptype);
00067     static hid_t ptype2hdf5(EPrimitiveType ptype);
00068     static hid_t new_stype2hdf5(EStructType stype,
00069                                 EPrimitiveType ptype);
00070     static bool isequal_stype2hdf5(EStructType stype,
00071                                    EPrimitiveType ptype, hid_t htype);
00072     static bool index2string(char* dest, size_t n, EContainerType ctype,
00073                              index_t y, index_t x);
00074 
00075     void init(const char* fname);
00076     bool dspace_select(EContainerType ctype, index_t y, index_t x);
00077 
00078     bool attr_write_scalar(hid_t datatype, const char* name,
00079                            const void* val);
00080     bool attr_write_string(const char* name, const char* val);
00081     bool attr_exists(const char* name);
00082     size_t attr_get_size(const char* name);
00083     bool attr_read_scalar(hid_t datatype, const char* name, void* val);
00084     bool attr_read_string(const char* name, char* val, size_t n);
00085 
00086     bool group_create(const char* name, const char* prefix);
00087     bool group_open(const char* name, const char* prefix);
00088     bool group_close();
00089 
00090 protected:
00091     virtual TSerializableReader* new_reader(
00092         char* dest_version, size_t n);
00093 
00094     virtual bool write_scalar_wrapped(
00095         const TSGDataType* type, const void* param);
00096 
00097     virtual bool write_cont_begin_wrapped(
00098         const TSGDataType* type, index_t len_real_y,
00099         index_t len_real_x);
00100     virtual bool write_cont_end_wrapped(
00101         const TSGDataType* type, index_t len_real_y,
00102         index_t len_real_x);
00103 
00104     virtual bool write_string_begin_wrapped(
00105         const TSGDataType* type, index_t length);
00106     virtual bool write_string_end_wrapped(
00107         const TSGDataType* type, index_t length);
00108 
00109     virtual bool write_stringentry_begin_wrapped(
00110         const TSGDataType* type, index_t y);
00111     virtual bool write_stringentry_end_wrapped(
00112         const TSGDataType* type, index_t y);
00113 
00114     virtual bool write_sparse_begin_wrapped(
00115         const TSGDataType* type, index_t length);
00116     virtual bool write_sparse_end_wrapped(
00117         const TSGDataType* type, index_t length);
00118 
00119     virtual bool write_sparseentry_begin_wrapped(
00120         const TSGDataType* type, const SGSparseVectorEntry<char>* first_entry,
00121         index_t feat_index, index_t y);
00122     virtual bool write_sparseentry_end_wrapped(
00123         const TSGDataType* type, const SGSparseVectorEntry<char>* first_entry,
00124         index_t feat_index, index_t y);
00125 
00126     virtual bool write_item_begin_wrapped(
00127         const TSGDataType* type, index_t y, index_t x);
00128     virtual bool write_item_end_wrapped(
00129         const TSGDataType* type, index_t y, index_t x);
00130 
00131     virtual bool write_sgserializable_begin_wrapped(
00132         const TSGDataType* type, const char* sgserializable_name,
00133         EPrimitiveType generic);
00134     virtual bool write_sgserializable_end_wrapped(
00135         const TSGDataType* type, const char* sgserializable_name,
00136         EPrimitiveType generic);
00137 
00138     virtual bool write_type_begin_wrapped(
00139         const TSGDataType* type, const char* name,
00140         const char* prefix);
00141     virtual bool write_type_end_wrapped(
00142         const TSGDataType* type, const char* name,
00143         const char* prefix);
00144 
00145 public:
00147     explicit CSerializableHdf5File();
00148 
00154     explicit CSerializableHdf5File(const char* fname, char rw='r');
00155 
00157     virtual ~CSerializableHdf5File();
00158 
00160     virtual const char* get_name() const {
00161         return "SerializableHdf5File";
00162     }
00163 
00164     virtual void close();
00165     virtual bool is_opened();
00166 };
00167 }
00168 #endif /* HAVE_HDF5  */
00169 #endif /* __SERIALIZABLE_HDF5_FILE_H__  */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation