1#ifndef CHIHAYA_TRANSPOSE_HPP
2#define CHIHAYA_TRANSPOSE_HPP
5#include "ritsuko/ritsuko.hpp"
6#include "ritsuko/hdf5/hdf5.hpp"
13#include "utils_misc.hpp"
33template<
typename Perm_>
34std::vector<size_t> check_permutation(
const H5::DataSet& phandle,
size_t ndims,
const H5::PredType& h5type,
const std::vector<size_t>& input_dimensions,
bool details_only) {
35 if (ndims != input_dimensions.size()) {
36 throw std::runtime_error(
"length of 'permutation' should match dimensionality of 'seed'");
39 std::vector<Perm_> permutation(ndims);
40 phandle.read(permutation.data(), h5type);
42 std::vector<size_t> new_dimensions(ndims);
43 for (
size_t p = 0; p < ndims; ++p) {
44 auto current = permutation[p];
46 throw std::runtime_error(
"'permutation' should contain non-negative indices");
48 if (
static_cast<size_t>(current) >= ndims) {
49 throw std::runtime_error(
"'permutation' contains out-of-bounds indices");
51 new_dimensions[p] = input_dimensions[permutation[p]];
55 std::sort(permutation.begin(), permutation.end());
56 for (
size_t p = 0; p < permutation.size(); ++p) {
57 if (p !=
static_cast<size_t>(permutation[p])) {
58 throw std::runtime_error(
"indices in 'permutation' should be unique for a transpose operation");
63 return new_dimensions;
80 auto seed_details = internal_misc::load_seed_details(handle,
"seed", version, options);
82 auto phandle = ritsuko::hdf5::open_dataset(handle,
"permutation");
83 auto ndims = ritsuko::hdf5::get_1d_length(phandle,
false);
85 if (version.lt(1, 1, 0)) {
86 if (phandle.getTypeClass() != H5T_INTEGER) {
87 throw std::runtime_error(
"'permutation' should be integer");
89 seed_details.dimensions = internal::check_permutation<int>(phandle, ndims, H5::PredType::NATIVE_INT, seed_details.dimensions, options.
details_only);
91 if (ritsuko::hdf5::exceeds_integer_limit(phandle, 64,
false)) {
92 throw std::runtime_error(
"'permutation' should have a datatype that can be represented by a 64-bit unsigned integer");
94 seed_details.dimensions = internal::check_permutation<uint64_t>(phandle, ndims, H5::PredType::NATIVE_UINT64, seed_details.dimensions, options.
details_only);
ArrayDetails validate(const H5::Group &handle, const ritsuko::Version &version, Options &options)
Definition: transpose.hpp:79
Namespace for all chihaya functions.
Definition: binary_arithmetic.hpp:22
Details about an array.
Definition: utils_public.hpp:36
Validation options.
Definition: utils_public.hpp:66
bool details_only
Definition: utils_public.hpp:71