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