#include <h5cpp/all>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <vector>
namespace {
void section(const char* title) {
}
std::cout <<
" " << label <<
" (" << rows <<
"x" << cols <<
")\n";
}
}
}
int main() {
auto fd =
h5::create(
"datasets.h5", H5F_ACC_TRUNC);
section("1. one-shot create + write");
{
std::cout <<
" wrote " << v.
size() <<
" uniform doubles to /one_shot/vec\n";
}
section("2. explicit create then write");
{
h5::current_dims{4, 5},
h5::chunk{2, 5} | h5::gzip{6});
ds["units"] = "meters";
ds["captured"] = "2026-05-27";
std::cout <<
" wrote 4x5 to /explicit/mat with chunk{2,5} | gzip{6}\n";
}
section("3. reading back — three reader shapes");
{
h5::offset{0, 0}, h5::count{4, 1});
std::cout <<
" vector<double>: size = " << v.
size() <<
" first = " << v[0] <<
"\n";
std::cout <<
" raw double[20]: first = " << buf[0] <<
" last = " << buf[19] <<
"\n";
std::cout <<
" column 0 (4x1): " << col0[0] <<
" " << col0[1] <<
" " << col0[2] <<
" " << col0[3] <<
"\n";
}
section("4. chunking + filter chain");
{
for (
size_t i = 0; i < v.
size(); ++i) v[i] =
std::sin(i * 0.01);
h5::current_dims{100, 100},
h5::chunk{20, 20} | h5::shuffle | h5::gzip{6} | h5::fletcher32);
hsize_t storage = H5Dget_storage_size(static_cast<hid_t>(ds));
std::cout <<
" raw size: " << v.
size() *
sizeof(double) <<
" bytes\n";
std::cout <<
" on-disk size: " << storage <<
" bytes\n";
<< double(v.
size() *
sizeof(
double)) / double(storage) <<
"x\n";
}
section("5. fill values");
{
show("/fill/preset (read back before write)", buf, 3, 4);
}
section("6. unlimited dimensions + append (packet table)");
{
{
h5::max_dims{H5S_UNLIMITED},
h5::chunk{20} | h5::gzip{4});
for (int i = 0; i < 100; ++i) h5::append(pt, *dist);
}
std::cout <<
" appended " << out.size() <<
" random ints" <<
" (last = " << out.back() <<
")\n";
}
section("7. hyperslab selection — offset / count / stride / block");
{
h5::current_dims{6, 8},
h5::chunk{3, 4} | h5::fill_value<double>{0.0});
h5::write(ds, patch.data(), h5::offset{1, 1}, h5::count{2, 3});
show("/hyperslab/grid", buf, 6, 8);
}
section("8. partial read");
{
h5::offset{0, 0}, h5::count{3, 4});
show("/hyperslab/grid [0:3, 0:4]", sub, 3, 4);
}
section("9. reusable property lists");
{
h5::lcpl_t deep_path = h5::create_path | h5::utf8;
for (int i = 0; i < 3; ++i) {
h5::current_dims{128, 128}, deep_path, fast_chunked);
}
std::cout <<
" created 3 datasets sharing the same dcpl + lcpl\n";
}
#if defined(H5CPP_HAS_MDSPAN)
section("10. std::mdspan (C++23) — non-owning view");
{
std::iota(storage.begin(), storage.end(), 100.0);
std::mdspan<double, std::dextents<std::size_t, 2>>
view(storage.data(), rows, cols);
std::mdspan<double, std::dextents<std::size_t, 2>>
back(back_buf.data(), rows, cols);
h5::count{rows, cols});
show("/mdspan/view (read back into 3x4 mdspan)", back_buf, rows, cols);
}
#else
section("10. std::mdspan (C++23)");
std::cout <<
" skipped: this TU was not built with __cpp_lib_mdspan\n";
std::cout <<
" (libstdc++ 14+, libc++ 17+, compiled at C++23)\n";
#endif
std::cout <<
"\nWrote everything to ./datasets.h5\n";
std::cout <<
"Inspect with: h5dump -pH datasets.h5\n";
return 0;
}
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
T setprecision(T... args)