ritsuko
Helper utilities for ArtifactDB C++ code
Loading...
Searching...
No Matches
Stream1dNumericDataset.hpp
Go to the documentation of this file.
1#ifndef RITSUKO_HDF5_STREAM_1D_NUMERIC_DATASET_HPP
2#define RITSUKO_HDF5_STREAM_1D_NUMERIC_DATASET_HPP
3
4#include "H5Cpp.h"
5
6#include <vector>
7#include <stdexcept>
8
10#include "get_1d_length.hpp"
11#include "get_name.hpp"
13
19namespace ritsuko {
20
21namespace hdf5 {
22
30template<typename Type_>
32public:
39 Stream1dNumericDataset(const H5::DataSet* ptr, hsize_t length, hsize_t buffer_size) :
40 ptr(ptr),
41 full_length(length),
42 block_size(pick_1d_block_size(ptr->getCreatePlist(), full_length, buffer_size)),
43 mspace(1, &block_size),
44 dspace(1, &full_length),
45 buffer(block_size)
46 {}
47
54 Stream1dNumericDataset(const H5::DataSet* ptr, hsize_t buffer_size) :
55 Stream1dNumericDataset(ptr, get_1d_length(ptr->getSpace(), false), buffer_size)
56 {}
57
58public:
62 Type_ get() {
63 while (consumed >= available) {
64 consumed -= available;
65 load();
66 }
67 return buffer[consumed];
68 }
69
75 std::pair<const Type_*, size_t> get_many() {
76 while (consumed >= available) {
77 consumed -= available;
78 load();
79 }
80 return std::make_pair(buffer.data() + consumed, available - consumed);
81 }
82
88 void next(size_t jump = 1) {
89 consumed += jump;
90 }
91
95 hsize_t length() const {
96 return full_length;
97 }
98
102 hsize_t position() const {
103 return consumed + last_loaded;
104 }
105
106private:
107 const H5::DataSet* ptr;
108 hsize_t full_length, block_size;
109 H5::DataSpace mspace;
110 H5::DataSpace dspace;
111 std::vector<Type_> buffer;
112
113 hsize_t last_loaded = 0;
114 hsize_t consumed = 0;
115 hsize_t available = 0;
116
117 void load() {
118 if (last_loaded >= full_length) {
119 throw std::runtime_error("requesting data beyond the end of the dataset at '" + get_name(*ptr) + "'");
120 }
121 available = std::min(full_length - last_loaded, block_size);
122 constexpr hsize_t zero = 0;
123 mspace.selectHyperslab(H5S_SELECT_SET, &available, &zero);
124 dspace.selectHyperslab(H5S_SELECT_SET, &available, &last_loaded);
125 ptr->read(buffer.data(), as_numeric_datatype<Type_>(), mspace, dspace);
126 last_loaded += available;
127 }
128};
129
130}
131
132}
133
134#endif
Choose a HDF5 datatype.
Stream a numeric 1-dimensional HDF5 dataset into memory.
Definition Stream1dNumericDataset.hpp:31
Stream1dNumericDataset(const H5::DataSet *ptr, hsize_t buffer_size)
Definition Stream1dNumericDataset.hpp:54
hsize_t length() const
Definition Stream1dNumericDataset.hpp:95
std::pair< const Type_ *, size_t > get_many()
Definition Stream1dNumericDataset.hpp:75
Type_ get()
Definition Stream1dNumericDataset.hpp:62
hsize_t position() const
Definition Stream1dNumericDataset.hpp:102
void next(size_t jump=1)
Definition Stream1dNumericDataset.hpp:88
Stream1dNumericDataset(const H5::DataSet *ptr, hsize_t length, hsize_t buffer_size)
Definition Stream1dNumericDataset.hpp:39
Get the length of a 1-dimensional HDF5 dataset.
Get the name of a HDF5 object.
std::string get_name(const Handle_ &handle)
Definition get_name.hpp:24
hsize_t pick_1d_block_size(const H5::DSetCreatPropList &cplist, hsize_t full_length, hsize_t buffer_size=10000)
Definition pick_1d_block_size.hpp:26
const H5::PredType & as_numeric_datatype()
Definition as_numeric_datatype.hpp:26
hsize_t get_1d_length(const H5::DataSpace &space, bool allow_scalar)
Definition get_1d_length.hpp:25
Assorted helper functions for parsing and validation.
Definition choose_missing_placeholder.hpp:15
Pick a block size for a 1-dimensional HDF5 dataset.