dune-pdelab  2.7-git
uncachedvectorview.hh
Go to the documentation of this file.
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 
6 #include <dune/common/typetraits.hh>
8 
9 #include<memory>
10 
11 
12 namespace Dune {
13  namespace PDELab {
14 
15 
16  template<typename V, typename LFSC>
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 
30  : _container(nullptr)
31  , _lfs_cache(nullptr)
32  {}
33 
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  {
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 
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 
142 
143  };
144 
145 
146  template<typename V, typename LFSC>
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 
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.
165 
167  {}
168 
171  {}
172 
173  UncachedVectorView(std::shared_ptr<Container> 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 
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 
264  {
265  return container()[ci];
266  }
267 
268 
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
C & accessBaseContainer(C &c)
Definition: localvector.hh:368
For backward compatibility – Do not use this!
Definition: adaptivity.hh:28
Definition: uncachedvectorview.hh:18
void unbind()
Definition: uncachedvectorview.hh:64
void attach(V &container)
Definition: uncachedvectorview.hh:44
void detach()
Definition: uncachedvectorview.hh:54
const Container & container() const
Definition: uncachedvectorview.hh:128
const ElementType & operator[](const ContainerIndex &ci) const
Definition: uncachedvectorview.hh:122
const ElementType & operator[](size_type i) const
Definition: uncachedvectorview.hh:103
Container::E ElementType
Definition: uncachedvectorview.hh:23
size_type size() const
Definition: uncachedvectorview.hh:68
Container::size_type size_type
Definition: uncachedvectorview.hh:24
ConstUncachedVectorView(std::shared_ptr< V > container)
Definition: uncachedvectorview.hh:39
ConstUncachedVectorView(V &container)
Definition: uncachedvectorview.hh:34
LFSCache::DOFIndex DOFIndex
Definition: uncachedvectorview.hh:25
LFSC LFSCache
Definition: uncachedvectorview.hh:21
const LFSCache * _lfs_cache
Definition: uncachedvectorview.hh:141
std::remove_const< V >::type Container
Definition: uncachedvectorview.hh:20
void attach(std::shared_ptr< V > container)
Definition: uncachedvectorview.hh:49
void read(const ChildLFS &child_lfs, LC &local_container) const
Definition: uncachedvectorview.hh:83
void bind(const LFSCache &lfs_cache)
Definition: uncachedvectorview.hh:59
void read(LC &local_container) const
Definition: uncachedvectorview.hh:74
const LFSCache & cache() const
Definition: uncachedvectorview.hh:133
V * _container
Definition: uncachedvectorview.hh:140
ConstUncachedVectorView()
Definition: uncachedvectorview.hh:29
LFSCache::ContainerIndex ContainerIndex
Definition: uncachedvectorview.hh:26
void read_sub_container(const ChildLFS &child_lfs, LC &local_container) const
Definition: uncachedvectorview.hh:93
Definition: uncachedvectorview.hh:149
UncachedVectorView(Container &container)
Definition: uncachedvectorview.hh:169
void write(const ChildLFS &child_lfs, const LC &local_container)
Definition: uncachedvectorview.hh:198
LFSC LFSCache
Definition: uncachedvectorview.hh:155
LFSCache::ContainerIndex ContainerIndex
Definition: uncachedvectorview.hh:157
void commit()
Definition: uncachedvectorview.hh:240
UncachedVectorView()
Definition: uncachedvectorview.hh:166
V Container
Definition: uncachedvectorview.hh:151
ElementType & operator[](size_type i)
Definition: uncachedvectorview.hh:245
void write_sub_container(const ChildLFS &child_lfs, const LC &local_container)
Definition: uncachedvectorview.hh:221
void add_sub_container(const ChildLFS &child_lfs, const LC &local_container)
Definition: uncachedvectorview.hh:231
void add(const ChildLFS &child_lfs, const LC &local_container)
Definition: uncachedvectorview.hh:208
ElementType & operator[](const ContainerIndex &ci)
Definition: uncachedvectorview.hh:263
Container::ElementType ElementType
Definition: uncachedvectorview.hh:152
LFSCache::DOFIndex DOFIndex
Definition: uncachedvectorview.hh:156
Container::size_type size_type
Definition: uncachedvectorview.hh:153
UncachedVectorView(std::shared_ptr< Container > container)
Definition: uncachedvectorview.hh:173
void add(const LC &local_container)
Definition: uncachedvectorview.hh:187
void write(const LC &local_container)
Definition: uncachedvectorview.hh:178
Container & container()
Definition: uncachedvectorview.hh:269