22#ifndef LIBH5_STL_ARRAY_HPP
23#define LIBH5_STL_ARRAY_HPP
51 template <
typename T,
size_t N>
52 void h5_write(
group g, std::string
const &name, std::array<T, N>
const &a) {
53 if constexpr (std::is_same_v<T, std::string>) {
55 auto char_arr = std::array<const char *, N>{};
56 std::transform(cbegin(a), cend(a), begin(char_arr), [](std::string
const &s) {
return s.c_str(); });
58 }
else if constexpr (std::is_arithmetic_v<T> or
is_complex_v<T> or std::is_same_v<T, dcplx_t> or std::is_same_v<T, char *>
59 or std::is_same_v<T, const char *>) {
64 v.parent_shape[0] = N;
69 h5_write(g2,
"shape", std::array<long, 1>{N});
70 for (
int i = 0; i < N; ++i)
h5_write(g2, std::to_string(i), a[i]);
83 template <
typename T,
size_t N>
85 if constexpr (std::is_same_v<T, std::string>) {
87 auto char_arr = std::array<char *, N>{};
89 std::copy(cbegin(char_arr), cend(char_arr), begin(a));
90 std::for_each(begin(char_arr), end(char_arr), [](
char *cb) { free(cb); });
91 }
else if constexpr (std::is_arithmetic_v<T> or
is_complex_v<T> or std::is_same_v<T, dcplx_t> or std::is_same_v<T, char *>
92 or std::is_same_v<T, const char *>) {
95 H5_EXPECTS(ds_info.rank() == 1 + ds_info.has_complex_attribute);
96 H5_EXPECTS(N == ds_info.lengths[0]);
101 h5_read(g, name,
reinterpret_cast<std::array<dcplx_t, N> &
>(a));
106 if (!ds_info.has_complex_attribute) {
109 std::array<double, N> tmp{};
111 std::copy(begin(tmp), end(tmp), begin(a));
119 v.slab.stride[0] = 1;
120 v.parent_shape[0] = N;
127 auto h5_shape = std::array<long, 1>{};
128 h5_read(g2,
"shape", h5_shape);
129 H5_EXPECTS(N == h5_shape[0]);
132 for (
int i = 0; i < N; ++i)
h5_read(g2, std::to_string(i), a[i]);
Provides a generic interface to read/write n-dimensional arrays from/to HDF5.
A handle to an HDF5 group.
group create_group(std::string const &key, bool delete_if_exists=true) const
Create a subgroup with the given key in the group.
group open_group(std::string const &key) const
Open a subgroup with the given key in the group.
Provides a compound type and type traits for complex numbers.
bool hdf5_type_equal(datatype dt1, datatype dt2)
Check if two HDF5 datatypes are equal.
constexpr bool is_complex_v
Boolean type trait set to true for std::complex types.
datatype hdf5_type()
Map a given C++ type to an HDF5 datatype.
std::string get_name_of_h5_type(datatype dt)
Get the name of an h5::datatype (for error messages).
void read(group g, std::string const &name, array_view v, hyperslab sl)
Read a given hyperslab from an HDF5 dataset into an array view.
dataset_info get_dataset_info(dataset ds)
Retrieve the shape and the h5::datatype from a dataset.
void write(group g, std::string const &name, array_view const &v, bool compress)
Write an array view to an HDF5 dataset.
T h5_read(group g, std::string const &key)
Generic implementation for reading from an HDF5 dataset/subgroup.
void h5_write(group g, std::string const &name, T const &x) H5_REQUIRES(std
Write a scalar to an HDF5 dataset.
Macros used in the h5 library.
Struct representing a view on an n-dimensional array/dataspace.
hyperslab slab
h5::array_interface::hyperslab specifying the selection of the view.
v_t count
Number of elements or blocks to select along each dimension.