various fixes regarding extending ranges/arrays + hdf5

This commit is contained in:
Christian Zimmermann 2023-01-18 18:57:17 +01:00
parent e6da712482
commit 835b8e885f
7 changed files with 70 additions and 40 deletions

View file

@ -36,6 +36,9 @@ namespace CNORXZ
template <typename T> template <typename T>
MArray<T>& MArray<T>::extend(const RangePtr& range) MArray<T>& MArray<T>::extend(const RangePtr& range)
{ {
if(AB::mRange == nullptr) {
return this->init(range);
}
MArray<T> tmp(AB::mRange->extend(range)); MArray<T> tmp(AB::mRange->extend(range));
auto ei = this->end(); auto ei = this->end();
auto ti = tmp.begin(); auto ti = tmp.begin();

View file

@ -12,12 +12,12 @@
namespace CNORXZ namespace CNORXZ
{ {
class YIndex : public IndexInterface<YIndex,DType> class YIndex : public IndexInterface<YIndex,Vector<DType>>
{ {
public: public:
typedef IndexInterface<YIndex,DType> IB; typedef IndexInterface<YIndex,Vector<DType>> IB;
typedef YRange RangeType; typedef YRange RangeType;
typedef DType MetaType; typedef Vector<DType> MetaType;
YIndex() = default; YIndex() = default;
YIndex(YIndex&& i) = default; YIndex(YIndex&& i) = default;
@ -44,15 +44,15 @@ namespace CNORXZ
UPos lmax() const; UPos lmax() const;
IndexId<0> id() const; IndexId<0> id() const;
DType operator*() const; Vector<DType> operator*() const;
SizeT dim() const; SizeT dim() const;
Sptr<YRange> range() const; Sptr<YRange> range() const;
UPos stepSize(const IndexId<0> id) const; UPos stepSize(const IndexId<0> id) const;
String stringMeta() const; String stringMeta() const;
DType meta() const; Vector<DType> meta() const;
YIndex& at(const DType& meta); YIndex& at(const Vector<DType>& meta);
DXpr<SizeT> ifor(const DXpr<SizeT>& xpr, std::function<SizeT(SizeT,SizeT)>&& f) const; DXpr<SizeT> ifor(const DXpr<SizeT>& xpr, std::function<SizeT(SizeT,SizeT)>&& f) const;

View file

@ -125,7 +125,7 @@ namespace CNORXZ
***************/ ***************/
YIndex::YIndex(const YIndex& i) : YIndex::YIndex(const YIndex& i) :
IndexInterface<YIndex,DType>(i), IndexInterface<YIndex,Vector<DType>>(i),
mRange(rangeCast<YRange>(i.range())), mRange(rangeCast<YRange>(i.range())),
mIs(mkIndices()), mIs(mkIndices()),
mFormat(mkFormat()), mFormat(mkFormat()),
@ -138,7 +138,7 @@ namespace CNORXZ
YIndex& YIndex::operator=(const YIndex& i) YIndex& YIndex::operator=(const YIndex& i)
{ {
IndexInterface<YIndex,DType>::operator=(i); IndexInterface<YIndex,Vector<DType>>::operator=(i);
mRange = rangeCast<YRange>(i.range()); mRange = rangeCast<YRange>(i.range());
mIs = mkIndices(); mIs = mkIndices();
mFormat = mkFormat(); mFormat = mkFormat();
@ -149,7 +149,7 @@ namespace CNORXZ
} }
YIndex::YIndex(const Vector<XIndexPtr>& is) : YIndex::YIndex(const Vector<XIndexPtr>& is) :
IndexInterface<YIndex,DType>(0), IndexInterface<YIndex,Vector<DType>>(0),
mRange(std::dynamic_pointer_cast<YRange>(yrange(mkRangeVec(is)))), mRange(std::dynamic_pointer_cast<YRange>(yrange(mkRangeVec(is)))),
mIs(is), mIs(is),
mFormat(mkFormat()), mFormat(mkFormat()),
@ -161,7 +161,7 @@ namespace CNORXZ
} }
YIndex::YIndex(const YFormat& bs, const Vector<XIndexPtr>& is) : YIndex::YIndex(const YFormat& bs, const Vector<XIndexPtr>& is) :
IndexInterface<YIndex,DType>(0), IndexInterface<YIndex,Vector<DType>>(0),
mRange(std::dynamic_pointer_cast<YRange>(yrange(mkRangeVec(is)))), mRange(std::dynamic_pointer_cast<YRange>(yrange(mkRangeVec(is)))),
mIs(is), mIs(is),
mFormat(bs), mFormat(bs),
@ -173,7 +173,7 @@ namespace CNORXZ
} }
YIndex::YIndex(const RangePtr& range, SizeT lexpos) : YIndex::YIndex(const RangePtr& range, SizeT lexpos) :
IndexInterface<YIndex,DType>(0), IndexInterface<YIndex,Vector<DType>>(0),
mRange(rangeCast<YRange>(range)), mRange(rangeCast<YRange>(range)),
mIs(mkIndices()), mIs(mkIndices()),
mFormat(mkFormat()), mFormat(mkFormat()),
@ -185,7 +185,7 @@ namespace CNORXZ
} }
YIndex::YIndex(const RangePtr& range, const YFormat& bs, SizeT lexpos) : YIndex::YIndex(const RangePtr& range, const YFormat& bs, SizeT lexpos) :
IndexInterface<YIndex,DType>(0), IndexInterface<YIndex,Vector<DType>>(0),
mRange(rangeCast<YRange>(range)), mRange(rangeCast<YRange>(range)),
mIs(mkIndices()), mIs(mkIndices()),
mFormat(bs), mFormat(bs),
@ -282,7 +282,7 @@ namespace CNORXZ
return IndexId<0>(this->ptrId()); return IndexId<0>(this->ptrId());
} }
DType YIndex::operator*() const Vector<DType> YIndex::operator*() const
{ {
return meta(); return meta();
} }
@ -319,21 +319,20 @@ namespace CNORXZ
elim; elim;
} }
DType YIndex::meta() const Vector<DType> YIndex::meta() const
{ {
Vector<DType> v(mIs.size()); Vector<DType> v(mIs.size());
std::transform(mIs.all().begin(), mIs.all().end(), v.begin(), std::transform(mIs.all().begin(), mIs.all().end(), v.begin(),
[](const auto& x) { return x->meta(); }); [](const auto& x) { return x->meta(); });
return DType(v); return v;
} }
YIndex& YIndex::at(const DType& meta) YIndex& YIndex::at(const Vector<DType>& meta)
{ {
auto& v = std::any_cast<const Vector<DType>&>(meta.get()); assert(meta.size() == mIs.size());
assert(v.size() == mIs.size());
IB::mPos = 0; IB::mPos = 0;
for(SizeT i = 0; i != mIs.size(); ++i){ 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(); IB::mPos += mIs[i]->pos() * mFormat[i].val();
} }
return *this; return *this;
@ -481,7 +480,7 @@ namespace CNORXZ
const TypeInfo& YRange::metaType() const const TypeInfo& YRange::metaType() const
{ {
return typeid(DType); return typeid(Vector<DType>);
} }
RangePtr YRange::extend(const RangePtr& r) const RangePtr YRange::extend(const RangePtr& r) const
@ -489,8 +488,13 @@ namespace CNORXZ
CXZ_ASSERT(r->dim() == this->dim(), "cannot extend range of dimension " CXZ_ASSERT(r->dim() == this->dim(), "cannot extend range of dimension "
<< this->dim() << " by range of dimension " << r->dim()); << this->dim() << " by range of dimension " << r->dim());
Vector<RangePtr> rvec(this->dim()); Vector<RangePtr> rvec(this->dim());
for(SizeT i = 0; i != this->dim(); ++i){ if(this->dim() == 1 and r->sub(0) == nullptr){
rvec[i] = mRVec[i]->extend( r->sub(i) ); 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(); return YRangeFactory( rvec ).create();
} }

View file

@ -70,6 +70,7 @@ namespace CNORXZ
if(mId != 0){ if(mId != 0){
H5Fclose(mId); H5Fclose(mId);
} }
mId = 0;
return *this; return *this;
} }

View file

@ -51,6 +51,7 @@ namespace CNORXZ
if(mId != 0){ if(mId != 0){
H5Gclose(mId); H5Gclose(mId);
} }
mId = 0;
return *this; return *this;
} }
@ -80,6 +81,28 @@ namespace CNORXZ
return mCont; return mCont;
} }
Group& Group::addGroup(const String& name)
{
CXZ_ASSERT(this->isOpen(), "tried to extend closed group");
Vector<String> nvec({name});
Vector<DType> dvec({DType(name)});
auto extr = URangeFactory<String>( nvec ).create();
mCont.extend(extr);
auto ii = mCont.begin();
ii.at(dvec); // 'at' returns YIndex&, so cannot use it inline...
*ii = std::make_shared<Group>(name, this);
(*ii)->open(); // create new group
return *this;
}
template <typename T>
Group& Group::addData(const String& name, const ArrayBase<T>& data)
{
CXZ_ASSERT(this->isOpen(), "tried to extend closed group");
CXZ_ERROR(name << data.size() << " NOT IMPLEMENTED!!!");
return *this;
}
struct InitContData struct InitContData
{ {
const ContentBase* parent; const ContentBase* parent;

View file

@ -37,7 +37,6 @@ namespace
Group_Test() Group_Test()
{ {
mFileName = "test_file.h5"; mFileName = "test_file.h5";
std::remove(mFileName.c_str());
mGrps = { "gr1", "gr2" }; mGrps = { "gr1", "gr2" };
} }
@ -47,35 +46,35 @@ namespace
TEST_F(NoFile_Test, NoFile) TEST_F(NoFile_Test, NoFile)
{ {
auto l = [&](){ return std::make_shared<File>(mNoFileName, true); }; File f(mNoFileName, true);
EXPECT_THROW(l(), std::runtime_error); EXPECT_THROW(f.open(), std::runtime_error);
} }
TEST_F(NoFile_Test, NoH5Format) TEST_F(NoFile_Test, NoH5Format)
{ {
auto l1 = [&](){ return std::make_shared<File>(mWrongFileName, true); }; File f1(mWrongFileName, true);
auto l2 = [&](){ return std::make_shared<File>(mWrongFileName, false); }; File f2(mWrongFileName, false);
EXPECT_THROW(l1(), std::runtime_error); EXPECT_THROW(f1.open(), std::runtime_error);
EXPECT_THROW(l2(), std::runtime_error); EXPECT_THROW(f2.open(), std::runtime_error);
} }
TEST_F(Group_Test, Create) TEST_F(Group_Test, Create)
{ {
std::remove(mFileName.c_str());
File h5f(mFileName, false); File h5f(mFileName, false);
EXPECT_FALSE(h5f.ro()); EXPECT_FALSE(h5f.ro());
//h5f.append("gr1"); h5f.open();
auto grange = URangeFactory<String>( mGrps ).create(); h5f.addGroup("gr1");
h5f.set( grange ); h5f.addGroup("gr2");
for(auto i = grange->begin(); i != grange->end(); ++i){ EXPECT_EQ(h5f.get().size(), 2u);
(*h5f.get())[i] = std::make_shared<Group>( i.meta().str(), &h5f );
}
} }
TEST_F(Group_Test, Read) TEST_F(Group_Test, Read)
{ {
File h5f(mFileName, true); File h5f(mFileName, true);
h5f.open();
EXPECT_TRUE(h5f.ro()); EXPECT_TRUE(h5f.ro());
EXPECT_EQ(h5f.get().size(), 2u);
} }
} }

View file

@ -342,7 +342,7 @@ namespace
auto a = yi + i; auto a = yi + i;
EXPECT_EQ(a.lex(), i); EXPECT_EQ(a.lex(), i);
EXPECT_EQ(a.pos(), i); EXPECT_EQ(a.pos(), i);
auto mmi = DType(mkm(i)); auto mmi = mkm(i);
EXPECT_TRUE(a.meta() == mmi); EXPECT_TRUE(a.meta() == mmi);
EXPECT_TRUE(*a == mmi); EXPECT_TRUE(*a == mmi);
EXPECT_EQ(a.stringMeta(), toString(mmi)); EXPECT_EQ(a.stringMeta(), toString(mmi));
@ -350,7 +350,7 @@ namespace
for(SizeT j = 0; j != yr->size(); ++j){ for(SizeT j = 0; j != yr->size(); ++j){
const Int jj = static_cast<Int>(j) - static_cast<Int>(i); const Int jj = static_cast<Int>(j) - static_cast<Int>(i);
auto b = a + jj; auto b = a + jj;
auto mmj = DType(mkm(j)); auto mmj = mkm(j);
EXPECT_EQ(b.lex(), j); EXPECT_EQ(b.lex(), j);
EXPECT_EQ(b.pos(), j); EXPECT_EQ(b.pos(), j);
EXPECT_TRUE(*b == mmj); EXPECT_TRUE(*b == mmj);
@ -386,7 +386,7 @@ namespace
auto a = yi + i; auto a = yi + i;
EXPECT_EQ(a.lex(), i); EXPECT_EQ(a.lex(), i);
EXPECT_EQ(a.pos(), i); EXPECT_EQ(a.pos(), i);
auto mmi = DType(mkm(i)); auto mmi = mkm(i);
EXPECT_TRUE(a.meta() == mmi); EXPECT_TRUE(a.meta() == mmi);
EXPECT_TRUE(*a == mmi); EXPECT_TRUE(*a == mmi);
EXPECT_EQ(a.stringMeta(), toString(mmi)); EXPECT_EQ(a.stringMeta(), toString(mmi));
@ -394,7 +394,7 @@ namespace
for(SizeT j = 0; j != yr2->size(); ++j){ for(SizeT j = 0; j != yr2->size(); ++j){
const Int jj = static_cast<Int>(j) - static_cast<Int>(i); const Int jj = static_cast<Int>(j) - static_cast<Int>(i);
auto b = a + jj; auto b = a + jj;
auto mmj = DType(mkm(j)); auto mmj = mkm(j);
EXPECT_EQ(b.lex(), j); EXPECT_EQ(b.lex(), j);
EXPECT_EQ(b.pos(), j); EXPECT_EQ(b.pos(), j);
EXPECT_TRUE(*b == mmj); EXPECT_TRUE(*b == mmj);