From 835b8e885f3e39a36d05399214addae72caeed8e Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Wed, 18 Jan 2023 18:57:17 +0100 Subject: [PATCH] various fixes regarding extending ranges/arrays + hdf5 --- src/include/array/marray.cc.h | 3 ++ src/include/ranges/yrange.h | 12 ++++---- src/lib/ranges/yrange.cc | 36 +++++++++++++----------- src/opt/hdf5/lib/h5_file.cc | 1 + src/opt/hdf5/lib/h5_group.cc | 23 +++++++++++++++ src/opt/hdf5/tests/h5_basic_unit_test.cc | 27 +++++++++--------- src/tests/range_unit_test.cc | 8 +++--- 7 files changed, 70 insertions(+), 40 deletions(-) diff --git a/src/include/array/marray.cc.h b/src/include/array/marray.cc.h index 3310586..3155c72 100644 --- a/src/include/array/marray.cc.h +++ b/src/include/array/marray.cc.h @@ -36,6 +36,9 @@ namespace CNORXZ template MArray& MArray::extend(const RangePtr& range) { + if(AB::mRange == nullptr) { + return this->init(range); + } MArray tmp(AB::mRange->extend(range)); auto ei = this->end(); auto ti = tmp.begin(); diff --git a/src/include/ranges/yrange.h b/src/include/ranges/yrange.h index a7fe749..14d4f4b 100644 --- a/src/include/ranges/yrange.h +++ b/src/include/ranges/yrange.h @@ -12,12 +12,12 @@ namespace CNORXZ { - class YIndex : public IndexInterface + class YIndex : public IndexInterface> { public: - typedef IndexInterface IB; + typedef IndexInterface> IB; typedef YRange RangeType; - typedef DType MetaType; + typedef Vector MetaType; YIndex() = default; YIndex(YIndex&& i) = default; @@ -44,15 +44,15 @@ namespace CNORXZ UPos lmax() const; IndexId<0> id() const; - DType operator*() const; + Vector operator*() const; SizeT dim() const; Sptr range() const; UPos stepSize(const IndexId<0> id) const; String stringMeta() const; - DType meta() const; - YIndex& at(const DType& meta); + Vector meta() const; + YIndex& at(const Vector& meta); DXpr ifor(const DXpr& xpr, std::function&& f) const; diff --git a/src/lib/ranges/yrange.cc b/src/lib/ranges/yrange.cc index 1699712..25eeec8 100644 --- a/src/lib/ranges/yrange.cc +++ b/src/lib/ranges/yrange.cc @@ -125,7 +125,7 @@ namespace CNORXZ ***************/ YIndex::YIndex(const YIndex& i) : - IndexInterface(i), + IndexInterface>(i), mRange(rangeCast(i.range())), mIs(mkIndices()), mFormat(mkFormat()), @@ -138,7 +138,7 @@ namespace CNORXZ YIndex& YIndex::operator=(const YIndex& i) { - IndexInterface::operator=(i); + IndexInterface>::operator=(i); mRange = rangeCast(i.range()); mIs = mkIndices(); mFormat = mkFormat(); @@ -149,7 +149,7 @@ namespace CNORXZ } YIndex::YIndex(const Vector& is) : - IndexInterface(0), + IndexInterface>(0), mRange(std::dynamic_pointer_cast(yrange(mkRangeVec(is)))), mIs(is), mFormat(mkFormat()), @@ -161,7 +161,7 @@ namespace CNORXZ } YIndex::YIndex(const YFormat& bs, const Vector& is) : - IndexInterface(0), + IndexInterface>(0), mRange(std::dynamic_pointer_cast(yrange(mkRangeVec(is)))), mIs(is), mFormat(bs), @@ -173,7 +173,7 @@ namespace CNORXZ } YIndex::YIndex(const RangePtr& range, SizeT lexpos) : - IndexInterface(0), + IndexInterface>(0), mRange(rangeCast(range)), mIs(mkIndices()), mFormat(mkFormat()), @@ -185,7 +185,7 @@ namespace CNORXZ } YIndex::YIndex(const RangePtr& range, const YFormat& bs, SizeT lexpos) : - IndexInterface(0), + IndexInterface>(0), mRange(rangeCast(range)), mIs(mkIndices()), mFormat(bs), @@ -282,7 +282,7 @@ namespace CNORXZ return IndexId<0>(this->ptrId()); } - DType YIndex::operator*() const + Vector YIndex::operator*() const { return meta(); } @@ -319,21 +319,20 @@ namespace CNORXZ elim; } - DType YIndex::meta() const + Vector YIndex::meta() const { Vector v(mIs.size()); std::transform(mIs.all().begin(), mIs.all().end(), v.begin(), [](const auto& x) { return x->meta(); }); - return DType(v); + return v; } - YIndex& YIndex::at(const DType& meta) + YIndex& YIndex::at(const Vector& meta) { - auto& v = std::any_cast&>(meta.get()); - assert(v.size() == mIs.size()); + assert(meta.size() == mIs.size()); IB::mPos = 0; for(SizeT i = 0; i != mIs.size(); ++i){ - mIs[i]->at(v[i]); + mIs[i]->at(meta[i]); IB::mPos += mIs[i]->pos() * mFormat[i].val(); } return *this; @@ -481,7 +480,7 @@ namespace CNORXZ const TypeInfo& YRange::metaType() const { - return typeid(DType); + return typeid(Vector); } RangePtr YRange::extend(const RangePtr& r) const @@ -489,8 +488,13 @@ namespace CNORXZ CXZ_ASSERT(r->dim() == this->dim(), "cannot extend range of dimension " << this->dim() << " by range of dimension " << r->dim()); Vector rvec(this->dim()); - for(SizeT i = 0; i != this->dim(); ++i){ - rvec[i] = mRVec[i]->extend( r->sub(i) ); + if(this->dim() == 1 and r->sub(0) == nullptr){ + rvec[0] = mRVec[0]->extend( r ); + } + else { + for(SizeT i = 0; i != this->dim(); ++i){ + rvec[i] = mRVec[i]->extend( r->sub(i) ); + } } return YRangeFactory( rvec ).create(); } diff --git a/src/opt/hdf5/lib/h5_file.cc b/src/opt/hdf5/lib/h5_file.cc index 3f400ee..96fffd3 100644 --- a/src/opt/hdf5/lib/h5_file.cc +++ b/src/opt/hdf5/lib/h5_file.cc @@ -70,6 +70,7 @@ namespace CNORXZ if(mId != 0){ H5Fclose(mId); } + mId = 0; return *this; } diff --git a/src/opt/hdf5/lib/h5_group.cc b/src/opt/hdf5/lib/h5_group.cc index 3a512b8..c56ff48 100644 --- a/src/opt/hdf5/lib/h5_group.cc +++ b/src/opt/hdf5/lib/h5_group.cc @@ -51,6 +51,7 @@ namespace CNORXZ if(mId != 0){ H5Gclose(mId); } + mId = 0; return *this; } @@ -80,6 +81,28 @@ namespace CNORXZ return mCont; } + Group& Group::addGroup(const String& name) + { + CXZ_ASSERT(this->isOpen(), "tried to extend closed group"); + Vector nvec({name}); + Vector dvec({DType(name)}); + auto extr = URangeFactory( nvec ).create(); + mCont.extend(extr); + auto ii = mCont.begin(); + ii.at(dvec); // 'at' returns YIndex&, so cannot use it inline... + *ii = std::make_shared(name, this); + (*ii)->open(); // create new group + return *this; + } + + template + Group& Group::addData(const String& name, const ArrayBase& data) + { + CXZ_ASSERT(this->isOpen(), "tried to extend closed group"); + CXZ_ERROR(name << data.size() << " NOT IMPLEMENTED!!!"); + return *this; + } + struct InitContData { const ContentBase* parent; diff --git a/src/opt/hdf5/tests/h5_basic_unit_test.cc b/src/opt/hdf5/tests/h5_basic_unit_test.cc index 5635d55..6535862 100644 --- a/src/opt/hdf5/tests/h5_basic_unit_test.cc +++ b/src/opt/hdf5/tests/h5_basic_unit_test.cc @@ -37,7 +37,6 @@ namespace Group_Test() { mFileName = "test_file.h5"; - std::remove(mFileName.c_str()); mGrps = { "gr1", "gr2" }; } @@ -47,35 +46,35 @@ namespace TEST_F(NoFile_Test, NoFile) { - auto l = [&](){ return std::make_shared(mNoFileName, true); }; - EXPECT_THROW(l(), std::runtime_error); + File f(mNoFileName, true); + EXPECT_THROW(f.open(), std::runtime_error); } TEST_F(NoFile_Test, NoH5Format) { - auto l1 = [&](){ return std::make_shared(mWrongFileName, true); }; - auto l2 = [&](){ return std::make_shared(mWrongFileName, false); }; - EXPECT_THROW(l1(), std::runtime_error); - EXPECT_THROW(l2(), std::runtime_error); + File f1(mWrongFileName, true); + File f2(mWrongFileName, false); + EXPECT_THROW(f1.open(), std::runtime_error); + EXPECT_THROW(f2.open(), std::runtime_error); } TEST_F(Group_Test, Create) { + std::remove(mFileName.c_str()); File h5f(mFileName, false); EXPECT_FALSE(h5f.ro()); - //h5f.append("gr1"); - auto grange = URangeFactory( mGrps ).create(); - h5f.set( grange ); - for(auto i = grange->begin(); i != grange->end(); ++i){ - (*h5f.get())[i] = std::make_shared( i.meta().str(), &h5f ); - } + h5f.open(); + h5f.addGroup("gr1"); + h5f.addGroup("gr2"); + EXPECT_EQ(h5f.get().size(), 2u); } TEST_F(Group_Test, Read) { File h5f(mFileName, true); + h5f.open(); EXPECT_TRUE(h5f.ro()); - + EXPECT_EQ(h5f.get().size(), 2u); } } diff --git a/src/tests/range_unit_test.cc b/src/tests/range_unit_test.cc index 23ff686..b01a311 100644 --- a/src/tests/range_unit_test.cc +++ b/src/tests/range_unit_test.cc @@ -342,7 +342,7 @@ namespace auto a = yi + i; EXPECT_EQ(a.lex(), i); EXPECT_EQ(a.pos(), i); - auto mmi = DType(mkm(i)); + auto mmi = mkm(i); EXPECT_TRUE(a.meta() == mmi); EXPECT_TRUE(*a == mmi); EXPECT_EQ(a.stringMeta(), toString(mmi)); @@ -350,7 +350,7 @@ namespace for(SizeT j = 0; j != yr->size(); ++j){ const Int jj = static_cast(j) - static_cast(i); auto b = a + jj; - auto mmj = DType(mkm(j)); + auto mmj = mkm(j); EXPECT_EQ(b.lex(), j); EXPECT_EQ(b.pos(), j); EXPECT_TRUE(*b == mmj); @@ -386,7 +386,7 @@ namespace auto a = yi + i; EXPECT_EQ(a.lex(), i); EXPECT_EQ(a.pos(), i); - auto mmi = DType(mkm(i)); + auto mmi = mkm(i); EXPECT_TRUE(a.meta() == mmi); EXPECT_TRUE(*a == mmi); EXPECT_EQ(a.stringMeta(), toString(mmi)); @@ -394,7 +394,7 @@ namespace for(SizeT j = 0; j != yr2->size(); ++j){ const Int jj = static_cast(j) - static_cast(i); auto b = a + jj; - auto mmj = DType(mkm(j)); + auto mmj = mkm(j); EXPECT_EQ(b.lex(), j); EXPECT_EQ(b.pos(), j); EXPECT_TRUE(*b == mmj);