aGrUM  0.16.0
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 127 of file utils_misc.h.

Function Documentation

◆ __atexit()

void gum::__atexit ( )

Used for debug purpose.

Definition at line 50 of file utils_misc.cpp.

Referenced by gum::NullStream::NullStream().

50  {
51 #ifdef GUM_DEBUG_MODE
53 #endif
54  }
void __atexit()
Used for debug purpose.
Definition: utils_misc.cpp:50
+ Here is the caller graph for this function:

◆ 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 50 of file utils_string.cpp.

Referenced by gum::prm::o3prm::O3NameSolver< GUM_SCALAR >::resolveClass(), gum::prm::o3prm::O3NameSolver< GUM_SCALAR >::resolveClassElement(), gum::prm::o3prm::O3NameSolver< GUM_SCALAR >::resolveInterface(), gum::prm::o3prm::O3NameSolver< GUM_SCALAR >::resolveSlotType(), and gum::prm::o3prm::O3NameSolver< GUM_SCALAR >::resolveType().

50  {
51  if (ending.size() > value.size()) return false;
52  return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
53  }
+ Here is the caller graph for this function:

◆ getRandomGenerator()

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

define a random_engine with correct seed

Definition at line 49 of file utils_random.cpp.

References gum::randomGeneratorSeed().

Referenced by gum::MultiDimFunctionGraphGenerator::__generateVarPos().

49  {
50  std::default_random_engine generator;
51  generator.seed(seed ? seed : randomGeneratorSeed());
52  return generator;
53  }
unsigned int randomGeneratorSeed()
Returns the aGrUM&#39;s seed used by the std::generators.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ 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 37 of file utils_string.cpp.

Referenced by gum::credal::CredalNet< GUM_SCALAR >::__H2Vlrs().

37  {
38 #ifdef HAVE_MKSTEMP
39  char _tmpFileName[] = "fileXXXXXX";
40  int fd = mkstemp(_tmpFileName);
41  close(fd);
42 #else // mainly Windows
43  char _tmpFileName[] = "fileXXXXXX";
44  _mktemp_s(_tmpFileName, strlen(_tmpFileName));
45 #endif
46 
47  return std::string(_tmpFileName);
48  }
+ Here is the caller graph for this function:

◆ 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.
Todo:
WHY IN STD namespace ??

Definition at line 90 of file utils_misc_tpl.h.

Referenced by gum::build_node().

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

◆ initRandom()

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

Initialize random generator seed.

Definition at line 41 of file utils_random_inl.h.

References gum::randomGeneratorSeed().

41  {
42  if (seed) {
43  srand(seed);
44  } else {
45  srand(randomGeneratorSeed());
46  }
47  }
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
_inA pointer to the block of memory to copy.
_outA pointer to the block of memory receiving copy.
sizeNumber of bytes to copy.
Returns
Returns true if OK.

Definition at line 40 of file utils_misc.cpp.

Referenced by gum::NullStream::NullStream().

40  {
41  unsigned char* in = (unsigned char*)_in;
42  unsigned char* out = (unsigned char*)_out;
43 
44  for (unsigned long i = 0; i < size; ++i)
45  if (*(in++) != *(out++)) return false;
46 
47  return true;
48  }
+ Here is the caller graph for this function:

◆ 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 59 of file utils_misc_tpl.h.

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

◆ 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 82 of file utils_misc_tpl.h.

References std::__auxiliary_print_tuple< N >::print().

82  {
83  char quote = (std::is_convertible< T0, std::string >::value) ? '"' : 0;
84  os << '(' << quote << std::get< 0 >(t) << quote;
85  __auxiliary_print_tuple< 1 >::print(os, t);
86  return os << ')';
87  }
+ Here is the call graph for this function:

◆ 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 41 of file utils_misc_tpl.h.

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

◆ 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 35 of file utils_random_tpl.h.

References gum::randomProba().

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

◆ 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 41 of file utils_random.cpp.

Referenced by gum::getRandomGenerator(), and gum::initRandom().

41  {
42  return (unsigned int)((GUM_RANDOMSEED == 0) ? std::chrono::system_clock::now()
43  .time_since_epoch()
44  .count()
45  : GUM_RANDOMSEED);
46  }
+ Here is the caller graph for this function:

◆ 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 38 of file utils_random_inl.h.

Referenced by gum::SimpleCPTDisturber< GUM_SCALAR >::disturbAugmCPT(), gum::Potential< GUM_SCALAR >::draw(), gum::SimpleBayesNetGenerator< GUM_SCALAR, ICPTGenerator >::generateBN(), gum::Potential< GUM_SCALAR >::random(), and gum::randomDistribution().

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

◆ randomValue()

◆ 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 80 of file utils_string.cpp.

Referenced by gum::prm::o3prm::O3prmReader< double >::__clean(), gum::prm::o3prm::O3prmReader< double >::__parseImport(), and gum::prm::o3prmr::O3prmrInterpreter::import().

82  {
83  auto retVal = s;
84  auto pos = retVal.find(val);
85  while (pos != std::string::npos) {
86  std::stringstream sBuff;
87  sBuff << s.substr(0, pos) << new_val
88  << s.substr(pos + val.size(), std::string::npos);
89  retVal = sBuff.str();
90  pos = retVal.find(val);
91  }
92  return retVal;
93  }
+ Here is the caller graph for this function:

◆ 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 55 of file utils_string.cpp.

Referenced by gum::build_node(), and gum::BayesNet< double >::fastPrototype().

56  {
57  std::vector< std::string > tokens;
58  size_t prev = 0, pos = 0;
59  do {
60  pos = str.find(delim, prev);
61  if (pos == std::string::npos) pos = str.length();
62  std::string token = str.substr(prev, pos - prev);
63  if (!token.empty()) tokens.push_back(token);
64  prev = pos + delim.length();
65  } while (pos < str.length() && prev < str.length());
66  return tokens;
67  }
+ Here is the caller graph for this function:

◆ 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 37 of file utils_string_inl.h.

Referenced by gum::prm::PRMAggregate< double >::str2enum().

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