dune-fem  2.4.1-rc
vector_inline.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_VECTOR_INLINE_HH
2 #define DUNE_FEM_VECTOR_INLINE_HH
3 
4 #include "vector.hh"
5 
6 namespace Dune
7 {
8 
9  namespace Fem
10  {
11 
12  // VectorInterface
13  // ---------------
14 
15  template< class Traits >
16  template< class T >
17  inline typename Traits :: VectorType &
19  {
20  asImp().assign( v );
21  return asImp();
22  }
23 
24  template< class Traits >
25  inline typename Traits :: VectorType &
27  {
28  asImp().assign( v );
29  return asImp();
30  }
31 
32  template< class Traits >
33  inline typename Traits :: VectorType &
35  {
36  asImp().assign( s );
37  return asImp();
38  }
39 
40  template< class Traits >
41  inline const typename Traits :: FieldType &
42  VectorInterface< Traits > :: operator[] ( unsigned int index ) const
43  {
44  CHECK_INTERFACE_IMPLEMENTATION( asImp()[ index ] );
45  return asImp()[ index ];
46  }
47 
48  template< class Traits >
49  inline typename Traits :: FieldType &
51  {
52  CHECK_INTERFACE_IMPLEMENTATION( asImp()[ index ] );
53  return asImp()[ index ];
54  }
55 
56  template< class Traits >
57  template< class T >
58  inline typename Traits :: VectorType &
60  {
61  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION( asImp().operator+=( v.asImp() ) );
62  return asImp();
63  }
64 
65  template< class Traits >
66  template< class T >
67  inline typename Traits :: VectorType &
69  {
70  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION( asImp().operator-=( v.asImp() ) );
71  return asImp();
72  }
73 
74  template< class Traits >
75  inline typename Traits :: VectorType &
77  {
78  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION( asImp().operator*=( s ) );
79  return asImp();
80  }
81 
82  template< class Traits >
83  template< class T >
84  inline typename Traits :: VectorType &
86  const VectorInterface< T > &v )
87  {
88  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION( asImp().add( s, v.asImp() ) );
89  return asImp();
90  }
91 
92  template< class Traits >
93  template< class T >
95  {
96  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION( asImp().assign( v.asImp() ) );
97  }
98 
99  template< class Traits >
101  {
102  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION( asImp().assign( s ) );
103  }
104 
105  template< class Traits >
107  {
108  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION( asImp().clear() );
109  }
110 
111  template< class Traits >
112  inline typename Traits :: ConstIteratorType
114  {
115  CHECK_INTERFACE_IMPLEMENTATION( asImp().begin() );
116  return asImp().begin();
117  }
118 
119  template< class Traits >
120  inline typename Traits :: IteratorType
122  {
123  CHECK_INTERFACE_IMPLEMENTATION( asImp().begin() );
124  return asImp().begin();
125  }
126 
127  template< class Traits >
128  inline typename Traits :: ConstIteratorType
130  {
131  CHECK_INTERFACE_IMPLEMENTATION( asImp().end() );
132  return asImp().end();
133  }
134 
135  template< class Traits >
136  inline typename Traits :: IteratorType
138  {
139  CHECK_INTERFACE_IMPLEMENTATION( asImp().end() );
140  return asImp().end();
141  }
142 
143  template< class Traits >
144  inline unsigned int VectorInterface< Traits > :: size () const
145  {
146  CHECK_INTERFACE_IMPLEMENTATION( asImp().size() );
147  return asImp().size();
148  }
149 
150 
151  // other things...
152  // ---------------
153 
155  template< class Traits1, class Traits2 >
158  const VectorInterface< Traits2 > &v )
159  {
160  typedef typename ExtractCommonFieldType< Traits1, Traits2 > :: FieldType FieldType;
161 
162  const unsigned int size = u.size();
163  assert( size == v.size() );
164 
165  FieldType result = 0;
166  for( unsigned int i = 0; i < size; ++i )
167  result += u[ i ] * v[ i ];
168  return result;
169  }
170 
171 
172  // Stream Operators
173  // ----------------
174 
184  template< class StreamTraits, class VectorTraits >
186  operator<< ( OutStreamInterface< StreamTraits > &out,
188  {
189  typedef typename VectorInterface< VectorTraits > :: ConstIteratorType
190  ConstIteratorType;
191 
192  const ConstIteratorType end = v.end();
193  for( ConstIteratorType it = v.begin(); it != end; ++it )
194  out << *it;
195  return out;
196  }
197 
198 
208  template< class StreamTraits, class VectorTraits >
212  {
213  typedef typename VectorInterface< VectorTraits > :: IteratorType
214  IteratorType;
215 
216  const IteratorType end = v.end();
217  for( IteratorType it = v.begin(); it != end; ++it )
218  in >> *it;
219  return in;
220  }
221 
222 
231  template< class Traits >
232  inline std :: ostream &operator<< ( std :: ostream &out,
233  const VectorInterface< Traits > &v )
234  {
235  const unsigned int size = v.size();
236 
237  if( size > 0 ) {
238  out << "[ ";
239  out << v[ 0 ];
240  for( unsigned int i = 1; i < size; ++i )
241  out << ", " << v[ i ];
242  out << " ]";
243  } else
244  out << "[]";
245  return out;
246  }
247 
248  inline void match ( std :: istream &in, char excepted )
249  {
250  char c = 0;
251  in >> c;
252  if( c != excepted )
253  in.clear( std :: ios_base :: badbit );
254  }
255 
256 
257 
270  template< class Traits >
271  inline std :: istream &operator>> ( std :: istream &in,
273  {
274  const unsigned int size = v.size();
275 
277 
278  char expected = '[';
279  for( unsigned int i = 0; i < size; ++i )
280  {
281  match( in, expected );
282  in >> w[ i ];
283  expected = ',';
284  }
285  match( in, ']' );
286 
287  if( in )
288  v.assign( w );
289  return in;
290  }
291 
292  } // namespace Fem
293 
294 } // namespace Dune
295 
296 #endif // #ifndef DUNE_FEM_VECTOR_INLINE_HH
ConstIteratorType begin() const
obtain begin iterator
Definition: vector_inline.hh:113
unsigned int size() const
Returns the vector&#39;s size.
Definition: vector_inline.hh:144
void match(std::istream &in, char excepted)
Definition: vector_inline.hh:248
VectorType & operator-=(const VectorInterface< T > &v)
Subtract another vector from this one.
InStreamInterface< StreamTraits > & operator>>(InStreamInterface< StreamTraits > &in, DiscreteFunctionInterface< Impl > &df)
read a discrete function from an input stream
Definition: discretefunction_inline.hh:395
VectorType & operator=(const VectorInterface< T > &v)
Assign another vector to this one.
void clear()
initialize the vector to 0
Definition: vector_inline.hh:106
const FieldType & operator[](unsigned int index) const
Returns a const reference to the field indexed by index.
Definition: vector_inline.hh:42
ConstIteratorType end() const
obtain end iterator
Definition: vector_inline.hh:129
Definition: coordinate.hh:4
abstract interface for an input stream
Definition: streams.hh:177
void assign(const VectorInterface< T > &v)
copy another vector to this one
Definition: vector_inline.hh:94
Double operator*(const Double &a, const Double &b)
Definition: double.hh:495
Definition: vector.hh:142
An abstract vector interface.
Definition: vector.hh:37
VectorType & operator+=(const VectorInterface< T > &v)
Add another vector to this one.
VectorType & addScaled(const FieldType s, const VectorInterface< T > &v)
Add a multiple of another vector to this one.
OutStreamInterface< StreamTraits > & operator<<(OutStreamInterface< StreamTraits > &out, const DiscreteFunctionInterface< Impl > &df)
write a discrete function into an output stream
Definition: discretefunction_inline.hh:375
static const VectorInterfaceArrayTraits< VT >::ArrayType & asImp(const ThisType &other)
Definition: bartonnackmaninterface.hh:27
VectorType & operator*=(const FieldType s)
Multiply this vector by a scalar.
Definition: vector_inline.hh:76
abstract interface for an output stream
Definition: streams.hh:44
A vector using a DynamicArray as storage.
Definition: vector.hh:542
Traits::FieldType FieldType
field type for the vector
Definition: vector.hh:55