aGrUM  0.13.2
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 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)
 check if a vector consists in unique values (no duplicate). 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

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 112 of file utils_misc.h.

Function Documentation

void gum::__atexit ( )

Used for debug purpose.

Definition at line 41 of file utils_misc.cpp.

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

41  {
42 #ifndef NDEBUG
44 #endif
45  }
void __atexit()
Used for debug purpose.
Definition: utils_misc.cpp:41

+ Here is the caller graph for this function:

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 47 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().

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

+ Here is the caller graph for this function:

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

define a random_engine with correct seed

Definition at line 46 of file utils_random.cpp.

References gum::randomGeneratorSeed().

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

46  {
47  std::default_random_engine generator;
48  generator.seed(seed ? seed : randomGeneratorSeed());
49  return generator;
50  }
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:

std::string gum::getUniqueFileName ( )

Returns a path to a unique file name.

Returns
Returns a path to a unique file name.

Definition at line 34 of file utils_string.cpp.

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

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

+ Here is the caller graph for this function:

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

Referenced by gum::build_node().

62  {
63  if (x.size() <= 1) return true;
64  if (x.size() == 2) return x[0] != x[1];
65 
66  auto refless = [](T const* l, T const* r) { return *l < *r; };
67  auto refeq = [](T const* l, T const* r) { return *l == *r; };
68 
69  std::vector< T const* > vp;
70  vp.reserve(x.size());
71  for (size_t i = 0; i < x.size(); ++i)
72  vp.push_back(&x[i]);
73  sort(vp.begin(), vp.end(), refless); // O(N log N)
74  // if no adjacent pair (vp_n,vp_n+1) has *vp_n == *vp_n+1
75  return std::adjacent_find(vp.begin(), vp.end(), refeq) == vp.end();
76  }

+ Here is the caller graph for this function:

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

Initialize random generator seed.

Definition at line 38 of file utils_random_inl.h.

References gum::randomGeneratorSeed().

38  {
39  if (seed) {
40  srand(seed);
41  } else {
42  srand(randomGeneratorSeed());
43  }
44  }
unsigned int randomGeneratorSeed()
Returns the aGrUM&#39;s seed used by the std::generators.

+ Here is the call graph for this function:

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 31 of file utils_misc.cpp.

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

31  {
32  unsigned char* in = (unsigned char*)_in;
33  unsigned char* out = (unsigned char*)_out;
34 
35  for (unsigned long i = 0; i < size; ++i)
36  if (*(in++) != *(out++)) return false;
37 
38  return true;
39  }

+ Here is the caller graph for this function:

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

56  {
57  stream << "(" << val.first << "," << val.second << ")";
58  return stream;
59  }
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 38 of file utils_misc_tpl.h.

38  {
39  bool deja = false;
40  stream << "[";
41 
42  for (const auto& v : val) {
43  if (deja)
44  stream << " , ";
45  else
46  deja = true;
47  stream << v;
48  }
49 
50  stream << "]";
51 
52  return stream;
53  }
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 32 of file utils_random_tpl.h.

References gum::randomProba().

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

+ Here is the call graph for this function:

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

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

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

+ Here is the caller graph for this function:

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

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

35 { return ((double)rand()) / ((double)RAND_MAX); }

+ Here is the caller graph for this function:

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

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

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

+ Here is the caller graph for this function:

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

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

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

+ Here is the caller graph for this function:

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

Returns the lowercase version of str.

Returns
Returns the lowercase version of str.

Definition at line 34 of file utils_string_inl.h.

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

34  {
35  std::transform(str.begin(), str.end(), str.begin(), ::tolower);
36  return str;
37  }

+ Here is the caller graph for this function: