aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
Utilities

About aGrUM utilities. More...

+ Collaboration diagram for Utilities:

Detailed Description

About aGrUM utilities.

Modules

 Math
 All the maths you'll need.
 
 Configuration
 About aGrUM configuration.
 
 Smart Pointers
 RefPtr are a replacement for the usual pointers: they keep track of the number of "smart" pointers pointing to a given element.
 

Classes

class  gum::Directory
 Cross-platform directory utility. More...
 
template<typename T1 , typename T2 >
ostream & std::operator<< (ostream &stream, const pair< T1, T2 > &val)
 'std::cout<<' operator for pairs. More...
 
template<typename T0 , typename... T>
std::ostream & std::operator<< (std::ostream &os, const std::tuple< T0, T... > &t)
 'std::cout<<' operator for vectors. More...
 
template<typename T >
ostream & std::operator<< (ostream &stream, const vector< T > &val)
 'std::cout<<' operator for vectors. More...
 
template<typename T >
bool std::hasUniqueElts (std::vector< T > const &x)
 
More...
 
template<typename T1 , typename T2 >
using gum::forbidden_type = typename std::enable_if< !std::is_same< T1, T2 >::value, int >::type
 Forbidden_type<T1,T2> return the "int" type if T1 and T2 are of the same type, else nothing. More...
 
bool gum::Memcmp (const void *const in_, const void *const out_, unsigned long size)
 Cross-platform replacement for memcmp. More...
 
void gum::_atexit_ ()
 Used for debug purpose. More...
 
Idx gum::randomValue (const Size max=2)
 Returns a random Idx between 0 and max-1 included. More...
 
double gum::randomProba ()
 Returns a random double between 0 and 1 included (i.e. More...
 
template<typename GUM_SCALAR >
std::vector< GUM_SCALAR > gum::randomDistribution (Size n)
 Return a random discrete distribution. More...
 
unsigned int gum::randomGeneratorSeed ()
 Returns the aGrUM's seed used by the std::generators. More...
 
void gum::initRandom (unsigned int seed=0)
 Initialize random generator seed. More...
 
std::default_random_engine gum::getRandomGenerator (unsigned int seed=0)
 define a random_engine with correct seed More...
 
std::string gum::getUniqueFileName ()
 Returns a path to a unique file name. More...
 
bool gum::endsWith (std::string const &value, std::string const &ending)
 Returns true if value ends with ending. More...
 
std::vector< std::string > gum::split (const std::string &orig, const std::string &delimiter)
 Split str using the delimiter. More...
 
std::string gum::replace (const std::string &s, const std::string &val, const std::string &new_val)
 not usable for gcc 4.8 std::vector<std::string> split( const std::string& orig, const std::string& delimiter ) { More...
 
std::string gum::toLower (std::string str)
 Returns the lowercase version of str. More...
 

Typedef Documentation

◆ forbidden_type

template<typename T1 , typename T2 >
using gum::forbidden_type = typedef typename std::enable_if< !std::is_same< T1, T2 >::value, int >::type

Forbidden_type<T1,T2> return the "int" type if T1 and T2 are of the same type, else nothing.

Use it as a guard in template specification :

// Creates a template except if T is int or char
template<T,forbidden_type<T,int> =0,forbidden_type<T,char> =0> // ...
Parameters
T1The type to test for.
T2The expected type.

Definition at line 123 of file utils_misc.h.

Function Documentation

◆ _atexit_()

void gum::_atexit_ ( )

Used for debug purpose.

Definition at line 47 of file utils_misc.cpp.

47  {
48 #ifdef GUM_DEBUG_MODE
50 #endif
51  }
void _atexit_()
Used for debug purpose.
Definition: utils_misc.cpp:47

◆ endsWith()

bool gum::endsWith ( std::string const &  value,
std::string const &  ending 
)

Returns true if value ends with ending.

Returns
Returns true if value ends with ending.

Definition at line 49 of file utils_string.cpp.

49  {
50  if (ending.size() > value.size()) return false;
51  return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
52  }

◆ getRandomGenerator()

std::default_random_engine gum::getRandomGenerator ( unsigned int  seed)

define a random_engine with correct seed

Definition at line 47 of file utils_random.cpp.

47  {
48  std::default_random_engine generator;
49  generator.seed(seed ? seed : randomGeneratorSeed());
50  return generator;
51  }
unsigned int randomGeneratorSeed()
Returns the aGrUM&#39;s seed used by the std::generators.

◆ getUniqueFileName()

std::string gum::getUniqueFileName ( )

Returns a path to a unique file name.

Returns
Returns a path to a unique file name.

Definition at line 36 of file utils_string.cpp.

36  {
37 #ifdef HAVE_MKSTEMP
38  char tmpFileName_[] = "fileXXXXXX";
39  int fd = mkstemp(tmpFileName_);
40  close(fd);
41 #else // mainly Windows
42  char tmpFileName_[] = "fileXXXXXX";
43  _mktemp_s(tmpFileName_, strlen(tmpFileName_));
44 #endif
45 
46  return std::string(tmpFileName_);
47  }

◆ hasUniqueElts()

template<typename T >
bool std::hasUniqueElts ( std::vector< T > const &  x)

check if a vector consists in unique values (no duplicate).

Template Parameters
TThe vector's elements type.
Parameters
xthe vector
Returns
Returns true if the vector has no duplicate.

Definition at line 87 of file utils_misc_tpl.h.

87  {
88  if (x.size() <= 1) return true;
89  if (x.size() == 2) return x[0] != x[1];
90 
91  auto refless = [](T const* l, T const* r) {
92  return *l < *r;
93  };
94  auto refeq = [](T const* l, T const* r) {
95  return *l == *r;
96  };
97 
98  std::vector< T const* > vp;
99  vp.reserve(x.size());
100  for (size_t i = 0; i < x.size(); ++i)
101  vp.push_back(&x[i]);
102  sort(vp.begin(), vp.end(), refless); // O(N log N)
103  // if no adjacent pair (vp_n,vp_n+1) has *vp_n == *vp_n+1
104  return std::adjacent_find(vp.begin(), vp.end(), refeq) == vp.end();
105  }

◆ initRandom()

INLINE void gum::initRandom ( unsigned int  seed = 0)

Initialize random generator seed.

Definition at line 40 of file utils_random_inl.h.

References gum::Set< Key, Alloc >::emplace().

40  {
41  if (seed) {
42  srand(seed);
43  } else {
44  srand(randomGeneratorSeed());
45  }
46  }
unsigned int randomGeneratorSeed()
Returns the aGrUM&#39;s seed used by the std::generators.
+ Here is the call graph for this function:

◆ Memcmp()

bool gum::Memcmp ( const void *const  in_,
const void *const  out_,
unsigned long  size 
)

Cross-platform replacement for memcmp.

Parameters
in_A pointer to the block of memory to copy.
out_A pointer to the block of memory receiving copy.
sizeNumber of bytes to copy.
Returns
Returns true if OK.

Definition at line 37 of file utils_misc.cpp.

37  {
38  unsigned char* in = (unsigned char*)in_;
39  unsigned char* out = (unsigned char*)out_;
40 
41  for (unsigned long i = 0; i < size; ++i)
42  if (*(in++) != *(out++)) return false;
43 
44  return true;
45  }

◆ operator<<() [1/3]

template<typename T1 , typename T2 >
ostream & std::operator<< ( ostream &  stream,
const pair< T1, T2 > &  val 
)

'std::cout<<' operator for pairs.

Template Parameters
T1The pair's first's type.
T2The pair's second's type.
Parameters
streamThe stream to print to.
valThe pair to print to stream.
Returns
Returns stream.

Definition at line 58 of file utils_misc_tpl.h.

58  {
59  stream << "(" << val.first << "," << val.second << ")";
60  return stream;
61  }

◆ operator<<() [2/3]

template<typename T0 , typename... T>
std::ostream & std::operator<< ( std::ostream &  os,
const std::tuple< T0, T... > &  t 
)

'std::cout<<' operator for vectors.

Parameters
streamThe stream to print to.
Template Parameters
tThe std::tuple
Parameters
valThe std::vector to print to stream.
Returns
Returns stream.

Definition at line 79 of file utils_misc_tpl.h.

79  {
80  char quote = (std::is_convertible< T0, std::string >::value) ? '"' : 0;
81  os << '(' << quote << std::get< 0 >(t) << quote;
82  _auxiliary_print_tuple_< 1 >::print(os, t);
83  return os << ')';
84  }

◆ operator<<() [3/3]

template<typename T >
ostream & std::operator<< ( ostream &  stream,
const vector< T > &  val 
)

'std::cout<<' operator for vectors.

Template Parameters
TThe vector's elements type.
Parameters
streamThe stream to print to.
valThe std::vector to print to stream.
Returns
Returns stream.

Definition at line 40 of file utils_misc_tpl.h.

40  {
41  bool deja = false;
42  stream << "[";
43 
44  for (const auto& v: val) {
45  if (deja)
46  stream << " , ";
47  else
48  deja = true;
49  stream << v;
50  }
51 
52  stream << "]";
53 
54  return stream;
55  }

◆ randomDistribution()

template<typename GUM_SCALAR >
std::vector< GUM_SCALAR > gum::randomDistribution ( Size  n)

Return a random discrete distribution.

Parameters
nThe number of modalities for the ditribution.
Returns
Return a random discrete distribution.
Template Parameters
GUM_SCALARThe type used for representing probabilities.

Definition at line 34 of file utils_random_tpl.h.

34  {
35  if (n < 2) n = 2;
36 
37  std::vector< GUM_SCALAR > v(n);
38  GUM_SCALAR s;
39 
40  do {
41  for (Idx i = 0; i < n; i++) {
42  v[i] = (GUM_SCALAR)randomProba();
43  }
44 
45  s = std::accumulate(v.begin(), v.end(), (GUM_SCALAR)0.0);
46 
47  } while (s < (GUM_SCALAR)(1e-5));
48 
49  for (Idx i = 0; i < n; i++) {
50  v[i] /= s;
51  }
52 
53  return v;
54  }
double randomProba()
Returns a random double between 0 and 1 included (i.e.

◆ randomGeneratorSeed()

unsigned int gum::randomGeneratorSeed ( )

Returns the aGrUM's seed used by the std::generators.

Returns
Returns the aGrUM's seed used by the std::generators.

Definition at line 40 of file utils_random.cpp.

40  {
41  return (unsigned int)((GUM_RANDOMSEED == 0)
42  ? std::chrono::system_clock::now().time_since_epoch().count()
43  : GUM_RANDOMSEED);
44  }

◆ randomProba()

INLINE double gum::randomProba ( )

Returns a random double between 0 and 1 included (i.e.

a proba).

Returns
Returns a random double between 0 and 1 included (i.e. a proba).

Definition at line 37 of file utils_random_inl.h.

References gum::Set< Key, Alloc >::emplace().

37 { return ((double)rand()) / ((double)RAND_MAX); }
+ Here is the call graph for this function:

◆ randomValue()

INLINE Idx gum::randomValue ( const Size  max = 2)

Returns a random Idx between 0 and max-1 included.

Returns
Returns a random Odxbetween 0 and max-1 included (i.e. a proba). By default, max=2

Definition at line 34 of file utils_random_inl.h.

References gum::Set< Key, Alloc >::emplace().

34 { return (Idx)(rand() % max); }
+ Here is the call graph for this function:

◆ replace()

std::string gum::replace ( const std::string &  s,
const std::string &  val,
const std::string &  new_val 
)

not usable for gcc 4.8 std::vector<std::string> split( const std::string& orig, const std::string& delimiter ) {

Replace val by new_val in s.

std::regex rgx( delimiter );

std::sregex_token_iterator first{begin( orig ), end( orig ), rgx, -1}, last;

return {first, last}; }

Parameters
sA string.
valThe value to replace in s.
new_valThe new value to replace val in s.
Returns
A new string with val replaced by new_val.

Definition at line 78 of file utils_string.cpp.

78  {
79  auto retVal = s;
80  auto pos = retVal.find(val);
81  while (pos != std::string::npos) {
82  std::stringstream sBuff;
83  sBuff << s.substr(0, pos) << new_val << s.substr(pos + val.size(), std::string::npos);
84  retVal = sBuff.str();
85  pos = retVal.find(val);
86  }
87  return retVal;
88  }

◆ split()

std::vector< std::string > gum::split ( const std::string &  orig,
const std::string &  delimiter 
)

Split str using the delimiter.

Parameters
origsearched string
delimiterstring
Returns
Vector of splitted strings

Definition at line 54 of file utils_string.cpp.

54  {
55  std::vector< std::string > tokens;
56  size_t prev = 0, pos = 0;
57  do {
58  pos = str.find(delim, prev);
59  if (pos == std::string::npos) pos = str.length();
60  std::string token = str.substr(prev, pos - prev);
61  if (!token.empty()) tokens.push_back(token);
62  prev = pos + delim.length();
63  } while (pos < str.length() && prev < str.length());
64  return tokens;
65  }

◆ toLower()

INLINE std::string gum::toLower ( std::string  str)

Returns the lowercase version of str.

Returns
Returns the lowercase version of str.

Definition at line 36 of file utils_string_inl.h.

References gum::Set< Key, Alloc >::emplace().

36  {
37  std::transform(str.begin(), str.end(), str.begin(), ::tolower);
38  return str;
39  }
+ Here is the call graph for this function: