chihaya
Validating delayed array operations in HDF5
Loading...
Searching...
No Matches
transpose.hpp
Go to the documentation of this file.
1#ifndef CHIHAYA_TRANSPOSE_HPP
2#define CHIHAYA_TRANSPOSE_HPP
3
4#include "H5Cpp.h"
5#include "ritsuko/ritsuko.hpp"
6
7#include <stdexcept>
8#include <exception>
9#include <algorithm>
10#include <vector>
11#include <cstdint>
12#include <cstddef>
13
14#include "utils_public.hpp"
15#include "utils_misc.hpp"
16#include "utils_type.hpp"
17
23namespace chihaya {
24
28template<typename Perm_>
29std::vector<std::size_t> check_permutation(const H5::DataSet& phandle, hsize_t plen, const std::vector<std::size_t>& input_dimensions, bool details_only) {
30 if (!sanisizer::is_equal(plen, input_dimensions.size())) {
31 throw std::runtime_error("length should match dimensionality of 'seed'");
32 }
33
34 auto permutation = sanisizer::create<std::vector<Perm_> >(plen);
35 phandle.read(permutation.data(), ritsuko::hdf5::as_numeric_datatype<Perm_>());
36
37 auto new_dimensions = sanisizer::create<std::vector<std::size_t> >(plen);
38 for (I<decltype(plen)> p = 0; p < plen; ++p) {
39 const auto current = permutation[p];
40 if (current < 0) {
41 throw std::runtime_error("expected non-negative indices");
42 }
43 if (sanisizer::is_greater_than_or_equal(current, plen)) {
44 throw std::runtime_error("detected out-of-bounds indices");
45 }
46 new_dimensions[p] = input_dimensions[permutation[p]];
47 }
48
49 if (!details_only) {
50 std::sort(permutation.begin(), permutation.end());
51 for (I<decltype(plen)> p = 0; p < plen; ++p) {
52 if (!sanisizer::is_equal(p, permutation[p])) {
53 throw std::runtime_error("indices should be unique");
54 }
55 }
56 }
57
58 return new_dimensions;
59}
76inline ArrayDetails validate_transpose(const H5::Group& group, const ritsuko::Version& version, const Options& options) {
77 ArrayDetails seed_details;
78 try {
79 auto shandle = group.openGroup("seed");
80 seed_details = validate(shandle, version, options);
81 } catch (...) {
82 std::throw_with_nested(std::runtime_error("failed to validate 'seed'"));
83 }
84
85 try {
86 auto phandle = group.openDataSet("permutation");
87 auto pspace = phandle.getSpace();
88 if (pspace.getSimpleExtentNdims() != 1) {
89 throw std::runtime_error("expected a 1-dimensional dataset");
90 }
91 hsize_t plen;
92 pspace.getSimpleExtentDims(&plen);
93
94 if (version.lt(1, 1, 0)) {
95 // Older versions didn't actually specify the integer type, so we just check we can load it into an 'int' of any type.
96 if (!ritsuko::hdf5::exceeds_integer_limit(phandle, 64, true)) {
97 seed_details.dimensions = check_permutation<std::int64_t>(phandle, plen, seed_details.dimensions, options.details_only);
98 } else if (!ritsuko::hdf5::exceeds_integer_limit(phandle, 64, false)) {
99 seed_details.dimensions = check_permutation<std::uint64_t>(phandle, plen, seed_details.dimensions, options.details_only);
100 } else {
101 throw create_integer_error_0_99(phandle.getTypeClass());
102 }
103 } else {
104 if (ritsuko::hdf5::exceeds_integer_limit(phandle, 64, false)) {
105 throw std::runtime_error("datatype should fit in a 64-bit unsigned integer");
106 }
107 seed_details.dimensions = check_permutation<std::uint64_t>(phandle, plen, seed_details.dimensions, options.details_only);
108 }
109 } catch (...) {
110 std::throw_with_nested(std::runtime_error("failed to validate 'permutation'"));
111 }
112
113 return seed_details;
114}
115
116}
117
118#endif
Namespace for all chihaya functions.
Definition binary_arithmetic.hpp:21
ArrayDetails validate_transpose(const H5::Group &group, const ritsuko::Version &version, const Options &options)
Definition transpose.hpp:76
ArrayDetails validate(const H5::Group &group, const ritsuko::Version &version, const Options &options)
Definition validate.hpp:96
Container_ create(Value_ x, Args_ &&... args)
constexpr bool is_equal(Left_ left, Right_ right)
constexpr bool is_greater_than_or_equal(Left_ left, Right_ right)
Details about an array.
Definition utils_public.hpp:38
std::vector< std::size_t > dimensions
Definition utils_public.hpp:58
Options for validate().
Definition utils_public.hpp:79
bool details_only
Definition utils_public.hpp:84
Various public utilities.