dune-pdelab  2.0.0
localvector.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_LOCALVECTOR_HH
4 #define DUNE_PDELAB_LOCALVECTOR_HH
5 
6 #include <vector>
7 #include <algorithm>
8 #include <functional>
10 
16 namespace Dune {
17  namespace PDELab {
18 
24  template<typename C>
27  {
28  public:
29 
31  typedef C Container;
32 
34  typedef typename Container::BaseContainer BaseContainer;
35 
37  typedef typename Container::value_type value_type;
38 
40  typedef typename Container::weight_type weight_type;
41 
45 
49  {
50  return WeightedAccumulationView(container(),weight*this->weight());
51  }
52 
54  typedef typename Container::size_type size_type;
55 
57 
62  {
63  _modified = true;
64  return _weight;
65  }
66 
68 
73  {
74  _weight = weight;
75  }
76 
78  template<typename LFS>
79  void accumulate(const LFS& lfs, size_type n, value_type v)
80  {
81  _modified = true;
82  _container(lfs,n) += _weight * v;
83  }
84 
86 
90  template<typename LFS>
91  void rawAccumulate(const LFS& lfs, size_type n, value_type v)
92  {
93  _modified = true;
94  _container(lfs,n) += v;
95  }
96 
99  : _container(container)
100  , _weight(weight)
101  , _modified(false)
102  {}
103 
105  size_type size() const
106  {
107  return _container.size();
108  }
109 
111  bool modified() const
112  {
113  return _modified;
114  }
115 
117 
122  {
123  _modified = false;
124  }
125 
128  {
129  _modified = true;
130  return _container;
131  }
132 
134  const Container& container() const
135  {
136  return _container;
137  }
138 
141  {
142  _modified = true;
143  return _container.base();
144  }
145 
147  const BaseContainer& base() const
148  {
149  return _container.base();
150  }
151 
152  private:
153  C& _container;
154  weight_type _weight;
155  bool _modified;
156  };
157 
158 
160 
170  template<typename T, typename LFSFlavorTag = AnySpaceTag, typename W = T>
172  {
173  public:
174 
176  typedef std::vector<T> BaseContainer;
177 
179  typedef typename BaseContainer::value_type value_type;
180 
182  typedef typename BaseContainer::size_type size_type;
183 
185  typedef typename BaseContainer::reference reference;
186 
188  typedef typename BaseContainer::const_reference const_reference;
189 
191 
195  typedef W weight_type;
196 
199 
202  {
203  return WeightedAccumulationView(*this,weight);
204  }
205 
207 
212  template<typename LFS>
213  reference operator()(const LFS& lfs, size_type i)
214  {
215  return _container[lfs.localIndex(i)];
216  }
217 
219 
224  template<typename LFS>
225  const_reference operator()(const LFS& lfs, size_type i) const
226  {
227  return _container[lfs.localIndex(i)];
228  }
229 
232  {
233  std::fill(_container.begin(),_container.end(),v);
234  return *this;
235  }
236 
239  {
240  std::transform(_container.begin(),_container.end(),_container.begin(),std::bind1st(std::multiplies<value_type>(),v));
241  return *this;
242  }
243 
245  size_type size() const
246  {
247  return _container.size();
248  }
249 
252  {
253  _container.resize(size);
254  }
255 
257  void assign(size_type size, const T& value)
258  {
259  _container.assign(size,value);
260  }
261 
264  {
265  return _container;
266  }
267 
269  const BaseContainer& base() const
270  {
271  return _container;
272  }
273 
276  {}
277 
279  explicit LocalVector(size_type n)
280  : _container(n)
281  {}
282 
285  : _container(n,v)
286  {}
287 
288  private:
289 
290  BaseContainer _container;
291 
292  };
293 
294 
295  template<typename C>
297  {
298  return c;
299  }
300 
301  template<typename T, typename Tag, typename W>
303  {
304  return c.base();
305  }
306 
307  template<typename C>
308  typename WeightedVectorAccumulationView<C>::BaseContainer& accessBaseContainer
310  {
311  return c.base();
312  }
313 
314  template<typename C>
315  const C& accessBaseContainer(const C& c)
316  {
317  return c;
318  }
319 
320  template<typename T, typename Tag, typename W>
322  {
323  return c.base();
324  }
325 
326  template<typename C>
327  const typename WeightedVectorAccumulationView<C>::BaseContainer& accessBaseContainer
329  {
330  return c.base();
331  }
332 
337  } // end namespace PDELab
338 } // end namespace Dune
339 
340 #endif // DUNE_PDELAB_LOCALVECTOR_HH
WeightedAccumulationView weightedAccumulationView(weight_type weight)
Returns a WeighedAccumulationView of this container with the given weight.
Definition: localvector.hh:201
BaseContainer::const_reference const_reference
The const reference type of this container.
Definition: localvector.hh:188
bool modified() const
Returns whether this view has been written to.
Definition: localvector.hh:111
WeightedVectorAccumulationView(C &container, weight_type weight)
Constructor.
Definition: localvector.hh:98
WeightedVectorAccumulationView WeightedAccumulationView
Export this type for uniform handling of the containers themselves and their views.
Definition: localvector.hh:44
reference operator()(const LFS &lfs, size_type i)
Access the value in this container associated with the i-th degree of freedom of the LocalFunctionSpa...
Definition: localvector.hh:213
Container & container()
Returns the container (of type LocalVector) that this view is based on.
Definition: localvector.hh:127
Container::size_type size_type
The size_type of the underlying container.
Definition: localvector.hh:54
C & accessBaseContainer(C &c)
Definition: localvector.hh:296
Container::BaseContainer BaseContainer
The type of the storage container underlying the LocalVector.
Definition: localvector.hh:34
BaseContainer::value_type value_type
The value type of this container.
Definition: localvector.hh:179
BaseContainer & base()
Returns the underlying, std::vector-like storage container.
Definition: localvector.hh:263
LocalVector()
Default constructor.
Definition: localvector.hh:275
size_type size() const
Returns the size of the underlying container.
Definition: localvector.hh:105
const BaseContainer & base() const
Returns the underlying, std::vector-like storage container (const version).
Definition: localvector.hh:269
const BaseContainer & base() const
Returns the storage container of the underlying LocalVector (const version).
Definition: localvector.hh:147
C Container
The type of the underlying LocalVector.
Definition: localvector.hh:31
const Container & container() const
Returns the container (of type LocalVector) that this view is based on (const version).
Definition: localvector.hh:134
LocalVector & operator*=(const value_type &v)
Multiplies all entries by v.
Definition: localvector.hh:238
void rawAccumulate(const LFS &lfs, size_type n, value_type v)
Adds v to the n-th degree of freedom of the lfs without applying the current weight.
Definition: localvector.hh:91
size_type size() const
The size of the container.
Definition: localvector.hh:245
BaseContainer::reference reference
The reference type of this container.
Definition: localvector.hh:185
Container::value_type value_type
The value type of the entries.
Definition: localvector.hh:37
void assign(size_type size, const T &value)
Resize the container to size and assign the passed value to all entries.
Definition: localvector.hh:257
WeightedVectorAccumulationView< LocalVector > WeightedAccumulationView
An accumulate-only view of this container that automatically applies a weight to all contributions...
Definition: localvector.hh:198
W weight_type
The weight type of this container.
Definition: localvector.hh:195
void accumulate(const LFS &lfs, size_type n, value_type v)
Applies the current weight to v and adds the result to the n-th degree of freedom of the lfs...
Definition: localvector.hh:79
LocalVector & operator=(const value_type &v)
Assigns v to all entries.
Definition: localvector.hh:231
WeightedAccumulationView weightedAccumulationView(weight_type weight)
Returns a WeighedAccumulationView with some weight in addition to this view's weight.
Definition: localvector.hh:48
static const unsigned int value
Definition: gridfunctionspace/tags.hh:175
void setWeight(weight_type weight)
Resets the weighting coefficient of the view.
Definition: localvector.hh:72
LocalVector(size_type n)
Construct a LocalVector with size n.
Definition: localvector.hh:279
const_reference operator()(const LFS &lfs, size_type i) const
Access the value in this container associated with the i-th degree of freedom of the LocalFunctionSpa...
Definition: localvector.hh:225
std::vector< T > BaseContainer
The type of the underlying storage container.
Definition: localvector.hh:176
A container for storing data associated with the degrees of freedom of a LocalFunctionSpace.
Definition: localvector.hh:171
weight_type weight()
Returns the weight associated with this view.
Definition: localvector.hh:61
LocalVector(size_type n, const value_type &v)
Construct a LocalVector with size n and initialize all entries with v.
Definition: localvector.hh:284
BaseContainer::size_type size_type
The size type of this container.
Definition: localvector.hh:182
void resize(size_type size)
Resize the container.
Definition: localvector.hh:251
Container::weight_type weight_type
The type of the weight applied when accumulating contributions.
Definition: localvector.hh:40
An accumulate-only view on a local vector that automatically takes into account an accumulation weigh...
Definition: localvector.hh:26
void resetModified()
Resets the modification state of the view to not modified.
Definition: localvector.hh:121
BaseContainer & base()
Returns the storage container of the underlying LocalVector.
Definition: localvector.hh:140