Fleet  0.0.9
Inference in the LOT
Functions | Variables
Strings.h File Reference
#include <sstream>
#include <queue>
#include <array>
#include <map>
#include <set>
#include <atomic>
#include <string.h>
#include "Miscellaneous.h"
#include "Numerics.h"
#include "Random.h"
#include "Vector3D.h"
Include dependency graph for Strings.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

template<typename T >
std::string str (T x)
 
std::string str (const std::string &x)
 
template<typename... T, size_t... I>
std::string str (const std::tuple< T... > &x, std::index_sequence< I... > idx)
 
template<typename... T>
std::string str (const std::tuple< T... > &x)
 
template<typename A , typename B >
std::string str (const std::pair< A, B > x)
 
template<typename T , size_t N>
std::string str (const std::array< T, N > &a)
 
template<typename T >
std::string str (const std::vector< T > &a, const std::string _sep=",")
 
template<typename T , typename U >
std::string str (const std::map< T, U > &a)
 
template<typename T >
std::string str (const std::set< T > &a)
 
template<typename T >
std::string str (const std::atomic< T > &a)
 
template<typename... Args>
std::string str (std::string _sep, Args... args)
 
template<typename T >
std::string to_string_with_precision (const T a_value, const int n=14)
 
template<typename T >
bool is_prefix (const T &prefix, const T &x)
 Check if prefix is a prefix of x – works with iterables, including strings and vectors. More...
 
bool contains (const std::string &s, const std::string &x)
 
bool contains (const std::string &s, const char x)
 
void replace_all (std::string &s, const std::string &x, const std::string &y, int add=0)
 Replace all occurances of x with y in s. More...
 
template<const float & add_p, const float & del_p>
double p_delete_append (const std::string &x, const std::string &y, const float log_alphabet)
 Probability of converting x into y by deleting some number (each with del_p, then stopping with prob 1-del_p), adding with probability add_p, and then when we add selecting from an alphabet of size alpha_n. More...
 
std::deque< std::string > split (const std::string &s, const char delimiter)
 Split is returns a deque of s split up at the character delimiter. It handles these special cases: split("a:", ':') -> ["a", ""] split(":", ':') -> [""] split(":a", ':') -> ["", "a"]. More...
 
std::pair< std::string, std::string > divide (const std::string &s, const char delimiter)
 
unsigned int levenshtein_distance (const std::string &s1, const std::string &s2)
 
size_t count (const std::string &str, const std::string &sub)
 
size_t count (const std::string &str, const char x)
 
std::string reverse (std::string x)
 
std::string QQ (std::string x)
 
std::string Q (std::string x)
 
void check_alphabet (const std::string &s, const std::string &a)
 Check that s only uses characters from a. On failure, we print the string and assert false. More...
 
void check_alphabet (std::vector< std::string > t, const std::string &a)
 
double p_KashyapOommen1984_edit (const std::string x, const std::string y, const double perr, const size_t nalphabet)
 The string probability model from Kashyap & Oommen, 1983, basically giving a string edit distance that is a probability model. This could really use some unit tests, but it is hard to find implementations. More...
 
template<typename T >
string_to (const std::string s)
 Fleet includes this templated function to allow us to convert strings to a variety of formats. This is mostly used for reading data from text files. This recursively will unpack containers with some standard delimiters. Since it can handle Fleet types (e.g. defaultdata_t) it can be used to unpack data stored in strings (see e.g. Models/Sorting) More...
 
template<>
std::string string_to (const std::string s)
 
template<>
int string_to (const std::string s)
 
template<>
long string_to (const std::string s)
 
template<>
unsigned long string_to (const std::string s)
 
template<>
double string_to (const std::string s)
 
template<>
float string_to (const std::string s)
 
template<>
bool string_to (const std::string s)
 

Variables

const std::string EMPTY_STRING = ""
 
const std::string LAMBDA_STRING = "\u03BB"
 
const std::string LAMBDAXDOT_STRING = LAMBDA_STRING+"x."
 

Function Documentation

◆ check_alphabet() [1/2]

void check_alphabet ( const std::string &  s,
const std::string &  a 
)

Check that s only uses characters from a. On failure, we print the string and assert false.

Parameters
s
a

◆ check_alphabet() [2/2]

void check_alphabet ( std::vector< std::string >  t,
const std::string &  a 
)

◆ contains() [1/2]

bool contains ( const std::string &  s,
const std::string &  x 
)

◆ contains() [2/2]

bool contains ( const std::string &  s,
const char  x 
)

◆ count() [1/2]

size_t count ( const std::string &  str,
const std::string &  sub 
)

How many times does sub occur in str? Does not count overlapping substrings

Parameters
str
sub
Returns

◆ count() [2/2]

size_t count ( const std::string &  str,
const char  x 
)

◆ divide()

std::pair<std::string, std::string> divide ( const std::string &  s,
const char  delimiter 
)

◆ is_prefix()

template<typename T >
bool is_prefix ( const T &  prefix,
const T &  x 
)

Check if prefix is a prefix of x – works with iterables, including strings and vectors.

Parameters
prefix
x
Returns

For any number of iterable types, is prefix a prefix of x

Parameters
prefix
x
Returns

◆ levenshtein_distance()

unsigned int levenshtein_distance ( const std::string &  s1,
const std::string &  s2 
)

Compute levenshtein distiance between two strings (NOTE: Or O(N^2))

Parameters
s1
s2
Returns

◆ p_delete_append()

template<const float & add_p, const float & del_p>
double p_delete_append ( const std::string &  x,
const std::string &  y,
const float  log_alphabet 
)
inline

Probability of converting x into y by deleting some number (each with del_p, then stopping with prob 1-del_p), adding with probability add_p, and then when we add selecting from an alphabet of size alpha_n.

Parameters
x
y
del_p- probability of deleting the next character (geometric)
add_p- probability of adding (geometric)
log_alphabet- log of the size of alphabet
Returns
The probability of converting x to y by deleting characters with probability del_p and then adding with probability add_p

This function computes the probability that x would be converted into y, when we insert with probability add_p and delete with probabiltiy del_p and when we add we add from an alphabet of size log_alphabet. Note that this is a template function because otherwise we end up computing log(add_p) and log(del_p) a lot, and these are in fact constant.

Parameters
x
y
log_alphabet
Returns

◆ p_KashyapOommen1984_edit()

double p_KashyapOommen1984_edit ( const std::string  x,
const std::string  y,
const double  perr,
const size_t  nalphabet 
)

The string probability model from Kashyap & Oommen, 1983, basically giving a string edit distance that is a probability model. This could really use some unit tests, but it is hard to find implementations.

This assumes that the deletions, insertions, and changes all happen with a constant, equal probability of perr, but note that swaps and insertions also have to choose the character out of nalphabet. This could probably be optimized to not have to compute these logs

Parameters
x
y
perr
nalphabet
Returns

◆ Q()

std::string Q ( std::string  x)

Handy adding single quotes to a string

Parameters
x- input string
Returns

◆ QQ()

std::string QQ ( std::string  x)

Handy adding double quotes to a string

Parameters
x- input string
Returns

◆ replace_all()

void replace_all ( std::string &  s,
const std::string &  x,
const std::string &  y,
int  add = 0 
)

Replace all occurances of x with y in s.

Parameters
s
x
y
add– replace x plus this many characters

◆ reverse()

std::string reverse ( std::string  x)

◆ split()

std::array< std::string, N > split ( const std::string &  s,
const char  delimiter 
)

Split is returns a deque of s split up at the character delimiter. It handles these special cases: split("a:", ':') -> ["a", ""] split(":", ':') -> [""] split(":a", ':') -> ["", "a"].

Split iwith a fixed return size, useful in parsing csv.

Parameters
s
delimiter
Returns

◆ str() [1/11]

template<typename T >
std::string str ( x)

c A pythonesque string function

Parameters
x
Returns

◆ str() [2/11]

std::string str ( const std::string &  x)

◆ str() [3/11]

template<typename... T, size_t... I>
std::string str ( const std::tuple< T... > &  x,
std::index_sequence< I... >  idx 
)

A pythonesque string function

Parameters
x
Returns

◆ str() [4/11]

template<typename... T>
std::string str ( const std::tuple< T... > &  x)

A pythonesque string function

Parameters
x
Returns

◆ str() [5/11]

template<typename A , typename B >
std::string str ( const std::pair< A, B >  x)

A pythonesque string function

Parameters
x
Returns

◆ str() [6/11]

template<typename T , size_t N>
std::string str ( const std::array< T, N > &  a)

A pythonesque string function

Parameters
x
Returns

◆ str() [7/11]

template<typename T >
std::string str ( const std::vector< T > &  a,
const std::string  _sep = "," 
)

A pythonesque string function

Parameters
x
Returns

◆ str() [8/11]

template<typename T , typename U >
std::string str ( const std::map< T, U > &  a)

A pythonesque string function

Parameters
x
Returns

◆ str() [9/11]

template<typename T >
std::string str ( const std::set< T > &  a)

A pythonesque string function

Parameters
x
Returns

◆ str() [10/11]

template<typename T >
std::string str ( const std::atomic< T > &  a)

A pythonesque string function

Parameters
x
Returns

◆ str() [11/11]

template<typename... Args>
std::string str ( std::string  _sep,
Args...  args 
)

◆ string_to() [1/8]

template<typename T >
T string_to ( const std::string  s)

Fleet includes this templated function to allow us to convert strings to a variety of formats. This is mostly used for reading data from text files. This recursively will unpack containers with some standard delimiters. Since it can handle Fleet types (e.g. defaultdata_t) it can be used to unpack data stored in strings (see e.g. Models/Sorting)

Parameters
s
Returns

◆ string_to() [2/8]

template<>
std::string string_to ( const std::string  s)

◆ string_to() [3/8]

template<>
int string_to ( const std::string  s)

◆ string_to() [4/8]

template<>
long string_to ( const std::string  s)

◆ string_to() [5/8]

template<>
unsigned long string_to ( const std::string  s)

◆ string_to() [6/8]

template<>
double string_to ( const std::string  s)

◆ string_to() [7/8]

template<>
float string_to ( const std::string  s)

◆ string_to() [8/8]

template<>
bool string_to ( const std::string  s)

◆ to_string_with_precision()

template<typename T >
std::string to_string_with_precision ( const T  a_value,
const int  n = 14 
)

Variable Documentation

◆ EMPTY_STRING

const std::string EMPTY_STRING = ""

◆ LAMBDA_STRING

const std::string LAMBDA_STRING = "\u03BB"

◆ LAMBDAXDOT_STRING

const std::string LAMBDAXDOT_STRING = LAMBDA_STRING+"x."