DUNE-FUNCTIONS (unstable)

flatvectorview.hh
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3
4// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file AUTHORS.md
5// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
6
7#ifndef DUNE_FUNCTIONS_FUNCTIONSPACEBASES_FLATVECTORVIEW_HH
8#define DUNE_FUNCTIONS_FUNCTIONSPACEBASES_FLATVECTORVIEW_HH
9
10
11#include <array>
12
13#include <dune/common/concept.hh>
14#include <dune/common/hybridutilities.hh>
15#include <dune/common/indices.hh>
16
17#include <dune/functions/functionspacebases/concepts.hh>
18
19
20
21
22namespace Dune {
23namespace Functions {
24namespace Impl {
25
26
27template<class V>
28struct FlatVectorBackend
29{
30
31 template<class VV, class Index,
32 std::enable_if_t< models<Concept::HasIndexAccess, VV, Index>(), int> = 0>
33 static decltype(auto) getEntry(VV&& v, const Index& i)
34 {
35 return v[i];
36 }
37
38 template<class VV, class Index,
39 std::enable_if_t< not models<Concept::HasIndexAccess, VV, Index>(), int> = 0>
40 static decltype(auto) getEntry(VV&& v, const Index&)
41 {
42 return std::forward<VV>(v);
43 }
44
45 template<class VV,
46 std::enable_if_t< models<Concept::HasSizeMethod, VV>(), int> = 0>
47 static auto size(VV&& v)
48 {
49 return Dune::Hybrid::size(v);
50 }
51
52 template<class VV,
53 std::enable_if_t< not models<Concept::HasSizeMethod, VV>(), int>type = 0>
54 static auto size(VV&&)
55 {
56 return Dune::index_constant<1>{};
57 }
58};
59
60
61
62
63template<class K, int n, int m>
64struct FlatVectorBackend<typename Dune::FieldMatrix<K, n, m> >
65{
66
67 template<class VV, class Index>
68 static decltype(auto) getEntry(VV&& v, const Index& i)
69 {
70 return v[i/m][i%m];
71 }
72
73 template<class VV>
74 static auto size(VV&& v)
75 {
76 return Dune::index_constant<n*m>{};
77 }
78};
79
80
81
82template<class K, std::size_t n>
83struct FlatVectorBackend< std::array<K, n> >
84{
85
86 template<class VV, class Index>
87 static decltype(auto) getEntry(VV&& v, const Index& i)
88 {
89 const auto innerSize = decltype(FlatVectorBackend<K>::size(v[0]))::value;
90 return FlatVectorBackend<K>::getEntry(v[i/innerSize], i%innerSize);
91 }
92
93 template<class VV>
94 static auto size(VV&& v)
95 {
96 const auto innerSize = decltype(FlatVectorBackend<K>::size(v[0]))::value;
97 return Dune::index_constant<n*innerSize>{};
98 }
99
100};
101
102
103
104
105template<class T>
106class FlatVectorView
107{
108 using Backend = FlatVectorBackend<std::decay_t<T>>;
109public:
110 FlatVectorView(T& t) :
111 t_(&t)
112 {}
113
114 auto size() const
115 {
116 return Backend::size(*t_);
117 }
118
119 template<class Index>
120 decltype(auto) operator[](const Index& i) const
121 {
122 return Backend::getEntry(*t_, i);
123 }
124
125 template<class Index>
126 decltype(auto) operator[](const Index& i)
127 {
128 return Backend::getEntry(*t_, i);
129 }
130
131private:
132 T* t_;
133};
134
135
136template<class T>
137class FlatVectorView<T&&>
138{
139 using Backend = FlatVectorBackend<std::decay_t<T>>;
140public:
141 FlatVectorView(T&& t) :
142 t_(std::move(t))
143 {}
144
145 auto size() const
146 {
147 return Backend::size(t_);
148 }
149
150 template<class Index>
151 decltype(auto) operator[](const Index& i) const
152 {
153 return Backend::getEntry(t_, i);
154 }
155
156 template<class Index>
157 decltype(auto) operator[](const Index& i)
158 {
159 return Backend::getEntry(t_, i);
160 }
161
162private:
163 T t_;
164};
165
166} // namespace Impl
167
168
169
182template<class T>
183auto flatVectorView(T& t)
184{
185 return Impl::FlatVectorView<T>(t);
186}
187
200template<class T>
201auto flatVectorView(const T& t)
202{
203 return Impl::FlatVectorView<const T>(t);
204}
205
218template<class T>
219auto flatVectorView(T&& t)
220{
221 return Impl::FlatVectorView<T&&>(std::move(t));
222}
223
224
225} // namespace Dune::Functions
226} // namespace Dune
227
228
229#endif // DUNE_FUNCTIONS_FUNCTIONSPACEBASES_FLATVECTORVIEW_HH
Definition: polynomial.hh:17
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Aug 14, 22:29, 2024)