chihaya
Validating delayed array operations in HDF5
Loading...
Searching...
No Matches
combine.hpp
Go to the documentation of this file.
1#ifndef CHIHAYA_COMBINE_HPP
2#define CHIHAYA_COMBINE_HPP
3
4#include "H5Cpp.h"
5#include "ritsuko/ritsuko.hpp"
7
8#include <stdexcept>
9#include <exception>
10#include <vector>
11#include <string>
12#include <cstddef>
13
14#include "utils_public.hpp"
15#include "utils_misc.hpp"
16#include "utils_list.hpp"
17#include "utils_dimensions.hpp"
18
24namespace chihaya {
25
38inline ArrayDetails validate_combine(const H5::Group& group, const ritsuko::Version& version, const Options& options) {
39 std::uint64_t along;
40 try {
41 along = load_along(group.openDataSet("along"), version);
42 } catch (...) {
43 std::throw_with_nested(std::runtime_error("failed to validate 'along'"));
44 }
45
46 ArrayType type = BOOLEAN;
47 std::vector<std::size_t> dimensions;
48
49 try {
50 const auto shandle = group.openGroup("seeds");
51 auto list_params = validate_list(shandle, version);
52 if (list_params.present.size() != list_params.length) {
53 throw std::runtime_error("missing elements in the 'seeds' list");
54 }
55
56 bool first = true;
57 I<decltype(list_params.length)> num_strings = 0;
58
59 for (const auto& p : list_params.present) {
60 try {
61 auto sdhandle = shandle.openGroup(p.second);
62 auto cur_seed = validate(sdhandle, version, options);
63
64 if (first) {
65 type = cur_seed.type;
66 dimensions = std::move(cur_seed.dimensions);
67 if (sanisizer::is_greater_than_or_equal(along, dimensions.size())) {
68 throw std::runtime_error("'along' should be less than the seed dimensionality");
69 }
70 first = false;
71
72 } else {
73 if (type < cur_seed.type) {
74 type = cur_seed.type;
75 }
76 const auto ndims = dimensions.size();
77 if (ndims != cur_seed.dimensions.size()) {
78 throw std::runtime_error("detected dimensionality mismatch between seeds");
79 }
80 for (I<decltype(ndims)> d = 0; d < ndims; ++d) {
81 if (sanisizer::is_equal(d, along)) {
82 dimensions[d] = sanisizer::sum<std::size_t>(dimensions[d], cur_seed.dimensions[d]);
83 } else if (dimensions[d] != cur_seed.dimensions[d]) {
84 throw std::runtime_error("inconsistent dimension extents between seeds");
85 }
86 }
87 }
88
89 num_strings += (cur_seed.type == STRING);
90 } catch (...) {
91 std::throw_with_nested(std::runtime_error("failed to validate seed " + p.second));
92 }
93 }
94
95 if (num_strings != 0 && num_strings != list_params.length) {
96 throw std::runtime_error("either none or all of the arrays to be combined should contain strings");
97 }
98 } catch (...) {
99 std::throw_with_nested(std::runtime_error("failed to validate 'seeds'"));
100 }
101
102 return ArrayDetails(type, std::move(dimensions));
103}
104
105}
106
107#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_combine(const H5::Group &group, const ritsuko::Version &version, const Options &options)
Definition combine.hpp:38
ArrayType
Definition utils_public.hpp:29
constexpr bool is_equal(Left_ left, Right_ right)
constexpr bool is_greater_than_or_equal(Left_ left, Right_ right)
constexpr Dest_ sum(First_ first, Args_... more)
Details about an array.
Definition utils_public.hpp:38
Options for validate().
Definition utils_public.hpp:79
Various public utilities.