dune-common  2.2.0
fassign.hh
Go to the documentation of this file.
1 #ifndef DUNE_ASSIGN_HH
2 #define DUNE_ASSIGN_HH
3 
4 #include <dune/common/fvector.hh>
5 #include <dune/common/fmatrix.hh>
6 
7 namespace Dune {
8 
21 namespace {
22 
28  struct Zero {
29  explicit Zero (int) {};
31  operator double () { return 0.0; }
33  operator int () { return 0; }
34  } zero(0);
35 
41  struct NextRow {
42  explicit NextRow (int) {};
43  } nextRow(0);
44 
45 } // end empty namespace
46 
67 template <class T, int s>
69 {
70 private:
71  FieldVector<T,s> & v;
72  int c;
73  bool temporary;
75 public:
77  fvector_assigner(fvector_assigner & a) : v(a.v), c(a.c), temporary(false)
78  {
79  }
84  fvector_assigner(FieldVector<T,s> & _v, bool t) : v(_v), c(0), temporary(t)
85  {
86  };
92  {
93  if (!temporary && c!=s)
94  DUNE_THROW(MathError, "Trying to assign " << c <<
95  " entries to a FieldVector of size " << s);
96  }
98  fvector_assigner & append (const T & t)
99  {
100  v[c++] = t;
101  return *this;
102  }
106  {
107  while (c!=s) v[c++] = 0;
108  return *this;
109  }
115  {
116  return append(t);
117  }
123  {
124  return append(z);
125  }
126 };
127 
134 template <class T, class K, int s>
135 fvector_assigner<T,s> operator <<= (FieldVector<T,s> & v, const K & t)
136 {
137  return fvector_assigner<T,s>(v,true).append(t);
138 }
139 
145 template <class T, int s>
146 fvector_assigner<T,s> operator <<= (FieldVector<T,s> & v, Zero z)
147 {
148  return fvector_assigner<T,s>(v,true).append(z);
149 }
150 
171 template <class T, int n, int m>
173 {
174 private:
175  FieldMatrix<T,n,m> & A;
176  int c;
177  int r;
178  bool temporary;
179  void end_row()
180  {
181  if (!temporary && c!=m)
182  DUNE_THROW(MathError, "Trying to assign " << c <<
183  " entries to a FieldMatrix row of size " << m);
184  c=0;
185  }
186 public:
188  fmatrix_assigner(fmatrix_assigner & a) : A(a.A), c(a.c), r(a.r), temporary(false)
189  {
190  }
195  fmatrix_assigner(FieldMatrix<T,n,m> & _A, bool t) : A(_A), c(0), r(0), temporary(t)
196  {
197  };
203  {
204  end_row();
205  if (!temporary && r!=n-1)
206  DUNE_THROW(MathError, "Trying to assign " << r <<
207  " rows to a FieldMatrix of size " << n << " x " << m);
208  }
210  fmatrix_assigner & append (const T & t)
211  {
212  A[r][c++] = t;
213  return *this;
214  }
218  {
219  while (c!=m) A[r][c++] = 0;
220  return *this;
221  }
225  {
226  end_row();
227  r++;
228  return *this;
229  }
235  {
236  return append(t);
237  }
243  {
244  return append(z);
245  }
251  {
252  return append(nr);
253  }
254 };
255 
262 template <class T, class K, int n, int m>
263 fmatrix_assigner<T,n,m> operator <<= (FieldMatrix<T,n,m> & v, const K & t)
264 {
265  return fmatrix_assigner<T,n,m>(v,true).append(t);
266 }
267 
273 template <class T, int n, int m>
274 fmatrix_assigner<T,n,m> operator <<= (FieldMatrix<T,n,m> & v, Zero z)
275 {
276  return fmatrix_assigner<T,n,m>(v,true).append(z);
277 }
278 
279 } // end namespace Dune
280 
281 #endif // DUNE_ASSIGN_HH