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 "ritsuko/hdf5/hdf5.hpp"
7
8#include <vector>
9#include <cstdint>
10
11#include "utils_public.hpp"
12#include "utils_type.hpp"
13#include "utils_dimnames.hpp"
14
20namespace chihaya {
21
26namespace dense_array {
27
36inline ArrayDetails validate(const H5::Group& handle, const ritsuko::Version& version, [[maybe_unused]] Options& options) {
37 ArrayDetails output;
38
39 {
40 auto dhandle = ritsuko::hdf5::open_dataset(handle, "data");
41 auto dspace = dhandle.getSpace();
42 auto ndims = dspace.getSimpleExtentNdims();
43 if (ndims == 0) {
44 throw std::runtime_error("'data' should have non-zero dimensions for a dense array");
45 }
46
47 std::vector<hsize_t> dims(ndims);
48 dspace.getSimpleExtentDims(dims.data());
49 output.dimensions.insert(output.dimensions.end(), dims.begin(), dims.end());
50
51 try {
52 if (version.lt(1, 1, 0)) {
53 output.type = internal_type::translate_type_0_0(dhandle.getTypeClass());
54 if (internal_type::is_boolean(dhandle)) {
55 output.type = BOOLEAN;
56 }
57 } else {
58 auto type = ritsuko::hdf5::open_and_load_scalar_string_attribute(dhandle, "type");
59 output.type = internal_type::translate_type_1_1(type);
60 internal_type::check_type_1_1(dhandle, output.type);
61 }
62
63 if (!options.details_only) {
64 internal_misc::validate_missing_placeholder(dhandle, version);
65 }
66
67 if (dhandle.getTypeClass() == H5T_STRING) {
68 ritsuko::hdf5::validate_nd_string_dataset(dhandle, dims, 1000000);
69 }
70
71 } catch (std::exception& e) {
72 throw std::runtime_error("failed to validate 'data'; " + std::string(e.what()));
73 }
74 }
75
76 bool native;
77 {
78 auto nhandle = ritsuko::hdf5::open_dataset(handle, "native");
79 if (!ritsuko::hdf5::is_scalar(nhandle)) {
80 throw std::runtime_error("'native' should be a scalar");
81 }
82
83 if (version.lt(1, 1, 0)) {
84 if (nhandle.getTypeClass() != H5T_INTEGER) {
85 throw std::runtime_error("'native' should have an integer datatype");
86 }
87 native = ritsuko::hdf5::load_scalar_numeric_dataset<int>(nhandle);
88 } else {
89 if (ritsuko::hdf5::exceeds_integer_limit(nhandle, 8, true)) {
90 throw std::runtime_error("'native' should have a datatype that fits into an 8-bit signed integer");
91 }
92 native = ritsuko::hdf5::load_scalar_numeric_dataset<int8_t>(nhandle);
93 }
94 }
95
96 // Do this before the 'native' check.
97 if (!options.details_only) {
98 if (handle.exists("dimnames")) {
99 internal_dimnames::validate(handle, output.dimensions, version);
100 }
101 }
102
103 if (!native) {
104 std::reverse(output.dimensions.begin(), output.dimensions.end());
105 }
106
107 return output;
108}
109
110}
111
112}
113
114#endif
ArrayDetails validate(const H5::Group &handle, const ritsuko::Version &version, Options &options)
Definition: dense_array.hpp:36
Namespace for all chihaya functions.
Definition: binary_arithmetic.hpp:22
Details about an array.
Definition: utils_public.hpp:36
std::vector< size_t > dimensions
Definition: utils_public.hpp:56
ArrayType type
Definition: utils_public.hpp:50
Validation options.
Definition: utils_public.hpp:66
Various public utilities.