#include <unsupported/Eigen/FFT>
#include <h5cpp/all>
#include <cstddef>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

int main() {
  {
    int nrow = 3;
    int ncol = 2;

    Eigen::ArrayXXf d1 = Eigen::ArrayXXf::Random(nrow, ncol);
    Eigen::MatrixXf M = Eigen::MatrixXf::Random(nrow, ncol);
    std::cout << M << std::endl;

    h5::fd_t fd = h5::create("h001.h5", H5F_ACC_TRUNC);
    h5::write(fd, "/M", M);
  }

  {
    h5::fd_t fd = h5::open("h001.h5", H5F_ACC_RDWR);
    auto M = h5::read<Eigen::MatrixXf>(fd, "/M");
    std::cout << M << std::endl;
  }

  // RAII will close resource, noo need H5Fclose( any_longer );
  h5::fd_t fd = h5::create("h002.h5", H5F_ACC_TRUNC);
  {
    std::vector<double> v(10);
    std::fill(std::begin(v), std::end(v), 1e0);
    std::cout << "v.size() : " << v.size() << std::endl;
    h5::write(fd, "stl/vector/full.dat", v); // simplest example

    // An elaborate example to demonstrate how to use H5CPP when you know the
    // details, but no time/budget to code it. The performance must be on par
    // with the best C implementation -- if not: shoot an email and I fix it
    h5::create<double>(fd, "stl/vector/partial.dat",
                       // arguments can be written any order without loss of
                       // performance thanks to compile time parsing
                       h5::current_dims{20, 10, 5},
                       h5::max_dims{H5S_UNLIMITED, 10, 5},
                       h5::chunk{1, 10, 5} | h5::gzip{9});

    // you have some memory region you liked to read/write from, and H5CPP
    // doesn't know of your object + no time to fiddle around you want it done:
    // SOLUTION: write/read from/to memory region, NOTE the type cast:
    // h5::write<DOUBLE>( ... );
    h5::write<double>(fd, "stl/vector/partial.dat", v.data(),
                      h5::count{3, 1, 1}, h5::offset{2, 1, 1});
  }

  std::cout << "Success" << std::endl;
  return 0;
}
