SquidMath.h File Reference
#include "base/forward.h"
#include "base/TypeTraits.h"
#include <limits>
#include <optional>
Include dependency graph for SquidMath.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Namespaces

namespace  Math
 

Typedefs

template<typename T , typename U >
using AllUnsigned = typename std::conditional< std::is_unsigned< T >::value &&std::is_unsigned< U >::value, std::true_type, std::false_type >::type
 

Functions

int Math::intPercent (const int a, const int b)
 
int64_t Math::int64Percent (const int64_t a, const int64_t b)
 
double Math::doublePercent (const double, const double)
 
int Math::intAverage (const int, const int, int, const int)
 
double Math::doubleAverage (const double, const double, int, const int)
 
template<typename A , typename B >
constexpr bool Less (const A a, const B b)
 whether integer a is less than integer b, with correct overflow handling More...
 
template<typename T >
constexpr void AssertNaturalType ()
 ensure that T is supported by NaturalSum() and friends More...
 
template<typename S , typename A , typename B , std::enable_if_t< AllUnsigned< A, B >::value, int > = 0>
std::optional< S > IncreaseSumInternal (const A a, const B b)
 
template<typename S , typename A , typename B , std::enable_if_t<!AllUnsigned< A, B >::value, int > = 0>
std::optional< S > constexpr IncreaseSumInternal (const A a, const B b)
 
template<typename S , typename T >
std::optional< S > IncreaseSum (const S s, const T t)
 argument pack expansion termination for IncreaseSum<S, T, Args...>() More...
 
template<typename S , typename T , typename... Args>
std::optional< S > IncreaseSum (const S sum, const T t, const Args... args)
 
template<typename SummationType , typename... Args>
std::optional< SummationType > NaturalSum (const Args... args)
 
template<typename S , typename... Args>
SetToNaturalSumOrMax (S &var, const Args... args)
 
template<typename Result , typename Source >
Result NaturalCast (const Source s)
 

Typedef Documentation

◆ AllUnsigned

template<typename T , typename U >
using AllUnsigned = typename std::conditional< std::is_unsigned<T>::value && std::is_unsigned<U>::value, std::true_type, std::false_type >::type

detects a pair of unsigned types reduces code duplication in declarations further below

Definition at line 38 of file SquidMath.h.

Function Documentation

◆ AssertNaturalType()

template<typename T >
constexpr void AssertNaturalType ( )
constexpr

Definition at line 62 of file SquidMath.h.

◆ IncreaseSum() [1/2]

template<typename S , typename T >
std::optional< S > IncreaseSum ( const S  s,
const T  t 
)

◆ IncreaseSum() [2/2]

template<typename S , typename T , typename... Args>
std::optional< S > IncreaseSum ( const S  sum,
const T  t,
const Args...  args 
)
Returns
a non-overflowing sum of the arguments (or nothing)

Definition at line 155 of file SquidMath.h.

References head, and IncreaseSum().

◆ IncreaseSumInternal() [1/2]

template<typename S , typename A , typename B , std::enable_if_t< AllUnsigned< A, B >::value, int > = 0>
std::optional< S > IncreaseSumInternal ( const A  a,
const B  b 
)

This IncreaseSumInternal() overload is optimized for speed.

Returns
a non-overflowing sum of the two unsigned arguments (or nothing) \prec both argument types are unsigned

Definition at line 76 of file SquidMath.h.

References A, B, and max().

◆ IncreaseSumInternal() [2/2]

template<typename S , typename A , typename B , std::enable_if_t<!AllUnsigned< A, B >::value, int > = 0>
std::optional< S > constexpr IncreaseSumInternal ( const A  a,
const B  b 
)
constexpr

This IncreaseSumInternal() overload supports a larger variety of types.

Returns
a non-overflowing sum of the two arguments (or nothing)
nothing if at least one of the arguments is negative \prec at least one of the argument types is signed

Definition at line 112 of file SquidMath.h.

References A, B, Less(), and max().

◆ Less()

◆ NaturalCast()

template<typename Result , typename Source >
Result NaturalCast ( const Source  s)

converts a given non-negative integer into an integer of a given type without loss of information or undefined behavior

Definition at line 186 of file SquidMath.h.

◆ NaturalSum()

template<typename SummationType , typename... Args>
std::optional< SummationType > NaturalSum ( const Args...  args)
Returns
an exact, non-overflowing sum of the arguments (or nothing)

Definition at line 167 of file SquidMath.h.

◆ SetToNaturalSumOrMax()

template<typename S , typename... Args>
S SetToNaturalSumOrMax ( S &  var,
const Args...  args 
)

Safely resets the given variable to NaturalSum() of the given arguments. If the sum overflows, resets to variable's maximum possible value.

Returns
the new variable value (like an assignment operator would)

Definition at line 176 of file SquidMath.h.

References max().

Referenced by ClpMap< Key, Value, MemoryUsedBy >::Entry::Entry(), and TestMath::testNaturalSum().

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors