mosesdecoder/moses/TranslationModel/DynSAInclude/utils.h
Jeroen Vermeulen 38d790cac0 Add cross-platform randomizer module.
The code uses two mechanisms for generating random numbers: srand()/rand(),
which is not thread-safe, and srandom()/random(), which is POSIX-specific.

Here I add a util/random.cc module that centralizes these calls, and unifies
some common usage patterns.  If the implementation is not good enough, we can
now change it in a single place.

To keep things simple, this uses the portable srand()/rand() but protects them
with a lock to avoid concurrency problems.

The hard part was to keep the regression tests passing: they rely on fixed
sequences of random numbers, so a small code change could break them very
thoroughly.  Util::rand(), for wide types like size_t, calls std::rand() not
once but twice.  This behaviour was generalized into utils::wide_rand() and
friends.
2015-04-23 23:46:04 +07:00

68 lines
1.9 KiB
C++

#ifndef moses_DynSAInclude_utils_h
#define moses_DynSAInclude_utils_h
#include <cstdlib>
#include <vector>
#include <string>
#include <sstream>
#include <cctype>
#include <cmath>
#include <cstring>
//! @todo ask abby2
class Utils
{
public:
static void trim(std::string& str, const std::string dropChars = " \t\n\r") {
str.erase(str.find_last_not_of(dropChars)+1);
str.erase(0, str.find_first_not_of(dropChars));
}
static void rtrim(std::string& str, const std::string dropChars = " \t\n\r") {
str.erase(str.find_last_not_of(dropChars)+1);
}
static void ltrim(std::string& str, const std::string dropChars = " \t\n\r") {
str.erase(0, str.find_first_not_of(dropChars));
}
static std::string IntToStr(int integer) {
std::ostringstream stream;
stream << integer;
return stream.str();
}
static int splitToStr(const char * str,
std::vector<std::string> & items,
const char * delm = "\t") {
char * buff = const_cast<char *>(str);
items.clear();
char * pch = strtok(buff, delm);
while( pch != NULL ) {
items.push_back(pch);
pch = strtok(NULL, delm);
}
return items.size();
}
static int splitToStr(std::string buff,
std::vector<std::string> & items,
std::string delm = "\t") {
std::string cp = buff.substr();
return splitToStr(cp.c_str(), items, delm.c_str());
}
static int splitToInt(std::string buff, std::vector<int>& items,
std::string delm = ",") {
items.clear();
std::vector<std::string> tmpVector(0);
int i = 0;
i = splitToStr(buff.c_str(), tmpVector, delm.c_str());
if( i > 0 )
for( int j = 0; j < i; j++ )
items.push_back(atoi(tmpVector[j].c_str()));
return i;
}
static void strToLowercase(std::string& str) {
for(unsigned i=0; i < str.length(); i++) {
str[i] = tolower(str[i]);
}
}
};
#endif