test_data.hpp

来自「Boost provides free peer-reviewed portab」· HPP 代码 · 共 768 行 · 第 1/2 页

HPP
768
字号
      break;   case power_series:      {         BOOST_ASSERT(arg1.n1 < arg1.n2);         typedef float random_type;         typedef typename boost::mpl::if_<            ::boost::is_floating_point<T>,            T, long double>::type power_type;         std::tr1::mt19937 rnd;         std::tr1::uniform_real<random_type> ur_a(1.0, 2.0);         std::tr1::variate_generator<std::tr1::mt19937, std::tr1::uniform_real<random_type> > gen(rnd, ur_a);         for(int power = arg1.n1; power <= arg1.n2; ++power)         {            random_type r = gen();            power_type p = ldexp(static_cast<power_type>(r), power);            points.insert(truncate_to_float(real_cast<float>(arg1.z1 + p)));         }      }      break;   default:      BOOST_ASSERT(0 == "Invalid parameter_info object");      // Assert will fail if get here.      // Triggers warning 4130) // '==' : logical operation on address of string constant.   }}//// Prompt a user for information on a parameter range://template <class T>bool get_user_parameter_info(parameter_info<T>& info, const char* param_name){#ifdef BOOST_MSVC#  pragma warning(push)#  pragma warning(disable: 4127)#endif   std::string line;   do{      std::cout << "What kind of distribution do you require for parameter " << param_name << "?\n"         "Choices are:\n"         "  r     Random values in a half open range\n"         "  p     Evenly spaced periodic values in a half open range\n"         "  e     Exponential power series at a particular point: a + 2^b for some range of b\n"         "[Default=r]";      std::getline(std::cin, line);      boost::algorithm::trim(line);      if(line == "r")      {         info.type = random_in_range;         break;      }      else if(line == "p")      {         info.type = periodic_in_range;         break;      }      else if(line == "e")      {         info.type = power_series;         break;      }      else if(line == "")      {         info.type = random_in_range;         break;      }      //      // Ooops, not a valid input....      //      std::cout << "Sorry don't recognise \"" << line << "\" as a valid input\n"         "do you want to try again [y/n]?";      std::getline(std::cin, line);      boost::algorithm::trim(line);      if(line == "n")         return false;      else if(line == "y")         continue;      std::cout << "Sorry don't recognise that either, giving up...\n\n";      return false;   }while(true);   switch(info.type & ~dummy_param)   {   case random_in_range:   case periodic_in_range:      // get start and end points of range:      do{         std::cout << "Data will be in the half open range a <= x < b,\n"            "enter value for the start point fo the range [default=0]:";         std::getline(std::cin, line);         boost::algorithm::trim(line);         if(line == "")         {            info.z1 = 0;            break;         }         try{            info.z1 = boost::lexical_cast<T>(line);            break;         }         catch(const boost::bad_lexical_cast&)         {            std::cout << "Sorry, that was not valid input, try again [y/n]?";            std::getline(std::cin, line);            boost::algorithm::trim(line);            if(line == "y")               continue;            if(line == "n")               return false;            std::cout << "Sorry don't recognise that either, giving up...\n\n";            return false;         }      }while(true);      do{         std::cout << "Enter value for the end point fo the range [default=1]:";         std::getline(std::cin, line);         boost::algorithm::trim(line);         if(line == "")         {            info.z2 = 1;         }         else         {            try            {               info.z2 = boost::lexical_cast<T>(line);            }            catch(const boost::bad_lexical_cast&)            {               std::cout << "Sorry, that was not valid input, try again [y/n]?";               std::getline(std::cin, line);               boost::algorithm::trim(line);               if(line == "y")                  continue;               if(line == "n")                  return false;               std::cout << "Sorry don't recognise that either, giving up...\n\n";               return false;            }         }         if(info.z1 >= info.z2)         {            std::cout << "The end point of the range was <= the start point\n"               "try a different value for the endpoint [y/n]?";            std::getline(std::cin, line);            boost::algorithm::trim(line);            if(line == "y")               continue;            if(line == "n")               return false;            std::cout << "Sorry don't recognise that either, giving up...\n\n";            return false;         }         break;      }while(true);      do{         // get the number of points:         std::cout << "How many data points do you want?";         std::getline(std::cin, line);         boost::algorithm::trim(line);         try{            info.n1 = boost::lexical_cast<int>(line);            info.n2 = 0;            if(info.n1 <= 0)            {               std::cout << "The number of points should be > 0\n"                  "try again [y/n]?";               std::getline(std::cin, line);               boost::algorithm::trim(line);               if(line == "y")                  continue;               if(line == "n")                  return false;               std::cout << "Sorry don't recognise that either, giving up...\n\n";               return false;            }            break;         }         catch(const boost::bad_lexical_cast&)         {            std::cout << "Sorry, that was not valid input, try again [y/n]?";            std::getline(std::cin, line);            boost::algorithm::trim(line);            if(line == "y")               continue;            if(line == "n")               return false;            std::cout << "Sorry don't recognise that either, giving up...\n\n";            return false;         }      }while(true);      break;   case power_series:      // get start and end points of range:      info.z2 = 0;      do{         std::cout << "Data will be in the form a + r*2^b\n"            "for random value r,\n"            "enter value for the point a [default=0]:";         std::getline(std::cin, line);         boost::algorithm::trim(line);         if(line == "")         {            info.z1 = 0;            break;         }         try{            info.z1 = boost::lexical_cast<T>(line);            break;         }         catch(const boost::bad_lexical_cast&)         {            std::cout << "Sorry, that was not valid input, try again [y/n]?";            std::getline(std::cin, line);            boost::algorithm::trim(line);            if(line == "y")               continue;            if(line == "n")               return false;            std::cout << "Sorry don't recognise that either, giving up...\n\n";            return false;         }      }while(true);      do{         std::cout << "Data will be in the form a + r*2^b\n"            "for random value r,\n"            "enter value for the starting exponent b:";         std::getline(std::cin, line);         boost::algorithm::trim(line);         try{            info.n1 = boost::lexical_cast<int>(line);            break;         }         catch(const boost::bad_lexical_cast&)         {            std::cout << "Sorry, that was not valid input, try again [y/n]?";            std::getline(std::cin, line);            boost::algorithm::trim(line);            if(line == "y")               continue;            if(line == "n")               return false;            std::cout << "Sorry don't recognise that either, giving up...\n\n";            return false;         }      }while(true);      do{         std::cout << "Data will be in the form a + r*2^b\n"            "for random value r,\n"            "enter value for the ending exponent b:";         std::getline(std::cin, line);         boost::algorithm::trim(line);         try{            info.n2 = boost::lexical_cast<int>(line);            break;         }         catch(const boost::bad_lexical_cast&)         {            std::cout << "Sorry, that was not valid input, try again [y/n]?";            std::getline(std::cin, line);            boost::algorithm::trim(line);            if(line == "y")               continue;            if(line == "n")               return false;            std::cout << "Sorry don't recognise that either, giving up...\n\n";            return false;         }      }while(true);      break;   default:      BOOST_ASSERT(0); // should never get here!!   }   return true;#ifdef BOOST_MSVC#  pragma warning(pop)#endif}template <class charT, class traits, class T>inline std::basic_ostream<charT, traits>& write_csv(std::basic_ostream<charT, traits>& os,                                             const test_data<T>& data){   const charT defarg[] = { ',', ' ', '\0' };   return write_csv(os, data, defarg);}template <class charT, class traits, class T>std::basic_ostream<charT, traits>& write_csv(std::basic_ostream<charT, traits>& os,                                             const test_data<T>& data,                                             const charT* separator){   typedef typename test_data<T>::const_iterator it_type;   typedef typename test_data<T>::value_type value_type;   typedef typename value_type::const_iterator value_type_iterator;   it_type a, b;   a = data.begin();   b = data.end();   while(a != b)   {      value_type_iterator x, y;      bool sep = false;      x = a->begin();      y = a->end();      while(x != y)      {         if(sep)            os << separator;         os << *x;         sep = true;         ++x;      }      os << std::endl;      ++a;   }   return os;}template <class T>std::ostream& write_code(std::ostream& os,                         const test_data<T>& data,                         const char* name){   typedef typename test_data<T>::const_iterator it_type;   typedef typename test_data<T>::value_type value_type;   typedef typename value_type::const_iterator value_type_iterator;   BOOST_ASSERT(os.good());   it_type a, b;   a = data.begin();   b = data.end();   if(a == b)      return os;   os << "#define SC_(x) static_cast<T>(BOOST_JOIN(x, L))\n"   "   static const boost::array<boost::array<T, "   << a->size() << ">, " << data.size() << "> " << name << " = {{\n";   while(a != b)   {      if(a != data.begin())         os << ", \n";      value_type_iterator x, y;      x = a->begin();      y = a->end();      os << "      { ";      while(x != y)      {         if(x != a->begin())            os << ", ";         os << "SC_(" << *x << ")";         ++x;      }      os << " }";      ++a;   }   os << "\n   }};\n#undef SC_\n\n";   return os;}} // namespace tools} // namespace math} // namespace boost#ifdef BOOST_MSVC#pragma warning(pop)#endif#endif // BOOST_MATH_TOOLS_TEST_DATA_HPP

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?