#include <iostream>
#include <vector>
#if __has_include(<mdspan>)
# include <mdspan>
#endif
#include <h5cpp/all>
#if defined(__cpp_lib_mdspan) && __cpp_lib_mdspan >= 202207L
namespace {
int errors = 0;
template <class A, class B>
void check(const char* tag, const A& expected, const B& got) {
const bool ok = (expected == got);
if (!ok) ++errors;
std::cout << (ok ?
"✔ ok " :
"✘ failed ") << tag <<
"\n";
}
}
int main() {
{
for (
std::size_t i = 0; i < buf.size(); ++i) buf[i] =
static_cast<double>(i);
std::mdspan<double, std::dextents<std::size_t, 2>> view(buf.data(), 4, 6);
check(
"rank-2 mdspan write — element count",
std::size_t(24), back.size());
check("rank-2 mdspan write — element (0,0) = 0", 0.0, back[0]);
check("rank-2 mdspan write — element (3,5) = 23", 23.0, back[23]);
}
{
std::mdspan<double, std::dextents<std::size_t, 2>> view(sink.data(), 4, 6);
check("buffer-out mdspan read — element (0,0)", 0.0, sink[0]);
check("buffer-out mdspan read — element (3,5)", 23.0, sink[23]);
check("buffer-out mdspan read — view(2,3) = 15", 15.0, view[2, 3]);
}
{
for (
std::size_t i = 0; i < cube.size(); ++i) cube[i] =
static_cast<float>(i);
std::mdspan<float, std::dextents<std::size_t, 3>> view(cube.data(), 2, 3, 4);
check(
"rank-3 mdspan write — element count",
std::size_t(24), back.size());
check("rank-3 mdspan write — first/last match", 23.0f, back[23]);
}
<< (errors == 0 ? "✔ all checks passed"
: "✘ some checks failed")
<< ", errors=" << errors << "\n";
return errors;
}
#else
int main() {
std::cout <<
"◇ skipped — std::mdspan not available on this stdlib.\n"
<< " Requires libstdc++ >= 15 or libc++ >= 17 (P0009 / __cpp_lib_mdspan >= 202207L).\n"
<< " h5cpp's H5Mmdspan.hpp guards on the same feature macro and is inert here.\n";
return 0;
}
#endif
h5::at_t create(const hid_t &parent, const std::string &path, args_t &&... args)
Create a new attribute of element type T on a parent HDF5 object.
Definition H5Acreate.hpp:100
T aread(const hid_t &ds, const std::string &name, const h5::acpl_t &acpl=h5::default_acpl)
Read an attribute by name and return its value as type T.
Definition H5Aread.hpp:76
h5::gr_t write(const LOC &parent, const std::string &path, const T &src)
Write a sparse matrix or vector as a CSC group.
Definition H5Dsparse.hpp:185