DUNE PDELab (git)

aliasedvectorview.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_ALIASEDVECTORVIEW_HH
4#define DUNE_PDELAB_BACKEND_COMMON_ALIASEDVECTORVIEW_HH
5
8
9#include <memory>
10
11
12namespace Dune {
13 namespace PDELab {
14
15
16 template<typename V, typename LFSC>
17 struct ConstAliasedVectorView
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 using value_type = ElementType;
29
30
31 ConstAliasedVectorView()
32 : _container(nullptr)
33 , _lfs_cache(nullptr)
34 , _data(nullptr)
35 {}
36
37 ConstAliasedVectorView(V& container)
38 : _container(&container)
39 , _lfs_cache(nullptr)
40 , _data(nullptr)
41 {}
42
43 ConstAliasedVectorView(std::shared_ptr<V> container)
44 : _container(container.get())
45 , _lfs_cache(nullptr)
46 , _data(nullptr)
47 {}
48
49 void attach(V& container)
50 {
51 _container = &container;
52 }
53
54 void attach(std::shared_ptr<V> container)
55 {
56 _container = container.get();
57 }
58
59 void detach()
60 {
61 _container = nullptr;
62 }
63
64 void bind(const LFSCache& lfs_cache)
65 {
66 _lfs_cache = &lfs_cache;
67 _data = _container->data(lfs_cache);
68 }
69
70 const ElementType* data() const
71 {
72 return _data;
73 }
74
75 void unbind()
76 {
77 _lfs_cache = nullptr;
78 _data = nullptr;
79 }
80
81 size_type size() const
82 {
83 return cache().size();
84 }
85
86 const ElementType& operator[](size_type i) const
87 {
88 return _data[i];
89 }
90
91 const ElementType& operator[](const ContainerIndex& ci) const
92 {
93 return container()[ci];
94 }
95
96 template<typename LFS>
97 const ElementType& operator()(const LFS& lfs, size_type i) const
98 {
99 return this->_data[lfs.localIndex(i)];
100 }
101
102 const Container& container() const
103 {
104 return *_container;
105 }
106
107 const LFSCache& cache() const
108 {
109 return *_lfs_cache;
110 }
111
112 protected:
113
114 V* _container;
115 const LFSCache* _lfs_cache;
116 typename std::conditional<
117 std::is_const<V>::value,
118 const ElementType*,
119 ElementType*
120 >::type _data;
121
122 };
123
124
125 template<typename V, typename LFSC>
126 struct AliasedVectorView
127 : public ConstAliasedVectorView<V,LFSC>
128 {
129
130 typedef V Container;
131 typedef typename Container::ElementType ElementType;
132 typedef typename Container::size_type size_type;
133
134 typedef LFSC LFSCache;
135 typedef typename LFSCache::DOFIndex DOFIndex;
136 typedef typename LFSCache::ContainerIndex ContainerIndex;
137
138 using value_type = ElementType;
139 using weight_type = ElementType;
140
141 using ConstAliasedVectorView<V,LFSC>::cache;
142 using ConstAliasedVectorView<V,LFSC>::size;
143
144 // Explicitly pull in operator[] from the base class to work around a problem
145 // with clang not finding the const overloads of the operator from the base class.
146 using ConstAliasedVectorView<V,LFSC>::operator[];
147
148 // pull in const version of data access
149 using ConstAliasedVectorView<V,LFSC>::data;
150
151 AliasedVectorView()
152 : weight_(1.0)
153 {}
154
155 AliasedVectorView(Container& container)
156 : ConstAliasedVectorView<V,LFSC>(container)
157 , weight_(1.0)
158 {}
159
160 AliasedVectorView(std::shared_ptr<Container> container)
161 : ConstAliasedVectorView<V,LFSC>(container)
162 , weight_(1.0)
163 {}
164
165 void commit()
166 {}
167
168 template<typename LFS>
169 void accumulate(const LFS& lfs, size_type n, value_type v)
170 {
171 this->_data[lfs.localIndex(n)] += weight_ * v;
172 }
173
174 template<typename LFS>
175 void rawAccumulate(const LFS& lfs, size_type n, value_type v)
176 {
177 accumulate(lfs,n,v);
178 }
179
180 ElementType& operator[](size_type i)
181 {
182 return this->_data[i];
183 }
184
185 ElementType& operator[](const ContainerIndex& ci)
186 {
187 return container()[ci];
188 }
189
190 ElementType* data()
191 {
192 return this->_data;
193 }
194
195 const ElementType* data() const
196 {
197 return this->_data;
198 }
199
200 Container& container()
201 {
202 return *(this->_container);
203 }
204
205 void setWeight(weight_type weight)
206 {
207 weight_ = weight;
208 }
209
210 weight_type weight()
211 {
212 return weight_;
213 }
214
215 private :
216 weight_type weight_;
217 };
218
219 } // namespace PDELab
220} // namespace Dune
221
222#endif // DUNE_PDELAB_BACKEND_COMMON_ALIASEDVECTORVIEW_HH
Traits for type conversions and type information.
constexpr T accumulate(Range &&range, T value, F &&f)
Accumulate values.
Definition: hybridutilities.hh:279
Dune namespace.
Definition: alignedallocator.hh:13
constexpr std::integral_constant< std::size_t, sizeof...(II)> size(std::integer_sequence< T, II... >)
Return the size of the sequence.
Definition: integersequence.hh:75
constexpr auto get(std::integer_sequence< T, II... >, std::integral_constant< std::size_t, pos >={})
Return the entry at position pos of the given sequence.
Definition: integersequence.hh:22
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 12, 23:30, 2024)