chihaya
Validating delayed array operations in HDF5
Loading...
Searching...
No Matches
unary_comparison.hpp
Go to the documentation of this file.
1#ifndef CHIHAYA_UNARY_COMPARISON_HPP
2#define CHIHAYA_UNARY_COMPARISON_HPP
3
4#include "H5Cpp.h"
5#include "ritsuko/ritsuko.hpp"
6
7#include <stdexcept>
8#include <exception>
9#include <string>
10
11#include "utils_public.hpp"
12#include "utils_misc.hpp"
13#include "utils_type.hpp"
14#include "utils_nary.hpp"
15
21namespace chihaya {
22
35inline ArrayDetails validate_unary_comparison(const H5::Group& group, const ritsuko::Version& version, const Options& options) {
36 ArrayDetails seed_details;
37 try {
38 auto shandle = group.openGroup("seed");
39 seed_details = validate(shandle, version, options);
40 } catch (...) {
41 std::throw_with_nested(std::runtime_error("failed to validate 'seed'"));
42 }
43
44 if (!options.details_only) {
45 std::string method;
46 try {
47 auto mhandle = group.openDataSet("method");
48 method = read_scalar_string_dataset(mhandle);
49 if (!is_valid_comparison_operation(method)) {
50 throw std::runtime_error("unrecognized operation '" + method + "'");
51 }
52 } catch (...) {
53 std::throw_with_nested(std::runtime_error("failed to validate 'method'"));
54 }
55
56 std::string side;
57 try {
58 auto shandle = group.openDataSet("side");
59 auto side = read_scalar_string_dataset(shandle);
60 if (side != "left" && side != "right") {
61 throw std::runtime_error("expected either 'left' or 'right'");
62 }
63 } catch (...) {
64 std::throw_with_nested(std::runtime_error("failed to validate 'side'"));
65 }
66
67 enum Failure { VALUE, ALONG };
68 Failure who_failed = VALUE;
69 try {
70 auto vhandle = group.openDataSet("value");
71
72 ArrayType val_type;
73 if (version.lt(1, 1, 0)) {
74 val_type = translate_type_0_99(vhandle.getTypeClass());
75 } else {
76 try {
77 auto thandle = vhandle.openAttribute("type");
78 auto type = read_scalar_string_attribute(thandle);
79 val_type = translate_type_1_1(type);
80 check_type_1_1(vhandle, val_type);
81 } catch (...) {
82 std::throw_with_nested(std::runtime_error("failed to validate the 'type' attribute"));
83 }
84 }
85 if ((val_type == STRING) != (seed_details.type == STRING)) {
86 throw std::runtime_error("both or neither of 'seed' and 'value' should contain strings");
87 }
88
89 validate_missing_placeholder(vhandle, version);
90
91 auto ndims = vhandle.getSpace().getSimpleExtentNdims();
92 if (ndims == 0) { // scalar operation.
93 if (vhandle.getTypeClass() == H5T_STRING) {
94 ritsuko::hdf5::validate_scalar_string(vhandle);
95 }
96
97 } else if (ndims == 1) {
98 hsize_t extent;
99 vhandle.getSpace().getSimpleExtentDims(&extent);
100 who_failed = ALONG;
101 auto ahandle = group.openDataSet("along");
102 check_unary_along(ahandle, version, seed_details.dimensions, extent);
103 who_failed = VALUE;
104
105 if (vhandle.getTypeClass() == H5T_STRING) {
106 ritsuko::hdf5::validate_1d_strings(
107 vhandle,
108 extent,
109 [&]{
110 ritsuko::hdf5::Validate1dStringsOptions opt;
111 opt.contiguous_chunk_size = options.contiguous_chunk_size;
112 return opt;
113 }()
114 );
115 }
116
117 } else {
118 throw std::runtime_error("dataset should be scalar or 1-dimensional");
119 }
120
121 } catch (...) {
122 std::string desc;
123 switch (who_failed) {
124 case VALUE: desc = "value"; break;
125 case ALONG: desc = "along"; break;
126 }
127 std::throw_with_nested(std::runtime_error("failed to validate '" + desc + "'"));
128 }
129 }
130
131 seed_details.type = BOOLEAN;
132 return seed_details;
133}
134
135}
136
137#endif
Namespace for all chihaya functions.
Definition binary_arithmetic.hpp:21
ArrayDetails validate(const H5::Group &group, const ritsuko::Version &version, const Options &options)
Definition validate.hpp:96
ArrayDetails validate_unary_comparison(const H5::Group &group, const ritsuko::Version &version, const Options &options)
Definition unary_comparison.hpp:35
ArrayType
Definition utils_public.hpp:29
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
bool details_only
Definition utils_public.hpp:84
hsize_t contiguous_chunk_size
Definition utils_public.hpp:90
Various public utilities.