maiacore 1.10.1
Music analisys library
utils.h File Reference

Utility functions and templates for internal maiacore operations. More...

#include <iostream>
#include <numeric>
Include dependency graph for utils.h:

Go to the source code of this file.

Functions

template<typename T >
void ignore (T &&)
 Suppresses unused variable warnings by accepting and discarding a forwarding reference. More...
 
constexpr unsigned int hash (const char *s, int off=0)
 Compile-time string hash function using the djb2 algorithm. More...
 
constexpr int factorial (const int n)
 Compile-time factorial calculation using recursive template evaluation. More...
 
bool isFloatEqual (float A, float B, float epsilon=0.005f)
 Floating-point equality comparison with epsilon tolerance for approximate matching. More...
 

Detailed Description

Utility functions and templates for internal maiacore operations.

Provides compile-time and runtime utilities including hashing for switch-case optimization, mathematical functions, and floating-point comparison with epsilon tolerance for music analysis applications.

Function Documentation

◆ factorial()

constexpr int factorial ( const int  n)
constexpr

Compile-time factorial calculation using recursive template evaluation.

Parameters
nNon-negative integer input (n >= 0).
Returns
Factorial of n (n! = n × (n-1) × ... × 2 × 1), with 0! = 1.

Recursively computes factorial at compile time. Used for combinatorial calculations in harmonic analysis and voice permutation computations.

Warning
Only suitable for small values of n due to integer overflow constraints. For n > 12, results exceed 32-bit integer capacity.

◆ hash()

constexpr unsigned int hash ( const char *  s,
int  off = 0 
)
constexpr

Compile-time string hash function using the djb2 algorithm.

Parameters
sNull-terminated C-string to hash.
offOffset for recursive character processing (default: 0, internal use).
Returns
Unsigned integer hash value computed at compile time.

Implements the djb2 hash algorithm recursively for constexpr evaluation. Used primarily for optimizing switch-case statements with string literals in MusicXML parsing and enumeration conversions. The hash formula is: hash * 33 XOR current_character, starting with seed value 5381.

◆ ignore()

template<typename T >
void ignore ( T &&  )

Suppresses unused variable warnings by accepting and discarding a forwarding reference.

Template Parameters
TType of the unused variable.
Parameters
Unnamedforwarding reference to ignore.

Utility template for suppressing compiler warnings when function parameters are intentionally unused in certain code paths or conditional compilation scenarios.

◆ isFloatEqual()

bool isFloatEqual ( float  A,
float  B,
float  epsilon = 0.005f 
)
inline

Floating-point equality comparison with epsilon tolerance for approximate matching.

Parameters
AFirst value to compare.
BSecond value to compare.
epsilonTolerance threshold for considering values equal (default: 0.005).
Returns
True if the absolute difference |A - B| is strictly less than epsilon.

Uses absolute difference comparison suitable for music analysis where small deviations from rounding errors (e.g., frequency calculations, rhythmic quantization, spectral analysis) should be treated as equivalent. The default epsilon of 0.005 accommodates typical floating-point precision errors in quarter-note duration and frequency ratio computations.

Note
The comparison is symmetric: isFloatEqual(A, B) == isFloatEqual(B, A). However, transitivity is not guaranteed: if isFloatEqual(A, B) and isFloatEqual(B, C), it does not necessarily follow that isFloatEqual(A, C).