chihaya
Validating delayed array operations in HDF5
Loading...
Searching...
No Matches
unary_arithmetic.hpp
Go to the documentation of this file.
1#ifndef CHIHAYA_UNARY_ARITHMETIC_HPP
2#define CHIHAYA_UNARY_ARITHMETIC_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_type.hpp"
13#include "utils_misc.hpp"
14#include "utils_nary.hpp"
15
21namespace chihaya {
22
35inline ArrayDetails validate_unary_arithmetic(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_numeric_seed(shandle, version, options);
40 } catch (...) {
41 std::throw_with_nested(std::runtime_error("failed to validate 'seed'"));
42 }
43
44 std::string method;
45 try {
46 auto mhandle = group.openDataSet("method");
47 method = read_scalar_string_dataset(mhandle);
48
49 if (!options.details_only) {
50 if (!is_valid_arithmetic_operation(method)) {
51 throw std::runtime_error("unrecognized operation '" + method + "'");
52 }
53 }
54 } catch (...) {
55 std::throw_with_nested(std::runtime_error("failed to validate 'method'"));
56 }
57
58 std::string side;
59 try {
60 auto shandle = group.openDataSet("side");
61 side = read_scalar_string_dataset(shandle);
62
63 if (!options.details_only) {
64 if (side == "none") {
65 if (method != "+" && method != "-") {
66 throw std::runtime_error("cannot be 'none' for operation '" + method + "'");
67 }
68 } else if (side != "left" && side != "right") {
69 throw std::runtime_error("expected 'left' or 'right' for operation '" + method + "'");
70 }
71 }
72 } catch (...) {
73 std::throw_with_nested(std::runtime_error("failed to validate 'side'"));
74 }
75
76 // If side = none, we set it to INTEGER to promote BOOLEANs to integer (implicit multiplication by +/-1).
77 ArrayType val_type = INTEGER;
78
79 if (side != "none") {
80 enum Failure { VALUE, ALONG };
81 Failure who_failed = VALUE;
82 try {
83 auto vhandle = group.openDataSet("value");
84
85 if (version.lt(1, 1, 0)) {
86 val_type = translate_type_0_99(vhandle.getTypeClass());
87 } else {
88 try {
89 auto thandle = vhandle.openAttribute("type");
90 auto type = read_scalar_string_attribute(thandle);
91 val_type = translate_type_1_1(type);
92 check_type_1_1(vhandle, val_type);
93 } catch (...) {
94 std::throw_with_nested(std::runtime_error("failed to validate the 'type' attribute"));
95 }
96 }
97
98 if (val_type != INTEGER && val_type != BOOLEAN && val_type != FLOAT) {
99 throw std::runtime_error("dataset should be integer, float or boolean");
100 }
101
102 if (!options.details_only) {
103 validate_missing_placeholder(vhandle, version);
104
105 auto vspace = vhandle.getSpace();
106 auto ndims = vspace.getSimpleExtentNdims();
107 if (ndims == 0) {
108 // scalar operation.
109 } else if (ndims == 1) {
110 hsize_t extent;
111 vspace.getSimpleExtentDims(&extent);
112 who_failed = ALONG;
113 auto ahandle = group.openDataSet("along");
114 check_unary_along(ahandle, version, seed_details.dimensions, extent);
115 who_failed = VALUE;
116 } else {
117 throw std::runtime_error("dataset should be scalar or 1-dimensional");
118 }
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 // Determining type promotion rules.
132 seed_details.type = determine_arithmetic_output_type(val_type, seed_details.type, method);
133
134 return seed_details;
135}
136
137}
138
139#endif
Namespace for all chihaya functions.
Definition binary_arithmetic.hpp:21
ArrayDetails validate_unary_arithmetic(const H5::Group &group, const ritsuko::Version &version, const Options &options)
Definition unary_arithmetic.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
Various public utilities.