DUNE PDELab (2.8)

uncachedvectorview.hh
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3#ifndef DUNE_PDELAB_BACKEND_COMMON_UNCACHEDVECTORVIEW_HH
4#define DUNE_PDELAB_BACKEND_COMMON_UNCACHEDVECTORVIEW_HH
5
8
9#include<memory>
10
11
12namespace Dune {
13 namespace PDELab {
14
15
16 template<typename V, typename LFSC>
17 struct ConstUncachedVectorView
18 {
19
20 typedef typename std::remove_const<V>::type Container;
21 typedef LFSC LFSCache;
22
23 typedef typename Container::E ElementType;
24 typedef typename Container::size_type size_type;
25 typedef typename LFSCache::DOFIndex DOFIndex;
26 typedef typename LFSCache::ContainerIndex ContainerIndex;
27
28
29 ConstUncachedVectorView()
30 : _container(nullptr)
31 , _lfs_cache(nullptr)
32 {}
33
34 ConstUncachedVectorView(V& container)
35 : _container(&container)
36 , _lfs_cache(nullptr)
37 {}
38
39 ConstUncachedVectorView(std::shared_ptr<V> container)
40 : _container(container.get())
41 , _lfs_cache(nullptr)
42 {}
43
44 void attach(V& container)
45 {
46 _container = &container;
47 }
48
49 void attach(std::shared_ptr<V> container)
50 {
51 _container = container.get();
52 }
53
54 void detach()
55 {
56 _container = nullptr;
57 }
58
59 void bind(const LFSCache& lfs_cache)
60 {
61 _lfs_cache = &lfs_cache;
62 }
63
64 void unbind()
65 {
66 }
67
68 size_type size() const
69 {
70 return cache().size();
71 }
72
73 template<typename LC>
74 void read(LC& local_container) const
75 {
76 for (size_type i = 0; i < size(); ++i)
77 {
78 accessBaseContainer(local_container)[i] = container()[cache().containerIndex(i)];
79 }
80 }
81
82 template<typename ChildLFS, typename LC>
83 void read(const ChildLFS& child_lfs, LC& local_container) const
84 {
85 for (size_type i = 0; i < child_lfs.size(); ++i)
86 {
87 const size_type local_index = child_lfs.localIndex(i);
88 accessBaseContainer(local_container)[local_index] = container()[cache().containerIndex(local_index)];
89 }
90 }
91
92 template<typename ChildLFS, typename LC>
93 void read_sub_container(const ChildLFS& child_lfs, LC& local_container) const
94 {
95 for (size_type i = 0; i < child_lfs.size(); ++i)
96 {
97 const size_type local_index = child_lfs.localIndex(i);
98 accessBaseContainer(local_container)[i] = container()[cache().containerIndex(local_index)];
99 }
100 }
101
102
103 const ElementType& operator[](size_type i) const
104 {
105 return container()[cache().containerIndex(i)];
106 }
107
108
109 // disable this function if DOFIndex and ContainerIndex have the same type - required for interoperability
110 // with function spaces based on dune-functions bases
111 template<typename DI>
112 std::enable_if_t<
113 (std::is_same<DI,DOFIndex>{} and not std::is_same<DI,ContainerIndex>{}),
114 const ElementType&
115 >
116 operator[](const DI& di) const
117 {
118 return container()[cache().containerIndex(di)];
119 }
120
121
122 const ElementType& operator[](const ContainerIndex& ci) const
123 {
124 return container()[ci];
125 }
126
127
128 const Container& container() const
129 {
130 return *_container;
131 }
132
133 const LFSCache& cache() const
134 {
135 return *_lfs_cache;
136 }
137
138 protected:
139
140 V* _container;
141 const LFSCache* _lfs_cache;
142
143 };
144
145
146 template<typename V, typename LFSC>
147 struct UncachedVectorView
148 : public ConstUncachedVectorView<V,LFSC>
149 {
150
151 typedef V Container;
152 typedef typename Container::ElementType ElementType;
153 typedef typename Container::size_type size_type;
154
155 typedef LFSC LFSCache;
156 typedef typename LFSCache::DOFIndex DOFIndex;
157 typedef typename LFSCache::ContainerIndex ContainerIndex;
158
159 using ConstUncachedVectorView<V,LFSC>::cache;
160 using ConstUncachedVectorView<V,LFSC>::size;
161
162 // Explicitly pull in operator[] from the base class to work around a problem
163 // with clang not finding the const overloads of the operator from the base class.
164 using ConstUncachedVectorView<V,LFSC>::operator[];
165
166 UncachedVectorView()
167 {}
168
169 UncachedVectorView(Container& container)
170 : ConstUncachedVectorView<V,LFSC>(container)
171 {}
172
173 UncachedVectorView(std::shared_ptr<Container> container)
174 : ConstUncachedVectorView<V,LFSC>(container)
175 {}
176
177 template<typename LC>
178 void write(const LC& local_container)
179 {
180 for (size_type i = 0; i < size(); ++i)
181 {
182 container()[cache().containerIndex(i)] = accessBaseContainer(local_container)[i];
183 }
184 }
185
186 template<typename LC>
187 void add(const LC& local_container)
188 {
189 for (size_type i = 0; i < size(); ++i)
190 {
191 container()[cache().containerIndex(i)] += accessBaseContainer(local_container)[i];
192 }
193 }
194
195
196
197 template<typename ChildLFS, typename LC>
198 void write(const ChildLFS& child_lfs, const LC& local_container)
199 {
200 for (size_type i = 0; i < child_lfs.size(); ++i)
201 {
202 const size_type local_index = child_lfs.localIndex(i);
203 container()[cache().containerIndex(local_index)] = accessBaseContainer(local_container)[local_index];
204 }
205 }
206
207 template<typename ChildLFS, typename LC>
208 void add(const ChildLFS& child_lfs, const LC& local_container)
209 {
210 for (size_type i = 0; i < child_lfs.size(); ++i)
211 {
212 const size_type local_index = child_lfs.localIndex(i);
213 container()[cache().containerIndex(local_index)] += accessBaseContainer(local_container)[local_index];
214 }
215 }
216
217
218
219
220 template<typename ChildLFS, typename LC>
221 void write_sub_container(const ChildLFS& child_lfs, const LC& local_container)
222 {
223 for (size_type i = 0; i < child_lfs.size(); ++i)
224 {
225 const size_type local_index = child_lfs.localIndex(i);
226 container()[cache().containerIndex(local_index)] = accessBaseContainer(local_container)[i];
227 }
228 }
229
230 template<typename ChildLFS, typename LC>
231 void add_sub_container(const ChildLFS& child_lfs, const LC& local_container)
232 {
233 for (size_type i = 0; i < child_lfs.size(); ++i)
234 {
235 const size_type local_index = child_lfs.localIndex(i);
236 container()[cache().containerIndex(local_index)] += accessBaseContainer(local_container)[i];
237 }
238 }
239
240 void commit()
241 {
242 }
243
244
245 ElementType& operator[](size_type i)
246 {
247 return container()[cache().containerIndex(i)];
248 }
249
250 // disable this function if DOFIndex and ContainerIndex have the same type - required for interoperability
251 // with function spaces based on dune-functions bases
252 template<typename DI>
253 std::enable_if_t<
254 (std::is_same<DI,DOFIndex>{} and not std::is_same<DI,ContainerIndex>{}),
255 ElementType&
256 >
257 operator[](const DOFIndex& di)
258 {
259 return container()[cache().containerIndex(di)];
260 }
261
262
263 ElementType& operator[](const ContainerIndex& ci)
264 {
265 return container()[ci];
266 }
267
268
269 Container& container()
270 {
271 return *(this->_container);
272 }
273
274
275 };
276
277 } // namespace PDELab
278} // namespace Dune
279
280#endif // DUNE_PDELAB_BACKEND_COMMON_UNCACHEDVECTORVIEW_HH
Traits for type conversions and type information.
Dune namespace.
Definition: alignedallocator.hh:11
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Dec 22, 23:30, 2024)