dune-common  2.3.0
fassign.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 #ifndef DUNE_ASSIGN_HH
4 #define DUNE_ASSIGN_HH
5 
6 #include <dune/common/fvector.hh>
7 #include <dune/common/fmatrix.hh>
8 #include <dune/common/unused.hh>
9 
10 namespace Dune {
11 
24  namespace {
25 
31  struct Zero {
32  explicit Zero (int) {};
34  operator double () { return 0.0; }
36  operator int () { return 0; }
37  } zero(0);
38 
44  struct NextRow {
45  explicit NextRow (int) {};
46  } nextRow(0);
47  } // end empty namespace
48 
69  template <class T, int s>
71  {
72  private:
73  FieldVector<T,s> & v;
74  int c;
75  bool temporary;
77  public:
79  fvector_assigner(fvector_assigner & a) : v(a.v), c(a.c), temporary(false)
80  {}
85  fvector_assigner(FieldVector<T,s> & _v, bool t) : v(_v), c(0), temporary(t)
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  {
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  {}
199  fmatrix_assigner(FieldMatrix<T,n,m> & _A, bool t) : A(_A), c(0), r(0), temporary(t),
200  thrown(false)
201  {};
207  {
208  end_row();
209  if (!temporary && r!=n-1 && !thrown) {
210  thrown=true;
211  DUNE_THROW(MathError, "Trying to assign " << r <<
212  " rows to a FieldMatrix of size " << n << " x " << m);
213  }
214  }
216  fmatrix_assigner & append (const T & t)
217  {
218  // Check whether we have passed the last row
219  if(r>=m) {
220  thrown=true;
221  DUNE_THROW(MathError, "Trying to assign more than " << m <<
222  " rows to a FieldMatrix of size " << n << " x " << m);
223  }
224  A[r][c++] = t;
225  return *this;
226  }
230  {
232  while (c!=m) A[r][c++] = 0;
233  return *this;
234  }
238  {
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