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"
6#include "sanisizer/sanisizer.hpp"
7
8#include <vector>
9#include <cstdint>
10#include <stdexcept>
11#include <cstddef>
12
13#include "utils_public.hpp"
14#include "utils_misc.hpp"
15#include "utils_type.hpp"
16#include "utils_dimensions.hpp"
17
23namespace chihaya {
24
28template<typename Output_>
29void transplant_dimensions(std::vector<hsize_t>& src, std::vector<Output_>& output) {
30 if constexpr(std::is_same<hsize_t, Output_>::value) {
31 // Avoid a copy if we can.
32 output = std::move(src);
33 } else {
34 const auto ndims = src.size();
35 sanisizer::resize(output, ndims);
36 for (I<decltype(ndims)> d = 0; d < ndims; ++d) {
37 output[d] = sanisizer::cast<Output_>(src[d]);
38 }
39 }
40}
53inline ArrayDetails validate_dense_array(const H5::Group& group, const ritsuko::Version& version, [[maybe_unused]] const Options& options) {
54 ArrayDetails output;
55
56 {
57 auto dhandle = group.openDataSet("data");
58 auto dspace = dhandle.getSpace();
59 const auto ndims = dspace.getSimpleExtentNdims();
60 if (ndims == 0) {
61 throw std::runtime_error("'data' should have non-zero dimensions");
62 }
63 auto dims = sanisizer::create<std::vector<hsize_t> >(ndims);
64 dspace.getSimpleExtentDims(dims.data());
65
66 try {
67 if (version.lt(1, 1, 0)) {
68 output.type = translate_type_0_99(dhandle.getTypeClass());
69 if (is_boolean_0_99(dhandle)) {
70 output.type = BOOLEAN;
71 }
72 } else {
73 auto type = load_scalar_string_attribute(dhandle, "type");
74 output.type = translate_type_1_1(type);
75 if (!options.details_only) {
76 check_type_1_1(dhandle, output.type);
77 }
78 }
79
80 if (!options.details_only) {
81 validate_missing_placeholder(dhandle, version);
82 if (dhandle.getTypeClass() == H5T_STRING) {
83 ritsuko::hdf5::validate_nd_strings(dhandle, dims);
84 }
85 }
86
87 } catch (std::exception& e) {
88 throw std::runtime_error("failed to validate 'data'; " + std::string(e.what()));
89 }
90
91 transplant_dimensions(dims, output.dimensions);
92 }
93
94 bool native;
95 {
96 auto nhandle = group.openDataSet("native");
97 if (nhandle.getSpace().getSimpleExtentNdims() != 0) {
98 throw std::runtime_error("'native' attribute should be a scalar");
99 }
100
101 if (version.lt(1, 1, 0)) {
102 native = load_boolean_scalar_0_99(nhandle);
103 } else {
104 if (ritsuko::hdf5::exceeds_integer_limit(nhandle, 8, true)) {
105 throw std::runtime_error("'native' attribute should use a datatype that fits into an 8-bit signed integer");
106 }
107 std::int8_t tmp_native;
108 nhandle.read(&tmp_native, H5::PredType::NATIVE_INT);
109 native = tmp_native;
110 }
111 }
112
113 // Do this before applying the 'native' reversal.
114 if (!options.details_only) {
115 if (group.exists("dimnames")) {
116 validate_dimnames_internal(group, output.dimensions, version);
117 }
118 }
119
120 if (!native) {
121 std::reverse(output.dimensions.begin(), output.dimensions.end());
122 }
123
124 return output;
125}
126
127}
128
129#endif
Namespace for all chihaya functions.
Definition binary_arithmetic.hpp:20
ArrayDetails validate_dense_array(const H5::Group &group, const ritsuko::Version &version, const Options &options)
Definition dense_array.hpp:53
Details about an array.
Definition utils_public.hpp:37
std::vector< std::size_t > dimensions
Definition utils_public.hpp:57
ArrayType type
Definition utils_public.hpp:51
Validation options.
Definition utils_public.hpp:67
Various public utilities.