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