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 #ifndef H5DATASPACE_MISC_HPP
10 #define H5DATASPACE_MISC_HPP
11 
12 #include <vector>
13 #include <array>
14 #include <initializer_list>
15 
16 #include <H5Spublic.h>
17 
18 #include "../H5DataSpace.hpp"
19 #include "../H5Exception.hpp"
20 
21 #include "H5Utils.hpp"
22 
23 namespace HighFive {
24 
25 inline DataSpace::DataSpace(const std::vector<size_t>& dims)
26  : DataSpace(dims.begin(), dims.end()) {}
27 
28 inline DataSpace::DataSpace(std::initializer_list<size_t> items)
29  : DataSpace(std::vector<size_t>(items)) {}
30 
31 template<typename... Args>
32  inline DataSpace::DataSpace(size_t dim1, Args... dims)
33  : DataSpace(std::vector<size_t>{static_cast<size_t>(dim1),
34  static_cast<size_t>(dims)...}){}
35 
36 template <class IT, typename>
37 inline DataSpace::DataSpace(const IT begin, const IT end) {
38  std::vector<hsize_t> real_dims(begin, end);
39 
40  if ((_hid = H5Screate_simple(int(real_dims.size()), real_dims.data(),
41  NULL)) < 0) {
42  throw DataSpaceException("Impossible to create dataspace");
43  }
44 }
45 
46 inline DataSpace::DataSpace(const std::vector<size_t>& dims,
47  const std::vector<size_t>& maxdims) {
48 
49  if (dims.size() != maxdims.size()) {
50  throw DataSpaceException("dims and maxdims must be the same length.");
51  }
52 
53  std::vector<hsize_t> real_dims(dims.begin(), dims.end());
54  std::vector<hsize_t> real_maxdims(maxdims.begin(), maxdims.end());
55 
56  // Replace unlimited flag with actual HDF one
57  std::replace(real_maxdims.begin(), real_maxdims.end(),
58  static_cast<hsize_t>(DataSpace::UNLIMITED), H5S_UNLIMITED);
59 
60  if ((_hid = H5Screate_simple(int(dims.size()), real_dims.data(),
61  real_maxdims.data())) < 0) {
62  throw DataSpaceException("Impossible to create dataspace");
63  }
64 } // namespace HighFive
65 
67  H5S_class_t h5_dataspace_type;
68  switch (dtype) {
70  h5_dataspace_type = H5S_SCALAR;
71  break;
73  h5_dataspace_type = H5S_NULL;
74  break;
75  default:
76  throw DataSpaceException("Invalid dataspace type: should be "
77  "datascape_scalar or datascape_null");
78  }
79 
80  if ((_hid = H5Screate(h5_dataspace_type)) < 0) {
81  throw DataSpaceException("Unable to create dataspace");
82  }
83 }
84 
86 
87 inline DataSpace DataSpace::clone() const {
88  DataSpace res;
89  if ((res._hid = H5Scopy(_hid)) < 0) {
90  throw DataSpaceException("Unable to copy dataspace");
91  }
92  return res;
93 }
94 
95 inline size_t DataSpace::getNumberDimensions() const {
96  const int ndim = H5Sget_simple_extent_ndims(_hid);
97  if (ndim < 0) {
98  HDF5ErrMapper::ToException<DataSetException>(
99  "Unable to get dataspace number of dimensions");
100  }
101  return size_t(ndim);
102 }
103 
104 inline std::vector<size_t> DataSpace::getDimensions() const {
105 
106  std::vector<hsize_t> dims(getNumberDimensions());
107  if (dims.size() > 0) {
108  if (H5Sget_simple_extent_dims(_hid, dims.data(), NULL) < 0) {
109  HDF5ErrMapper::ToException<DataSetException>(
110  "Unable to get dataspace dimensions");
111  }
112  }
113  return details::to_vector_size_t(std::move(dims));
114 }
115 
116 inline std::vector<size_t> DataSpace::getMaxDimensions() const {
117  std::vector<hsize_t> maxdims(getNumberDimensions());
118  if (H5Sget_simple_extent_dims(_hid, NULL, maxdims.data()) < 0) {
119  HDF5ErrMapper::ToException<DataSetException>(
120  "Unable to get dataspace dimensions");
121  }
122 
123  std::vector<size_t> res(maxdims.begin(), maxdims.end());
124  std::replace(maxdims.begin(), maxdims.end(),
125  static_cast<size_t>(H5S_UNLIMITED), DataSpace::UNLIMITED);
126  return res;
127 }
128 
129 template <typename ScalarValue>
130 inline DataSpace DataSpace::From(const ScalarValue& scalar) {
131  (void)scalar;
132 #if H5_USE_CXX11
133  static_assert(
134  (std::is_arithmetic<ScalarValue>::value ||
135  std::is_enum<ScalarValue>::value ||
136  std::is_same<std::string, ScalarValue>::value),
137  "Only the following types are supported by DataSpace::From: \n"
138  " signed_arithmetic_types = int | long | float | double \n"
139  " unsigned_arithmetic_types = unsigned signed_arithmetic_types \n"
140  " string_types = std::string \n"
141  " all_basic_types = string_types | unsigned_arithmetic_types | "
142  "signed_arithmetic_types \n "
143  " stl_container_types = std::vector<all_basic_types> "
144  " boost_container_types = "
145  "boost::numeric::ublas::matrix<all_basic_types> | "
146  "boost::multi_array<all_basic_types> \n"
147  " all_supported_types = all_basic_types | stl_container_types | "
148  "boost_container_types");
149 #endif
151 }
152 
153 template <typename Value>
154 inline DataSpace DataSpace::From(const std::vector<Value>& container) {
155  return DataSpace(details::get_dim_vector<Value>(container));
156 }
157 
159 template <typename Value, std::size_t N>
160 inline DataSpace DataSpace::From(const std::array<Value, N>& ) {
161  std::vector<size_t> dims;
162  dims.push_back(N);
163  return DataSpace(dims);
164 }
165 
166 #ifdef H5_USE_BOOST
167 template <typename Value, std::size_t Dims>
168 inline DataSpace
169 DataSpace::From(const boost::multi_array<Value, Dims>& container) {
170  std::vector<size_t> dims(Dims);
171  for (std::size_t i = 0; i < Dims; ++i) {
172  dims[i] = container.shape()[i];
173  }
174  return DataSpace(dims);
175 }
176 
177 template <typename Value>
178 inline DataSpace
179 DataSpace::From(const boost::numeric::ublas::matrix<Value>& mat) {
180  std::vector<size_t> dims(2);
181  dims[0] = mat.size1();
182  dims[1] = mat.size2();
183  return DataSpace(dims);
184 }
185 
186 #endif
187 
188 namespace details {
189 
191 inline bool checkDimensions(const DataSpace& mem_space, size_t input_dims) {
192  size_t dataset_dims = mem_space.getNumberDimensions();
193  if (input_dims == dataset_dims)
194  return true;
195 
196  const std::vector<size_t> dims = mem_space.getDimensions();
197  for (std::vector<size_t>::const_reverse_iterator i = dims.rbegin();
198  i != --dims.rend() && *i == 1; ++i)
199  --dataset_dims;
200 
201  if (input_dims == dataset_dims)
202  return true;
203 
204  dataset_dims = dims.size();
205  for (std::vector<size_t>::const_iterator i = dims.begin();
206  i != --dims.end() && *i == 1; ++i)
207  --dataset_dims;
208 
209  if (input_dims == dataset_dims)
210  return true;
211 
212  // The final tests is for scalars
213  return input_dims == 0 && dataset_dims == 1 && dims[dims.size() - 1] == 1;
214 }
215 
216 } // namespace details
217 } // namespace HighFive
218 
219 #endif // H5DATASPACE_MISC_HPP
HighFive::DataSpace::DataspaceType
DataspaceType
dataspace type
Definition: H5DataSpace.hpp:36
HighFive::DataSpace::UNLIMITED
static const size_t UNLIMITED
Definition: H5DataSpace.hpp:33
HighFive::DataSpace::getDimensions
std::vector< size_t > getDimensions() const
getDimensions
Definition: H5Dataspace_misc.hpp:104
HighFive::DataSpace::datascape_null
@ datascape_null
Definition: H5DataSpace.hpp:38
NULL
#define NULL
Definition: mydefs.hpp:141
HighFive::DataSpace::From
static DataSpace From(const ScalarValue &scalar_value)
Definition: H5Dataspace_misc.hpp:130
HighFive::DataSpace::DataSpace
DataSpace()
Definition: H5Dataspace_misc.hpp:85
HighFive::DataSpace::getMaxDimensions
std::vector< size_t > getMaxDimensions() const
getMaxDimensions
Definition: H5Dataspace_misc.hpp:116
HighFive::DataSpace::datascape_scalar
@ datascape_scalar
Definition: H5DataSpace.hpp:37
std
Definition: HalfEdge.hpp:124
HighFive::Object::_hid
hid_t _hid
Definition: H5Object.hpp:48
HighFive::details::to_vector_size_t
std::vector< std::size_t > to_vector_size_t(std::vector< Size > vec)
Definition: H5Utils.hpp:169
HighFive::DataSpace
Definition: H5DataSpace.hpp:30
HighFive::DataSpace::clone
DataSpace clone() const
Definition: H5Dataspace_misc.hpp:87
H5Utils.hpp
HighFive::details::checkDimensions
bool checkDimensions(const DataSpace &mem_space, size_t input_dims)
dimension checks
Definition: H5Dataspace_misc.hpp:191
HighFive::DataSpace::getNumberDimensions
size_t getNumberDimensions() const
getNumberDimensions
Definition: H5Dataspace_misc.hpp:95
HighFive
Definition: H5Annotate_traits.hpp:14
HighFive::DataSpaceException
Exception specific to HighFive DataSpace interface.
Definition: H5Exception.hpp:98


lvr2
Author(s): Thomas Wiemann , Sebastian Pütz , Alexander Mock , Lars Kiesow , Lukas Kalbertodt , Tristan Igelbrink , Johan M. von Behren , Dominik Feldschnieders , Alexander Löhr
autogenerated on Wed Mar 2 2022 00:37:23