From 677f40259ea74ff6ac9f8ace93ac44feb9e65931 Mon Sep 17 00:00:00 2001 From: Wolfgang Bangerth Date: Mon, 19 Dec 2005 21:38:34 +0000 Subject: [PATCH] Move a few functions from fe_tools.cc to namespace Utilities. git-svn-id: https://svn.dealii.org/trunk@11885 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/base/include/base/utilities.h | 29 +++++- deal.II/base/source/utilities.cc | 60 ++++++++++++ deal.II/deal.II/source/fe/fe_tools.cc | 129 ++++++++------------------ deal.II/doc/news/changes.html | 8 ++ 4 files changed, 135 insertions(+), 91 deletions(-) diff --git a/deal.II/base/include/base/utilities.h b/deal.II/base/include/base/utilities.h index 0f492d86f6..7b1d6349f3 100644 --- a/deal.II/base/include/base/utilities.h +++ b/deal.II/base/include/base/utilities.h @@ -16,6 +16,7 @@ #include #include +#include #include #ifdef DEAL_II_USE_PETSC @@ -95,7 +96,33 @@ namespace Utilities break_text_into_lines (const std::string &original_text, const unsigned int width); - + + /** + * Teturn true if the given pattern + * string appears in the first + * position of the string. + */ + bool + match_at_string_start (const std::string &name, + const std::string &pattern); + + /** + * Read a (signed) integer starting + * at the position in #name + * indicated by the second + * argument, and retun this integer + * as a pair together with how many + * characters it takes up in the + * string. + * + * If no integer can be read at the + * indicated position, return + * (-1,deal_II_numbers::invalid_unsigned_int) + */ + std::pair + get_integer_at_position (const std::string &name, + const unsigned int position); + /** * Generate a random number from a * normalized Gaussian probability diff --git a/deal.II/base/source/utilities.cc b/deal.II/base/source/utilities.cc index 5f1f620bdd..c1c499d7f0 100644 --- a/deal.II/base/source/utilities.cc +++ b/deal.II/base/source/utilities.cc @@ -236,6 +236,66 @@ namespace Utilities return lines; } + + + bool + match_at_string_start (const std::string &name, + const std::string &pattern) + { + if (pattern.size() > name.size()) + return false; + + for (unsigned int i=0; i + get_integer_at_position (const std::string &name, + const unsigned int position) + { + Assert (position < name.size(), ExcInternalError()); + + const std::string test_string (name.begin()+position, + name.end()); + +#ifdef HAVE_STD_STRINGSTREAM + std::istringstream str(test_string); +#else + std::istrstream str(test_string.c_str()); +#endif + + int i; + if (str >> i) + { + // compute the number of + // digits of i. assuming it + // is less than 6 is likely + // ok + if (i<10) + return std::make_pair (i, 1U); + else if (i<100) + return std::make_pair (i, 2U); + else if (i<1000) + return std::make_pair (i, 3U); + else if (i<10000) + return std::make_pair (i, 4U); + else if (i<100000) + return std::make_pair (i, 5U); + else + { + Assert (false, ExcNotImplemented()); + return std::make_pair (-1, deal_II_numbers::invalid_unsigned_int); + } + } + else + return std::make_pair (-1, deal_II_numbers::invalid_unsigned_int); + } + double diff --git a/deal.II/deal.II/source/fe/fe_tools.cc b/deal.II/deal.II/source/fe/fe_tools.cc index 588c9ca252..f2d69ff5c3 100644 --- a/deal.II/deal.II/source/fe/fe_tools.cc +++ b/deal.II/deal.II/source/fe/fe_tools.cc @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -111,79 +112,6 @@ namespace interpolation_matrix = tmp; } - // return true if the given pattern - // string matches the given name at - // the first position of the string - inline - bool - match_at_string_start (const std::string &name, - const std::string &pattern) - { - if (pattern.size() > name.size()) - return false; - - for (unsigned int i=0; i - get_integer (const std::string &name, - const unsigned int position) - { - Assert (position < name.size(), ExcInternalError()); - - const std::string test_string (name.begin()+position, - name.end()); - -#ifdef HAVE_STD_STRINGSTREAM - std::istringstream str(test_string); -#else - std::istrstream str(test_string.c_str()); -#endif - - int i; - if (str >> i) - { - // compute the number of - // digits of i. assuming it - // is less than 6 is likely - // ok - if (i<10) - return std::make_pair (i, 1U); - else if (i<100) - return std::make_pair (i, 2U); - else if (i<1000) - return std::make_pair (i, 3U); - else if (i<10000) - return std::make_pair (i, 4U); - else if (i<100000) - return std::make_pair (i, 5U); - else - { - Assert (false, ExcNotImplemented()); - return std::make_pair (-1, deal_II_numbers::invalid_unsigned_int); - } - } - else - return std::make_pair (-1, deal_II_numbers::invalid_unsigned_int); - } - // return how many characters @@ -1343,7 +1271,7 @@ FETools::get_fe_from_name_aux (const std::string &name) // make sure we don't match FE_Q // when it's actually a // FE_Q_Hierarchic - if (match_at_string_start (name, std::string("FE_Q_Hierarchical"))) + if (Utilities::match_at_string_start (name, std::string("FE_Q_Hierarchical"))) { unsigned int position = std::string("FE_Q_Hierarchical").size(); // as described in the @@ -1364,7 +1292,8 @@ FETools::get_fe_from_name_aux (const std::string &name) ++position; // next thing is to parse the // degree of the finite element - const std::pair tmp = get_integer (name, position); + const std::pair tmp + = Utilities::get_integer_at_position (name, position); AssertThrow (tmp.first>=0, ExcInvalidFEName(name)); position += tmp.second; @@ -1383,13 +1312,16 @@ FETools::get_fe_from_name_aux (const std::string &name) } // check other possibilities in // exactly the same way - else if (match_at_string_start (name, std::string("FE_RaviartThomas"))) + else if (Utilities::match_at_string_start (name, std::string("FE_RaviartThomas"))) { unsigned int position = std::string("FE_RaviartThomas").size(); position += match_dimension (name, position); AssertThrow (name[position] == '(', ExcInvalidFEName(name)); ++position; - const std::pair tmp = get_integer (name, position); + + const std::pair tmp + = Utilities::get_integer_at_position (name, position); + AssertThrow (tmp.first>=0, ExcInvalidFEName(name)); position += tmp.second; AssertThrow (name[position] == ')', ExcInvalidFEName(name)); @@ -1398,13 +1330,16 @@ FETools::get_fe_from_name_aux (const std::string &name) (new FE_RaviartThomas(tmp.first)), position); } - else if (match_at_string_start (name, std::string("FE_Nedelec"))) + else if (Utilities::match_at_string_start (name, std::string("FE_Nedelec"))) { unsigned int position = std::string("FE_Nedelec").size(); position += match_dimension (name, position); AssertThrow (name[position] == '(', ExcInvalidFEName(name)); ++position; - const std::pair tmp = get_integer (name, position); + + const std::pair tmp + = Utilities::get_integer_at_position (name, position); + AssertThrow (tmp.first>=0, ExcInvalidFEName(name)); position += tmp.second; AssertThrow (name[position] == ')', ExcInvalidFEName(name)); @@ -1413,13 +1348,16 @@ FETools::get_fe_from_name_aux (const std::string &name) (new FE_Nedelec(tmp.first)), position); } - else if (match_at_string_start (name, std::string("FE_DGPNonparametric"))) + else if (Utilities::match_at_string_start (name, std::string("FE_DGPNonparametric"))) { unsigned int position = std::string("FE_DGPNonparametric").size(); position += match_dimension (name, position); AssertThrow (name[position] == '(', ExcInvalidFEName(name)); ++position; - const std::pair tmp = get_integer (name, position); + + const std::pair tmp + = Utilities::get_integer_at_position (name, position); + AssertThrow (tmp.first>=0, ExcInvalidFEName(name)); position += tmp.second; AssertThrow (name[position] == ')', ExcInvalidFEName(name)); @@ -1428,13 +1366,16 @@ FETools::get_fe_from_name_aux (const std::string &name) (new FE_DGPNonparametric(tmp.first)), position); } - else if (match_at_string_start (name, std::string("FE_DGP"))) + else if (Utilities::match_at_string_start (name, std::string("FE_DGP"))) { unsigned int position = std::string("FE_DGP").size(); position += match_dimension (name, position); AssertThrow (name[position] == '(', ExcInvalidFEName(name)); ++position; - const std::pair tmp = get_integer (name, position); + + const std::pair tmp + = Utilities::get_integer_at_position (name, position); + AssertThrow (tmp.first>=0, ExcInvalidFEName(name)); position += tmp.second; AssertThrow (name[position] == ')', ExcInvalidFEName(name)); @@ -1442,13 +1383,16 @@ FETools::get_fe_from_name_aux (const std::string &name) return std::make_pair (static_cast*>(new FE_DGP(tmp.first)), position); } - else if (match_at_string_start (name, std::string("FE_DGQ"))) + else if (Utilities::match_at_string_start (name, std::string("FE_DGQ"))) { unsigned int position = std::string("FE_DGQ").size(); position += match_dimension (name, position); AssertThrow (name[position] == '(', ExcInvalidFEName(name)); ++position; - const std::pair tmp = get_integer (name, position); + + const std::pair tmp + = Utilities::get_integer_at_position (name, position); + AssertThrow (tmp.first>=0, ExcInvalidFEName(name)); position += tmp.second; AssertThrow (name[position] == ')', ExcInvalidFEName(name)); @@ -1456,13 +1400,16 @@ FETools::get_fe_from_name_aux (const std::string &name) return std::make_pair (static_cast*> (new FE_DGQ(tmp.first)), position); } - else if (match_at_string_start (name, std::string("FE_Q"))) + else if (Utilities::match_at_string_start (name, std::string("FE_Q"))) { unsigned int position = std::string("FE_Q").size(); position += match_dimension (name, position); AssertThrow (name[position] == '(', ExcInvalidFEName(name)); ++position; - const std::pair tmp = get_integer (name, position); + + const std::pair tmp + = Utilities::get_integer_at_position (name, position); + AssertThrow (tmp.first>=0, ExcInvalidFEName(name)); position += tmp.second; AssertThrow (name[position] == ')', ExcInvalidFEName(name)); @@ -1478,7 +1425,7 @@ FETools::get_fe_from_name_aux (const std::string &name) // have to figure out what the // base elements are. this can // only be done recursively - if (match_at_string_start (name, std::string("FESystem"))) + if (Utilities::match_at_string_start (name, std::string("FESystem"))) { unsigned int position = std::string("FESystem").size(); position += match_dimension (name, position); @@ -1531,8 +1478,10 @@ FETools::get_fe_from_name_aux (const std::string &name) // and read this // multiplicity ++position; - const std::pair tmp = get_integer (name, - position); + + const std::pair tmp + = Utilities::get_integer_at_position (name, position); + AssertThrow (tmp.first>=0, ExcInvalidFEName(name)); position += tmp.second; base_multiplicities.push_back (tmp.first); diff --git a/deal.II/doc/news/changes.html b/deal.II/doc/news/changes.html index 7731b131a8..8fc34c6c77 100644 --- a/deal.II/doc/news/changes.html +++ b/deal.II/doc/news/changes.html @@ -150,6 +150,14 @@ inconvenience this causes.

base

    +
  1. + New: There are new functions + Utilities::match_at_string_start and + Utilities::get_integer_at_position. +
    + (WB 2005/12/19) +

    +
  2. Fixed: The computation of HMIN and HMAX in