31inline const DataSet& get_dataset(
const Selection& sel) {
32 return sel.getDataset();
35inline const DataSet& get_dataset(
const DataSet& ds) {
42inline hid_t get_memspace_id(
const Selection& ptr) {
43 return ptr.getMemSpace().getId();
46inline hid_t get_memspace_id(
const DataSet&) {
55 :
ElementSet(std::vector<std::vector<std::size_t>>(list)) {}
58 : _ids(element_ids) {}
61 for (
const auto& vec: element_ids) {
62 std::copy(vec.begin(), vec.end(), std::back_inserter(_ids));
66template <
typename Derivate>
75 const auto& slice =
static_cast<const Derivate&
>(*this);
76 auto filespace = hyperslab.
apply(slice.getSpace());
78 return detail::make_selection(memspace, filespace, details::get_dataset(slice));
81template <
typename Derivate>
83 const auto& slice =
static_cast<const Derivate&
>(*this);
85 filespace = hyper_slab.
apply(filespace);
87 auto n_elements = H5Sget_select_npoints(filespace.getId());
88 auto memspace =
DataSpace(std::array<size_t, 1>{size_t(n_elements)});
90 return detail::make_selection(memspace, filespace, details::get_dataset(slice));
94template <
typename Derivate>
96 const std::vector<size_t>& count,
97 const std::vector<size_t>& stride,
98 const std::vector<size_t>& block)
const {
101 return select_impl(slab, memspace);
104template <
typename Derivate>
106 const auto& slice =
static_cast<const Derivate&
>(*this);
107 const DataSpace& space = slice.getSpace();
110 std::vector<size_t> counts = dims;
113 std::vector<size_t> offsets(dims.size(), 0);
116 for (
const auto& column: columns) {
117 offsets.back() = column;
121 std::vector<size_t> memdims = dims;
122 memdims.back() = columns.size();
124 return select_impl(slab,
DataSpace(memdims));
127template <
typename Derivate>
129 const auto& slice =
static_cast<const Derivate&
>(*this);
130 const hsize_t* data =
nullptr;
132 const std::size_t length = elements._ids.size();
135 "Number of coordinates in elements picking "
136 "should be a multiple of the dimensions.");
139 std::vector<hsize_t> raw_elements;
143 if (std::is_same<std::size_t, hsize_t>::value) {
145 data =
reinterpret_cast<const hsize_t*
>(&(elements._ids[0]));
147 raw_elements.resize(length);
148 std::copy(elements._ids.begin(), elements._ids.end(), raw_elements.begin());
149 data = raw_elements.data();
152 if (H5Sselect_elements(space.
getId(), H5S_SELECT_SET, num_elements, data) < 0) {
153 HDF5ErrMapper::ToException<DataSpaceException>(
"Unable to select elements");
156 return detail::make_selection(
DataSpace(num_elements), space, details::get_dataset(slice));
160template <
typename Derivate>
164 read(array, xfer_props);
169template <
typename Derivate>
172 const auto& slice =
static_cast<const Derivate&
>(*this);
173 const DataSpace& mem_space = slice.getMemSpace();
175 const details::BufferInfo<T> buffer_info(
177 [&slice]() -> std::string { return details::get_dataset(slice).getPath(); },
178 details::BufferInfo<T>::Operation::read);
180 if (!details::checkDimensions(mem_space, buffer_info.n_dimensions)) {
181 std::ostringstream ss;
183 <<
" into arrays of dimensions " << buffer_info.n_dimensions;
189 auto effective_dims = details::squeezeDimensions(dims,
190 details::inspector<T>::recursive_ndim);
192 details::inspector<T>::prepare(array, effective_dims);
196 auto r = details::data_converter::get_reader<T>(dims, array);
197 read(r.get_pointer(), buffer_info.data_type, xfer_props);
200 auto t = create_datatype<typename details::inspector<T>::base_type>();
201 auto c = t.getClass();
203#if H5_VERSION_GE(1, 12, 0)
205 (void) H5Treclaim(t.getId(), mem_space.
getId(), xfer_props.
getId(), r.get_pointer());
208 (void) H5Dvlen_reclaim(t.getId(), mem_space.
getId(), xfer_props.
getId(), r.get_pointer());
214template <
typename Derivate>
219 static_assert(!std::is_const<T>::value,
220 "read() requires a non-const structure to read data into");
221 const auto& slice =
static_cast<const Derivate&
>(*this);
222 using element_type =
typename details::inspector<T>::base_type;
225 const DataType& mem_datatype = dtype.
empty() ? create_and_check_datatype<element_type>()
228 if (H5Dread(details::get_dataset(slice).getId(),
229 mem_datatype.
getId(),
230 details::get_memspace_id(slice),
231 slice.getSpace().getId(),
233 static_cast<void*
>(array)) < 0) {
234 HDF5ErrMapper::ToException<DataSetException>(
"Error during HDF5 Read.");
239template <
typename Derivate>
242 const auto& slice =
static_cast<const Derivate&
>(*this);
243 const DataSpace& mem_space = slice.getMemSpace();
249 const details::BufferInfo<T> buffer_info(
251 [&slice]() -> std::string { return details::get_dataset(slice).getPath(); },
252 details::BufferInfo<T>::Operation::write);
254 if (!details::checkDimensions(mem_space, buffer_info.n_dimensions)) {
255 std::ostringstream ss;
256 ss <<
"Impossible to write buffer of dimensions "
258 <<
" into dataset with n = " << buffer_info.n_dimensions <<
" dimensions.";
261 auto w = details::data_converter::serialize<T>(buffer);
262 write_raw(w.get_pointer(), buffer_info.data_type, xfer_props);
266template <
typename Derivate>
271 using element_type =
typename details::inspector<T>::base_type;
272 const auto& slice =
static_cast<const Derivate&
>(*this);
273 const auto& mem_datatype = dtype.
empty() ? create_and_check_datatype<element_type>() : dtype;
275 if (H5Dwrite(details::get_dataset(slice).getId(),
276 mem_datatype.getId(),
277 details::get_memspace_id(slice),
278 slice.getSpace().getId(),
280 static_cast<const void*
>(buffer)) < 0) {
281 HDF5ErrMapper::ToException<DataSetException>(
"Error during HDF5 Write: ");
Exception specific to HighFive DataSpace interface.
Definition H5Exception.hpp:112
Class representing the space (dimensions) of a dataset.
Definition H5DataSpace.hpp:25
size_t getNumberDimensions() const
getNumberDimensions
Definition H5Dataspace_misc.hpp:93
size_t getElementCount() const
getElementCount
Definition H5Dataspace_misc.hpp:112
std::vector< size_t > getDimensions() const
getDimensions
Definition H5Dataspace_misc.hpp:102
DataSpace clone() const
Definition H5Dataspace_misc.hpp:85
HDF5 Data Type.
Definition H5DataType.hpp:54
bool empty() const noexcept
Check the DataType was default constructed. Such value might represent auto-detection of the datatype...
Definition H5DataType_misc.hpp:35
Definition H5Slice_traits.hpp:21
ElementSet(std::initializer_list< std::size_t > list)
Create a list of points of N-dimension for selection.
Definition H5Slice_traits_misc.hpp:51
Definition H5Slice_traits.hpp:120
DataSpace apply(const DataSpace &space_) const
Definition H5Slice_traits.hpp:173
hid_t getId() const noexcept
getId
Definition H5Object_misc.hpp:65
HDF5 property Lists.
Definition H5PropertyList.hpp:79
Selection: represent a view on a slice/part of a dataset.
Definition H5Selection.hpp:27
DataSpace getSpace() const noexcept
getSpace
Definition H5Selection_misc.hpp:20
T read(const DataTransferProps &xfer_props=DataTransferProps()) const
Definition H5Slice_traits_misc.hpp:162
void write(const T &buffer, const DataTransferProps &xfer_props=DataTransferProps())
Definition H5Slice_traits_misc.hpp:241
void write_raw(const T *buffer, const DataType &dtype=DataType(), const DataTransferProps &xfer_props=DataTransferProps())
Definition H5Slice_traits_misc.hpp:268
Selection select(const HyperSlab &hyperslab) const
Select an hyperslab in the current Slice/Dataset.
Definition H5Slice_traits_misc.hpp:82
Selection select_impl(const HyperSlab &hyperslab, const DataSpace &memspace) const
Definition H5Slice_traits_misc.hpp:67
Definition H5_definitions.hpp:15
Definition H5Slice_traits.hpp:72