Dune-Fufem 2.11-git
Loading...
Searching...
No Matches
frombuffer.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright © DUNE-FUFEM Project contributors, see file AUTHORS.md
2// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
3
4#ifndef DUNE_FUFEM_HDF5_FROMBUFFER_HH
5#define DUNE_FUFEM_HDF5_FROMBUFFER_HH
6
7#include <numeric>
8
10#include <dune/istl/bvector.hh>
11#include <dune/istl/matrix.hh>
12
13template <int k, typename ctype, typename T>
14void fromBuffer(std::vector<ctype> const &buffer, std::array<T, 3> dimensions,
16 auto const entrySize = std::accumulate(dimensions.begin(), dimensions.end(),
18 assert(buffer.size() == entrySize);
19 assert(dimensions[2] == k);
20 data.setSize(dimensions[0], dimensions[1]);
21
22 auto it = buffer.begin();
23 for (auto &&row : data)
24 for (auto &&localVector : row) {
25 auto const nextIt = it + k;
26 std::copy(it, nextIt, localVector.begin());
27 it = nextIt;
28 }
29 assert(it == buffer.end());
30}
31
32template <int k, typename ctype, typename T>
33void fromBuffer(std::vector<ctype> const &buffer, std::array<T, 2> &dimensions,
35 assert(buffer.size() == std::accumulate(dimensions.begin(), dimensions.end(),
36 1ul, std::multiplies<T>()));
37 assert(dimensions[1] == k);
38 data.resize(dimensions[0]);
39
40 auto it = buffer.begin();
41 for (auto &&localVector : data) {
42 auto const nextIt = it + k;
43 std::copy(it, nextIt, localVector.begin());
44 it = nextIt;
45 }
46 assert(it == buffer.end());
47}
48
49template <typename ctype, typename T>
50void fromBuffer(std::vector<ctype> const &buffer, std::array<T, 1> dimensions,
52 assert(buffer.size() == dimensions[0]);
53 data.resize(dimensions[0]);
54
55 auto it = buffer.begin();
56 for (auto &&localVector : data)
57 localVector[0] = *(it++);
58}
59
60template <typename ctype, typename T>
61void fromBuffer(std::vector<ctype> const &buffer, std::array<T, 1> dimensions,
62 std::vector<ctype> &data) {
63 assert(buffer.size() == dimensions[0]);
64
65 data = buffer;
66}
67
68template <typename ctype, typename T>
69void fromBuffer(std::vector<ctype> const &buffer, std::array<T, 0> dimensions,
70 ctype &data) {
71 assert(buffer.size() == 1);
72 data = buffer[0];
73}
74#endif
void fromBuffer(std::vector< ctype > const &buffer, std::array< T, 3 > dimensions, Dune::Matrix< Dune::FieldVector< ctype, k > > &data)
Definition frombuffer.hh:14
T accumulate(T... args)
T begin(T... args)
T copy(T... args)
T end(T... args)
T size(T... args)