BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
BaseUtils::algo Namespace Reference

Description

Some additions to standard library algorithms.

Functions

bool almostEqual (double a, double b)
 Returns true if two doubles agree within machine epsilon. More...
 
template<class T >
std::vector< T > concat (const std::vector< T > &v1, const std::vector< T > &v2)
 Returns the concatenation of two std::vectors. More...
 
template<typename Evaluator , typename Iterator >
double max_value (const Iterator &begin, const Iterator &end, const Evaluator &evaluate)
 Returns the maximum value of function evaluate as applied to the elements of an iterator range. More...
 
template<typename Evaluator , typename Iterator >
double min_value (const Iterator &begin, const Iterator &end, const Evaluator &evaluate)
 Returns the minimum value of function evaluate as applied to the elements of an iterator range. More...
 

Function Documentation

◆ almostEqual()

bool BaseUtils::algo::almostEqual ( double  a,
double  b 
)
inline

Returns true if two doubles agree within machine epsilon.

Definition at line 33 of file Algorithms.h.

34 {
35  constexpr double eps = std::numeric_limits<double>::epsilon();
36  return std::abs(a - b) <= eps * std::max(eps, (std::abs(a) + std::abs(b)) / 2);
37 }

Referenced by VerticalLine::contains(), HorizontalLine::contains(), ConstKBinAxis::equals(), CustomBinAxis::equals(), FixedBinAxis::equals(), VariableBinAxis::equals(), Py::Fmt::isHexagonal(), and Py::Fmt::isSquare().

◆ concat()

template<class T >
std::vector< T > BaseUtils::algo::concat ( const std::vector< T > &  v1,
const std::vector< T > &  v2 
)

Returns the concatenation of two std::vectors.

Definition at line 82 of file Algorithms.h.

83 {
84  std::vector<T> v = v1;
85  v.insert(v.end(), v2.begin(), v2.end());
86  return v;
87 }

◆ max_value()

template<typename Evaluator , typename Iterator >
double BaseUtils::algo::max_value ( const Iterator &  begin,
const Iterator &  end,
const Evaluator &  evaluate 
)

Returns the maximum value of function evaluate as applied to the elements of an iterator range.

Definition at line 70 of file Algorithms.h.

72 {
73  ASSERT(begin != end);
74  double result = evaluate(*begin);
75  Iterator it = begin;
76  while (++it != end)
77  result = std::max(result, evaluate(*it));
78  return result;
79 }
#define ASSERT(condition)
Definition: Assert.h:45

References ASSERT.

Referenced by ReCompound::topZ(), and PolyhedralUtil::TopZ().

◆ min_value()

template<typename Evaluator , typename Iterator >
double BaseUtils::algo::min_value ( const Iterator &  begin,
const Iterator &  end,
const Evaluator &  evaluate 
)

Returns the minimum value of function evaluate as applied to the elements of an iterator range.

Definition at line 58 of file Algorithms.h.

60 {
61  ASSERT(begin != end);
62  double result = evaluate(*begin);
63  Iterator it = begin;
64  while (++it != end)
65  result = std::min(result, evaluate(*it));
66  return result;
67 }

References ASSERT.

Referenced by ReCompound::bottomZ(), and PolyhedralUtil::BottomZ().