dune-common  2.2.1
fassign.hh
Go to the documentation of this file.
1 // -*- tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 // vi: set ts=8 sw=4 et sts=4:
3 #ifndef DUNE_ASSIGN_HH
4 #define DUNE_ASSIGN_HH
5 
6 #include <dune/common/fvector.hh>
7 #include <dune/common/fmatrix.hh>
8 
9 namespace Dune {
10 
23 namespace {
24 
30  struct Zero {
31  explicit Zero (int) {};
33  operator double () { return 0.0; }
35  operator int () { return 0; }
36  } zero(0);
37 
43  struct NextRow {
44  explicit NextRow (int) {};
45  } nextRow(0);
46 } // end empty namespace
47 
68 template <class T, int s>
70 {
71 private:
72  FieldVector<T,s> & v;
73  int c;
74  bool temporary;
76 public:
78  fvector_assigner(fvector_assigner & a) : v(a.v), c(a.c), temporary(false)
79  {
80  }
85  fvector_assigner(FieldVector<T,s> & _v, bool t) : v(_v), c(0), temporary(t)
86  {
87  };
93  {
94  if (!temporary && c!=s)
95  DUNE_THROW(MathError, "Trying to assign " << c <<
96  " entries to a FieldVector of size " << s);
97  }
99  fvector_assigner & append (const T & t)
100  {
101  v[c++] = t;
102  return *this;
103  }
107  {
108  while (c!=s) v[c++] = 0;
109  return *this;
110  }
116  {
117  return append(t);
118  }
124  {
125  return append(z);
126  }
127 };
128 
135 template <class T, class K, int s>
136 fvector_assigner<T,s> operator <<= (FieldVector<T,s> & v, const K & t)
137 {
138  return fvector_assigner<T,s>(v,true).append(t);
139 }
140 
146 template <class T, int s>
147 fvector_assigner<T,s> operator <<= (FieldVector<T,s> & v, Zero z)
148 {
149  return fvector_assigner<T,s>(v,true).append(z);
150 }
151 
172 template <class T, int n, int m>
174 {
175 private:
176  FieldMatrix<T,n,m> & A;
177  int c;
178  int r;
179  bool temporary;
180  bool thrown;
181 
182  void end_row()
183  {
184  if (!temporary && c!=m && !thrown){
185  thrown=true;
186  DUNE_THROW(MathError, "Trying to assign " << c <<
187  " entries to a FieldMatrix row of size " << m);
188  }
189  c=0;
190  }
191 public:
193  fmatrix_assigner(fmatrix_assigner & a) : A(a.A), c(a.c), r(a.r), temporary(false), thrown(a.thrown)
194  {
195  }
200  fmatrix_assigner(FieldMatrix<T,n,m> & _A, bool t) : A(_A), c(0), r(0), temporary(t),
201  thrown(false)
202  {
203  };
209  {
210  end_row();
211  if (!temporary && r!=n-1 && !thrown){
212  thrown=true;
213  DUNE_THROW(MathError, "Trying to assign " << r <<
214  " rows to a FieldMatrix of size " << n << " x " << m);
215  }
216  }
218  fmatrix_assigner & append (const T & t)
219  {
220  // Check whether we have passed the last row
221  if(r>=m){
222  thrown=true;
223  DUNE_THROW(MathError, "Trying to assign more than " << m <<
224  " rows to a FieldMatrix of size " << n << " x " << m);
225  }
226  A[r][c++] = t;
227  return *this;
228  }
232  {
233  while (c!=m) A[r][c++] = 0;
234  return *this;
235  }
239  {
240  end_row();
241  r++;
242  return *this;
243  }
249  {
250  return append(t);
251  }
257  {
258  return append(z);
259  }
266  {
267  return append(nr);
268  }
269 };
270 
277 template <class T, class K, int n, int m>
278 fmatrix_assigner<T,n,m> operator <<= (FieldMatrix<T,n,m> & v, const K & t)
279 {
280  return fmatrix_assigner<T,n,m>(v,true).append(t);
281 }
282 
288 template <class T, int n, int m>
289 fmatrix_assigner<T,n,m> operator <<= (FieldMatrix<T,n,m> & v, Zero z)
290 {
291  return fmatrix_assigner<T,n,m>(v,true).append(z);
292 }
293 
294 } // end namespace Dune
295 
296 #endif // DUNE_ASSIGN_HH