echelon  0.8.0
hdf5/slice.hpp
1 // Copyright (c) 2012-2014 Christopher Hinz
2 //
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 
6 #ifndef ECHELON_HDF5_SLICE_HPP
7 #define ECHELON_HDF5_SLICE_HPP
8 
9 #include <echelon/hdf5/precursor/dataspace.hpp>
10 #include <echelon/hdf5/precursor/dataset.hpp>
11 #include <echelon/hdf5/storage_layer.hpp>
12 #include <echelon/hdf5/container_adaption.hpp>
13 #include <echelon/hdf5/range.hpp>
14 #include <echelon/hdf5/array_slice.hpp>
15 
16 #include <cassert>
17 #include <vector>
18 #include <tuple>
19 
20 namespace echelon
21 {
22 namespace hdf5
23 {
24 
27 class slice
28 {
29 public:
30  slice(hdf5::precursor::dataset sliced_dataset_,
31  const std::vector<totally_bound_range_t>& ranges);
32 
42  template <typename T>
43  void operator<<=(const T& source)
44  {
45  auto current_shape = shape_adl(source);
46 
47  std::vector<hsize_t> mem_shape(begin(current_shape), end(current_shape));
48 
49  hdf5::precursor::dataspace mem_space(mem_shape);
50  hdf5::precursor::dataspace file_space = selected_dataspace_;
51  hdf5::precursor::type datatype = sliced_dataset_.datatype();
52 
53  write(sliced_dataset_, datatype, mem_space, file_space, source);
54  }
55 
64  template <typename T>
65  friend void operator<<=(T& sink, const slice& source)
66  {
67  auto current_shape = shape_adl(sink);
68 
69  std::vector<hsize_t> mem_shape(begin(current_shape), end(current_shape));
70 
71  hdf5::precursor::dataspace mem_space(mem_shape);
72  hdf5::precursor::dataspace file_space = source.selected_dataspace_;
73  hdf5::precursor::type datatype = source.sliced_dataset_.datatype();
74 
75  read(source.sliced_dataset_, datatype, mem_space, file_space, sink);
76  }
77 
86  template <typename T>
87  void operator<<=(const array_slice<T>& source)
88  {
89  auto current_shape = source.original_shape();
90 
91  std::vector<hsize_t> mem_shape(begin(current_shape), end(current_shape));
92 
93  hdf5::precursor::dataspace mem_space(mem_shape);
94  hdf5::precursor::dataspace file_space = selected_dataspace_;
95  hdf5::precursor::type datatype = sliced_dataset_.datatype();
96 
97  auto slice_shape = source.shape();
98  std::vector<hsize_t> count;
99 
100  for (std::size_t i = 0; i < slice_shape.size(); ++i)
101  {
102  count.push_back(slice_shape[i] / source.stride()[i]);
103  }
104 
105  mem_space.select_hyperslab(H5S_SELECT_SET, source.offset(), source.stride(), count);
106 
107  write(sliced_dataset_, datatype, mem_space, file_space, source);
108  }
109 
118  template <typename T>
119  friend void operator<<=(const array_slice<T>& sink, const slice& source)
120  {
121  auto current_shape = sink.original_shape();
122 
123  std::vector<hsize_t> mem_shape(begin(current_shape), end(current_shape));
124 
125  hdf5::precursor::dataspace mem_space(mem_shape);
126  hdf5::precursor::dataspace file_space = source.selected_dataspace_;
127  hdf5::precursor::type datatype = source.sliced_dataset_.datatype();
128 
129  auto slice_shape = sink.shape();
130  std::vector<hsize_t> count;
131 
132  for (std::size_t i = 0; i < slice_shape.size(); ++i)
133  {
134  count.push_back(slice_shape[i] / sink.stride()[i]);
135  }
136 
137  mem_space.select_hyperslab(H5S_SELECT_SET, sink.offset(), sink.stride(), count);
138 
139  read(source.sliced_dataset_, datatype, mem_space, file_space, sink);
140  }
141 
144  const std::vector<hsize_t>& shape() const;
145 
146 private:
147  hdf5::precursor::dataset sliced_dataset_;
148  hdf5::precursor::dataspace selected_dataspace_;
149 
150  std::vector<hsize_t> size_;
151 };
152 }
153 }
154 
155 #endif
echelon&#39;s core namespace
Definition: attribute.cpp:10
void operator<<=(const T &source)
Writes the content of a data source into the slice.
Definition: hdf5/slice.hpp:43
A slice (rectangular portion) of an HDF5 dataset.
Definition: hdf5/slice.hpp:27
const std::vector< hsize_t > & shape() const
The shape of the slice.
Definition: hdf5/slice.cpp:42
friend void operator<<=(T &sink, const slice &source)
Reads the content of the slice into a data sink.
Definition: hdf5/slice.hpp:65