HighFive 3.3.0
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#include "h5s_wrapper.hpp"
21
22namespace HighFive {
23
24namespace detail {
25inline DataSpace make_data_space(hid_t hid) {
26 return DataSpace::fromId(hid);
27}
28} // namespace detail
29
30inline DataSpace::DataSpace(const std::vector<size_t>& dims)
31 : DataSpace(dims.begin(), dims.end()) {}
32
33template <size_t N>
34constexpr DataSpace::DataSpace(const std::array<size_t, N>& dims) {
35 std::array<hsize_t, N> real_dims;
36 std::copy(dims.begin(), dims.end(), real_dims.begin());
37 _hid = detail::h5s_create_simple(static_cast<int>(N), real_dims.data(), nullptr);
38}
39
40inline DataSpace::DataSpace(const std::initializer_list<size_t>& items)
41 : DataSpace(std::vector<size_t>(items)) {}
42
43template <typename... Args>
44inline DataSpace::DataSpace(size_t dim1, Args... dims)
45 : DataSpace(std::array<size_t, 1 + sizeof...(dims)>{dim1, static_cast<size_t>(dims)...}) {}
46
47template <class IT, typename>
48inline DataSpace::DataSpace(const IT begin, const IT end) {
49 std::vector<hsize_t> real_dims(begin, end);
50
51 _hid = detail::h5s_create_simple(int(real_dims.size()), real_dims.data(), nullptr);
52}
53
57
61
62inline DataSpace::DataSpace(const std::vector<size_t>& dims, const std::vector<size_t>& maxdims) {
63 if (dims.size() != maxdims.size()) {
64 throw DataSpaceException("dims and maxdims must be the same length.");
65 }
66
67 std::vector<hsize_t> real_dims(dims.begin(), dims.end());
68 std::vector<hsize_t> real_maxdims(maxdims.begin(), maxdims.end());
69
70 // Replace unlimited flag with actual HDF one
71 std::replace(real_maxdims.begin(),
72 real_maxdims.end(),
73 static_cast<hsize_t>(DataSpace::UNLIMITED),
74 H5S_UNLIMITED);
75
76 _hid = detail::h5s_create_simple(int(dims.size()), real_dims.data(), real_maxdims.data());
77}
78
80 auto to_hdf5 = [](auto _space_type) -> H5S_class_t {
81 switch (_space_type) {
83 return H5S_SCALAR;
85 return H5S_NULL;
86 default:
88 "Invalid dataspace type: should be "
89 "dataspace_scalar or dataspace_null");
90 }
91 };
92
93 _hid = detail::h5s_create(to_hdf5(space_type));
94}
95
97 DataSpace res;
98 res._hid = detail::h5s_copy(_hid);
99 return res;
100}
101
102inline size_t DataSpace::getNumberDimensions() const {
103 return static_cast<size_t>(detail::h5s_get_simple_extent_ndims(_hid));
104}
105
106inline std::vector<size_t> DataSpace::getDimensions() const {
107 std::vector<hsize_t> dims(getNumberDimensions());
108 if (!dims.empty()) {
109 detail::h5s_get_simple_extent_dims(_hid, dims.data(), nullptr);
110 }
111 return details::to_vector_size_t(std::move(dims));
112}
113
114inline size_t DataSpace::getElementCount() const {
115 return static_cast<size_t>(detail::h5s_get_simple_extent_npoints(_hid));
116}
117
118inline std::vector<size_t> DataSpace::getMaxDimensions() const {
119 std::vector<hsize_t> maxdims(getNumberDimensions());
120 detail::h5s_get_simple_extent_dims(_hid, nullptr, maxdims.data());
121
122 std::replace(maxdims.begin(),
123 maxdims.end(),
124 H5S_UNLIMITED,
125 static_cast<hsize_t>(DataSpace::UNLIMITED));
126 return details::to_vector_size_t(maxdims);
127}
128
129template <typename T>
130inline DataSpace DataSpace::From(const T& value) {
131 auto dims = details::inspector<T>::getDimensions(value);
132 return DataSpace(dims);
133}
134
135template <std::size_t N, std::size_t Width>
136inline DataSpace DataSpace::FromCharArrayStrings(const char (&)[N][Width]) {
137 return DataSpace(N);
138}
139
140namespace details {
141
142inline bool checkDimensions(const DataSpace& mem_space,
143 size_t min_dim_requested,
144 size_t max_dim_requested) {
145 return checkDimensions(mem_space.getDimensions(), min_dim_requested, max_dim_requested);
146}
147
148} // namespace details
149} // namespace HighFive
Exception specific to HighFive DataSpace interface.
Definition H5Exception.hpp:115
Class representing the space (dimensions) of a DataSet.
Definition H5DataSpace.hpp:39
static DataSpace FromCharArrayStrings(const char(&string_array)[N][Width])
Create a DataSpace from a value of type string array.
Definition H5Dataspace_misc.hpp:136
static DataSpace fromId(hid_t hid)
Definition H5DataSpace.hpp:265
static DataSpace From(const T &value)
Automatically deduce the DataSpace from a container/value.
Definition H5Dataspace_misc.hpp:130
size_t getNumberDimensions() const
Returns the number of dimensions of a DataSpace.
Definition H5Dataspace_misc.hpp:102
std::vector< size_t > getMaxDimensions() const
Returns the maximum size of the dataset in each dimension.
Definition H5Dataspace_misc.hpp:118
DataspaceType
An enum to create scalar and null DataSpace with DataSpace::DataSpace(DataspaceType dtype).
Definition H5DataSpace.hpp:56
static DataSpace Scalar()
Create a scalar DataSpace.
Definition H5Dataspace_misc.hpp:54
static constexpr DataspaceType dataspace_scalar
Definition H5DataSpace.hpp:63
size_t getElementCount() const
Return the number of elements in this DataSpace.
Definition H5Dataspace_misc.hpp:114
std::vector< size_t > getDimensions() const
Returns the size of the dataset in each dimension.
Definition H5Dataspace_misc.hpp:106
DataSpace clone() const
Create a copy of the DataSpace which will have different id.
Definition H5Dataspace_misc.hpp:96
static constexpr DataspaceType dataspace_null
Definition H5DataSpace.hpp:64
static DataSpace Null()
Create a null DataSpace.
Definition H5Dataspace_misc.hpp:58
static const size_t UNLIMITED
Magic value to specify that a DataSpace can grow without limit.
Definition H5DataSpace.hpp:49
hid_t _hid
Definition H5Object.hpp:98
Definition assert_compatible_spaces.hpp:15