DUNE-FEM (unstable)

subvector.hh
1#ifndef DUNE_FEM_SUBVECTOR_HH
2#define DUNE_FEM_SUBVECTOR_HH
3
4#include <algorithm>
5
8
9#include <dune/fem/misc/bartonnackmaninterface.hh>
10
11namespace Dune
12{
13
14 namespace Fem
15 {
16 // forward declaration
17 template< class K, class M > class SubVector;
18 }
19
20 // specialization of DenseMatVecTraits for SubVector
21 template< class K, class M >
22 struct DenseMatVecTraits< Fem::SubVector< K, M > >
23 {
24 typedef Fem::SubVector< K, M > derived_type;
25 typedef K container_type;
26
27 typedef std::decay_t< decltype( std::declval< K >().size() ) > size_type;
28 typedef std::decay_t< decltype( std::declval< K >()[ std::declval< size_type >() ] ) > value_type;
29 };
30
31 template< class K, class M >
32 struct FieldTraits< Fem::SubVector< K, M > >
33 {
34 typedef typename FieldTraits< typename DenseMatVecTraits< Fem::SubVector< K, M > >::value_type >::field_type field_type;
35 typedef typename FieldTraits< typename DenseMatVecTraits< Fem::SubVector< K, M > >::value_type >::real_type real_type;
36 };
37
38 namespace Fem
39 {
40
42 template< class IM >
44 : public BartonNackmanInterface< IndexMapperInterface< IM >, IM >
45 {
46 typedef IndexMapperInterface< IM > ThisType;
47 typedef BartonNackmanInterface< ThisType, IM > BaseType;
48
49 public:
51 typedef IM IndexMapperType;
52
54 typedef ThisType IndexMapperInterfaceType;
55
57 unsigned int operator[] ( unsigned int index ) const
58 {
59 return asImp().operator[]( index );
60 }
61
63 unsigned int range () const
64 {
65 return asImp().range();
66 }
67
69 unsigned int size () const
70 {
71 return asImp().size();
72 }
73
74 protected:
75 using BaseType::asImp;
76 };
77
78
79
82 : public IndexMapperInterface< OffsetSubMapper >
83 {
84 typedef OffsetSubMapper ThisType;
86
87 public:
88 OffsetSubMapper( unsigned int size, unsigned int offset )
89 : size_( size ), offset_( offset )
90 {}
91
92 OffsetSubMapper( const ThisType& ) = default;
93 OffsetSubMapper( ThisType&& ) = default;
94
95 unsigned int size() const
96 {
97 return size_;
98 }
99
100 unsigned int range() const
101 {
102 return size_;
103 }
104
105 unsigned int operator[]( unsigned int i) const
106 {
107 return i+offset_;
108 }
109
110 private:
111 const unsigned int size_;
112 const unsigned int offset_;
113 };
114
115
116
118 template<unsigned int dim>
120 : public IndexMapperInterface< StaticOffsetSubMapper< dim > >
121 {
122 typedef StaticOffsetSubMapper< dim > ThisType;
124
125 public:
126 StaticOffsetSubMapper( unsigned int offset )
127 : offset_( offset )
128 {}
129
130 StaticOffsetSubMapper( const ThisType& ) = default;
131 StaticOffsetSubMapper( ThisType&& ) = default;
132
133 static constexpr unsigned int size()
134 {
135 return dim;
136 }
137
138 static constexpr unsigned int range()
139 {
140 return dim;
141 }
142
143 unsigned int operator[]( unsigned int i) const
144 {
145 return i+offset_;
146 }
147
148 private:
149 const unsigned int offset_;
150 };
151
152
153
159 template< class BaseVectorImp, class IndexMapperImp >
160 class SubVector : public DenseVector< SubVector< BaseVectorImp, IndexMapperImp > >
161 {
164
165 public:
166 typedef typename BaseType::size_type size_type;
167 typedef typename BaseType::value_type value_type;
168
169 using BaseType::operator=;
170
172 typedef BaseVectorImp BaseVectorType;
173
175 typedef IndexMapperImp IndexMapperType;
176
178 typedef value_type FieldType;
179
181 explicit SubVector( BaseVectorType& baseVector, IndexMapperType&& indexMapper )
182 : baseVector_( baseVector ), indexMapper_( indexMapper )
183 {}
184
185 SubVector( const ThisType & other )
186 : baseVector_( other.baseVector_ ), indexMapper_( other.indexMapper_ )
187 {}
188
190 ThisType& operator=( const ThisType & other)
191 {
192 std::copy( other.begin(), other.end(), this->begin() );
193 return *this;
194 }
195
196 void clear()
197 {
198 std::fill( this->begin(), this->end(), FieldType(0) );
199 }
200
201 void resize( size_type )
202 {}
203
204 const value_type& operator[]( size_type i ) const
205 {
206 return baseVector_[ indexMapper_[ i ] ];
207 }
208
209 value_type& operator[]( size_type i )
210 {
211 return baseVector_[ indexMapper_[ i ] ];
212 }
213
214 size_type size() const
215 {
216 return indexMapper_.size();
217 }
218
219 private:
220 BaseVectorType& baseVector_;
221 IndexMapperType indexMapper_;
222 };
223
224
225 } // namespace Fem
226
227} // namespace Dune
228
229#endif
Interface for a class of dense vectors over a given field.
Definition: densevector.hh:229
Traits::value_type value_type
export the type representing the field
Definition: densevector.hh:250
Iterator begin()
begin iterator
Definition: densevector.hh:347
Iterator end()
end iterator
Definition: densevector.hh:353
Traits::size_type size_type
The type used for the index access and size operation.
Definition: densevector.hh:259
Abstract index mapper interface.
Definition: subvector.hh:45
unsigned int operator[](unsigned int index) const
Maps an index onto another one.
Definition: subvector.hh:57
unsigned int size() const
Returns the map's size.
Definition: subvector.hh:69
ThisType IndexMapperInterfaceType
Type of the interface.
Definition: subvector.hh:54
unsigned int range() const
Returns the map's range.
Definition: subvector.hh:63
IM IndexMapperType
Type of the implementation (Barton-Nackman)
Definition: subvector.hh:51
Index mapper which simply adds an offset to the index.
Definition: subvector.hh:83
Index mapper with static size which simply adds an offset to the index.
Definition: subvector.hh:121
An implementation of DenseVector to extract a portion, not necessarly contiguos, of a vector.
Definition: subvector.hh:161
ThisType & operator=(const ThisType &other)
Copy entries.
Definition: subvector.hh:190
BaseVectorImp BaseVectorType
Type of the base vector.
Definition: subvector.hh:172
value_type FieldType
Type of vector elements.
Definition: subvector.hh:178
IndexMapperImp IndexMapperType
Type of the index mapper.
Definition: subvector.hh:175
SubVector(BaseVectorType &baseVector, IndexMapperType &&indexMapper)
Constructor.
Definition: subvector.hh:181
Implements the dense vector interface, with an exchangeable storage class.
Type traits to determine the type of reals (when working with complex numbers)
Dune namespace.
Definition: alignedallocator.hh:13
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 21, 23:30, 2024)