dune-fem  2.4.1-rc
subarray.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_SUBARRAY_HH
2 #define DUNE_FEM_SUBARRAY_HH
3 
4 #include <dune/common/typetraits.hh>
5 
8 
9 namespace Dune
10 {
11  namespace Fem
12  {
13 
15  template< class IM >
17  : public BartonNackmanInterface< IndexMapperInterface< IM >, IM >
18  {
21 
22  public:
24  typedef IM IndexMapperType;
25 
27  typedef ThisType IndexMapperInterfaceType;
28 
29  public:
31  unsigned int operator[] ( unsigned int index ) const
32  {
33  return asImp().operator[]( index );
34  }
35 
37  unsigned int range () const
38  {
39  return asImp().range();
40  }
41 
43  unsigned int size () const
44  {
45  return asImp().size();
46  }
47 
48  protected:
49  using BaseType::asImp;
50  };
51 
52 
55  : public IndexMapperInterface< OffsetSubMapper >
56  {
57  typedef OffsetSubMapper ThisType;
59 
60  public:
61  OffsetSubMapper( unsigned int size, unsigned int offset )
62  : size_( size ), offset_( offset )
63  {}
64 
65  OffsetSubMapper( const ThisType& ) = default;
66  OffsetSubMapper( ThisType&& ) = default;
67  ThisType& operator=( const ThisType& ) = default;
68  ThisType& operator=( ThisType&& ) = default;
69 
70  unsigned int size() const
71  {
72  return size_;
73  }
74 
75  unsigned int range() const
76  {
77  return size_;
78  }
79 
80  unsigned int operator[]( unsigned int i) const
81  {
82  return i+offset_;
83  }
84 
85  private:
86  const unsigned int size_;
87  const unsigned int offset_;
88  };
89 
91  template<unsigned int dim>
93  : public IndexMapperInterface< StaticOffsetSubMapper< dim > >
94  {
97 
98  public:
99  StaticOffsetSubMapper( unsigned int offset )
100  : offset_( offset )
101  {}
102 
103  StaticOffsetSubMapper( const ThisType& ) = default;
104  StaticOffsetSubMapper( ThisType&& ) = default;
105  ThisType& operator=( const ThisType& ) = default;
106  ThisType& operator=( ThisType&& ) = default;
107 
108  static constexpr unsigned int size()
109  {
110  return dim;
111  }
112 
113  static constexpr unsigned int range()
114  {
115  return dim;
116  }
117 
118  unsigned int operator[]( unsigned int i) const
119  {
120  return i+offset_;
121  }
122 
123  private:
124  const unsigned int offset_;
125  };
126 
127 
128 
129  template< class IndexMapper >
131  {
133  static const bool v = Conversion< IndexMapper, IndexMapperInterfaceType >::exists;
134  };
135 
136 
137  // SubArray
138  template< class BaseArrayImp, class IndexMapperImp >
139  class SubArray
140  : public ArrayDefault< typename BaseArrayImp :: ElementType,
141  SubArray< BaseArrayImp, IndexMapperImp > >
142  {
143  public:
145  typedef BaseArrayImp BaseArrayType;
146 
148  typedef IndexMapperImp IndexMapperType;
149 
151  typedef typename BaseArrayType :: ElementType ElementType;
152 
153  private:
156 
157  private:
158  BaseArrayType &baseArray_;
159  const IndexMapperType &indexMapper_;
160 
161  public:
162  SubArray( BaseArrayType &baseArray, const IndexMapperType &indexMapper )
163  : baseArray_( baseArray ),
164  indexMapper_( indexMapper )
165  {
166  static_assert( SupportsArrayInterface< BaseArrayType >::v, "SubArray can only wrap arrays." );
167  static_assert( SupportsIndexMapperInterface< IndexMapperType >::v, "Invalid index mapper." );
168  assert( baseArray_.size() == indexMapper_.range() );
169  }
170 
171  SubArray ( const ThisType &other )
172  : baseArray_( other.baseArray_ ),
173  indexMapper_( other.indexMapper_ )
174  {}
175 
176  private:
177  ThisType &operator= ( const ThisType &other );
178 
179  public:
180  const ElementType &operator[] ( unsigned int index ) const
181  {
182  return baseArray_[ indexMapper_[ index ] ];
183  }
184 
185  ElementType &operator[] ( unsigned int index )
186  {
187  return baseArray_[ indexMapper_[ index ] ];
188  }
189 
190  unsigned int size () const
191  {
192  return indexMapper_.size();
193  }
194  };
195 
196 
197  // SubVector
198  template< class BaseVectorImp, class IndexMapperImp >
199  class SubVector
200  : public Fem :: VectorDefault< typename BaseVectorImp :: FieldType,
201  SubVector< BaseVectorImp, IndexMapperImp > >
202  {
203  public:
205  typedef BaseVectorImp BaseVectorType;
206 
208  typedef IndexMapperImp IndexMapperType;
209 
211  typedef typename BaseVectorType :: FieldType FieldType;
212 
213  private:
216 
217  private:
218  BaseVectorType &baseVector_;
219  const IndexMapperType &indexMapper_;
220 
221  public:
222  SubVector( BaseVectorType &baseVector, const IndexMapperType &indexMapper )
223  : baseVector_( baseVector ),
224  indexMapper_( indexMapper )
225  {
226  // used for StaticArray which only implements parts of the VectorInterface (axpy <-> addScaled for example)
227  // but SubVector obly requires parts of the VectorInterface (operator[]...)
228  // static_assert( SupportsVectorInterface< BaseVectorType >::v, "SubVector can only wrap vectors." );
229  // static_assert( SupportsIndexMapperInterface< IndexMapperType >::v, "Invalid index mapper." );
230 
231  assert( (unsigned int)baseVector_.size() == indexMapper_.range() );
232  }
233 
234 
235  private:
236  SubVector ( const ThisType & );
237  ThisType &operator= ( const ThisType & );
238 
239  public:
240  const FieldType &operator[] ( unsigned int index ) const
241  {
242  return baseVector_[ indexMapper_[ index ] ];
243  }
244 
245  FieldType &operator[] ( unsigned int index )
246  {
247  return baseVector_[ indexMapper_[ index ] ];
248  }
249 
250  unsigned int size () const
251  {
252  return indexMapper_.size();
253  }
254  };
255 
256  } // namespace Fem
257 
258 } // namespace Dune
259 
260 #endif // #ifndef DUNE_FEM_SUBARRAY_HH
unsigned int range() const
Definition: subarray.hh:75
unsigned int operator[](unsigned int index) const
Maps an index onto another one.
Definition: subarray.hh:31
BaseVectorType::FieldType FieldType
type of array elements
Definition: subarray.hh:211
unsigned int size() const
Definition: subarray.hh:70
Definition: subarray.hh:139
IM IndexMapperType
type of the implementation (Barton-Nackman)
Definition: subarray.hh:24
unsigned int size() const
Definition: subarray.hh:190
unsigned int size() const
Definition: subarray.hh:250
unsigned int size() const
Returns the map&#39;s size.
Definition: subarray.hh:43
Definition: subarray.hh:130
default implementation of VectorInterface
Definition: vector.hh:173
Index mapper with static size which simply adds an offset to the index.
Definition: subarray.hh:92
default implementation of the ArrayInterface
Definition: array.hh:248
static constexpr unsigned int range()
Definition: subarray.hh:113
const IM & asImp() const
Definition: bartonnackmaninterface.hh:37
static constexpr unsigned int size()
Definition: subarray.hh:108
SubArray(const ThisType &other)
Definition: subarray.hh:171
IndexMapperInterface< IndexMapper > IndexMapperInterfaceType
Definition: subarray.hh:132
ThisType IndexMapperInterfaceType
type of the interface
Definition: subarray.hh:27
unsigned int operator[](unsigned int i) const
Definition: subarray.hh:80
BaseArrayImp BaseArrayType
type of the base array
Definition: subarray.hh:145
Definition: array.hh:164
BaseVectorImp BaseVectorType
type of the base array
Definition: subarray.hh:205
Definition: coordinate.hh:4
BaseArrayType::ElementType ElementType
type of array elements
Definition: subarray.hh:151
unsigned int range() const
Returns the map&#39;s range.
Definition: subarray.hh:37
Index mapper which simply adds an offset to the index.
Definition: subarray.hh:54
Definition: subarray.hh:199
IndexMapperImp IndexMapperType
type of the index mapper
Definition: subarray.hh:208
SubVector(BaseVectorType &baseVector, const IndexMapperType &indexMapper)
Definition: subarray.hh:222
OffsetSubMapper(unsigned int size, unsigned int offset)
Definition: subarray.hh:61
StaticOffsetSubMapper(unsigned int offset)
Definition: subarray.hh:99
Abstract index mapper interface.
Definition: subarray.hh:16
SubArray(BaseArrayType &baseArray, const IndexMapperType &indexMapper)
Definition: subarray.hh:162
unsigned int operator[](unsigned int i) const
Definition: subarray.hh:118
IndexMapperImp IndexMapperType
type of the index mapper
Definition: subarray.hh:148
Definition: bartonnackmaninterface.hh:15