9#ifndef H5SLICE_TRAITS_MISC_HPP
10#define H5SLICE_TRAITS_MISC_HPP
21#include <boost/multi_array.hpp>
22#include <boost/numeric/ublas/matrix.hpp>
23#include <boost/serialization/vector.hpp>
39inline const DataSet& get_dataset(
const Selection& sel) {
40 return sel.getDataset();
43inline const DataSet& get_dataset(
const DataSet& ds) {
50inline hid_t get_memspace_id(
const Selection& ptr) {
51 return ptr.getMemSpace().getId();
54inline hid_t get_memspace_id(
const DataSet&) {
63 :
ElementSet(std::vector<std::vector<std::size_t>>(list)) {}
66 : _ids(element_ids) {}
69 for (
const auto& vec : element_ids) {
70 std::copy(vec.begin(), vec.end(), std::back_inserter(_ids));
74template <
typename Derivate>
76 const std::vector<size_t>& count,
77 const std::vector<size_t>& stride)
const {
80 const auto& slice =
static_cast<const Derivate&
>(*this);
81 std::vector<hsize_t> offset_local(offset.size());
82 std::vector<hsize_t> count_local(count.size());
83 std::vector<hsize_t> stride_local(stride.size());
84 std::copy(offset.begin(), offset.end(), offset_local.begin());
85 std::copy(count.begin(), count.end(), count_local.begin());
86 std::copy(stride.begin(), stride.end(), stride_local.begin());
89 if (H5Sselect_hyperslab(space.
getId(), H5S_SELECT_SET, offset_local.data(),
90 stride.empty() ? NULL : stride_local.data(),
91 count_local.data(), NULL) < 0) {
92 HDF5ErrMapper::ToException<DataSpaceException>(
"Unable to select hyperslap");
98template <
typename Derivate>
100 const auto& slice =
static_cast<const Derivate&
>(*this);
101 const DataSpace& space = slice.getSpace();
102 const DataSet& dataset = details::get_dataset(slice);
104 std::vector<hsize_t> counts(dims.size());
105 std::copy(dims.begin(), dims.end(), counts.begin());
106 counts[dims.size() - 1] = 1;
107 std::vector<hsize_t> offsets(dims.size(), 0);
109 H5Sselect_none(space.
getId());
111 for (
const auto& column : columns) {
112 offsets[offsets.size() - 1] = column;
114 if (H5Sselect_hyperslab(space.
getId(), H5S_SELECT_OR, offsets.data(), 0,
115 counts.data(), 0) < 0) {
116 HDF5ErrMapper::ToException<DataSpaceException>(
"Unable to select hyperslap");
120 dims[dims.size() - 1] = columns.size();
126typename std::enable_if<std::is_same<std::size_t, T>::value>::type
128 typename std::vector<T>&,
130 const std::vector<std::size_t>& element_ids) {
131 data =
reinterpret_cast<const T*
>(&(element_ids[0]));
136typename std::enable_if<!std::is_same<std::size_t, T>::value>::type
138 typename std::vector<T>& raw_elements,
139 const std::size_t length,
140 const std::vector<std::size_t>& element_ids) {
141 raw_elements.resize(length);
142 std::copy(element_ids.begin(), element_ids.end(), raw_elements.begin());
143 data = raw_elements.data();
146template <
typename Derivate>
148 const auto& slice =
static_cast<const Derivate&
>(*this);
149 const hsize_t* data =
nullptr;
151 const std::size_t length = elements._ids.size();
154 "should be a multiple of the dimensions.");
157 std::vector<hsize_t> raw_elements;
160 access_with_conversion<>(data, raw_elements, length, elements._ids);
162 if (H5Sselect_elements(space.
getId(), H5S_SELECT_SET, num_elements, data) < 0) {
163 HDF5ErrMapper::ToException<DataSpaceException>(
"Unable to select elements");
169template <
typename Derivate>
172 const auto& slice =
static_cast<const Derivate&
>(*this);
173 const DataSpace& mem_space = slice.getMemSpace();
174 const details::BufferInfo<T> buffer_info(slice.getDataType(),
175 [slice]() -> std::string { return details::get_dataset(slice).getPath(); });
177 if (!details::checkDimensions(mem_space, buffer_info.n_dimensions)) {
178 std::ostringstream ss;
179 ss <<
"Impossible to read DataSet of dimensions "
181 << buffer_info.n_dimensions;
184 details::data_converter<T> converter(mem_space);
185 read(converter.transform_read(array), buffer_info.data_type);
187 converter.process_result(array);
191template <
typename Derivate>
194 static_assert(!std::is_const<T>::value,
195 "read() requires a non-const structure to read data into");
196 const auto& slice =
static_cast<const Derivate&
>(*this);
197 using element_type =
typename details::inspector<T>::base_type;
201 dtype.
empty() ? create_and_check_datatype<element_type>() : dtype;
203 if (H5Dread(details::get_dataset(slice).getId(),
204 mem_datatype.
getId(),
205 details::get_memspace_id(slice),
206 slice.getSpace().getId(), H5P_DEFAULT,
static_cast<void*
>(array)) < 0) {
207 HDF5ErrMapper::ToException<DataSetException>(
"Error during HDF5 Read: ");
212template <
typename Derivate>
215 const auto& slice =
static_cast<const Derivate&
>(*this);
216 const DataSpace& mem_space = slice.getMemSpace();
217 const details::BufferInfo<T> buffer_info(slice.getDataType(),
218 [slice]() -> std::string { return details::get_dataset(slice).getPath(); });
220 if (!details::checkDimensions(mem_space, buffer_info.n_dimensions)) {
221 std::ostringstream ss;
222 ss <<
"Impossible to write buffer of dimensions " << buffer_info.n_dimensions
226 details::data_converter<T> converter(mem_space);
227 write_raw(converter.transform_write(buffer), buffer_info.data_type);
231template <
typename Derivate>
234 using element_type =
typename details::inspector<T>::base_type;
235 const auto& slice =
static_cast<const Derivate&
>(*this);
236 const auto& mem_datatype =
237 dtype.
empty() ? create_and_check_datatype<element_type>() : dtype;
239 if (H5Dwrite(details::get_dataset(slice).getId(),
240 mem_datatype.getId(),
241 details::get_memspace_id(slice),
242 slice.getSpace().getId(), H5P_DEFAULT,
243 static_cast<const void*
>(buffer)) < 0) {
244 HDF5ErrMapper::ToException<DataSetException>(
"Error during HDF5 Write: ");
Class representing a dataset.
Definition: H5DataSet.hpp:31
Exception specific to HighFive DataSpace interface.
Definition: H5Exception.hpp:99
Class representing the space (dimensions) of a dataset.
Definition: H5DataSpace.hpp:37
size_t getNumberDimensions() const
getNumberDimensions
Definition: H5Dataspace_misc.hpp:94
std::vector< size_t > getDimensions() const
getDimensions
Definition: H5Dataspace_misc.hpp:103
DataSpace clone() const
Definition: H5Dataspace_misc.hpp:86
HDF5 Data Type.
Definition: H5DataType.hpp:42
bool empty() const noexcept
Check the DataType was default constructed. Such value might represent auto-detection of the datatype...
Definition: H5DataType_misc.hpp:28
Definition: H5Slice_traits.hpp:20
ElementSet(std::initializer_list< std::size_t > list)
Create a list of points of N-dimension for selection.
Definition: H5Slice_traits_misc.hpp:59
hid_t getId() const noexcept
getId
Definition: H5Object_misc.hpp:55
Selection: represent a view on a slice/part of a dataset.
Definition: H5Selection.hpp:23
void read(T &array) const
Definition: H5Slice_traits_misc.hpp:171
void write(const T &buffer)
Definition: H5Slice_traits_misc.hpp:214
Selection select(const std::vector< size_t > &offset, const std::vector< size_t > &count, const std::vector< size_t > &stride=std::vector< size_t >()) const
Select a region in the current Slice/Dataset of count points at offset separated by stride....
Definition: H5Slice_traits_misc.hpp:75
void write_raw(const T *buffer, const DataType &dtype=DataType())
Definition: H5Slice_traits_misc.hpp:233
Definition: H5_definitions.hpp:15
std::enable_if< std::is_same< std::size_t, T >::value >::type access_with_conversion(const T *&data, typename std::vector< T > &, const std::size_t, const std::vector< std::size_t > &element_ids)
Definition: H5Slice_traits_misc.hpp:127