From: Wolfgang Bangerth Date: Mon, 18 May 1998 08:30:16 +0000 (+0000) Subject: Performance tweaks with assumptions on the size of passed vectors. X-Git-Tag: v8.0.0~22956 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e981827ca19ecdc770ff01acdb686608c3499858;p=dealii.git Performance tweaks with assumptions on the size of passed vectors. git-svn-id: https://svn.dealii.org/trunk@298 0785d39b-7218-0410-832d-ea1e28bc413d --- diff --git a/deal.II/base/include/base/function.h b/deal.II/base/include/base/function.h index c0eed3783a..adc4fab474 100644 --- a/deal.II/base/include/base/function.h +++ b/deal.II/base/include/base/function.h @@ -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 > &points, vector &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 > &points, vector > &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 { /** * 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 > &points, vector &values) const; @@ -124,8 +132,9 @@ class ZeroFunction : public 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 > &points, vector > &gradients) const; @@ -137,9 +146,14 @@ class ZeroFunction : public Function { /** * 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 class ConstantFunction : public ZeroFunction { @@ -164,8 +178,10 @@ class ConstantFunction : public ZeroFunction { /** * 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 > &points, vector &values) const; diff --git a/deal.II/base/source/function.cc b/deal.II/base/source/function.cc index fdf4ec5596..26350a3ca3 100644 --- a/deal.II/base/source/function.cc +++ b/deal.II/base/source/function.cc @@ -5,7 +5,6 @@ #include - template Function::~Function () {}; @@ -21,12 +20,11 @@ double Function::operator () (const Point &) const { template void Function::value_list (const vector > &points, vector &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; ioperator() (points[i])); + values[i] = this->operator() (points[i]); }; @@ -43,12 +41,11 @@ Point Function::gradient (const Point &) const { template void Function::gradient_list (const vector > &points, vector > &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::operator () (const Point &) const { template void ZeroFunction::value_list (const vector > &points, vector &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 ZeroFunction::gradient (const Point &) const { template void ZeroFunction::gradient_list (const vector > &points, - vector > &values) const { - Assert (values.size() == 0, - ExcVectorNotEmpty()); + vector > &gradients) const { + Assert (gradients.size() == points.size(), + ExcVectorHasWrongSize(gradients.size(), points.size())); - values.reserve (points.size()); - values.insert (values.begin(), points.size(), Point()); + fill_n (gradients.begin(), points.size(), Point()); }; @@ -121,11 +116,10 @@ double ConstantFunction::operator () (const Point &) const { template void ConstantFunction::value_list (const vector > &points, vector &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); };