Loading...
Searching...
No Matches
PLMD::Tools Class Reference

Empty class which just contains several (static) tools. More...

#include <Tools.h>

Classes

class  CriticalSectionWithKey
 Utility to create named critical sections Key should be usable in a std::map. More...
 
class  DirectoryChanger
 Tiny class that changes directory and comes back when going out of scope. More...
 
class  FastStringUnorderedMap
 Tiny class implementing faster std::string_view access to an unordered_map It exposes a limited number of methods of std::unordered_map. More...
 

Static Public Member Functions

static double bessel0 (const double &val)
 Modified 0th-order Bessel function of the first kind.
 
static bool caseInSensStringCompare (std::string_view str1, std::string_view str2)
 compare two string in a case insensitive manner
 
static std::string concatenateExceptionMessages ()
 Build a concatenated exception message.
 
template<typename T, typename U>
static void convert (const T &t, U &u)
 
static bool convertNoexcept (const std::string &str, AtomNumber &t)
 Convert a string to a atom number, reading it.
 
static bool convertNoexcept (const std::string &str, double &t)
 Convert a string to a double, reading it.
 
static bool convertNoexcept (const std::string &str, float &t)
 Convert a string to a float, reading it.
 
static bool convertNoexcept (const std::string &str, int &t)
 Convert a string to a int, reading it.
 
static bool convertNoexcept (const std::string &str, long double &t)
 Convert a string to a long double, reading it.
 
static bool convertNoexcept (const std::string &str, long int &t)
 Convert a string to a long int, reading it.
 
static bool convertNoexcept (const std::string &str, long long int &t)
 Convert a string to a long long int, reading it.
 
static bool convertNoexcept (const std::string &str, long long unsigned &t)
 Convert a string to a long long unsigned int, reading it.
 
static bool convertNoexcept (const std::string &str, long unsigned &t)
 Convert a string to a long unsigned int, reading it.
 
static bool convertNoexcept (const std::string &str, std::string &t)
 Convert a string to a string (i.e. copy)
 
static bool convertNoexcept (const std::string &str, unsigned &t)
 Convert a string to an unsigned int, reading it.
 
template<typename T>
static bool convertNoexcept (T i, std::string &str)
 Convert anything into a string.
 
static std::string convertRegexForJson (const std::string &command)
 Correct the "escape sequences" from regexes to be compatible with the json format.
 
static std::string extension (const std::string &)
 Extract the extensions from a file name.
 
template<int exp, typename T = double>
static constexpr T fastpow (T base)
 Fast int power for power known at compile time.
 
template<typename T>
static constexpr T fastpow (T base, int exp)
 Fast int power.
 
static bool findKeyword (const std::vector< std::string > &line, const std::string &key)
 Find a keyword on the input line, just reporting if it exists or not.
 
static bool getKey (std::vector< std::string > &line, const std::string &key, std::string &s, int rep=-1)
 Retrieve a key from a vector of options.
 
static bool getline (FILE *, std::string &line)
 Get a line from the file pointer ifile.
 
static bool getParsedLine (IFile &ifile, std::vector< std::string > &line, const bool trimcomments=true)
 Get a parsed line from the file pointer ifile This function already takes care of joining continued lines and splitting the resulting line into an array of words.
 
static std::vector< std::string > getWords (std::string_view line, const char *sep=NULL, int *parlevel=NULL, const char *parenthesis="{", bool delete_parenthesis=true)
 Split the line in words using separators.
 
static void getWordsSimple (gch::small_vector< std::string_view > &words, std::string_view line)
 Faster version This version does not parse parenthesis and operates on a preallocated small_vector of string_view's.
 
static void getWordsSimple (gch::small_vector< std::string_view > &words, std::string_view line, std::string_view sep)
 
static void interpretLabel (std::vector< std::string > &s)
 interpret ":" syntax for labels
 
static void interpretRanges (std::vector< std::string > &)
 Interpret atom ranges.
 
static std::vector< std::string > ls (const std::string &)
 list files in a directory
 
static void ltrim (std::string &s)
 Remove leading blanks.
 
template<class T, class... Args>
static auto make_unique (Args &&... args)
 
static std::unique_ptr< std::lock_guard< std::mutex > > molfile_lock ()
 
template<typename T>
static bool parse (std::string_view argument, T &val)
 Parse the argument and eventually unrave the variant for the current replica.
 
template<typename T>
static bool parse (std::vector< std::string > &line, const std::string &key, T &val, int rep=-1)
 Find a keyword on the input line, eventually deleting it, and saving its value to val.
 
static bool parseFlag (std::vector< std::string > &line, const std::string &key, bool &val)
 Find a keyword without arguments on the input line.
 
template<class T>
static bool parseVector (const std::string_view argument, std::vector< T > &val, int rep=-1)
 
template<class T>
static bool parseVector (std::vector< std::string > &line, const std::string &key, std::vector< T > &val, int rep=-1)
 Find a keyword on the input line, eventually deleting it, and saving its value to a vector.
 
static double pbc (double)
 Apply pbc for a unitary cell.
 
template<typename T>
static void removeDuplicates (std::vector< T > &vec)
 Remove duplicates from a vector of type T.
 
template<typename T, unsigned n, unsigned m>
static void set_to_zero (std::vector< TensorTyped< T, n, m > > &vec)
 
template<typename T, unsigned n>
static void set_to_zero (std::vector< VectorTyped< T, n > > &vec)
 
template<typename T>
static void set_to_zero (T *ptr, const unsigned n)
 
static bool startWith (std::string_view full, std::string_view start)
 Check if a string full starts with string start.
 
static void stripLeadingAndTrailingBlanks (std::string &str)
 removes leading and trailing blanks from a string
 
static void trim (std::string &s)
 Convert anything into anything, throwing an exception in case there is an error Remove trailing blanks.
 
static void trimComments (std::string &s)
 Remove trailing comments.
 
template<typename T>
static std::vector< const T * > unique2raw (const std::vector< std::unique_ptr< const T > > &)
 Tool to create a vector of raw pointers from a vector of unique_pointers.
 
template<typename T>
static std::vector< T * > unique2raw (const std::vector< std::unique_ptr< T > > &)
 Tool to create a vector of raw pointers from a vector of unique_pointers (const version).
 
static std::string_view unravelReplicas (std::string_view argument, int rep=-1)
 

Static Public Attributes

static constexpr std::string_view replicaToken ="@replicas:"
 

Static Private Member Functions

template<class T>
static bool convertToAny (const std::string &str, T &t)
 class to convert a string to a generic type T
 
template<class T>
static bool convertToInt (const std::string &str, T &t)
 class to convert a string to a int type T
 
template<class T>
static bool convertToReal (const std::string &str, T &t)
 class to convert a string to a real type T.
 
template<int exp, typename T = double, std::enable_if_t<(exp >=0), bool > = true>
static constexpr T fastpow_rec (T base, T result)
 the recursive part of the template fastpow implementation
 

Detailed Description

Empty class which just contains several (static) tools.

Member Function Documentation

◆ bessel0()

double PLMD::Tools::bessel0 ( const double & val)
static

Modified 0th-order Bessel function of the first kind.

◆ caseInSensStringCompare()

bool PLMD::Tools::caseInSensStringCompare ( std::string_view str1,
std::string_view str2 )
static

compare two string in a case insensitive manner

◆ concatenateExceptionMessages()

std::string PLMD::Tools::concatenateExceptionMessages ( )
static

Build a concatenated exception message.

Should be called with an in-flight exception.

◆ convert()

template<typename T, typename U>
static void PLMD::Tools::convert ( const T & t,
U & u )
inlinestatic

◆ convertNoexcept() [1/12]

bool PLMD::Tools::convertNoexcept ( const std::string & str,
AtomNumber & t )
static

Convert a string to a atom number, reading it.

◆ convertNoexcept() [2/12]

bool PLMD::Tools::convertNoexcept ( const std::string & str,
double & t )
static

Convert a string to a double, reading it.

◆ convertNoexcept() [3/12]

bool PLMD::Tools::convertNoexcept ( const std::string & str,
float & t )
static

Convert a string to a float, reading it.

◆ convertNoexcept() [4/12]

bool PLMD::Tools::convertNoexcept ( const std::string & str,
int & t )
static

Convert a string to a int, reading it.

◆ convertNoexcept() [5/12]

bool PLMD::Tools::convertNoexcept ( const std::string & str,
long double & t )
static

Convert a string to a long double, reading it.

◆ convertNoexcept() [6/12]

bool PLMD::Tools::convertNoexcept ( const std::string & str,
long int & t )
static

Convert a string to a long int, reading it.

◆ convertNoexcept() [7/12]

bool PLMD::Tools::convertNoexcept ( const std::string & str,
long long int & t )
static

Convert a string to a long long int, reading it.

◆ convertNoexcept() [8/12]

bool PLMD::Tools::convertNoexcept ( const std::string & str,
long long unsigned & t )
static

Convert a string to a long long unsigned int, reading it.

◆ convertNoexcept() [9/12]

bool PLMD::Tools::convertNoexcept ( const std::string & str,
long unsigned & t )
static

Convert a string to a long unsigned int, reading it.

◆ convertNoexcept() [10/12]

bool PLMD::Tools::convertNoexcept ( const std::string & str,
std::string & t )
static

Convert a string to a string (i.e. copy)

◆ convertNoexcept() [11/12]

bool PLMD::Tools::convertNoexcept ( const std::string & str,
unsigned & t )
static

Convert a string to an unsigned int, reading it.

◆ convertNoexcept() [12/12]

template<typename T>
bool PLMD::Tools::convertNoexcept ( T i,
std::string & str )
static

Convert anything into a string.

◆ convertRegexForJson()

std::string PLMD::Tools::convertRegexForJson ( const std::string & command)
static

Correct the "escape sequences" from regexes to be compatible with the json format.

C++ wants a raw string (like R"<<(content)<<") for the regexes and the python package json (and the json format itself actually) wants the various '\' to be escaped with and '\' becasue the interpreter tries to read anything that begins with '\' as an escape sequence (it does not know that it is an input for a regex)

For example '
' will not cause an error but it will be intepreted as and explicit newline, so we we want '\n' in the json whereas '.' will cause an erron when read by python

◆ convertToAny()

template<class T>
bool PLMD::Tools::convertToAny ( const std::string & str,
T & t )
staticprivate

class to convert a string to a generic type T

◆ convertToInt()

template<class T>
bool PLMD::Tools::convertToInt ( const std::string & str,
T & t )
staticprivate

class to convert a string to a int type T

◆ convertToReal()

template<class T>
bool PLMD::Tools::convertToReal ( const std::string & str,
T & t )
staticprivate

class to convert a string to a real type T.

T should be either float, double, or long double

◆ extension()

std::string PLMD::Tools::extension ( const std::string & s)
static

Extract the extensions from a file name.

E.g.: extension("pippo.xyz")="xyz". It only returns extensions with a length between 1 and 4 E.g.: extension("pippo.12345")="" whereas extenion("pippo.1234")="1234"; It is also smart enough to detect "/", so that extension("pippo/.t")="" whereas extension("pippo/a.t")="t"

◆ fastpow() [1/2]

template<int exp, typename T>
T PLMD::Tools::fastpow ( T base)
inlinestaticconstexpr

Fast int power for power known at compile time.

◆ fastpow() [2/2]

template<typename T>
T PLMD::Tools::fastpow ( T base,
int exp )
inlinestaticconstexpr

Fast int power.

◆ fastpow_rec()

template<int exp, typename T, std::enable_if_t<(exp >=0), bool >>
T PLMD::Tools::fastpow_rec ( T base,
T result )
inlinestaticconstexprprivate

the recursive part of the template fastpow implementation

◆ findKeyword()

bool PLMD::Tools::findKeyword ( const std::vector< std::string > & line,
const std::string & key )
static

Find a keyword on the input line, just reporting if it exists or not.

◆ getKey()

bool PLMD::Tools::getKey ( std::vector< std::string > & line,
const std::string & key,
std::string & s,
int rep = -1 )
static

Retrieve a key from a vector of options.

It finds a key starting with "key=" or equal to "key" and copy the part after the = on s. E.g.: line.push_back("aa=xx"); getKey(line,"aa",s); will set s="xx"

◆ getline()

bool PLMD::Tools::getline ( FILE * fp,
std::string & line )
static

Get a line from the file pointer ifile.

◆ getParsedLine()

bool PLMD::Tools::getParsedLine ( IFile & ifile,
std::vector< std::string > & line,
const bool trimcomments = true )
static

Get a parsed line from the file pointer ifile This function already takes care of joining continued lines and splitting the resulting line into an array of words.

◆ getWords()

std::vector< std::string > PLMD::Tools::getWords ( std::string_view line,
const char * sep = NULL,
int * parlevel = NULL,
const char * parenthesis = "{",
bool delete_parenthesis = true )
static

Split the line in words using separators.

It also take into account parenthesis. Outer parenthesis found are removed from output, and the text between them is considered as a single word. Only the outer parenthesis are processed, to allow nesting them. parlevel, if not NULL, is increased or decreased according to the number of opened/closed parenthesis

◆ getWordsSimple() [1/2]

void PLMD::Tools::getWordsSimple ( gch::small_vector< std::string_view > & words,
std::string_view line )
static

Faster version This version does not parse parenthesis and operates on a preallocated small_vector of string_view's.

◆ getWordsSimple() [2/2]

void PLMD::Tools::getWordsSimple ( gch::small_vector< std::string_view > & words,
std::string_view line,
std::string_view sep )
static

◆ interpretLabel()

void PLMD::Tools::interpretLabel ( std::vector< std::string > & s)
static

interpret ":" syntax for labels

◆ interpretRanges()

void PLMD::Tools::interpretRanges ( std::vector< std::string > & s)
static

Interpret atom ranges.

◆ ls()

std::vector< std::string > PLMD::Tools::ls ( const std::string & d)
static

list files in a directory

◆ ltrim()

void PLMD::Tools::ltrim ( std::string & s)
static

Remove leading blanks.

◆ make_unique()

template<class T, class... Args>
static auto PLMD::Tools::make_unique ( Args &&... args)
inlinestatic

◆ molfile_lock()

std::unique_ptr< std::lock_guard< std::mutex > > PLMD::Tools::molfile_lock ( )
static

◆ parse() [1/2]

template<class T>
bool PLMD::Tools::parse ( std::string_view argument,
T & val )
static

Parse the argument and eventually unrave the variant for the current replica.

◆ parse() [2/2]

template<class T>
bool PLMD::Tools::parse ( std::vector< std::string > & line,
const std::string & key,
T & val,
int rep = -1 )
static

Find a keyword on the input line, eventually deleting it, and saving its value to val.

◆ parseFlag()

bool PLMD::Tools::parseFlag ( std::vector< std::string > & line,
const std::string & key,
bool & val )
inlinestatic

Find a keyword without arguments on the input line.

◆ parseVector() [1/2]

template<class T>
bool PLMD::Tools::parseVector ( const std::string_view argument,
std::vector< T > & val,
int rep = -1 )
static

◆ parseVector() [2/2]

template<class T>
bool PLMD::Tools::parseVector ( std::vector< std::string > & line,
const std::string & key,
std::vector< T > & val,
int rep = -1 )
static

Find a keyword on the input line, eventually deleting it, and saving its value to a vector.

◆ pbc()

double PLMD::Tools::pbc ( double x)
inlinestatic

Apply pbc for a unitary cell.

beware: this brings any number into a pbc that ranges from -0.5 to 0.5

◆ removeDuplicates()

template<typename T>
void PLMD::Tools::removeDuplicates ( std::vector< T > & vec)
static

Remove duplicates from a vector of type T.

◆ set_to_zero() [1/3]

template<typename T, unsigned n, unsigned m>
static void PLMD::Tools::set_to_zero ( std::vector< TensorTyped< T, n, m > > & vec)
inlinestatic

◆ set_to_zero() [2/3]

template<typename T, unsigned n>
static void PLMD::Tools::set_to_zero ( std::vector< VectorTyped< T, n > > & vec)
inlinestatic

◆ set_to_zero() [3/3]

template<typename T>
static void PLMD::Tools::set_to_zero ( T * ptr,
const unsigned n )
inlinestatic

◆ startWith()

bool PLMD::Tools::startWith ( std::string_view full,
std::string_view start )
static

Check if a string full starts with string start.

Same as full.find(start)==0, but faster

◆ stripLeadingAndTrailingBlanks()

void PLMD::Tools::stripLeadingAndTrailingBlanks ( std::string & str)
static

removes leading and trailing blanks from a string

◆ trim()

void PLMD::Tools::trim ( std::string & s)
static

Convert anything into anything, throwing an exception in case there is an error Remove trailing blanks.

◆ trimComments()

void PLMD::Tools::trimComments ( std::string & s)
static

Remove trailing comments.

◆ unique2raw() [1/2]

template<typename T>
std::vector< const T * > PLMD::Tools::unique2raw ( const std::vector< std::unique_ptr< const T > > & x)
static

Tool to create a vector of raw pointers from a vector of unique_pointers.

See the non const version.

◆ unique2raw() [2/2]

template<typename T>
std::vector< T * > PLMD::Tools::unique2raw ( const std::vector< std::unique_ptr< T > > & x)
static

Tool to create a vector of raw pointers from a vector of unique_pointers (const version).

Returning a vector is fast in C++11. It can be used in order to feed a vector<unique_ptr<T>> to a function that takes a vector<T*>.

// some function that takes a vec
void func(std::vector<Data*> & vec);
std::vector<std::unique_ptr<Data>> vec;
// func(vec); // does not compile
func(Tools::unique2raw(vec)); // compiles

Notice that the conversion is fast but takes some time to allocate the new vector and copy the pointers. In case the function acting on the vector<T*> is very fast and we do not want to add significant overhead, it might be convenient to store a separate set of raw pointers.

// some function that takes a vec
void func(std::vector<Data*> & vec);
std::vector<std::unique_ptr<Data>> vec;

// conversion done only once:
auto vec_ptr=Tools::unique2raw(vec);

for(int i=0;i<1000;i++){
  func(vec_ptr);
}

◆ unravelReplicas()

std::string_view PLMD::Tools::unravelReplicas ( std::string_view argument,
int rep = -1 )
static

Member Data Documentation

◆ replicaToken

std::string_view PLMD::Tools::replicaToken ="@replicas:"
staticconstexpr

The documentation for this class was generated from the following files: