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 <string>
9
10#include "utils_public.hpp"
11#include "utils_type.hpp"
12#include "utils_misc.hpp"
13#include "utils_nary.hpp"
14
20namespace chihaya {
21
30inline ArrayDetails validate_unary_arithmetic(const H5::Group& group, const ritsuko::Version& version, const Options& options) {
31 auto seed_details = fetch_numeric_seed(group, "seed", version, options);
32
33 auto method = load_scalar_string_dataset(group, "method");
34 if (!options.details_only) {
35 if (!is_valid_arithmetic_operation(method)) {
36 throw std::runtime_error("unrecognized operation in 'method' (got '" + method + "')");
37 }
38 }
39
40 auto side = load_scalar_string_dataset(group, "side");
41 if (!options.details_only) {
42 if (side == "none") {
43 if (method != "+" && method != "-") {
44 throw std::runtime_error("'side' cannot be 'none' for operation '" + method + "'");
45 }
46 } else if (side != "left" && side != "right") {
47 throw std::runtime_error("'side' for operation '" + method + "' should be 'left' or 'right' (got '" + side + "')");
48 }
49 }
50
51 // If side = none, we set it to INTEGER to promote BOOLEANs to integer (implicit multiplication by +/-1).
52 ArrayType val_type = INTEGER;
53
54 if (side != "none") {
55 auto vhandle = group.openDataSet("value");
56
57 try {
58 if (version.lt(1, 1, 0)) {
59 val_type = translate_type_0_99(vhandle.getTypeClass());
60 } else {
61 auto type = load_scalar_string_attribute(vhandle, "type");
62 val_type = translate_type_1_1(type);
63 check_type_1_1(vhandle, val_type);
64 }
65
66 if (val_type != INTEGER && val_type != BOOLEAN && val_type != FLOAT) {
67 throw std::runtime_error("dataset should be integer, float or boolean");
68 }
69
70 if (!options.details_only) {
71 validate_missing_placeholder(vhandle, version);
72
73 auto vspace = vhandle.getSpace();
74 const auto ndims = vspace.getSimpleExtentNdims();
75 if (ndims == 0) {
76 // scalar operation.
77 } else if (ndims == 1) {
78 hsize_t extent;
79 vspace.getSimpleExtentDims(&extent);
80 check_unary_along(group, version, seed_details.dimensions, extent);
81 } else {
82 throw std::runtime_error("dataset should be scalar or 1-dimensional");
83 }
84 }
85
86 } catch (std::exception& e) {
87 throw std::runtime_error("failed to validate 'value'; " + std::string(e.what()));
88 }
89 }
90
91 // Determining type promotion rules.
92 seed_details.type = determine_arithmetic_output_type(val_type, seed_details.type, method);
93
94 return seed_details;
95}
96
97}
98
99#endif
Namespace for all chihaya functions.
Definition binary_arithmetic.hpp:20
ArrayDetails validate_unary_arithmetic(const H5::Group &group, const ritsuko::Version &version, const Options &options)
Definition unary_arithmetic.hpp:30
ArrayType
Definition utils_public.hpp:28
Details about an array.
Definition utils_public.hpp:37
Validation options.
Definition utils_public.hpp:67
bool details_only
Definition utils_public.hpp:72
Various public utilities.