ritsuko
Helper utilities for ArtifactDB C++ code
Loading...
Searching...
No Matches
IterateNdDataset.hpp
Go to the documentation of this file.
1#ifndef RITSUKO_ITERATE_ND_DATASET_HPP
2#define RITSUKO_ITERATE_ND_DATASET_HPP
3
4#include "H5Cpp.h"
5
6#include <vector>
7#include <algorithm>
8#include <cmath>
9
15namespace ritsuko {
16
17namespace hdf5 {
18
31 IterateNdDataset(std::vector<hsize_t> d, std::vector<hsize_t> b) :
32 data_extent(std::move(d)),
33 block_extent(std::move(b)),
34 ndims(data_extent.size()),
35 starts_internal(ndims),
36 counts_internal(block_extent),
37 dspace(ndims, data_extent.data())
38 {
39 for (auto b : block_extent) {
40 total_size *= b;
41 }
42
43 if (total_size) {
44 dspace.selectHyperslab(H5S_SELECT_SET, counts_internal.data(), starts_internal.data());
45 mspace.setExtentSimple(ndims, counts_internal.data());
46 } else {
47 finished_internal = true;
48 }
49 }
50
55 void next() {
56 // Attempting a shift from the last dimension as this is the fastest-changing.
57 for (size_t i = ndims; i > 0; --i) {
58 auto d = i - 1;
59 starts_internal[d] += block_extent[d];
60
61 // Shift was possible, breaking out.
62 if (starts_internal[d] < data_extent[d]) {
63 total_size /= counts_internal[d];
64 counts_internal[d] = std::min(data_extent[d] - starts_internal[d], block_extent[d]);
65 total_size *= counts_internal[d];
66 break;
67 }
68
69 // Next step isn't possible as we've reached the end of the dataset.
70 if (d == 0) {
71 finished_internal = true;
72 return;
73 }
74
75 // Reached the end of the current dimension extent; set it to zero,
76 // move to the next dimension and increment it.
77 starts_internal[d] = 0;
78 total_size /= counts_internal[d];
79 counts_internal[d] = std::min(data_extent[d], block_extent[d]);
80 total_size *= counts_internal[d];
81 }
82
83 dspace.selectHyperslab(H5S_SELECT_SET, counts_internal.data(), starts_internal.data());
84 mspace.setExtentSimple(ndims, counts_internal.data());
85 }
86
87public:
92 bool finished() const {
93 return finished_internal;
94 }
95
101 size_t current_block_size() const {
102 return total_size;
103 }
104
108 const std::vector<hsize_t>& starts () const {
109 return starts_internal;
110 }
111
117 const std::vector<hsize_t>& counts () const {
118 return counts_internal;
119 }
120
124 const H5::DataSpace& file_space() const {
125 return dspace;
126 }
127
132 const H5::DataSpace& memory_space() const {
133 return mspace;
134 }
135
139 const std::vector<hsize_t>& dimensions() const {
140 return data_extent;
141 }
142
146 const std::vector<hsize_t>& block_dimensions() const {
147 return block_extent;
148 }
149
150private:
151 std::vector<hsize_t> data_extent, block_extent;
152 size_t ndims;
153
154 std::vector<hsize_t> starts_internal, counts_internal;
155 H5::DataSpace mspace, dspace;
156 bool finished_internal = false;
157 size_t total_size = 1;
158};
159
160}
161
162}
163
164#endif
Assorted helper functions for parsing and validation.
Definition choose_missing_placeholder.hpp:15
Iterate through an N-dimensional dataset by block.
Definition IterateNdDataset.hpp:25
IterateNdDataset(std::vector< hsize_t > d, std::vector< hsize_t > b)
Definition IterateNdDataset.hpp:31
const H5::DataSpace & memory_space() const
Definition IterateNdDataset.hpp:132
const std::vector< hsize_t > & counts() const
Definition IterateNdDataset.hpp:117
const std::vector< hsize_t > & starts() const
Definition IterateNdDataset.hpp:108
bool finished() const
Definition IterateNdDataset.hpp:92
const H5::DataSpace & file_space() const
Definition IterateNdDataset.hpp:124
const std::vector< hsize_t > & block_dimensions() const
Definition IterateNdDataset.hpp:146
size_t current_block_size() const
Definition IterateNdDataset.hpp:101
const std::vector< hsize_t > & dimensions() const
Definition IterateNdDataset.hpp:139
void next()
Definition IterateNdDataset.hpp:55