Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
Loading...
Searching...
No Matches
Utils.h File Reference
#include <algorithm>
#include <complex>
#include <iomanip>
#include <iostream>
#include <ranges>
#include <string>
#include <vector>
#include <unordered_set>
#include <unordered_map>
#include <map>
#include <memory>
#include <limits>
Include dependency graph for Utils.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef std::complex< double > complex_t
 Convenience alias for std::complex<double>.
 

Functions

bool ends_with (const std::string &str, const std::string &suffix)
 Tests whether a string ends with a given suffix.
 
std::vector< std::string > split (const std::string &s, char delimiter)
 Splits a string into parts using a single-character delimiter.
 
std::string doubleToString (double value, int precision)
 Converts a double to a fixed-format string with a given precision.
 
std::string to_lowercase (const std::string &str)
 Returns a lowercase copy of the input string.
 
template<typename T , typename U >
std::unordered_set< Tget_keys (const std::map< T, U > &map)
 Extracts the key set from a std::map into an unordered_set.
 
template<typename T , typename U >
std::unordered_set< Tget_keys (const std::unordered_map< T, U > &map)
 Extracts the key set from a std::unordered_map into an unordered_set.
 
template<typename T , typename U >
bool haveSameKeys (const std::map< T, U > &map1, const std::map< T, U > &map2)
 Checks whether two std::map instances have the same set of keys.
 
template<typename T >
void print_value (std::ostream &os, const T &value)
 Generic helper to print a value to an output stream.
 
template<typename T >
void print_value (std::ostream &os, const std::shared_ptr< T > &ptr)
 Specialization of print_value for std::shared_ptr.
 
template<typename Map >
std::enable_if_t< std::is_same_v< typename Map::value_type, std::pair< const typename Map::key_type, typename Map::mapped_type > >, std::ostream & > operator<< (std::ostream &os, const Map &m)
 Generic stream output operator for map-like containers.
 
template<typename T , std::size_t cache_size, typename Func , typename... Args>
void fill_cache (Func &&f, double a, double b, std::array< T, cache_size > &cache, Args &&... args)
 Fills a lookup cache for a function on a finite interval [a, b].
 
template<typename T , typename U , std::size_t cache_size>
T lerp (U x, const std::array< T, cache_size > &lookup, double a=0.0, double b=1.0)
 Linearly interpolates a cached function on [a, b].
 

Variables

const std::numeric_limits< double > nld = *new std::numeric_limits<double>
 Numeric limits for double-precision floating point.
 

Typedef Documentation

◆ complex_t

Convenience alias for std::complex<double>.

Definition at line 35 of file Utils.h.

Function Documentation

◆ doubleToString()

std::string doubleToString ( double  value,
int  precision 
)

Converts a double to a fixed-format string with a given precision.

The output uses std::fixed formatting and precision digits after the decimal point.

Parameters
valueFloating-point value to convert.
precisionNumber of digits after the decimal point.
Returns
Formatted string representation of value.

Definition at line 25 of file Utils.cpp.

◆ ends_with()

bool ends_with ( const std::string &  str,
const std::string &  suffix 
)

Tests whether a string ends with a given suffix.

The comparison is case-sensitive and operates on the raw bytes of the strings.

Parameters
strString to test.
suffixSuffix to look for.
Returns
true if str ends with suffix, false otherwise.

Definition at line 3 of file Utils.cpp.

◆ fill_cache()

template<typename T , std::size_t cache_size, typename Func , typename... Args>
void fill_cache ( Func &&  f,
double  a,
double  b,
std::array< T, cache_size > &  cache,
Args &&...  args 
)

Fills a lookup cache for a function on a finite interval [a, b].

The function f is evaluated at cache_size sample points in the interval [a, b], and the results are stored in cache. The first and last points are slightly shifted away from the exact endpoints using nld.epsilon() to avoid potential issues (e.g. singularities) exactly at the boundaries.

The intermediate points are placed uniformly between a and b.

Template Parameters
TValue type stored in the cache.
cache_sizeSize of the cache array.
FuncCallable type; must be invocable as f(double, Args...).
ArgsAdditional argument types forwarded to f.
Parameters
fFunction to sample.
aLower bound of the interval.
bUpper bound of the interval.
cachePreallocated array to fill with sampled values.
argsAdditional arguments forwarded to f.

Definition at line 241 of file Utils.h.

◆ get_keys() [1/2]

template<typename T , typename U >
std::unordered_set< T > get_keys ( const std::map< T, U > &  map)
inline

Extracts the key set from a std::map into an unordered_set.

This utility is useful when one is interested only in the set of keys (e.g. to compare maps by their domains rather than by their values).

Template Parameters
TKey type of the map.
UMapped type of the map.
Parameters
mapInput std::map.
Returns
An unordered_set containing all keys of map.

Definition at line 108 of file Utils.h.

◆ get_keys() [2/2]

template<typename T , typename U >
std::unordered_set< T > get_keys ( const std::unordered_map< T, U > &  map)
inline

Extracts the key set from a std::unordered_map into an unordered_set.

Template Parameters
TKey type of the map.
UMapped type of the map.
Parameters
mapInput std::unordered_map.
Returns
An unordered_set containing all keys of map.

Definition at line 125 of file Utils.h.

◆ haveSameKeys()

template<typename T , typename U >
bool haveSameKeys ( const std::map< T, U > &  map1,
const std::map< T, U > &  map2 
)
inline

Checks whether two std::map instances have the same set of keys.

The associated values are ignored; only the key sets are compared.

Template Parameters
TKey type of the maps.
UMapped type of the maps.
Parameters
map1First map.
map2Second map.
Returns
true if both maps have identical key sets, false otherwise.

Definition at line 145 of file Utils.h.

◆ lerp()

template<typename T , typename U , std::size_t cache_size>
T lerp ( x,
const std::array< T, cache_size > &  lookup,
double  a = 0.0,
double  b = 1.0 
)

Linearly interpolates a cached function on [a, b].

Given a lookup table lookup filled with values of some function f at uniformly spaced points in [a, b] (as produced by fill_cache), this routine returns an interpolated value for an input x.

The input x is first clamped to [a, b]. Then:

  • it is mapped to a floating-point index in [0, cache_size - 1],
  • the two neighboring cache entries are linearly combined according to the fractional part of the index.
Template Parameters
TValue type stored in the cache and returned.
UType of x (convertible to double).
cache_sizeSize of the lookup array.
Parameters
xInput coordinate at which the function is approximated.
lookupCache/lookup table of sampled values.
aLower bound of the sampling interval (default: 0.0).
bUpper bound of the sampling interval (default: 1.0).
Returns
Interpolated value at x.

Definition at line 276 of file Utils.h.

◆ operator<<()

template<typename Map >
std::enable_if_t< std::is_same_v< typename Map::value_type, std::pair< const typename Map::key_type, typename Map::mapped_type > >, std::ostream & > operator<< ( std::ostream &  os,
const Map &  m 
)

Generic stream output operator for map-like containers.

This template is enabled only for types Map whose value_type matches std::pair<const key_type, mapped_type>, i.e. true associative maps. The contents are printed in a JSON-like style:

{ key1: value1, key2: value2, ... }

where values are printed using print_value(), so pointer-like values are handled gracefully.

Template Parameters
MapMap-like container type.
Parameters
osOutput stream.
mMap to print.
Returns
Reference to the output stream.

Definition at line 176 of file Utils.h.

◆ print_value() [1/2]

template<typename T >
void print_value ( std::ostream &  os,
const std::shared_ptr< T > &  ptr 
)

Specialization of print_value for std::shared_ptr.

If the pointer is non-null, the pointed-to object is printed. Otherwise the string "nullptr" is written to the stream.

Template Parameters
TPointee type.
Parameters
osOutput stream.
ptrShared pointer to print.

Definition at line 176 of file Utils.h.

◆ print_value() [2/2]

template<typename T >
void print_value ( std::ostream &  os,
const T value 
)

Generic helper to print a value to an output stream.

This overload simply forwards the value to the stream using operator<<. It serves as the default implementation for print_value and can be specialized/overloaded for more complex types.

Template Parameters
TValue type.
Parameters
osOutput stream.
valueValue to print.

Definition at line 161 of file Utils.h.

◆ split()

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

Splits a string into parts using a single-character delimiter.

Consecutive delimiters yield empty tokens. The delimiter itself is not included in the resulting strings.

Example:

  • split("a,b,c", ',') → {"a","b","c"}
  • split("a,,c", ',') → {"a","","c"}
Parameters
sInput string to split.
delimiterCharacter used as separator.
Returns
Vector of substrings.

Definition at line 8 of file Utils.cpp.

◆ to_lowercase()

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

Returns a lowercase copy of the input string.

All characters are converted using std::tolower with unsigned char promotion to avoid undefined behavior.

Parameters
strInput string.
Returns
Lowercased copy of str.

Definition at line 31 of file Utils.cpp.

Variable Documentation

◆ nld

const std::numeric_limits<double> nld = *new std::numeric_limits<double>

Numeric limits for double-precision floating point.

This object is used to access properties such as machine epsilon via nld.epsilon() in numerical routines (e.g. cache filling).

Definition at line 43 of file Utils.h.