BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
Algorithms.h
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Base/Util/Algorithms.h
6 //! @brief Defines and implements namespace BaseUtils::algo with some algorithms
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
15 #ifdef SWIG
16 #error no need to expose this header to Swig
17 #endif
18 
19 #ifndef USER_API
20 #ifndef BORNAGAIN_BASE_UTIL_ALGORITHMS_H
21 #define BORNAGAIN_BASE_UTIL_ALGORITHMS_H
22 
23 #include "Base/Util/Assert.h"
24 #include <algorithm>
25 #include <limits>
26 #include <vector>
27 
28 //! Some additions to standard library algorithms.
29 
30 namespace BaseUtils::algo {
31 
32 //! Returns true if two doubles agree within machine epsilon.
33 inline bool almostEqual(double a, double b)
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 }
38 
39 //! Returns the minimum value of function evaluate as applied to the elements of an iterator range.
40 template <typename Evaluator, typename Iterator>
41 double min_value(const Iterator& begin, const Iterator& end, const Evaluator& evaluate);
42 
43 //! Returns the maximum value of function evaluate as applied to the elements of an iterator range.
44 template <typename Evaluator, typename Iterator>
45 double max_value(const Iterator& begin, const Iterator& end, const Evaluator& evaluate);
46 
47 //! Returns the concatenation of two std::vector%s.
48 template <class T>
49 std::vector<T> concat(const std::vector<T>& v1, const std::vector<T>& v2);
50 
51 } // namespace BaseUtils::algo
52 
53 // ************************************************************************************************
54 // Implementation
55 // ************************************************************************************************
56 
57 template <typename Evaluator, typename Iterator>
58 double BaseUtils::algo::min_value(const Iterator& begin, const Iterator& end,
59  const Evaluator& evaluate)
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 }
68 
69 template <typename Evaluator, typename Iterator>
70 double BaseUtils::algo::max_value(const Iterator& begin, const Iterator& end,
71  const Evaluator& evaluate)
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 }
80 
81 template <class T>
82 std::vector<T> BaseUtils::algo::concat(const std::vector<T>& v1, const std::vector<T>& v2)
83 {
84  std::vector<T> v = v1;
85  v.insert(v.end(), v2.begin(), v2.end());
86  return v;
87 }
88 
89 #endif // BORNAGAIN_BASE_UTIL_ALGORITHMS_H
90 #endif // USER_API
Defines the macro ASSERT.
#define ASSERT(condition)
Definition: Assert.h:45
Some additions to standard library algorithms.
Definition: Algorithms.h:30
std::vector< T > concat(const std::vector< T > &v1, const std::vector< T > &v2)
Returns the concatenation of two std::vectors.
Definition: Algorithms.h:82
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.
Definition: Algorithms.h:70
bool almostEqual(double a, double b)
Returns true if two doubles agree within machine epsilon.
Definition: Algorithms.h:33
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.
Definition: Algorithms.h:58