21 #ifndef GLOBAL_FUNCTIONS_HPP 22 #define GLOBAL_FUNCTIONS_HPP 38 inline std::string
toString (
const T& x, uint pr = 2 ) {
39 std::stringstream aux;
40 aux << std::fixed << std::setprecision ( pr ) << x;
43 USER_CHECK ( !aux.fail(),
"Converting Number to string failed" );
56 inline void toString (
const T& x, std::string& ss, uint pr = 2 ) {
57 std::stringstream aux;
58 if ( pr != std::numeric_limits<T>::max() ) aux << std::fixed <<
59 std::setprecision ( pr ) << x;
62 USER_CHECK ( !aux.fail(),
"Converting Number to string failed" );
73 std::stringstream aux;
77 USER_CHECK ( !aux.fail() && aux.eof(),
"Converting string to number failed" );
82 inline bool exists (
const std::string& source,
const std::string& needle ) {
83 return ( source.find ( needle ) != std::string::npos );
89 const std::string& replace ) {
91 for ( ; ( j = haystack.find ( needle ) ) != std::string::npos ; ) {
92 haystack.replace ( j, needle.length(), replace );
98 inline bool ends_with ( std::string
const& haystack,
99 std::string
const& needle ) {
100 if ( haystack.length() >= needle.length() )
101 return ( 0 == haystack.compare ( haystack.length() - needle.length(),
102 needle.length(), needle ) );
108 std::size_t start, std::size_t end ) {
110 for (
const char *c = haystack.c_str() + start; c < haystack.c_str() + end;
112 if ( *c == needle ) ++count;
117 inline void trim_spaces (
const std::string& input, std::string *output ) {
118 boost::regex pattern (
"\\s+",
119 boost::regex_constants::icase | boost::regex_constants::perl );
120 std::string replace (
" " );
121 *output = boost::regex_replace ( input, pattern, replace );
122 boost::algorithm::trim ( *output );
130 for (k = snumber.size() - 1; k > 0 && snumber[k] ==
'0'; --k);
131 if (k == snumber.size() - 1 || !k)
return;
133 for (; j > 0 && snumber[j] !=
'.'; --j);
134 if (j != k) snumber = snumber.substr (0, k + 1);
135 else if (j > 0) snumber = snumber.substr (0, k);
144 static const boost::regex e (
"^\\d+( \\d+)*$" );
145 return boost::regex_match ( s, e );
152 char time_buffer[128];
153 sprintf ( time_buffer,
"%s", ctime ( <ime ) );
154 time_buffer[strlen ( time_buffer ) - 1] = 0;
156 times.append ( time_buffer );
162 inline bool DirName ( std::string& dirname,
const std::string& filename ) {
163 std::string::size_type loc = filename.find_last_of (
"/", filename.size() );
164 if ( loc == std::string::npos )
return false;
165 dirname.append ( filename, 0, loc );
173 fin.open ( fileName.c_str(), std::ios::in );
174 if ( fin.is_open() ) {
183 inline float dotproduct ( std::vector<float>& v1, std::vector<float>& v2 ) {
184 std::size_t lowertop = v1.size() > v2.size() ? v2.size() : v1.size();
186 for ( std::size_t k = 0; k < lowertop; ++k )
187 result += v1[k] * v2[k];
198 VecT
const& v2 )
const {
206 class HashFVec :
public std::unary_function<VecT, std::size_t> {
211 for (
unsigned int i = 0; i < v.size();
212 i++ ) res += ( std::size_t ) pow ( (
double ) v[i],
213 double ( ( ( i ) % 6 ) + 1 ) );
227 ,
template<
typename ElemT,
typename AllocT=std::allocator<ElemT> >
class Container
229 inline std::ostream& operator<< (std::ostream& o, Container<T>
const& container) {
230 typename Container<T>::const_iterator itx = container.begin();
231 while(itx != container.end()) {
239 ,
template<
typename ElemT,
typename AllocT=std::allocator<ElemT> >
class Container
241 inline std::string
printout(Container<T>
const& container) {
242 std::ostringstream ss;
247 inline std::ostream& operator<< (std::ostream& o, std::basic_string<unsigned>
const& container) {
248 std::basic_string<unsigned>::const_iterator itx = container.begin();
249 while(itx != container.end()) {
bool operator()(VecT const &v1, VecT const &v2) const
Implements comparison.
std::string toString(const T &x, uint pr=2)
Converts an arbitrary type to string Converts to string integers, floats, doubles Quits execution if ...
void trim_spaces(const std::string &input, std::string *output)
Trims spaces at the edges (no spaces) and also between words (only one space)
HashFVec< std::vector< long long > > hashfvecint64
std::string getTimestamp(void)
Generates time stamp.
HashFVec< std::basic_string< unsigned > > hashfvecuint
void trim_trailing_zeros(std::string &snumber)
bool exists(const std::string &source, const std::string &needle)
Convenience function to find out whether a needle exists in a text.
bool DirName(std::string &dirname, const std::string &filename)
HashEqVec< std::basic_string< unsigned > > hasheqvecuint
uint count_needles(const std::string &haystack, const char needle, std::size_t start, std::size_t end)
Convenience function that counts the number of times a needle appears.
float dotproduct(std::vector< float > &v1, std::vector< float > &v2)
Implements dot product.
bool fileExists(const std::string &fileName)
#define USER_CHECK(exp, comment)
Tests whether exp is true. If not, comment is printed and program ends.
T toNumber(const std::string &x)
Converts a string to an arbitrary number Converts strings to a number. Quits execution if conversion ...
void find_and_replace(std::string &haystack, const std::string &needle, const std::string &replace)
bool ends_with(std::string const &haystack, std::string const &needle)
std::string printout(Container< T > const &container)
HashEqVec< std::vector< long long > > hasheqvecint64
bool validate_source_sentence(const std::string &s)
Checks whether the sentence is in format ^\d+( \d+)*$.