Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-04-19 08:55:35

0001 /*
0002  *  Copyright (c), 2017, Adrien Devresse <adrien.devresse@epfl.ch>
0003  *
0004  *  Distributed under the Boost Software License, Version 1.0.
0005  *    (See accompanying file LICENSE_1_0.txt or copy at
0006  *          http://www.boost.org/LICENSE_1_0.txt)
0007  *
0008  */
0009 #pragma once
0010 
0011 #include "../H5Easy.hpp"
0012 #include "H5Easy_misc.hpp"
0013 #include "H5Easy_scalar.hpp"
0014 
0015 #ifdef H5_USE_XTENSOR
0016 
0017 namespace H5Easy {
0018 
0019 namespace detail {
0020 
0021 template <typename T>
0022 struct io_impl<T, typename std::enable_if<xt::is_xexpression<T>::value>::type> {
0023     inline static std::vector<size_t> shape(const T& data) {
0024         return std::vector<size_t>(data.shape().cbegin(), data.shape().cend());
0025     }
0026 
0027     inline static DataSet dump(File& file,
0028                                const std::string& path,
0029                                const T& data,
0030                                const DumpOptions& options) {
0031         using value_type = typename std::decay_t<T>::value_type;
0032         DataSet dataset = initDataset<value_type>(file, path, shape(data), options);
0033         dataset.write_raw(data.data());
0034         if (options.flush()) {
0035             file.flush();
0036         }
0037         return dataset;
0038     }
0039 
0040     inline static T load(const File& file, const std::string& path) {
0041         static_assert(
0042             xt::has_data_interface<T>::value,
0043             "Cannot load to xt::xfunction or xt::xgenerator, use e.g. xt::xtensor or xt::xarray");
0044         DataSet dataset = file.getDataSet(path);
0045         std::vector<size_t> dims = dataset.getDimensions();
0046         T data = T::from_shape(dims);
0047         dataset.read_raw(data.data());
0048         return data;
0049     }
0050 
0051     inline static Attribute dumpAttribute(File& file,
0052                                           const std::string& path,
0053                                           const std::string& key,
0054                                           const T& data,
0055                                           const DumpOptions& options) {
0056         using value_type = typename std::decay_t<T>::value_type;
0057         Attribute attribute = initAttribute<value_type>(file, path, key, shape(data), options);
0058         attribute.write_raw(data.data());
0059         if (options.flush()) {
0060             file.flush();
0061         }
0062         return attribute;
0063     }
0064 
0065     inline static T loadAttribute(const File& file,
0066                                   const std::string& path,
0067                                   const std::string& key) {
0068         static_assert(
0069             xt::has_data_interface<T>::value,
0070             "Cannot load to xt::xfunction or xt::xgenerator, use e.g. xt::xtensor or xt::xarray");
0071         DataSet dataset = file.getDataSet(path);
0072         Attribute attribute = dataset.getAttribute(key);
0073         DataSpace dataspace = attribute.getSpace();
0074         std::vector<size_t> dims = dataspace.getDimensions();
0075         T data = T::from_shape(dims);
0076         attribute.read_raw(data.data());
0077         return data;
0078     }
0079 };
0080 
0081 }  // namespace detail
0082 }  // namespace H5Easy
0083 
0084 #endif  // H5_USE_XTENSOR