dev #2

Merged
chizeta merged 32 commits from dev into main 2024-02-02 20:36:53 +01:00
5 changed files with 68 additions and 35 deletions
Showing only changes of commit b2637802be - Show all commits

View file

@ -14,20 +14,21 @@ namespace CNORXZ
{ {
auto i = this->getIndexTo(name); auto i = this->getIndexTo(name);
auto tab = std::dynamic_pointer_cast<Table>( *i ); auto tab = std::dynamic_pointer_cast<Table>( *i );
CXZ_ASSERT(tab->type() == ContentType::TABLE,
"element '" << name << "' is not of type TABLE");
if(tab == nullptr){ if(tab == nullptr){
auto stab = std::dynamic_pointer_cast<STable<Ts...>>(*i); auto stab = std::dynamic_pointer_cast<STable<Ts...>>(*i);
CXZ_ASSERT(stab != nullptr, "wrong format for table '" << name << "'"); CXZ_ASSERT(stab != nullptr, "wrong format for table '" << name << "'");
return stab; return stab;
} }
else { else {
const RangePtr fields = tab->fields();
(*i)->close(); (*i)->close();
*i = std::make_shared<STable<Ts...>>(name, this, fields); auto stab = std::make_shared<STable<Ts...>>(name, this);
return *i; *i = stab;
return stab;
} }
} }
template <typename T> template <typename T>
Group& Group::addData(const String& name, const ArrayBase<T>& data) Group& Group::addData(const String& name, const ArrayBase<T>& data)
{ {
@ -38,7 +39,7 @@ namespace CNORXZ
template <typename... Ts> template <typename... Ts>
Group& Group::addTable(const String& name, const ArrayBase<Tuple<Ts...>>& data, Group& Group::addTable(const String& name, const ArrayBase<Tuple<Ts...>>& data,
const Vector<String>& fnames) const Arr<String,sizeof...(Ts)>& fnames)
{ {
CXZ_ASSERT(this->isOpen(), "tried to extend closed group"); CXZ_ASSERT(this->isOpen(), "tried to extend closed group");
Vector<String> nvec({name}); Vector<String> nvec({name});

View file

@ -40,7 +40,7 @@ namespace CNORXZ
template <typename... Ts> template <typename... Ts>
Group& addTable(const String& name, const ArrayBase<Tuple<Ts...>>& data, Group& addTable(const String& name, const ArrayBase<Tuple<Ts...>>& data,
const Vector<String>& fnames); const Arr<String,sizeof...(Ts)>& fnames);
template <class F> template <class F>
decltype(auto) iter(F&& f) const; decltype(auto) iter(F&& f) const;

View file

@ -18,36 +18,13 @@ namespace CNORXZ
} }
template <typename... Ts> template <typename... Ts>
STable<Ts...>::STable(const String& name, const ContentBase* _parent, STable<Ts...>::STable(const String& name, const ContentBase* _parent) :
const Vector<String>& fnames) :
Table(name, _parent) Table(name, _parent)
{ {
constexpr SizeT N = sizeof...(Ts); constexpr SizeT N = sizeof...(Ts);
if(mFields == nullptr){ if(mFields == nullptr){
CXZ_ASSERT(fnames.size() != 0, "field names have to be initialized");
Vector<FieldID> fields(fnames.size());
for(SizeT i = 0; i != fields.size(); ++i){
fields[i].first = i;
fields[i].second = fnames[i];
}
mFields = URangeFactory<FieldID>(fields).create();
}
CXZ_ASSERT(mFields->size() == sizeof...(Ts), "expected tuple of size = " << mFields->size()
<< ", got: " << sizeof...(Ts));
Tuple<Ts...> x; Tuple<Ts...> x;
if(mRecords == nullptr) {
mOffsets = MArray<SizeT>( mFields, iter<0,N>
( [&](auto i) { return getTupleOffset(x, i); },
[](const auto&... e) { return Vector<SizeT>({e...}); }) );
mSizes = MArray<SizeT>( mFields, iter<0,N>
( [&](auto i) { return sizeof(std::get<i>(x)); },
[](const auto&... e) { return Vector<SizeT>({e...}); }) );
mTypes = MArray<hid_t>( mFields, iter<0,N>
( [&](auto i) { return getTypeId(std::get<i>(x)); },
[](const auto&... e) { return Vector<hid_t>({e...}); }) );
}
else {
iter<0,N>( [&](auto i) { CXZ_ASSERT iter<0,N>( [&](auto i) { CXZ_ASSERT
( getTupleOffset(x, i) == mOffsets.data()[i], ( getTupleOffset(x, i) == mOffsets.data()[i],
"wrong offset for field " << i << ": " << getTupleOffset(x, i) "wrong offset for field " << i << ": " << getTupleOffset(x, i)
@ -61,6 +38,48 @@ namespace CNORXZ
"wrong type for field " << i << ": " << getTypeId(std::get<i>(x)) "wrong type for field " << i << ": " << getTypeId(std::get<i>(x))
<< " vs " << mTypes.data()[i] ); }, NoF{} ); << " vs " << mTypes.data()[i] ); }, NoF{} );
} }
else {
CXZ_ASSERT(mFields->size() == N, "expected tuple of size = " << mFields->size()
<< ", got: " << N);
}
}
template <typename... Ts>
STable<Ts...>::STable(const String& name, const ContentBase* _parent,
const Arr<String,sizeof...(Ts)>& fnames) :
Table(name, _parent)
{
initFields(fnames);
}
template <typename... Ts>
STable<Ts...>& STable<Ts...>::initFields(const Arr<String,sizeof...(Ts)>& fnames)
{
constexpr SizeT N = sizeof...(Ts);
CXZ_ASSERT(mFields == nullptr and mRecords == nullptr,
"tried to initialize an existing table");
Vector<FieldID> fields(fnames.size());
for(SizeT i = 0; i != fields.size(); ++i){
fields[i].first = i;
fields[i].second = fnames[i];
}
mFields = URangeFactory<FieldID>(fields).create();
Tuple<Ts...> x;
mOffsets = MArray<SizeT>
( mFields, iter<0,N>
( [&](auto i) { return getTupleOffset(x, i); },
[](const auto&... e) { return Vector<SizeT>({e...}); }) );
mSizes = MArray<SizeT>
( mFields, iter<0,N>
( [&](auto i) { return sizeof(std::get<i>(x)); },
[](const auto&... e) { return Vector<SizeT>({e...}); }) );
mTypes = MArray<hid_t>
( mFields, iter<0,N>
( [&](auto i) { return getTypeId(std::get<i>(x)); },
[](const auto&... e) { return Vector<hid_t>({e...}); }) );
return *this;
} }
template <typename... Ts> template <typename... Ts>
@ -75,6 +94,14 @@ namespace CNORXZ
} }
return *this; return *this;
} }
template <typename... Ts>
MArray<Tuple<Ts...>> STable<Ts...>::read()
{
MArray<Tuple<Ts...>> out(mRecords);
//...!!!
return out;
}
} }
} }

View file

@ -46,7 +46,11 @@ namespace CNORXZ
{ {
public: public:
DEFAULT_MEMBERS(STable); DEFAULT_MEMBERS(STable);
STable(const String& name, const ContentBase* _parent, const Vector<String>& fnames); STable(const String& name, const ContentBase* _parent);
STable(const String& name, const ContentBase* _parent,
const Arr<String,sizeof...(Ts)>& fnames);
STable& initFields(const Arr<String,sizeof...(Ts)>& fnames);
STable& appendRecord(const Tuple<Ts...>& t); STable& appendRecord(const Tuple<Ts...>& t);
STable& appendRecord(const MArray<Tuple<Ts...>>& t); STable& appendRecord(const MArray<Tuple<Ts...>>& t);

View file

@ -55,7 +55,7 @@ namespace
String mFileName; String mFileName;
//Vector<String> mGrps; //Vector<String> mGrps;
Vector<String> mFs; Arr<String,3> mFs;
MArray<Tuple<SizeT,Int,Double>> mTabA; MArray<Tuple<SizeT,Int,Double>> mTabA;
}; };
@ -142,13 +142,14 @@ namespace
TEST_F(Group_Test, ReadTable) TEST_F(Group_Test, ReadTable)
{ {
typedef Tuple<SizeT,Int,Double> RecType;
File h5f(mFileName, true); File h5f(mFileName, true);
h5f.open(); h5f.open();
auto tab = h5f.getGroup("gr1")->open().getTable("tab1"); auto tab = h5f.getGroup("gr1")->open().getTable("tab1", RecType());
EXPECT_EQ(tab->fields()->size(), 3u); EXPECT_EQ(tab->fields()->size(), 3u);
EXPECT_EQ(tab->records()->size(), 5u); EXPECT_EQ(tab->records()->size(), 5u);
} }
} }