h5gt 0.2.0
C++ wrapper for HDF5 library (based on HighFive project)
Loading...
Searching...
No Matches
H5Easy_xtensor.hpp
1/*
2 * Copyright (c), 2017, Adrien Devresse <adrien.devresse@epfl.ch>
3 *
4 * Distributed under the Boost Software License, Version 1.0.
5 * (See accompanying file LICENSE_1_0.txt or copy at
6 * http://www.boost.org/LICENSE_1_0.txt)
7 *
8 */
9#ifndef H5EASY_BITS_XTENSOR_HPP
10#define H5EASY_BITS_XTENSOR_HPP
11
12#include "../H5Easy.hpp"
13#include "H5Easy_misc.hpp"
14#include "H5Easy_scalar.hpp"
15
16#ifdef H5GT_USE_XTENSOR
17
18namespace H5Easy {
19
20namespace detail {
21
22template <class T>
23struct is_xtensor : std::false_type {};
24template <class T>
25struct is_xtensor<xt::xarray<T>> : std::true_type {};
26template <class T, size_t N>
27struct is_xtensor<xt::xtensor<T, N>> : std::true_type {};
28
29template <typename T>
30struct io_impl<T, typename std::enable_if<is_xtensor<T>::value>::type> {
31
32 inline static std::vector<size_t> shape(const T& data) {
33 return std::vector<size_t>(data.shape().cbegin(), data.shape().cend());
34 }
35
36 inline static DataSet dump(File& file,
37 const std::string& path,
38 const T& data,
39 const DumpOptions& options) {
40 using value_type = typename std::decay_t<T>::value_type;
41 DataSet dataset = initDataset<value_type>(file, path, shape(data), options);
42 dataset.write_raw(data.data());
43 if (options.flush()) {
44 file.flush();
45 }
46 return dataset;
47 }
48
49 inline static T load(const File& file, const std::string& path) {
50 DataSet dataset = file.getDataSet(path);
51 std::vector<size_t> dims = dataset.getDimensions();
52 T data = T::from_shape(dims);
53 dataset.read(data.data());
54 return data;
55 }
56
57 inline static Attribute dumpAttribute(File& file,
58 const std::string& path,
59 const std::string& key,
60 const T& data,
61 const DumpOptions& options) {
62 using value_type = typename std::decay_t<T>::value_type;
63 Attribute attribute = initAttribute<value_type>(file, path, key, shape(data), options);
64 attribute.write_raw(data.data());
65 if (options.flush()) {
66 file.flush();
67 }
68 return attribute;
69 }
70
71 inline static T loadAttribute(const File& file,
72 const std::string& path,
73 const std::string& key) {
74 DataSet dataset = file.getDataSet(path);
75 Attribute attribute = dataset.getAttribute(key);
76 DataSpace dataspace = attribute.getSpace();
77 std::vector<size_t> dims = dataspace.getDimensions();
78 T data = T::from_shape(dims);
79 attribute.read(data.data());
80 return data;
81 }
82};
83
84} // namespace detail
85} // namespace H5Easy
86
87#endif // H5GT_USE_XTENSOR
88#endif // H5EASY_BITS_XTENSOR_HPP