chihaya
Validating delayed array operations in HDF5
Loading...
Searching...
No Matches
dense_array.hpp
Go to the documentation of this file.
1#ifndef CHIHAYA_DENSE_ARRAY_HPP
2#define CHIHAYA_DENSE_ARRAY_HPP
3
4#include "H5Cpp.h"
5#include "ritsuko/ritsuko.hpp"
7
8#include <vector>
9#include <cstdint>
10#include <stdexcept>
11#include <exception>
12#include <cstddef>
13
14#include "utils_public.hpp"
15#include "utils_misc.hpp"
16#include "utils_type.hpp"
17#include "utils_dimensions.hpp"
18
24namespace chihaya {
25
29template<typename Output_>
30void transplant_dimensions(std::vector<hsize_t>& src, std::vector<Output_>& output) {
31 if constexpr(std::is_same<hsize_t, Output_>::value) {
32 // Avoid a copy if we can.
33 output = std::move(src);
34 } else {
35 const auto ndims = src.size();
36 sanisizer::resize(output, ndims);
37 for (I<decltype(ndims)> d = 0; d < ndims; ++d) {
38 output[d] = sanisizer::cast<Output_>(src[d]);
39 }
40 }
41}
58inline ArrayDetails validate_dense_array(const H5::Group& group, const ritsuko::Version& version, [[maybe_unused]] const Options& options) {
59 ArrayDetails output;
60
61 try {
62 auto dhandle = group.openDataSet("data");
63 auto dspace = dhandle.getSpace();
64 const auto ndims = dspace.getSimpleExtentNdims();
65 if (ndims == 0) {
66 throw std::runtime_error("expected at least one dimension");
67 }
68 auto dims = sanisizer::create<std::vector<hsize_t> >(ndims);
69 dspace.getSimpleExtentDims(dims.data());
70
71 if (version.lt(1, 1, 0)) {
72 output.type = translate_type_0_99(dhandle.getTypeClass());
73 if (is_boolean_0_99(dhandle)) {
74 output.type = BOOLEAN;
75 }
76 } else {
77 try {
78 auto thandle = dhandle.openAttribute("type");
79 auto type = read_scalar_string_attribute(thandle);
80 output.type = translate_type_1_1(type);
81 if (!options.details_only) {
82 check_type_1_1(dhandle, output.type);
83 }
84 } catch (...) {
85 std::throw_with_nested(std::runtime_error("failed to validate the 'type' attribute"));
86 }
87 }
88
89 if (!options.details_only) {
90 validate_missing_placeholder(dhandle, version);
91 if (dhandle.getTypeClass() == H5T_STRING) {
92 ritsuko::hdf5::validate_nd_strings(
93 dhandle,
94 dims,
95 [&]{
96 ritsuko::hdf5::ValidateNdStringsOptions opt;
97 opt.contiguous_chunk_size = options.contiguous_chunk_size;
98 return opt;
99 }()
100 );
101 }
102 }
103
104 transplant_dimensions(dims, output.dimensions);
105 } catch (...) {
106 std::throw_with_nested(std::runtime_error("failed to validate 'data'"));
107 }
108
109 bool native;
110 try {
111 auto nhandle = group.openDataSet("native");
112 if (nhandle.getSpace().getSimpleExtentNdims() != 0) {
113 throw std::runtime_error("expected a scalar dataset");
114 }
115
116 if (version.lt(1, 1, 0)) {
117 native = load_boolean_scalar_0_99(nhandle);
118 } else {
119 if (ritsuko::hdf5::exceeds_integer_limit(nhandle, 8, true)) {
120 throw std::runtime_error("expected a datatype that fits into an 8-bit signed integer");
121 }
122 std::int8_t tmp_native;
123 nhandle.read(&tmp_native, H5::PredType::NATIVE_INT);
124 native = tmp_native;
125 }
126 } catch (...) {
127 std::throw_with_nested(std::runtime_error("failed to validate 'native'"));
128 }
129
130 // Do this before applying the 'native' reversal.
131 if (!options.details_only) {
132 if (group.exists("dimnames")) {
133 try {
134 auto dnhandle = group.openGroup("dimnames");
135 validate_dimnames_internal(dnhandle, output.dimensions, version, options.contiguous_chunk_size);
136 } catch (...) {
137 std::throw_with_nested(std::runtime_error("failed to validate 'dimnames'"));
138 }
139 }
140 }
141
142 if (!native) {
143 std::reverse(output.dimensions.begin(), output.dimensions.end());
144 }
145
146 return output;
147}
148
149}
150
151#endif
Namespace for all chihaya functions.
Definition binary_arithmetic.hpp:21
ArrayDetails validate_dense_array(const H5::Group &group, const ritsuko::Version &version, const Options &options)
Definition dense_array.hpp:58
Container_ create(Value_ x, Args_ &&... args)
void resize(Container_ &container, Value_ x, Args_ &&... args)
constexpr Dest_ cast(Value_ x)
Details about an array.
Definition utils_public.hpp:38
std::vector< std::size_t > dimensions
Definition utils_public.hpp:58
ArrayType type
Definition utils_public.hpp:52
Options for validate().
Definition utils_public.hpp:79
Various public utilities.