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