]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Performance tweaks with assumptions on the size of passed vectors.
authorWolfgang Bangerth <bangerth@math.tamu.edu>
Mon, 18 May 1998 08:30:16 +0000 (08:30 +0000)
committerWolfgang Bangerth <bangerth@math.tamu.edu>
Mon, 18 May 1998 08:30:16 +0000 (08:30 +0000)
git-svn-id: https://svn.dealii.org/trunk@298 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/base/include/base/function.h
deal.II/base/source/function.cc

index c0eed3783a7242c86a1b1e0613223a5356a7f7a4..adc4fab474d3264eafe50d86c4b560b1f132ff52 100644 (file)
@@ -50,8 +50,10 @@ class Function {
                                     /**
                                      * Set #values# to the point values
                                      * of the function at the #points#.
-                                     * It is assumed that #values# be
-                                     * empty.
+                                     * It is assumed that #values#
+                                     * already has the right size, i.e.
+                                     * the same size as the #points#
+                                     * array.
                                      */
     virtual void value_list (const vector<Point<dim> > &points,
                             vector<double>            &values) const;
@@ -65,8 +67,9 @@ class Function {
                                     /**
                                      * Set #gradients# to the gradients of
                                      * the function at the #points#.
-                                     * It is assumed that #values# be
-                                     * empty.
+                                     * It is assumed that #values# 
+                                     * already has the right size, i.e.
+                                     * the same size as the #points# array.
                                      */
     virtual void gradient_list (const vector<Point<dim> > &points,
                                vector<Point<dim> >       &gradients) const;
@@ -79,7 +82,10 @@ class Function {
                                     /**
                                      * Exception
                                      */
-    DeclException0 (ExcVectorNotEmpty);
+    DeclException2 (ExcVectorHasWrongSize,
+                   int, int,
+                   << "The vector has size " << arg1 << " but should have "
+                   << arg2 << " elements.");
 };
 
 
@@ -109,8 +115,10 @@ class ZeroFunction : public Function<dim> {
                                     /**
                                      * Set #values# to the point values
                                      * of the function at the #points#.
-                                     * It is assumed that #values# be
-                                     * empty.
+                                     * It is assumed that #values#
+                                     * already has the right size, i.e.
+                                     * the same size as the #points#
+                                     * array.
                                      */
     virtual void value_list (const vector<Point<dim> > &points,
                             vector<double>            &values) const;
@@ -124,8 +132,9 @@ class ZeroFunction : public Function<dim> {
                                     /**
                                      * Set #gradients# to the gradients of
                                      * the function at the #points#.
-                                     * It is assumed that #values# be
-                                     * empty.
+                                     * It is assumed that #values#
+                                     * already has the right size, i.e.
+                                     * the same size as the #points# array.
                                      */
     virtual void gradient_list (const vector<Point<dim> > &points,
                                vector<Point<dim> >       &gradients) const;
@@ -137,9 +146,14 @@ class ZeroFunction : public Function<dim> {
 
 /**
  *  Provide a function which always returns a constant value, which is delivered
- *  upon construction. Obviously, the derivates of this function are zerom which
+ *  upon construction. Obviously, the derivates of this function are zero, which
  *  is why we derive this class from #ZeroFunction#: we then only have to
- *  overload th value functions, not all the derivatives.
+ *  overload th value functions, not all the derivatives. In some way, it would
+ *  be more obvious to do the derivation in the opposite direction, i.e. let
+ *  #ZeroFunction# be a more specialized version of #ConstantFunction#; however,
+ *  this would be more inefficient, since we could not make use of the fact that
+ *  the function value of the #ZeroFunction# is known at compile time and need
+ *  not be looked up somewhere in memory.
  */
 template <int dim>
 class ConstantFunction : public ZeroFunction<dim> {
@@ -164,8 +178,10 @@ class ConstantFunction : public ZeroFunction<dim> {
                                     /**
                                      * Set #values# to the point values
                                      * of the function at the #points#.
-                                     * It is assumed that #values# be
-                                     * empty.
+                                     * It is assumed that #values#
+                                     * already has the right size, i.e.
+                                     * the same size as the #points#
+                                     * array.
                                      */
     virtual void value_list (const vector<Point<dim> > &points,
                             vector<double>            &values) const;
index fdf4ec55961adf67f726bc5b84f811d7451c28c8..26350a3ca3a2f5cc6169ac8e490bce211c28ffea 100644 (file)
@@ -5,7 +5,6 @@
 #include <vector>
 
 
-
 template <int dim>
 Function<dim>::~Function () {};
 
@@ -21,12 +20,11 @@ double Function<dim>::operator () (const Point<dim> &) const {
 template <int dim>
 void Function<dim>::value_list (const vector<Point<dim> > &points,
                                vector<double>            &values) const {
-  Assert (values.size() == 0,
-         ExcVectorNotEmpty());
+  Assert (values.size() == points.size(),
+         ExcVectorHasWrongSize(values.size(), points.size()));
 
-  values.reserve (points.size());
   for (unsigned int i=0; i<points.size(); ++i)
-    values.push_back (this->operator() (points[i]));
+    values[i]  = this->operator() (points[i]);
 };
 
 
@@ -43,12 +41,11 @@ Point<dim> Function<dim>::gradient (const Point<dim> &) const {
 template <int dim>
 void Function<dim>::gradient_list (const vector<Point<dim> > &points,
                                   vector<Point<dim> >       &gradients) const {
-  Assert (gradients.size() == 0,
-         ExcVectorNotEmpty());
+  Assert (gradients.size() == points.size(),
+         ExcVectorHasWrongSize(gradients.size(), points.size()));
 
-  gradients.reserve (points.size());
   for (unsigned int i=0; i<points.size(); ++i)
-    gradients.push_back (gradient(points[i]));
+    gradients[i] = gradient(points[i]);
 };
 
 
@@ -72,11 +69,10 @@ double ZeroFunction<dim>::operator () (const Point<dim> &) const {
 template <int dim>
 void ZeroFunction<dim>::value_list (const vector<Point<dim> > &points,
                                    vector<double>            &values) const {
-  Assert (values.size() == 0,
-         ExcVectorNotEmpty());
+  Assert (values.size() == points.size(),
+         ExcVectorHasWrongSize(values.size(), points.size()));
 
-  values.reserve (points.size());
-  values.insert (values.begin(), points.size(), 0.);
+  fill_n (values.begin(), points.size(), 0.);
 };
 
 
@@ -90,12 +86,11 @@ Point<dim> ZeroFunction<dim>::gradient (const Point<dim> &) const {
 
 template <int dim>
 void ZeroFunction<dim>::gradient_list (const vector<Point<dim> > &points,
-                                      vector<Point<dim> >       &values) const {
-  Assert (values.size() == 0,
-         ExcVectorNotEmpty());
+                                      vector<Point<dim> >       &gradients) const {
+  Assert (gradients.size() == points.size(),
+         ExcVectorHasWrongSize(gradients.size(), points.size()));
 
-  values.reserve (points.size());
-  values.insert (values.begin(), points.size(), Point<dim>());
+  fill_n (gradients.begin(), points.size(), Point<dim>());
 };
 
 
@@ -121,11 +116,10 @@ double ConstantFunction<dim>::operator () (const Point<dim> &) const {
 template <int dim>
 void ConstantFunction<dim>::value_list (const vector<Point<dim> > &points,
                                    vector<double>            &values) const {
-  Assert (values.size() == 0,
-         ExcVectorNotEmpty());
+  Assert (values.size() == points.size(),
+         ExcVectorHasWrongSize(values.size(), points.size()));
 
-  values.reserve (points.size());
-  values.insert (values.begin(), points.size(), function_value);
+  fill_n (values.begin(), points.size(), function_value);
 };
 
 

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.