git HEAD
--------
+* Added fromString<>( const std::string& x )
* Added SmallSet::erase( const T& t )
* Added DECMAP algorithm.
* Added createFactorDelta( const VarSet& vs, size_t state )
}
-/// Converts a variable of type \a T to a \c std::string by using a \c std::stringstream
+/// Converts a variable of type \a T to a \c std::string by using a \c boost::lexical_cast
template<class T>
std::string toString( const T& x ) {
return boost::lexical_cast<std::string>(x);
}
+/// Converts a variable of type std::string to \a T by using a \c boost::lexical_cast
+template<class T>
+T fromString( const std::string& x ) {
+ return boost::lexical_cast<T>(x);
+}
+
+
/// Writes a \c std::vector<> to a \c std::ostream
template<class T>
std::ostream& operator << (std::ostream& os, const std::vector<T> & x) {
if( fields[i].size() > 0 ) { // skip if missing observation
if( fields[i].find_first_not_of("0123456789") != std::string::npos )
DAI_THROWE(INVALID_EVIDENCE_FILE,"Invalid state " + fields[i] + " in line " + boost::lexical_cast<std::string>(line_number));
- size_t state = atoi( fields[i].c_str() );
+ size_t state = fromString<size_t>( fields[i].c_str() );
if( state >= vars[i].states() )
DAI_THROWE(INVALID_EVIDENCE_FILE,"State " + fields[i] + " too large in line " + boost::lexical_cast<std::string>(line_number));
sample[vars[i]] = state;
}
+BOOST_AUTO_TEST_CASE( stringTest ) {
+ int a = 5;
+ BOOST_CHECK_EQUAL( toString( a ), std::string("5") );
+ BOOST_CHECK_EQUAL( fromString<int>( "5" ), a );
+
+ bool b = true;
+ BOOST_CHECK_EQUAL( toString( b ), std::string("1") );
+ BOOST_CHECK_EQUAL( fromString<bool>( "1" ), b );
+
+ double c = -3.5;
+ BOOST_CHECK_EQUAL( toString( c ), std::string("-3.5") );
+ BOOST_CHECK_EQUAL( fromString<double>( "-3.5" ), c );
+
+ unsigned char d = 'e';
+ BOOST_CHECK_EQUAL( toString( d ), std::string("e") );
+ BOOST_CHECK_EQUAL( fromString<unsigned char>( "e" ), d );
+}
+
+
BOOST_AUTO_TEST_CASE( tokenizeStringTest ) {
std::string s("Hello\tworld.\nThis is it.");
std::vector<std::string> words;
// Read factorgraph
FactorGraph fg;
char *infile = argv[1];
- int calc_tw = atoi(argv[2]);
+ size_t maxstates = fromString<size_t>( argv[2] );
fg.ReadFromFile( infile );
// Output various statistics
cout << "If run_jtree!=0, runs a junction tree and reports the results in the UAI 2008 results file format." << endl;
return 1;
} else {
- long verbose = atoi( argv[6] );
- long type = atoi( argv[4] );
- bool run_jtree = atoi( argv[5] );
+ long verbose = fromString<long>( argv[6] );
+ long type = fromString<long>( argv[4] );
+ bool run_jtree = fromString<bool>( argv[5] );
// read factor graph
vector<Var> vars;