KrisLibrary  1.0.0
Files | Namespaces | Classes | Functions
Utils

Files

file  arrayutils.h
 Convenience routines for C arrays and STL vectors.
 
file  combination.h
 Various combination utilities.
 
file  fileutils.h
 Cross-platform wrapper for file utilities.
 
file  ioutils.h
 Utilities for I/O.
 
file  iteratorutils.h
 Provides basic utilities to increment/decrement an iterator by a certain amount, 2D range iterators, etc.
 
file  permutation.h
 Various permutation utilities.
 
file  shift.h
 Cyclical shifts of array elements.
 
file  stringutils.h
 Utilities for string manipulation.
 

Namespaces

 ArrayUtils
 Convenience routines for C arrays and STL vectors.
 

Classes

struct  ArrayMapping
 An invertible mapping from indices in [0,imax), which operates in two modes: offset mode adds an offset, and mapping mode stores an explicit mapping. More...
 
struct  std::cmp_func< T, Less >
 A templated function class like strcmp: returns -1 if a<b, 0 if a==b, 1 if a>b. More...
 
class  Combination
 A class that enumerates combinations. More...
 
struct  EZTrace
 A Trace that dumps on destruction. Only one may be active at any time. More...
 
struct  EZCallTrace
 Helper class that eases function call tracing. More...
 
struct  FastSampler
 Samples from a weighted discrete set in O(log(n)) time after an O(n) initialization. More...
 
struct  IntPair
 A lightweight integer 2-tuple class. More...
 
struct  IntTriple
 A lightweight integer 3-tuple class. More...
 
struct  IntTuple
 An integer tuple class. More...
 
class  Permutation
 A class that enumerates permutations. More...
 
class  ProgressPrinter
 Prints the progress of an iterative, long computation. More...
 
struct  StandardRNG
 Interface for a random number generator. More...
 
struct  RNG48
 uses the ANSI C rand48 functions More...
 
class  RangeMap< T >
 Same as RangeSet, but a map rather than a set. O(1) find as well. More...
 
class  RangeMap2D< T >
 Same as RangeSet2D but a map. O(1) find as well as count. More...
 
class  RangeSet
 A set of integers within a range. Operates in two modes, set or bit vector mode. In bit-vector mode, allows O(1) membership testing (but the range is fixed). Set mode is just like a regular set. More...
 
class  RangeSet2D
 A set of 2d indices within a range. Operates in two modes, set or bit matrix mode. In bit-matrix mode, allows O(1) membership testing (but the range is fixed). Set mode is just like a regular set. More...
 
class  SignalHandler
 A base class for an object-oriented signal handler. Properly restores old signal handlers once the class is destroyed. More...
 
class  SimpleParser
 A simple class to break files into tokens, punctuation, comments, and whitespace. More...
 
class  SmartPointer< T >
 A smart pointer class. Performs automatic reference counting. More...
 
struct  StatCollector
 Collects statistics (min,max,mean,stddev,etc) on floating-point data. More...
 
struct  StatDatabase
 A heirarchical database of statistical data (counts and StatCollectors). Useful for performance testing. More...
 
class  UnionFind
 From an indexed set of elments, allows fast unioning of the elements into sets. More...
 

Functions

void FirstCombination (std::vector< int > &v, int n)
 
void LastCombination (std::vector< int > &v, int n)
 
int NextCombination (std::vector< int > &v, int n)
 
int PrevCombination (std::vector< int > &v, int n)
 
vector< bool > CalculateCoverset_BruteForce (const vector< vector< bool > > &sets)
 Calculate a minimal subset of items that has a non-empty intersection with each of the given sets. More...
 
vector< int > CalculateSetCover_Greedy (const vector< vector< bool > > &sets)
 Picks the smallest subset of the given sets that covers the whole space. More...
 
void InverseMapping (const std::vector< int > &fwd, std::vector< vector< int > > &bwd)
 For a mapping f:i->fwd[i], this gets the inverse multi-mapping g:j->{i|fwd[i]=j}.
 
template<class T , class EqFn >
void EquivalenceMap (const std::vector< T > &x, std::vector< vector< int > > &eq, EqFn &Eq)
 Forms the equivalence map on x, given an equivalence function. More...
 
void EatWhitespace (std::istream &in)
 Eats up whitespace at beginning of stream.
 
bool InputToken (std::istream &in, const char *characterSet, std::string &)
 
bool InputQuotedString (std::istream &in, char *str, int n)
 Gets a quoted string from the istream into a char buffer (or string)
 
bool InputQuotedString (std::istream &in, std::string &)
 
void OutputQuotedString (std::ostream &out, const char *str)
 Outputs "str". Outputs " for quote characters in str.
 
void OutputQuotedString (std::ostream &out, const std::string &)
 
bool StringContainsQuote (char *str)
 Returns true if str has a quote character.
 
bool StringContainsQuote (const std::string &str)
 
bool StringRequiresQuoting (char *str)
 Returns true if outputting string requires quotations.
 
bool StringRequiresQuoting (const std::string &str)
 
bool SafeInputString (std::istream &in, char *str, int n)
 Inputs the string with quotes if necessary.
 
bool SafeInputString (std::istream &in, std::string &)
 
void SafeOutputString (std::ostream &out, const char *str)
 Outputs the string with quotes if necessary.
 
void SafeOutputString (std::ostream &out, const std::string &)
 
bool SafeInputFloat (std::istream &in, float &f)
 I/O with denormalized floats (infinity, NaN)
 
bool SafeInputFloat (std::istream &in, double &f)
 
bool SafeOutputFloat (std::ostream &out, float f)
 
bool SafeOutputFloat (std::ostream &out, double f)
 
int TranslateEscape (int c)
 
std::string TranslateEscapes (const std::string &str)
 
bool GetFileContents (const char *filename, std::string &contents)
 Returns the entire contents of a file as a string.
 
bool GetURLContents (const char *url, std::string &contents)
 
bool GetURLDownload (const char *url, const char *filename)
 
template<class type >
bool ReadFile (File &f, std::vector< type > &v)
 ReadFile() for STL vectors. See File.h.
 
template<class type >
bool WriteFile (File &f, const std::vector< type > &v)
 WriteFile() for STL vectors. See File.h.
 
template<class type >
bool InputVector (std::istream &in, std::vector< type > &v)
 
template<class type >
bool OutputVector (std::ostream &out, const std::vector< type > &v)
 
template<class T >
increment (const T &a, int n=1)
 
template<class T >
decrement (const T &a, int n=1)
 
template<class T >
int iterator_diff (const T &a, const T &b)
 
void IdentityPermutation (int v[], int n)
 
template<class T >
void RandomlyPermute (T v[], int n)
 
void RandomPermutation (int v[], int n)
 
void IdentityPermutation (std::vector< int > &v)
 
template<class T >
void RandomlyPermute (std::vector< T > &v)
 
void RandomPermutation (std::vector< int > &v)
 
void FirstPermutation (int v[], int k, int n)
 
void FirstPermutation (std::vector< int > &v, int n)
 
void LastPermutation (int v[], int k, int n)
 
void LastPermutation (std::vector< int > &v, int n)
 
int NextPermutation (int v[], int k, int n)
 
int NextPermutation (std::vector< int > &v, int n)
 
int PrevPermutation (int v[], int k, int n)
 
int PrevPermutation (std::vector< int > &v, int n)
 
template<class T >
void ShiftForward (T &a, T &b, T &c)
 Shift forward: set a'=c,b'=a,c'=b.
 
template<class T >
void ShiftBackward (T &a, T &b, T &c)
 Shift backward: set a'=b,b'=c,c'=a.
 
template<class T >
void ShiftForward (std::vector< T > &v)
 For all i, v'[i] = v[i-1], v'[0] = v[n-1].
 
template<class T >
void ShiftBackward (std::vector< T > &v)
 For all i, v'[i] = v[i+1], v'[n-1] = v[0].
 
char CloseBracket (char c)
 Returns a "close bracket" character opposite c.
 
void Lowercase (char *str)
 Turns the string into lower/uppercase.
 
void Uppercase (char *str)
 
void Lowercase (std::string &str)
 
void Uppercase (std::string &str)
 
std::string Strip (const std::string &str)
 
std::string LStrip (const std::string &str)
 
std::string RStrip (const std::string &str)
 
std::vector< std::string > Split (const std::string &str, const std::string &delim)
 
int ReplaceAll (std::string &str, const char *strfind, const char *strreplace)
 Replace all instances of strfind with strreplace in str.
 
bool StartsWith (const char *str, const char *prefix)
 Returns true if the beginning of the given string matches prefix.
 
bool EndsWith (const char *str, const char *suffix)
 Returns true if the end of the given string matches prefix.
 
bool IsValidCToken (const char *str)
 
bool IsValidInteger (const char *str)
 
bool IsValidFloat (const char *str)
 
int DetectNumericalPattern (const char *str, char prefix[], char suffix[], int &numDigits)
 
void IncrementStringDigits (char *str)
 
void IncrementStringDigits (std::string &str)
 
void ToBase64 (const std::string &in, std::string &out)
 
void ToBase64 (const char *in, int length, std::string &out)
 
void FromBase64 (const std::string &in, std::string &out)
 
void FromBase64 (const char *in, std::string &out)
 
std::string ToBase64 (const std::string &in)
 
std::string ToBase64 (const char *in, int length)
 
std::string FromBase64 (const std::string &in)
 
std::string FromBase64 (const char *in)
 
int LengthWithDOSEndlines (const char *str)
 Dos-unix endline conversion.
 
bool EndlinesToDOS (const char *str, char *out, int max)
 
bool EndlinesFromDOS (const char *str, char *out, int max)
 
void EndlinesToDOS (std::string &str)
 
void EndlinesFromDOS (std::string &str)
 
const char * FileExtension (const char *str)
 Returns pointer to "ext" for str="filename.ext".
 
void ChangeFileExtension (char *str, const char *ext)
 Replaces the file extension of str with ext, or concatenates .ext onto str.
 
const char * GetFileName (const char *str)
 Returns "file.ext" for the str="dir1/dir2/.../file.ext".
 
void GetFilePath (const char *str, char *buf)
 Extracts the path from str (formatted as above) into buf, not including the trailing '/'.
 
void StripExtension (char *str)
 Removes the file extension of str.
 
std::string FileExtension (const std::string &str)
 Returns "ext" for str="filename.ext".
 
void ChangeFileExtension (std::string &str, const char *ext)
 Replaces the file extension of str with ext, or concatenates .ext onto str.
 
std::string GetFileName (const std::string &str)
 Returns "file.ext" for the str="dir1/dir2/.../file.ext".
 
std::string GetFilePath (const std::string &str)
 Extracts the path from str (formatted as above), not including the trailing '/'.
 
void StripExtension (std::string &str)
 Removes the file extension of str.
 
void SplitPath (const std::string &path, std::vector< std::string > &elements)
 
std::string ReducePath (const std::string &path)
 
std::string JoinPath (const std::vector< std::string > &path, char delim=0)
 
std::string JoinPath (const std::string &path1, const std::string &path2)
 

Detailed Description

Function Documentation

vector<bool> CalculateCoverset_BruteForce ( const vector< vector< bool > > &  sets)

Calculate a minimal subset of items that has a non-empty intersection with each of the given sets.

Each bit vector in the sets vector marks whether or not an item is included.

The greedy/incremental algorithm is an approximation algorithm, with degree of approximation H(s) where s is the number of subsets, and H is the harmonic number.

vector<int> CalculateSetCover_Greedy ( const vector< vector< bool > > &  sets)

Picks the smallest subset of the given sets that covers the whole space.

Each bit vector in the sets vector marks whether or not an item is included.

Very similar to the coverset problem.

The greedy algorithm is an approximation algorithm, with degree of approximation H(s) where s is the number of subsets is the harmonic number.

int DetectNumericalPattern ( const char *  str,
char  prefix[],
char  suffix[],
int &  numDigits 
)

Detects a pattern in str = [prefix][digits][suffix]. Returns the number specified by [digits], or -1 if no such pattern is found.

Detects a pattern in str = [prefix][digits][suffix] Returns the integer in [digits], or -1 if no such pattern is found.

References DetectNumericalPattern().

Referenced by DetectNumericalPattern().

template<class T , class EqFn >
void EquivalenceMap ( const std::vector< T > &  x,
std::vector< vector< int > > &  eq,
EqFn &  Eq 
)

Forms the equivalence map on x, given an equivalence function.

Forms a graph where an edge (i,j) exists if x[i]=x[j] for some i,j. Upon exit, each entry eq[k] contains a connected component of the graph.

The algorithm makes at most n(n+1)/2 comparisons.

References UnionFind::FindSet(), UnionFind::GetSets(), InverseMapping(), and UnionFind::Union().

bool GetURLContents ( const char *  url,
std::string &  contents 
)

Returns the contents of a URL as a string. Requires libcurl to be available when CMake is run.

Referenced by GetFileContents().

bool GetURLDownload ( const char *  url,
const char *  filename 
)

Downloads the contents of a URL to a file Requires libcurl to be available when CMake is run.

Referenced by GetFileContents().

bool InputToken ( std::istream &  in,
const char *  characterSet,
std::string &   
)

Inputs a "token" consisting of only the characters in characterSet. Does not ignore whitespace!

template<class type >
bool InputVector ( std::istream &  in,
std::vector< type > &  v 
)

Inputs a vector from an iostream using the format size v[0] v[1] ... v[size-1]

std::string JoinPath ( const std::vector< std::string > &  path,
char  delim = 0 
)

For elements dir1, dir2, ..., file, returns dir1/dir2/.../file in a cross-platform manner. The elements may also have leading/trailing path delimiters which are ignored

References JoinPath().

Referenced by ResourceLibrary::AddBaseDirectory(), ResourceLibrary::ChangeBaseDirectory(), JoinPath(), ResourceLibrary::LazyLoadAll(), ResourceLibrary::LoadAll(), and ReducePath().

int NextPermutation ( int  v[],
int  k,
int  n 
)
inline

Gets the next lexicographically ordered combination. Returns 1 if it reaches the end.

int NextPermutation ( std::vector< int > &  v,
int  n 
)
inline

Gets the next lexicographically ordered combination. Returns 1 if it reaches the end.

template<class type >
bool OutputVector ( std::ostream &  out,
const std::vector< type > &  v 
)

Outputs a vector to an iostream using the format size v[0] v[1] ... v[size-1]

int PrevPermutation ( int  v[],
int  k,
int  n 
)
inline

Gets the previous lexicographically ordered combination. Returns 1 if it reaches the beginning.

int PrevPermutation ( std::vector< int > &  v,
int  n 
)
inline

Gets the previous lexicographically ordered combination. Returns 1 if it reaches the beginning.

std::string ReducePath ( const std::string &  path)

For a path that might contain . and .. references this will take out the "." and non-leading ".." elements. For example, if the input is "dir/./subdir/../" this will return "dir".

References JoinPath(), and SplitPath().

void SplitPath ( const std::string &  path,
std::vector< std::string > &  elements 
)

For a path dir1/dir2/.../file, splits the path into dir1, dir2, ..., file in a cross platform manner

Referenced by ResourceLibrary::ChangeBaseDirectory(), and ReducePath().

int TranslateEscape ( int  c)

If c is preceded by a \, returns the translated ascii character. e.g. n->
, t->, etc.

References TranslateEscape().

Referenced by TranslateEscape().