Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-25 09:16:38

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 "default_io_impl.hpp"
0014 
0015 namespace H5Easy {
0016 
0017 namespace detail {
0018 
0019 /*
0020 Base template for partial specialization: the fallback if specialized templates don't match.
0021 Used e.g. for scalars.
0022 */
0023 template <typename T, typename = void>
0024 struct io_impl: public default_io_impl<T> {
0025     inline static DataSet dump_extend(File& file,
0026                                       const std::string& path,
0027                                       const T& data,
0028                                       const std::vector<size_t>& idx,
0029                                       const DumpOptions& options) {
0030         std::vector<size_t> ones(idx.size(), 1);
0031 
0032         if (file.exist(path)) {
0033             DataSet dataset = file.getDataSet(path);
0034             std::vector<size_t> dims = dataset.getDimensions();
0035             std::vector<size_t> shape = dims;
0036             if (dims.size() != idx.size()) {
0037                 throw detail::error(
0038                     file,
0039                     path,
0040                     "H5Easy::dump: Dimension of the index and the existing field do not match");
0041             }
0042             for (size_t i = 0; i < dims.size(); ++i) {
0043                 shape[i] = std::max(dims[i], idx[i] + 1);
0044             }
0045             if (shape != dims) {
0046                 dataset.resize(shape);
0047             }
0048             dataset.select(idx, ones).write(data);
0049             if (options.flush()) {
0050                 file.flush();
0051             }
0052             return dataset;
0053         }
0054 
0055         const size_t unlim = DataSpace::UNLIMITED;
0056         std::vector<size_t> unlim_shape(idx.size(), unlim);
0057         std::vector<hsize_t> chunks(idx.size(), 10);
0058         if (options.isChunked()) {
0059             chunks = options.getChunkSize();
0060             if (chunks.size() != idx.size()) {
0061                 throw error(file, path, "H5Easy::dump: Incorrect dimension ChunkSize");
0062             }
0063         }
0064         std::vector<size_t> shape(idx.size());
0065         for (size_t i = 0; i < idx.size(); ++i) {
0066             shape[i] = idx[i] + 1;
0067         }
0068         DataSpace dataspace = DataSpace(shape, unlim_shape);
0069         DataSetCreateProps props;
0070         props.add(Chunking(chunks));
0071         DataSet dataset = file.createDataSet(path, dataspace, AtomicType<T>(), props, {}, true);
0072         dataset.select(idx, ones).write(data);
0073         if (options.flush()) {
0074             file.flush();
0075         }
0076         return dataset;
0077     }
0078 
0079     inline static T load_part(const File& file,
0080                               const std::string& path,
0081                               const std::vector<size_t>& idx) {
0082         std::vector<size_t> ones(idx.size(), 1);
0083         return file.getDataSet(path).select(idx, ones).read<T>();
0084     }
0085 };
0086 
0087 }  // namespace detail
0088 }  // namespace H5Easy