1#ifndef TATAMI_HDF5_UTILS_HPP
2#define TATAMI_HDF5_UTILS_HPP
29enum class WriteStorageType { AUTOMATIC, INT8, UINT8, INT16, UINT16, INT32, UINT32, DOUBLE };
35const H5::PredType& define_mem_type() {
36 if constexpr(std::is_same<int, T>::value) {
37 return H5::PredType::NATIVE_INT;
38 }
else if (std::is_same<unsigned int, T>::value) {
39 return H5::PredType::NATIVE_UINT;
40 }
else if (std::is_same<long, T>::value) {
41 return H5::PredType::NATIVE_LONG;
42 }
else if (std::is_same<unsigned long, T>::value) {
43 return H5::PredType::NATIVE_ULONG;
44 }
else if (std::is_same<long long, T>::value) {
45 return H5::PredType::NATIVE_LLONG;
46 }
else if (std::is_same<unsigned long long, T>::value) {
47 return H5::PredType::NATIVE_ULLONG;
48 }
else if (std::is_same<short, T>::value) {
49 return H5::PredType::NATIVE_SHORT;
50 }
else if (std::is_same<unsigned short, T>::value) {
51 return H5::PredType::NATIVE_USHORT;
52 }
else if (std::is_same<char, T>::value) {
53 return H5::PredType::NATIVE_CHAR;
54 }
else if (std::is_same<unsigned char, T>::value) {
55 return H5::PredType::NATIVE_UCHAR;
56 }
else if (std::is_same<double, T>::value) {
57 return H5::PredType::NATIVE_DOUBLE;
58 }
else if (std::is_same<float, T>::value) {
59 return H5::PredType::NATIVE_FLOAT;
60 }
else if (std::is_same<long double, T>::value) {
61 return H5::PredType::NATIVE_LDOUBLE;
62 }
else if (std::is_same<uint8_t, T>::value) {
63 return H5::PredType::NATIVE_UINT8;
64 }
else if (std::is_same<int8_t, T>::value) {
65 return H5::PredType::NATIVE_INT8;
66 }
else if (std::is_same<uint16_t, T>::value) {
67 return H5::PredType::NATIVE_UINT16;
68 }
else if (std::is_same<int16_t, T>::value) {
69 return H5::PredType::NATIVE_INT16;
70 }
else if (std::is_same<uint32_t, T>::value) {
71 return H5::PredType::NATIVE_UINT32;
72 }
else if (std::is_same<int32_t, T>::value) {
73 return H5::PredType::NATIVE_INT32;
74 }
else if (std::is_same<uint64_t, T>::value) {
75 return H5::PredType::NATIVE_UINT64;
76 }
else if (std::is_same<int64_t, T>::value) {
77 return H5::PredType::NATIVE_INT64;
79 static_assert(
"unsupported HDF5 type for template parameter 'T'");
82template<
bool integer_only,
class GroupLike>
83H5::DataSet open_and_check_dataset(
const GroupLike& handle,
const std::string& name) {
85 if (!H5Lexists(handle.getId(), name.c_str(), H5P_DEFAULT) || handle.childObjType(name) != H5O_TYPE_DATASET) {
86 throw std::runtime_error(
"no child dataset named '" + name +
"'");
89 auto dhandle = handle.openDataSet(name);
90 auto type = dhandle.getTypeClass();
91 if constexpr(integer_only) {
92 if (type != H5T_INTEGER) {
93 throw std::runtime_error(std::string(
"expected integer values in the '") + name +
"' dataset");
96 if (type != H5T_INTEGER && type != H5T_FLOAT) {
97 throw std::runtime_error(std::string(
"expected numeric values in the '") + name +
"' dataset");
105std::array<hsize_t, N> get_array_dimensions(
const H5::DataSet& handle,
const std::string& name) {
106 auto space = handle.getSpace();
108 int ndim = space.getSimpleExtentNdims();
110 throw std::runtime_error(std::string(
"'") + name +
"' should be a " + std::to_string(N) +
"-dimensional array");
113 std::array<hsize_t, N> dims_out;
114 space.getSimpleExtentDims(dims_out.data(), NULL);
Representations for matrix data in HDF5 files.
Definition CompressedSparseMatrix.hpp:23
WriteStorageLayout
Definition utils.hpp:24
WriteStorageType
Definition utils.hpp:29