CNORXZ
Container with Native Operation Routines and Expressions
Loading...
Searching...
No Matches
h5_table.cc
Go to the documentation of this file.
1// -*- C++ -*-
12#include <hdf5_hl.h>
13#include "h5_table.h"
14
15namespace CNORXZ
16{
17 namespace hdf5
18 {
19 Table::Table(const String& name, const ContentBase* _parent) :
20 ContentBase(name, _parent)
21 {
22 if(exists()){
23 hsize_t nfields = 0;
24 hsize_t nrecords = 0;
29 for(SizeT i = 0; i != nfields; ++i){
30 fieldsptr[i] = fieldnames[i].data();
31 }
34 H5TBget_field_info(mParent->id(), mName.c_str(), fieldsptr.data(), sizes.data(),
35 offsets.data(), &mTypesize);
37 for(SizeT i = 0; i != nfields; ++i){
38 fields[i].first = i;
39 fields[i].second = fieldsptr[i];
40 }
41 mFields = URangeFactory<FieldID>( std::move(fields) ).create();
42 mSizes = MArray<SizeT>(mFields, std::move(sizes));
44 this->open();
46 CXZ_ASSERT(n == mFields->size(),
47 "number of dataset members does not match number of fields: "
48 << n << " vs " << mFields->size());
50 for(SizeT i = 0; i != n; ++i){
52 }
53 mTypes = MArray<hid_t>(mFields, std::move(types));
54 this->close();
55 }
56 }
57
59 {
60 this->close();
61 }
62
64 {
65 return ContentType::TABLE;
66 }
67
68 bool Table::ro() const
69 {
70 return mParent->ro();
71 }
72
74 {
75 if(mId == 0){
76 mId = H5Dopen(mParent->id(), mName.c_str(), H5P_DEFAULT);
78 mkTypes();
79 }
80 return *this;
81 }
82
84 {
85 if(mId != 0){
88 mId = 0;
89 }
90 return *this;
91 }
92
94 {
95 return mParent->path() + "/" + mName;
96 }
97
99 {
100 return mParent->filename();
101 }
102
103 bool Table::exists() const
104 {
105 return H5Lexists(mParent->id(), mName.c_str(), H5P_DEFAULT) > 0;
106 }
107
109 {
110 CXZ_ASSERT(mFields == nullptr, "fields already initialized");
112 for(SizeT i = 0; i != fields.size(); ++i){
113 fields[i].first = i;
114 fields[i].second = fnames[i];
115 }
117 return *this;
118 }
119
121 {
122 const Int compress = 0;
125 auto fr = std::dynamic_pointer_cast<URange<FieldID>>(mFields);
126 for(auto fi = fr->begin(); fi != fr->end(); ++fi){
127 fields[fi.lex()] = (*fi).second.c_str();
128 }
131 (mName.c_str(), mParent->id(), mName.c_str(), mFields->size(), mRecords->size(), mTypesize,
132 fields.data(), mOffsets.data(), mTypes.data(), chunk_size, NULL, compress, data);
133 CXZ_ASSERT(err >= 0, "error while initialzing table: error code = " << err);
134 return *this;
135 }
136
137 Table& Table::appendRecords(SizeT n, const void* data)
138 {
139 mRecords = mRecords->extend( CRangeFactory(n).create() );
140 const herr_t err = H5TBappend_records(mParent->id(), mName.c_str(), n, mTypesize,
141 mOffsets.data(), mSizes.data(), data);
142 CXZ_ASSERT(err >= 0, "error while appending record to table: error code = " << err);
143 return *this;
144 }
145
147 {
148 H5TBread_records(mParent->id(), mName.c_str(), pos, n, mTypesize, mOffsets.data(),
149 mSizes.data(), data);
150 return *this;
151 }
152
154 {
156 H5TBread_records(mParent->id(), mName.c_str(), pos, 1, mTypesize, mOffsets.data(),
157 mSizes.data(), buf.data());
159 auto fi = std::make_shared<CIndex>(mFields);
160
161 out(fi) = operation( [&](const SizeT& off, const std::function<DType(const char*)>& f){
162 return f( buf.data() + off );
163 }, mOffsets(fi), mInterpret(fi) );
164
165 return out;
166 }
167
169 {
172 mSizes.data(), buf.data());
174 auto fi = std::make_shared<CIndex>(mFields);
175 auto ri = std::make_shared<CIndex>(mRecords);
176
177 out(ri*fi) = operation( [&](const SizeT& off, const std::function<DType(const char*)>& f, const SizeT r){
178 return f( buf.data() + r*mTypesize + off );
179 }, mOffsets(fi), mInterpret(fi), xpr(ri) );
180
181 return out;
182 }
183
184 const RangePtr& Table::fields() const
185 {
186 return mFields;
187 }
188
190 {
191 return mRecords;
192 }
193
195 {
197 auto i = std::make_shared<CIndex>(mFields);
198 mTypes(i) = operation( [&](const SizeT n){ return H5Tget_member_type( mType, n ); } , xpr(i) );
199 mInterpret = MArray<std::function<DType(const char*)>>(mFields);
200 mInterpret(i) = operation( [](const hid_t tid){
201 const hid_t tc = H5Tget_class(tid);
202 const size_t ts = H5Tget_size(tid);
203 const bool sig = H5Tget_sign(tid);
204 std::function<DType(const char*)> of;
205 switch(tc){
206 case H5T_INTEGER:{
207 switch(ts){
208 case 1:{ // 8-bit
209 of = sig ?
210 [](const char* d){ const int8_t x = *reinterpret_cast<const int8_t*>(d); return DType(static_cast<Int>(x)); } :
211 [](const char* d){ const uint8_t x = *reinterpret_cast<const uint8_t*>(d); return DType(static_cast<SizeT>(x)); };
212 break;
213 }
214 case 2:{ // 16-bit
215 of = sig ?
216 [](const char* d){ const int16_t x = *reinterpret_cast<const int16_t*>(d); return DType(static_cast<Int>(x)); } :
217 [](const char* d){ const uint16_t x = *reinterpret_cast<const uint16_t*>(d); return DType(static_cast<SizeT>(x)); };
218 break;
219 }
220 case 4:{ // 32-bit
221 of = sig ?
222 [](const char* d){ const int32_t x = *reinterpret_cast<const int32_t*>(d); return DType(static_cast<Int>(x)); } :
223 [](const char* d){ const uint32_t x = *reinterpret_cast<const uint32_t*>(d); return DType(static_cast<SizeT>(x)); };
224 break;
225 }
226 case 8:{ // 64-bit
227 of = sig ?
228 [](const char* d){ const int64_t x = *reinterpret_cast<const int64_t*>(d); return DType(static_cast<LInt>(x)); } :
229 [](const char* d){ const uint64_t x = *reinterpret_cast<const uint64_t*>(d); return DType(static_cast<SizeT>(x)); };
230 break;
231 }
232 default:
233 CXZ_ERROR("got integer of weird size: " << ts);
234 }
235 break;
236 }
237 case H5T_FLOAT:{
238 if(ts == 4) {
239 of = [](const char* d){ const float x = *reinterpret_cast<const float*>(d); return DType(static_cast<Double>(x)); };
240 }
241 else {
242 CXZ_ASSERT(ts == 8, "got float of weird size: " << ts);
243 of = [](const char* d){ const double x = *reinterpret_cast<const double*>(d); return DType(static_cast<Double>(x)); };
244 }
245 break;
246 }
247 default:
248 CXZ_ERROR("got unsupported type");
249 };
250 return of;
251 }, mTypes(i) );
252 }
253 }
254}
#define CXZ_ERROR(errmsg)
Definition assert.h:26
#define CXZ_ASSERT(statement, errmsg)
Definition assert.h:40
virtual const T * data() const override
Definition marray.cc.h:65
virtual bool ro() const =0
virtual String path() const =0
const ContentBase * mParent
virtual String filename() const =0
Table & readRecords(SizeT pos, SizeT n, char *data)
Definition h5_table.cc:146
MArray< DType > read() const
Definition h5_table.cc:168
const RangePtr & records() const
Definition h5_table.cc:189
virtual Table & close() override final
Definition h5_table.cc:83
virtual ContentType type() const override final
Definition h5_table.cc:63
MArray< SizeT > mSizes
Definition h5_table.h:105
const RangePtr & fields() const
Definition h5_table.cc:184
Table & initTable(SizeT n, const void *data, SizeT dsize, SizeT chunk_size)
Definition h5_table.cc:120
virtual Table & open() override final
Definition h5_table.cc:73
Table & appendRecords(SizeT n, const void *data)
Definition h5_table.cc:137
virtual bool exists() const override final
Definition h5_table.cc:103
Table(const String &name, const ContentBase *_parent)
Definition h5_table.cc:19
virtual String filename() const override final
Definition h5_table.cc:98
MArray< DType > readRecord(SizeT pos) const
Definition h5_table.cc:153
virtual String path() const override final
Definition h5_table.cc:93
Table & initFieldNames(const Vector< String > &fnames)
Definition h5_table.cc:108
MArray< std::function< DType(const char *)> mInterpret)
Definition h5_table.h:110
virtual bool ro() const override final
Definition h5_table.cc:68
MArray< hid_t > mTypes
Definition h5_table.h:107
MArray< SizeT > mOffsets
Definition h5_table.h:106
Table declaration.
std::string String
Definition types.h:42
double Double
Definition types.h:39
uint64_t SizeT
Definition types.h:38
Sptr< RangeBase > RangePtr
Definition types.h:157
std::vector< T, Allocator< T > > Vector
Definition types.h:310
int32_t Int
Definition types.h:36
decltype(auto) xpr(const Sptr< I > &i)
Sptr< Range > rangeCast(const RangePtr r)
int64_t LInt
Definition types.h:37
constexpr decltype(auto) operation(F &&f, const Ops &... ops)