SquidMath.h File Reference
#include "base/forward.h"
#include "base/Optional.h"
#include <limits>
#include <type_traits>
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

 Math
 

Typedefs

template<bool B, class T = void>
using EnableIfType = typename std::enable_if< B, T >::type
 
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 bool AssertNaturalType ()
 ensure that T is supported by NaturalSum() and friends More...
 
template<typename S , typename A , typename B , EnableIfType< AllUnsigned< A, B >::value, int > = 0>
Optional< S > IncreaseSumInternal (const A a, const B b)
 
template<typename S , typename A , typename B , EnableIfType<!AllUnsigned< A, B >::value, int > = 0>
constexpr Optional< S > IncreaseSumInternal (const A a, const B b)
 
template<typename S , typename T >
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>
Optional< S > IncreaseSum (const S sum, const T t, const Args... args)
 
template<typename SummationType , typename... Args>
Optional< SummationType > NaturalSum (const Args... args)
 
template<typename S , typename... Args>
SetToNaturalSumOrMax (S &var, const Args... args)
 

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 43 of file SquidMath.h.

◆ EnableIfType

template<bool B, class T = void>
using EnableIfType = typename std::enable_if<B,T>::type

std::enable_if_t replacement until C++14 simplifies declarations further below

Definition at line 38 of file SquidMath.h.

Function Documentation

◆ AssertNaturalType()

template<typename T >
constexpr bool AssertNaturalType ( )
constexpr

Definition at line 67 of file SquidMath.h.

◆ IncreaseSum() [1/2]

template<typename S , typename T >
Optional<S> IncreaseSum ( const S  s,
const T  t 
)

Definition at line 151 of file SquidMath.h.

Referenced by IncreaseSum().

◆ IncreaseSum() [2/2]

template<typename S , typename T , typename... Args>
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 162 of file SquidMath.h.

References head, and IncreaseSum().

◆ IncreaseSumInternal() [1/2]

template<typename S , typename A , typename B , EnableIfType< AllUnsigned< A, B >::value, int > = 0>
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 82 of file SquidMath.h.

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

◆ IncreaseSumInternal() [2/2]

template<typename S , typename A , typename B , EnableIfType<!AllUnsigned< A, B >::value, int > = 0>
constexpr Optional<S> 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 119 of file SquidMath.h.

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

◆ Less()

template<typename A , typename B >
constexpr bool Less ( const A  a,
const B  b 
)
constexpr

Definition at line 53 of file SquidMath.h.

References false, and type.

Referenced by IncreaseSumInternal().

◆ NaturalSum()

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

Definition at line 173 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 182 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