25#ifndef __BITPIT_COMMON_UTILS_HPP__
26#define __BITPIT_COMMON_UTILS_HPP__
44#define BITPIT_STR2(X) #X
45#define BITPIT_STR(X) BITPIT_STR2(X)
48#define BITPIT_CAT(A, B) A ## B
50#define BITPIT_COUNT_ARGS_MAX6(_1, _2, _3, _4, _5, _6 , COUNT, ...) COUNT
51#define BITPIT_EXPAND_ARGS_FOR_COUNT(x) x
52#define BITPIT_ARGS_SIZE(...) BITPIT_EXPAND_ARGS_FOR_COUNT(BITPIT_COUNT_ARGS_MAX6(__VA_ARGS__, 6, 5, 4, 3, 2, 1, 0))
54#define BITPIT_SELECT_OVERLOAD(NAME, NUM) BITPIT_CAT(NAME ## _, NUM)
55#define BITPIT_OVERLOAD_CALL(NAME, ...) BITPIT_SELECT_OVERLOAD(NAME, BITPIT_ARGS_SIZE(__VA_ARGS__))(__VA_ARGS__)
76#define BITPIT_CREATE_WORKSPACE(workspace, item_type, size, stack_size) \
77item_type *workspace; \
78std::array<item_type, stack_size> workspace##_stack; \
79std::unique_ptr<std::vector<item_type>> workspace##_heap; \
80if (size <= stack_size) { \
81 workspace = workspace##_stack.data(); \
83 workspace##_heap = std::unique_ptr<std::vector<item_type>>(new std::vector<item_type>(size)); \
84 workspace = workspace##_heap->data(); \
91template <
typename T,
typename Comparator = std::less<T> >
92bool addToOrderedVector(
const T &value, std::vector<T> &list, Comparator comparator = Comparator());
94template <
typename T,
typename Comparator = std::less<T> >
95typename std::vector<T>::const_iterator findInOrderedVector(
const T &value,
const std::vector<T> &list, Comparator comparator = Comparator());
98void reorderVector(std::vector<size_t>& order, std::vector<T>& v, std::size_t size);
100template<
typename OrderContainer,
typename DataContainer>
101void reorderContainer(OrderContainer &order, DataContainer &v, std::size_t size);
103template<
typename Container,
typename Index>
104void swapValue(Container &v, Index i, Index j);
107void swapValue(std::vector<bool> &v, std::size_t i, std::size_t j);
115#ifndef __BITPIT_COMMON_UTILS_SRC__
116extern template bool addToOrderedVector<>(
const long&, std::vector<long>&, std::less<long>);
117extern template bool addToOrderedVector<>(
const unsigned long&, std::vector<unsigned long>&, std::less<unsigned long>);
119extern template std::vector<long>::const_iterator findInOrderedVector<>(
const long&,
const std::vector<long>&, std::less<long>);
120extern template std::vector<unsigned long>::const_iterator findInOrderedVector<>(
const unsigned long&,
const std::vector<unsigned long>&, std::less<unsigned long>);
144 constexpr static const double DEFAULT_ABS_TOL = 10 * std::numeric_limits<double>::epsilon();
145 constexpr static const double DEFAULT_REL_TOL = 10 * std::numeric_limits<double>::epsilon();
174 bool operator()(
double x,
double y,
double relativeTolerance = DEFAULT_REL_TOL,
double absoluteTolerance = DEFAULT_ABS_TOL)
const
178 double absoluteDifference = std::abs(x - y);
179 if (absoluteDifference <= absoluteTolerance) {
192 double relativeDifference = absoluteDifference / std::max(std::abs(x), std::abs(y));
194 return (relativeDifference <= relativeTolerance);
224 bool operator()(
double x,
double y,
double relativeTolerance = DEFAULT_REL_TOL,
double absoluteTolerance = DEFAULT_ABS_TOL)
const
265 bool operator()(
double x,
double y,
double relativeTolerance = DEFAULT_REL_TOL,
double absoluteTolerance = DEFAULT_ABS_TOL)
const
305 bool operator()(
double x,
double y,
double relativeTolerance = DEFAULT_REL_TOL,
double absoluteTolerance = DEFAULT_ABS_TOL)
const
347 bool operator()(
double x,
double y,
double relativeTolerance = DEFAULT_REL_TOL,
double absoluteTolerance = DEFAULT_ABS_TOL)
const
void swapValue(std::vector< bool > &v, std::size_t i, std::size_t j)
std::vector< T > intersectionVector(const std::vector< T > &, const std::vector< T > &)
unsigned long factorial(unsigned long n)
void reorderContainer(OrderContainer &order, DataContainer &v, std::size_t size)
void extractWithoutReplacement(int n, int m, std::vector< int > &list)
void reorderVector(std::vector< size_t > &order, std::vector< T > &v, std::size_t size)
std::size_t countDigits(int n)
void eraseValue(std::vector< T > &, const T &)
bool operator()(double x, double y, double relativeTolerance=DEFAULT_REL_TOL, double absoluteTolerance=DEFAULT_ABS_TOL) const
bool operator()(double x, double y, double relativeTolerance=DEFAULT_REL_TOL, double absoluteTolerance=DEFAULT_ABS_TOL) const
bool operator()(double x, double y, double relativeTolerance=DEFAULT_REL_TOL, double absoluteTolerance=DEFAULT_ABS_TOL) const
bool operator()(double x, double y, double relativeTolerance=DEFAULT_REL_TOL, double absoluteTolerance=DEFAULT_ABS_TOL) const
bool operator()(double x, double y, double relativeTolerance=DEFAULT_REL_TOL, double absoluteTolerance=DEFAULT_ABS_TOL) const