9 #ifndef H5EASY_BITS_EIGEN_HPP
10 #define H5EASY_BITS_EIGEN_HPP
12 #include "../H5Easy.hpp"
25 typename std::enable_if<std::is_base_of<Eigen::DenseBase<T>, T>::value>::type> {
31 using row_major = Eigen::Ref<
33 typename std::decay<T>::type::Scalar,
34 std::decay<T>::type::RowsAtCompileTime,
35 std::decay<T>::type::ColsAtCompileTime,
36 std::decay<T>::type::ColsAtCompileTime == 1 ? Eigen::ColMajor : Eigen::RowMajor,
37 std::decay<T>::type::MaxRowsAtCompileTime,
38 std::decay<T>::type::MaxColsAtCompileTime>,
40 Eigen::InnerStride<1>>;
42 using col_major = Eigen::Map<
44 typename std::decay<T>::type::Scalar,
45 std::decay<T>::type::RowsAtCompileTime,
46 std::decay<T>::type::ColsAtCompileTime,
47 std::decay<T>::type::ColsAtCompileTime == 1 ? Eigen::ColMajor : Eigen::RowMajor,
48 std::decay<T>::type::MaxRowsAtCompileTime,
49 std::decay<T>::type::MaxColsAtCompileTime>>;
53 inline static std::vector<size_t> shape(
const T& data) {
54 if (std::decay<T>::type::RowsAtCompileTime == 1) {
55 return {
static_cast<size_t>(data.cols())};
57 if (std::decay<T>::type::ColsAtCompileTime == 1) {
58 return {
static_cast<size_t>(data.rows())};
60 return {
static_cast<size_t>(data.rows()),
static_cast<size_t>(data.cols())};
63 using EigenIndex = Eigen::DenseIndex;
67 inline static std::vector<EigenIndex> shape(
const File& file,
70 int RowsAtCompileTime) {
71 std::vector<size_t> dims = dataset.getDimensions();
73 if (dims.size() == 1 && RowsAtCompileTime == 1) {
74 return std::vector<EigenIndex>{1u,
static_cast<EigenIndex
>(dims[0])};
76 if (dims.size() == 1) {
77 return std::vector<EigenIndex>{
static_cast<EigenIndex
>(dims[0]), 1u};
79 if (dims.size() == 2) {
80 return std::vector<EigenIndex>{
static_cast<EigenIndex
>(dims[0]),
81 static_cast<EigenIndex
>(dims[1])};
84 throw detail::error(file, path,
"H5Easy::load: Inconsistent rank");
87 inline static DataSet
dump(File& file,
90 const DumpOptions& options) {
91 using row_major_type =
typename types<T>::row_major;
92 using value_type =
typename std::decay<T>::type::Scalar;
93 row_major_type row_major(data);
94 DataSet dataset = initDataset<value_type>(file, path, shape(data), options);
95 dataset.write_raw(row_major.data());
96 if (options.flush()) {
103 DataSet dataset = file.getDataSet(path);
104 std::vector<typename T::Index> dims = shape(file, path, dataset, T::RowsAtCompileTime);
105 T data(dims[0], dims[1]);
106 dataset.read(data.data());
107 if (data.IsVectorAtCompileTime || data.IsRowMajor) {
110 using col_major =
typename types<T>::col_major;
111 return col_major(data.data(), dims[0], dims[1]);
118 const DumpOptions& options) {
119 using row_major_type =
typename types<T>::row_major;
120 using value_type =
typename std::decay<T>::type::Scalar;
121 row_major_type row_major(data);
122 Attribute attribute = initAttribute<value_type>(file, path, key, shape(data), options);
123 attribute.write_raw(row_major.data());
124 if (options.flush()) {
133 DataSet dataset = file.getDataSet(path);
134 Attribute attribute = dataset.getAttribute(key);
135 DataSpace dataspace = attribute.getSpace();
136 std::vector<typename T::Index> dims = shape(file, path, dataspace, T::RowsAtCompileTime);
137 T data(dims[0], dims[1]);
138 attribute.read(data.data());
139 if (data.IsVectorAtCompileTime || data.IsRowMajor) {
142 using col_major =
typename types<T>::col_major;
143 return col_major(data.data(), dims[0], dims[1]);
150 #endif // H5_USE_EIGEN
151 #endif // H5EASY_BITS_EIGEN_HPP