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