HighFive 2.7.1
HighFive - Header-only C++ HDF5 interface
Loading...
Searching...
No Matches
H5Dataspace_misc.hpp
Go to the documentation of this file.
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#pragma once
10
11#include <array>
12#include <initializer_list>
13#include <vector>
14#include <numeric>
15
16#include <H5Spublic.h>
17
18#include "H5Utils.hpp"
19#include "H5Converter_misc.hpp"
20
21namespace HighFive {
22
23inline DataSpace::DataSpace(const std::vector<size_t>& dims)
24 : DataSpace(dims.begin(), dims.end()) {}
25
26template <size_t N>
27inline DataSpace::DataSpace(const std::array<size_t, N>& dims)
28 : DataSpace(dims.begin(), dims.end()) {}
29
30inline DataSpace::DataSpace(const std::initializer_list<size_t>& items)
31 : DataSpace(std::vector<size_t>(items)) {}
32
33template <typename... Args>
34inline DataSpace::DataSpace(size_t dim1, Args... dims)
35 : DataSpace(std::vector<size_t>{dim1, static_cast<size_t>(dims)...}) {}
36
37template <class IT, typename>
38inline DataSpace::DataSpace(const IT begin, const IT end) {
39 std::vector<hsize_t> real_dims(begin, end);
40
41 if ((_hid = H5Screate_simple(int(real_dims.size()), real_dims.data(), NULL)) < 0) {
42 throw DataSpaceException("Impossible to create dataspace");
43 }
44}
45
46inline DataSpace::DataSpace(const std::vector<size_t>& dims, const std::vector<size_t>& maxdims) {
47 if (dims.size() != maxdims.size()) {
48 throw DataSpaceException("dims and maxdims must be the same length.");
49 }
50
51 std::vector<hsize_t> real_dims(dims.begin(), dims.end());
52 std::vector<hsize_t> real_maxdims(maxdims.begin(), maxdims.end());
53
54 // Replace unlimited flag with actual HDF one
55 std::replace(real_maxdims.begin(),
56 real_maxdims.end(),
57 static_cast<hsize_t>(DataSpace::UNLIMITED),
58 H5S_UNLIMITED);
59
60 if ((_hid = H5Screate_simple(int(dims.size()), real_dims.data(), real_maxdims.data())) < 0) {
61 throw DataSpaceException("Impossible to create dataspace");
62 }
63} // namespace HighFive
64
66 H5S_class_t h5_dataspace_type;
67 switch (dtype) {
69 h5_dataspace_type = H5S_SCALAR;
70 break;
72 h5_dataspace_type = H5S_NULL;
73 break;
74 default:
76 "Invalid dataspace type: should be "
77 "dataspace_scalar or dataspace_null");
78 }
79
80 if ((_hid = H5Screate(h5_dataspace_type)) < 0) {
81 throw DataSpaceException("Unable to create dataspace");
82 }
83}
84
86 DataSpace res;
87 if ((res._hid = H5Scopy(_hid)) < 0) {
88 throw DataSpaceException("Unable to copy dataspace");
89 }
90 return res;
91}
92
93inline size_t DataSpace::getNumberDimensions() const {
94 const int ndim = H5Sget_simple_extent_ndims(_hid);
95 if (ndim < 0) {
97 "Unable to get dataspace number of dimensions");
98 }
99 return size_t(ndim);
100}
101
102inline std::vector<size_t> DataSpace::getDimensions() const {
103 std::vector<hsize_t> dims(getNumberDimensions());
104 if (!dims.empty()) {
105 if (H5Sget_simple_extent_dims(_hid, dims.data(), NULL) < 0) {
106 HDF5ErrMapper::ToException<DataSetException>("Unable to get dataspace dimensions");
107 }
108 }
109 return details::to_vector_size_t(std::move(dims));
110}
111
112inline size_t DataSpace::getElementCount() const {
113 const std::vector<size_t>& dims = getDimensions();
114 return std::accumulate(dims.begin(), dims.end(), size_t{1u}, std::multiplies<size_t>());
115}
116
117inline std::vector<size_t> DataSpace::getMaxDimensions() const {
118 std::vector<hsize_t> maxdims(getNumberDimensions());
119 if (H5Sget_simple_extent_dims(_hid, NULL, maxdims.data()) < 0) {
120 HDF5ErrMapper::ToException<DataSetException>("Unable to get dataspace dimensions");
121 }
122
123 std::replace(maxdims.begin(),
124 maxdims.end(),
125 H5S_UNLIMITED,
126 static_cast<hsize_t>(DataSpace::UNLIMITED));
127 return details::to_vector_size_t(maxdims);
128}
129
130template <typename T>
131inline DataSpace DataSpace::From(const T& value) {
132 auto dims = details::inspector<T>::getDimensions(value);
133 return DataSpace(dims);
134}
135
136template <std::size_t N, std::size_t Width>
137inline DataSpace DataSpace::FromCharArrayStrings(const char (&)[N][Width]) {
138 return DataSpace(N);
139}
140
141namespace details {
142
144inline bool checkDimensions(const DataSpace& mem_space, size_t n_dim_requested) {
145 return checkDimensions(mem_space.getDimensions(), n_dim_requested);
146}
147
148} // namespace details
149} // namespace HighFive
Exception specific to HighFive DataSpace interface.
Definition H5Exception.hpp:112
Class representing the space (dimensions) of a dataset.
Definition H5DataSpace.hpp:25
static DataSpace FromCharArrayStrings(const char(&)[N][Width])
Definition H5Dataspace_misc.hpp:137
static DataSpace From(const T &value)
Create a dataspace matching a type accepted by details::inspector.
Definition H5Dataspace_misc.hpp:131
size_t getNumberDimensions() const
getNumberDimensions
Definition H5Dataspace_misc.hpp:93
std::vector< size_t > getMaxDimensions() const
getMaxDimensions
Definition H5Dataspace_misc.hpp:117
DataspaceType
dataspace type
Definition H5DataSpace.hpp:32
@ dataspace_scalar
Definition H5DataSpace.hpp:33
@ dataspace_null
Definition H5DataSpace.hpp:34
size_t getElementCount() const
getElementCount
Definition H5Dataspace_misc.hpp:112
std::vector< size_t > getDimensions() const
getDimensions
Definition H5Dataspace_misc.hpp:102
DataSpace clone() const
Definition H5Dataspace_misc.hpp:85
static const size_t UNLIMITED
Definition H5DataSpace.hpp:29
hid_t _hid
Definition H5Object.hpp:105
Definition H5_definitions.hpp:15
static void ToException(const std::string &prefix_msg)
Definition H5Exception_misc.hpp:42