diff --git a/src/include/base/types.h b/src/include/base/types.h index 8af8b40..77dbc98 100644 --- a/src/include/base/types.h +++ b/src/include/base/types.h @@ -6,7 +6,7 @@ This file contains the declaration of all library types - Copyright (c) 2022 Christian Zimmermann. All rights reserved. + Copyright (c) 2024 Christian Zimmermann. All rights reserved. Mail: chizeta@f3l.de **/ @@ -28,9 +28,9 @@ namespace CNORXZ { - /********************** - * standard types * - **********************/ + /*====================+ + | standard types | + =====================*/ typedef std::intptr_t PtrId; typedef int32_t Int; @@ -80,9 +80,9 @@ namespace CNORXZ template using CSizeT = std::integral_constant; - /********************* - * library types * - *********************/ + /*===================+ + | library types | + +===================*/ /*** Naming Prefixes: @@ -302,9 +302,9 @@ namespace CNORXZ template class Contraction; - /********************* - * derived types * - *********************/ + /*===================+ + | derived types | + +===================*/ template using Vector = std::vector>; diff --git a/src/opt/hdf5/include/h5_content_base.cc.h b/src/opt/hdf5/include/h5_content_base.cc.h index 690f82e..994c110 100644 --- a/src/opt/hdf5/include/h5_content_base.cc.h +++ b/src/opt/hdf5/include/h5_content_base.cc.h @@ -1,3 +1,13 @@ +// -*- C++ -*- +/** + + @file opt/hdf5/include/h5_content_base.cc.h + @brief Implementation of template member functions of ContentBase + + Copyright (c) 2024 Christian Zimmermann. All rights reserved. + Mail: chizeta@f3l.de + + **/ #ifndef __cxz_h5_content_base_cc_h__ #define __cxz_h5_content_base_cc_h__ diff --git a/src/opt/hdf5/include/h5_content_base.h b/src/opt/hdf5/include/h5_content_base.h index 7a4dc03..58923a2 100644 --- a/src/opt/hdf5/include/h5_content_base.h +++ b/src/opt/hdf5/include/h5_content_base.h @@ -81,6 +81,11 @@ namespace CNORXZ */ virtual String filename() const = 0; + /** Check if group exists in the parent object. + @return True if object exists, else false. + */ + virtual bool exists() const = 0; + /** Get object name. @return The name of this object. */ diff --git a/src/opt/hdf5/include/h5_dataset.cc.h b/src/opt/hdf5/include/h5_dataset.cc.h index 6df2e42..9c4aaeb 100644 --- a/src/opt/hdf5/include/h5_dataset.cc.h +++ b/src/opt/hdf5/include/h5_dataset.cc.h @@ -1,3 +1,13 @@ +// -*- C++ -*- +/** + + @file opt/hdf5/include/h5_dataset.cc.h + @brief Implementation of template member functions of Dataset and SDataset. + + Copyright (c) 2024 Christian Zimmermann. All rights reserved. + Mail: chizeta@f3l.de + + **/ #ifndef __cxz_h5_dataset_cc_h__ #define __cxz_h5_dataset_cc_h__ diff --git a/src/opt/hdf5/include/h5_dataset.h b/src/opt/hdf5/include/h5_dataset.h index b9b2ed6..ae998d6 100644 --- a/src/opt/hdf5/include/h5_dataset.h +++ b/src/opt/hdf5/include/h5_dataset.h @@ -1,3 +1,13 @@ +// -*- C++ -*- +/** + + @file opt/hdf5/include/h5_dataset.h + @brief Dataset declaration. + + Copyright (c) 2024 Christian Zimmermann. All rights reserved. + Mail: chizeta@f3l.de + + **/ #ifndef __cxz_h5_dataset_h__ #define __cxz_h5_dataset_h__ @@ -9,11 +19,21 @@ namespace CNORXZ { namespace hdf5 { + /** **** + Class to handle hdf5 datasets. + */ class Dataset : public ContentBase { public: - DEFAULT_MEMBERS(Dataset); + DEFAULT_MEMBERS(Dataset); /**< Default constructors and assignments. */ + + /** Construct the class. + @param _name Dataset name. + @param _parent Parent content object. + */ Dataset(const String& name, const ContentBase* _parent); + + /** Destructor. Release all involved hdf5 ids. */ ~Dataset(); virtual ContentType type() const override final; @@ -22,32 +42,59 @@ namespace CNORXZ virtual Dataset& close() override final; virtual String path() const override final; virtual String filename() const override final; + virtual bool exists() const override final; - bool exists() const; - + /** Initalize the dataset. + @param dataRange A potentially multi-dimensional range characterizing the dataset. + @param type Data type id. + */ Dataset& init(const RangePtr& dataRange, hid_t type); + /** Initalize the dataset. + @param data Array containing the dataset. + */ template Dataset& init(const ArrayBase& data); + /** Get the data range. + @return Pointer to the range. + */ const RangePtr& dataRange() const; protected: - RangePtr mDataRange; - hid_t mType; - hid_t mFilespace; + RangePtr mDataRange; /**< The data range. */ + hid_t mType; /**< The data type identifier. */ + hid_t mFilespace; /**< The hdf5 file space identifier. */ }; + /** **** + Class to handle hdf5 datasets, the value type is known at compile time. + @tparam T Dataset value type. + */ template class SDataset : public Dataset { public: - DEFAULT_MEMBERS(SDataset); + DEFAULT_MEMBERS(SDataset); /**< Default constructors and assignments. */ + + /** Construct the class. + @param _name Dataset name. + @param _parent Parent content object. + */ SDataset(const String& name, const ContentBase* _parent); + /** Read the dataset. + @return Array containing the dataset values. + */ MArray read() const; + /** Read a given subset of the dataset. + The subset needs to be hypercubic. + @param beg Index indicating the begin edge of the hypercube. + @param end Index indicating the end edge of the hypercube (inclusive). + @return Array containing the dataset values. + */ template MArray read(const IndexInterface& beg, const IndexInterface& end) const; diff --git a/src/opt/hdf5/include/h5_file.h b/src/opt/hdf5/include/h5_file.h index d6157cc..98a5b91 100644 --- a/src/opt/hdf5/include/h5_file.h +++ b/src/opt/hdf5/include/h5_file.h @@ -47,9 +47,13 @@ namespace CNORXZ virtual File& close() override final; virtual String path() const override final; virtual String filename() const override final; + virtual bool exists() const override final; + + /** Check if handled file is in hdf5 format. + @return True if file is in hdf5 format, else false. + */ + bool ishdf5() const; - virtual Int exists() const override final; - private: bool mRo = true; }; diff --git a/src/opt/hdf5/include/h5_group.cc.h b/src/opt/hdf5/include/h5_group.cc.h index 9ad13f6..e64fb38 100644 --- a/src/opt/hdf5/include/h5_group.cc.h +++ b/src/opt/hdf5/include/h5_group.cc.h @@ -1,3 +1,13 @@ +// -*- C++ -*- +/** + + @file opt/hdf5/include/h5_group.cc.h + @brief Implementation of template member functions of Group. + + Copyright (c) 2024 Christian Zimmermann. All rights reserved. + Mail: chizeta@f3l.de + + **/ #ifndef __cxz_h5_group_cc_h__ #define __cxz_h5_group_cc_h__ @@ -9,6 +19,26 @@ namespace CNORXZ { namespace hdf5 { + template + constexpr const auto& tget(const Tuple& t) + { + return std::get(t); + } + + template + constexpr auto& tget(Tuple& t) + { + return std::get(t); + } + + template + SizeT getTupleOffset(const Tuple& t, CSizeT i) + { + const PtrId beg = reinterpret_cast(&t); + const PtrId pos = reinterpret_cast(&tget(t)); + return pos - beg; + } + template Sptr> Group::getTable(const String& name, Tuple proto) { diff --git a/src/opt/hdf5/include/h5_group.h b/src/opt/hdf5/include/h5_group.h index 6b85f89..41d297a 100644 --- a/src/opt/hdf5/include/h5_group.h +++ b/src/opt/hdf5/include/h5_group.h @@ -42,11 +42,7 @@ namespace CNORXZ virtual Group& close() override; virtual String path() const override; virtual String filename() const override; - - /** Check if group exists in the parent object. - @return 1 if group exists, 0 if not, -1 if an error occured. - */ - virtual Int exists() const; // TODO: virtual function of base class + virtual bool exists() const override; /** Get object contained by this group. @param name Object name. diff --git a/src/opt/hdf5/include/h5_table.cc.h b/src/opt/hdf5/include/h5_table.cc.h index 5509ffa..d46d2c3 100644 --- a/src/opt/hdf5/include/h5_table.cc.h +++ b/src/opt/hdf5/include/h5_table.cc.h @@ -1,3 +1,13 @@ +// -*- C++ -*- +/** + + @file opt/hdf5/include/h5_table.cc.h + @brief Implementation of template member functions of Table and STable. + + Copyright (c) 2024 Christian Zimmermann. All rights reserved. + Mail: chizeta@f3l.de + + **/ #ifndef __cxz_h5_table_cc_h__ #define __cxz_h5_table_cc_h__ diff --git a/src/opt/hdf5/include/h5_table.h b/src/opt/hdf5/include/h5_table.h index 12eb075..79d193f 100644 --- a/src/opt/hdf5/include/h5_table.h +++ b/src/opt/hdf5/include/h5_table.h @@ -44,6 +44,7 @@ namespace CNORXZ virtual Table& close() override final; virtual String path() const override final; virtual String filename() const override final; + virtual bool exists() const override final; /** Ininitialize table field names. @param fnames Table field names. diff --git a/src/opt/hdf5/include/h5_type_id.cc.h b/src/opt/hdf5/include/h5_type_id.cc.h index 322ff76..b7624bd 100644 --- a/src/opt/hdf5/include/h5_type_id.cc.h +++ b/src/opt/hdf5/include/h5_type_id.cc.h @@ -1,3 +1,13 @@ +// -*- C++ -*- +/** + + @file opt/hdf5/include/h5_type_id.h + @brief TypeId template implementation. + + Copyright (c) 2024 Christian Zimmermann. All rights reserved. + Mail: chizeta@f3l.de + + **/ #ifndef __cxz_h5_type_id_cc_h__ #define __cxz_h5_type_id_cc_h__ @@ -8,37 +18,9 @@ namespace CNORXZ { namespace hdf5 { - template - decltype(auto) reverseTuple(const Tuple& t) - { - return iter<0,sizeof...(Ts)> - ( [&](auto i) { return tget(t); }, - [](const auto&... e) { return std::make_tuple(e...); } ); - } - - template - constexpr const auto& tget(const Tuple& t) - { - return std::get(t); - } - - template - constexpr auto& tget(Tuple& t) - { - return std::get(t); - } - - template - SizeT getTupleOffset(const Tuple& t, CSizeT i) - { - const PtrId beg = reinterpret_cast(&t); - const PtrId pos = reinterpret_cast(&tget(t)); - return pos - beg; - } - - /************** - * TypeId * - **************/ + /*============+ + | TypeId | + +============*/ template inline hid_t TypeId::get() @@ -68,9 +50,9 @@ namespace CNORXZ return arrtype; } - /***************** - * getTypeId * - *****************/ + /*===============+ + | getTypeId | + +===============*/ template hid_t getTypeId(T x) diff --git a/src/opt/hdf5/include/h5_type_id.h b/src/opt/hdf5/include/h5_type_id.h index b024d82..896de9f 100644 --- a/src/opt/hdf5/include/h5_type_id.h +++ b/src/opt/hdf5/include/h5_type_id.h @@ -1,3 +1,13 @@ +// -*- C++ -*- +/** + + @file opt/hdf5/include/h5_type_id.h + @brief TypeId template declaration. + + Copyright (c) 2024 Christian Zimmermann. All rights reserved. + Mail: chizeta@f3l.de + + **/ #ifndef __cxz_h5_type_id_h__ #define __cxz_h5_type_id_h__ @@ -8,43 +18,57 @@ namespace CNORXZ { namespace hdf5 { - template - decltype(auto) reverseTuple(const Tuple& t); - - template - using RTuple = typename std::remove_reference()))>::type; - template - constexpr const auto& tget(const Tuple& t); - - template - constexpr auto& tget(Tuple& t); - - template - SizeT getTupleOffset(const Tuple& t, CSizeT i); - - /************** - * TypeId * - **************/ - + /** **** + Get a valid hdf5 id for a given type. + Return 0 if not implemented. + @tparam T The type. + */ template - struct TypeId { static inline hid_t get(); }; + struct TypeId + { + /** Get the type id. */ + static inline hid_t get(); + }; + /** Specialization of TypeId for SizeT. */ template <> - struct TypeId { static inline hid_t get(); }; + struct TypeId + { + /** Get the type id for SizeT. */ + static inline hid_t get(); + }; + /** Specialization of TypeId for Int. */ template <> - struct TypeId { static inline hid_t get(); }; + struct TypeId + { + /** Get the type id for Int. */ + static inline hid_t get(); + }; + /** Specialization of TypeId for Double. */ template <> - struct TypeId { static inline hid_t get(); }; + struct TypeId + { + /** Get the type id for Double. */ + static inline hid_t get(); + }; + /** Specialization of TypeId for an array + @tparam T Element type. + @tparam N Array size. + */ template - struct TypeId> { static inline hid_t get(); }; - - /***************** - * getTypeId * - *****************/ + struct TypeId> + { + /** Get the type id for the given array. */ + static inline hid_t get(); + }; + /** Wrapper function for TypeId::get() + @param Variable of given type. + @return A type id of the input variable's type. + */ template hid_t getTypeId(T x); } diff --git a/src/opt/hdf5/include/h5_types.h b/src/opt/hdf5/include/h5_types.h index cbb4fa8..46beb8d 100644 --- a/src/opt/hdf5/include/h5_types.h +++ b/src/opt/hdf5/include/h5_types.h @@ -1,3 +1,15 @@ +// -*- C++ -*- +/** + + @file opt/hdf5/include/types.h + @brief Declaration of hdf5 related library types + + This file contains the declaration of all hdf5 related library types + + Copyright (c) 2024 Christian Zimmermann. All rights reserved. + Mail: chizeta@f3l.de + +**/ #ifndef __h5_types_h__ #define __h5_types_h__ diff --git a/src/opt/hdf5/lib/h5_file.cc b/src/opt/hdf5/lib/h5_file.cc index ea05180..8878062 100644 --- a/src/opt/hdf5/lib/h5_file.cc +++ b/src/opt/hdf5/lib/h5_file.cc @@ -33,7 +33,7 @@ namespace CNORXZ } Int ex = this->exists(); const String fn = this->filename(); - CXZ_ASSERT( ex != 2, "tried to open non-h5 file '" << fn << "'" ); + CXZ_ASSERT( ishdf5(), "tried to open non-h5 file '" << fn << "'" ); if(mRo){ CXZ_ASSERT( ex == 1, "could not open file as read-only: '" << fn << "' does not exist'"); @@ -79,21 +79,25 @@ namespace CNORXZ return name(); } - Int File::exists() const + bool File::exists() const { - Int ex = 0; + bool ex = false; std::ifstream fs(this->filename().c_str(), std::ios_base::binary); if(fs.good()){ - ex = 1; // file exists + ex = true; // file exists } fs.close(); - if(ex != 0){ - if(H5Fis_hdf5(this->filename().c_str()) <= 0){ - ex = 2; // file not in h5 format - } - } return ex; } - + + bool File::ishdf5() const + { + if(exists()){ + if(H5Fis_hdf5(this->filename().c_str()) <= 0){ + return false; + } + } + return true; // a non-existing file can be created in hdf5 format + } } } diff --git a/src/opt/hdf5/lib/h5_group.cc b/src/opt/hdf5/lib/h5_group.cc index 65b21aa..1541d4d 100644 --- a/src/opt/hdf5/lib/h5_group.cc +++ b/src/opt/hdf5/lib/h5_group.cc @@ -69,9 +69,9 @@ namespace CNORXZ return String(); } - Int Group::exists() const + bool Group::exists() const { - return H5Lexists(mParent->id(), mName.c_str(), H5P_DEFAULT) != 0 ? 1 : 0; + return H5Lexists(mParent->id(), mName.c_str(), H5P_DEFAULT) != 0; } const ContentPtr& Group::get(const String& name) const diff --git a/src/opt/hdf5/lib/h5_table.cc b/src/opt/hdf5/lib/h5_table.cc index e68754c..83a264e 100644 --- a/src/opt/hdf5/lib/h5_table.cc +++ b/src/opt/hdf5/lib/h5_table.cc @@ -9,7 +9,7 @@ namespace CNORXZ Table::Table(const String& name, const ContentBase* _parent) : ContentBase(name, _parent) { - if(H5Lexists(mParent->id(), mName.c_str(), H5P_DEFAULT)){ + if(exists()){ hsize_t nfields = 0; hsize_t nrecords = 0; H5TBget_table_info(mParent->id(), mName.c_str(), &nfields, &nrecords); @@ -90,6 +90,13 @@ namespace CNORXZ return mParent->filename(); } + bool Table::exists() const + { + htri_t x = H5Lexists(mParent->id(), mName.c_str(), H5P_DEFAULT); + VCHECK(x); + return x; + } + Table& Table::initFieldNames(const Vector& fnames) { CXZ_ASSERT(mFields == nullptr, "fields already initialized"); diff --git a/src/opt/hdf5/tests/h5_basic_unit_test.cc b/src/opt/hdf5/tests/h5_basic_unit_test.cc index 7e0cc87..69bacce 100644 --- a/src/opt/hdf5/tests/h5_basic_unit_test.cc +++ b/src/opt/hdf5/tests/h5_basic_unit_test.cc @@ -58,7 +58,7 @@ namespace CIndex j(fs); for(i = 0; i.lex() != rs->size(); ++i){ iter<0,3>( [&](auto jj) - { j = jj; mTabD[i*j] = DType(tget(v[i.lex()])); }, NoF{} ); + { j = jj; mTabD[i*j] = DType(std::get<3-jj-1>(v[i.lex()])); }, NoF{} ); } const SizeT ddim = 5;