9 #ifndef H5CONVERTER_MISC_HPP
10 #define H5CONVERTER_MISC_HPP
21 #include <boost/multi_array.hpp>
22 #include <boost/numeric/ublas/matrix.hpp>
25 #include <H5Dpublic.h>
26 #include <H5Ppublic.h>
28 #include "../H5DataSpace.hpp"
29 #include "../H5DataType.hpp"
37 inline bool is_1D(
const std::vector<size_t>& dims)
39 return std::count_if(dims.begin(), dims.end(),
40 [](
size_t i){ return i > 1; }) < 2;
45 return std::accumulate(dims.begin(), dims.end(), 1,
46 std::multiplies<size_t>());
52 std::ostringstream ss;
53 ss <<
"Mismatch between vector size (" << size_vec
55 ss <<
") on dimension " << dimension;
63 const std::vector<size_t>& dims,
64 const size_t current_dim,
65 std::vector<T>& buffer) {
69 buffer.insert(buffer.end(), vec_single_dim.begin(), vec_single_dim.end());
75 const std::vector<size_t>& dims,
size_t current_dim,
80 for (
typename std::vector<T>::const_iterator it = vec_multi_dim.begin();
81 it < vec_multi_dim.end(); ++it) {
89 inline typename std::vector<T>::iterator
91 typename std::vector<T>::iterator end_buffer,
92 const std::vector<size_t>& dims,
93 const size_t current_dim,
94 std::vector<T>& vec_single_dim) {
95 const size_t n_elems = dims[current_dim];
96 typename std::vector<T>::iterator end_copy_iter =
97 std::min(begin_buffer + n_elems, end_buffer);
98 vec_single_dim.assign(begin_buffer, end_copy_iter);
102 template <
typename T,
typename U>
103 inline typename std::vector<T>::iterator
105 typename std::vector<T>::iterator end_buffer,
106 const std::vector<size_t>& dims,
107 const size_t current_dim,
108 std::vector<U>& vec_multi_dim) {
110 const size_t n_elems = dims[current_dim];
111 vec_multi_dim.resize(n_elems);
113 for (
typename std::vector<U>::iterator it = vec_multi_dim.begin();
114 it < vec_multi_dim.end(); ++it) {
116 current_dim + 1, *it);
122 template <
typename Scalar,
class Enable =
void>
126 static_assert((std::is_arithmetic<Scalar>::value ||
127 std::is_enum<Scalar>::value ||
128 std::is_same<std::string, Scalar>::value),
129 "supported datatype should be an arithmetic value, a "
130 "std::string or a container/array");
142 template <
typename CArray>
144 typename
std::enable_if<(is_c_array<CArray>::value)>::type> {
155 template <
typename T>
158 typename std::enable_if<(
159 std::is_same<T, typename type_of_array<T>::type>::value)>::type> {
162 assert(
is_1D(_space.getDimensions()));
182 template <
typename T, std::
size_t S>
185 typename std::enable_if<(
186 std::is_same<T, typename type_of_array<T>::type>::value)>::type> {
193 std::ostringstream ss;
195 <<
" elements into an array with " << S <<
" elements.";
215 template <
typename T, std::
size_t Dims>
216 struct data_converter<boost::multi_array<T, Dims>, void> {
218 typedef typename boost::multi_array<T, Dims> MultiArray;
222 assert(_dims.size() == Dims);
226 if (
std::equal(_dims.begin(), _dims.end(), array.shape()) ==
false) {
227 boost::array<typename MultiArray::index, Dims> ext;
228 std::copy(_dims.begin(), _dims.end(), ext.begin());
240 std::vector<size_t> _dims;
244 template <
typename T>
245 struct data_converter<boost::numeric::ublas::matrix<T>, void> {
247 typedef typename boost::numeric::ublas::matrix<T> Matrix;
251 assert(_dims.size() == 2);
255 boost::array<std::size_t, 2> sizes = {{array.size1(), array.size2()}};
257 if (
std::equal(_dims.begin(), _dims.end(), sizes.begin()) ==
false) {
258 array.resize(_dims[0], _dims[1],
false);
262 return &(array(0, 0));
266 return &(array(0, 0));
271 std::vector<size_t> _dims;
276 template <
typename T>
278 typename std::enable_if<(is_container<T>::value)>::type> {
285 return _vec_align.data();
291 vectors_to_single_buffer<T>(vec, _dims, 0, _vec_align);
292 return _vec_align.data();
296 single_buffer_to_vectors<typename type_of_array<T>::type, T>(
297 _vec_align.begin(), _vec_align.end(), _dims, 0, vec);
309 : _c_vec(nullptr), _space(space) {
318 return const_cast<char*
>(str.c_str());
322 _c_vec =
const_cast<char*
>(str.c_str());
327 assert(_c_vec !=
nullptr);
328 str = std::string(_c_vec);
330 if (_c_vec !=
NULL) {
332 (void)H5Dvlen_reclaim(str_type.
getId(), _space.getId(), H5P_DEFAULT,
353 _c_vec.resize(_space.getDimensions()[0],
NULL);
358 return const_cast<char*
>(str.c_str());
362 _c_vec.resize(vec.size() + 1,
NULL);
363 std::transform(vec.begin(), vec.end(), _c_vec.begin(), &char_converter);
369 vec.resize(_c_vec.size());
370 for (
size_t i = 0; i < vec.size(); ++i) {
371 vec[i] = std::string(_c_vec[i]);
374 if (_c_vec.empty() ==
false && _c_vec[0] !=
NULL) {
376 (void)H5Dvlen_reclaim(str_type.
getId(), _space.getId(), H5P_DEFAULT,
387 #endif // H5CONVERTER_MISC_HPP