dune-fem  2.8-git
subvector.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_SUBVECTOR_HH
2 #define DUNE_FEM_SUBVECTOR_HH
3 
4 #include <algorithm>
5 
6 #include <dune/common/densevector.hh>
7 #include <dune/common/ftraits.hh>
8 
10 
11 namespace 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  {
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  {
48 
49  public:
51  typedef IM IndexMapperType;
52 
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  {
124 
125  public:
126  StaticOffsetSubMapper( unsigned int offset )
127  : offset_( offset )
128  {}
129 
130  StaticOffsetSubMapper( const 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  {
163  typedef DenseVector< ThisType > BaseType;
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 
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 
202  {}
203 
204  const value_type& operator[]( size_type i ) const
205  {
206  return baseVector_[ indexMapper_[ i ] ];
207  }
208 
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
Definition: bindguard.hh:11
Definition: bartonnackmaninterface.hh:17
const Implementation & asImp() const
Definition: bartonnackmaninterface.hh:37
An implementation of DenseVector to extract a portion, not necessarly contiguos, of a vector.
Definition: subvector.hh:161
void resize(size_type)
Definition: subvector.hh:201
SubVector(const ThisType &other)
Definition: subvector.hh:185
ThisType & operator=(const ThisType &other)
Copy entries.
Definition: subvector.hh:190
BaseType::value_type value_type
Definition: subvector.hh:167
BaseType::size_type size_type
Definition: subvector.hh:166
size_type size() const
Definition: subvector.hh:214
void clear()
Definition: subvector.hh:196
BaseVectorImp BaseVectorType
Type of the base vector.
Definition: subvector.hh:172
value_type & operator[](size_type i)
Definition: subvector.hh:209
value_type FieldType
Type of vector elements.
Definition: subvector.hh:178
const value_type & operator[](size_type i) const
Definition: subvector.hh:204
IndexMapperImp IndexMapperType
Type of the index mapper.
Definition: subvector.hh:175
SubVector(BaseVectorType &baseVector, IndexMapperType &&indexMapper)
Constructor.
Definition: subvector.hh:181
std::decay_t< decltype(std::declval< K >).size()) > size_type
Definition: subvector.hh:27
std::decay_t< decltype(std::declval< K >)[std::declval< size_type >)]) > value_type
Definition: subvector.hh:28
Fem::SubVector< K, M > derived_type
Definition: subvector.hh:24
FieldTraits< typename DenseMatVecTraits< Fem::SubVector< K, M > >::value_type >::real_type real_type
Definition: subvector.hh:35
FieldTraits< typename DenseMatVecTraits< Fem::SubVector< K, M > >::value_type >::field_type field_type
Definition: subvector.hh:34
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
static const Implementation & asImp(const ThisType &other)
Definition: bartonnackmaninterface.hh:27
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
OffsetSubMapper(ThisType &&)=default
OffsetSubMapper(unsigned int size, unsigned int offset)
Definition: subvector.hh:88
unsigned int size() const
Definition: subvector.hh:95
unsigned int operator[](unsigned int i) const
Definition: subvector.hh:105
unsigned int range() const
Definition: subvector.hh:100
OffsetSubMapper(const ThisType &)=default
Index mapper with static size which simply adds an offset to the index.
Definition: subvector.hh:121
StaticOffsetSubMapper(ThisType &&)=default
static constexpr unsigned int size()
Definition: subvector.hh:133
StaticOffsetSubMapper(const ThisType &)=default
StaticOffsetSubMapper(unsigned int offset)
Definition: subvector.hh:126
unsigned int operator[](unsigned int i) const
Definition: subvector.hh:143
static constexpr unsigned int range()
Definition: subvector.hh:138