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"
6#include "ritsuko/hdf5/hdf5.hpp"
7
8#include <stdexcept>
9#include <vector>
10#include <string>
11#include <cstdint>
12
13#include "utils_list.hpp"
14#include "utils_misc.hpp"
15
21namespace chihaya {
22
26inline ArrayDetails validate(const H5::Group&, const ritsuko::Version&, Options&);
35namespace combine {
36
45inline ArrayDetails validate(const H5::Group& handle, const ritsuko::Version& version, Options& options) {
46 uint64_t along = internal_misc::load_along(handle, version);
47
48 auto shandle = ritsuko::hdf5::open_group(handle, "seeds");
49 internal_list::ListDetails list_params;
50 try {
51 list_params = internal_list::validate(shandle, version);
52 } catch (std::exception& e) {
53 throw std::runtime_error(std::string("failed to load 'seeds' list; ") + e.what());
54 }
55 if (list_params.present.size() != list_params.length) {
56 throw std::runtime_error("missing elements in the 'seeds' list");
57 }
58
59 std::vector<size_t> dimensions;
60 ArrayType type = BOOLEAN;
61 {
62 bool first = true;
63 size_t num_strings = 0;
64
65 for (const auto& p : list_params.present) {
66 auto current = ritsuko::hdf5::open_group(shandle, p.second.c_str());
67
68 ArrayDetails cur_seed;
69 try {
70 cur_seed = ::chihaya::validate(current, version, options);
71 } catch (std::exception& e) {
72 throw std::runtime_error("failed to validate 'seeds/" + p.second + "'; " + std::string(e.what()));
73 }
74
75 if (first) {
76 type = cur_seed.type;
77 dimensions = cur_seed.dimensions;
78 if (static_cast<size_t>(along) >= dimensions.size()) {
79 throw std::runtime_error("'along' should be less than the seed dimensionality");
80 }
81 first = false;
82 } else {
83 if (type < cur_seed.type) {
84 type = cur_seed.type;
85 }
86 if (dimensions.size() != cur_seed.dimensions.size()) {
87 throw std::runtime_error("dimensionality mismatch between seeds");
88 }
89 for (size_t d = 0; d < dimensions.size(); ++d) {
90 if (d == static_cast<size_t>(along)) {
91 dimensions[d] += cur_seed.dimensions[d];
92 } else if (dimensions[d] != cur_seed.dimensions[d]) {
93 throw std::runtime_error("inconsistent dimension extents between seeds");
94 }
95 }
96 }
97
98 num_strings += (cur_seed.type == STRING);
99 }
100
101 if (num_strings != 0 && num_strings != list_params.length) {
102 throw std::runtime_error("either none or all of the arrays to be combined should contain strings");
103 }
104 }
105
106 return ArrayDetails(type, std::move(dimensions));
107}
108
109}
110
111}
112
113#endif
ArrayDetails validate(const H5::Group &handle, const ritsuko::Version &version, Options &options)
Definition: combine.hpp:45
Namespace for all chihaya functions.
Definition: binary_arithmetic.hpp:22
ArrayDetails validate(const H5::Group &handle, const ritsuko::Version &version, Options &options)
Definition: validate.hpp:92
ArrayType
Definition: utils_public.hpp:27
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