BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
Algorithms.h
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Base/Utils/Algorithms.h
6 //! @brief Defines and implements namespace 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_UTILS_ALGORITHMS_H
21 #define BORNAGAIN_BASE_UTILS_ALGORITHMS_H
22 
23 #include "Base/Utils/Assert.h"
24 #include <algorithm>
25 #include <cmath>
26 #include <functional>
27 #include <vector>
28 
29 //! Some additions to standard library algorithms.
30 
31 namespace algo {
32 
33 //! Returns true if two doubles agree within machine epsilon.
34 inline bool almostEqual(double a, double b)
35 {
36  constexpr double eps = std::numeric_limits<double>::epsilon();
37  return std::abs(a - b) <= eps * std::max(eps, (std::abs(a) + std::abs(b)) / 2);
38 }
39 
40 //! Returns the minimum value of function evaluate as applied to the elements of an iterator range.
41 template <typename Evaluator, typename Iterator>
42 double min_value(const Iterator& begin, const Iterator& end, const Evaluator& evaluate);
43 
44 //! Returns the maximum value of function evaluate as applied to the elements of an iterator range.
45 template <typename Evaluator, typename Iterator>
46 double max_value(const Iterator& begin, const Iterator& end, const Evaluator& evaluate);
47 
48 //! Returns the concatenation of two std::vector%s.
49 template <class T> std::vector<T> concat(const std::vector<T>& v1, const std::vector<T>& v2);
50 
51 } // namespace algo
52 
53 // ************************************************************************************************
54 // Implementation
55 // ************************************************************************************************
56 
57 template <typename Evaluator, typename Iterator>
58 double algo::min_value(const Iterator& begin, const Iterator& end, const Evaluator& evaluate)
59 {
60  ASSERT(begin != end);
61  double ret = evaluate(*begin);
62  Iterator it = begin;
63  while (++it != end)
64  ret = std::min(ret, evaluate(*it));
65  return ret;
66 }
67 
68 template <typename Evaluator, typename Iterator>
69 double algo::max_value(const Iterator& begin, const Iterator& end, const Evaluator& evaluate)
70 {
71  ASSERT(begin != end);
72  double ret = evaluate(*begin);
73  Iterator it = begin;
74  while (++it != end)
75  ret = std::max(ret, evaluate(*it));
76  return ret;
77 }
78 
79 template <class T> std::vector<T> algo::concat(const std::vector<T>& v1, const std::vector<T>& v2)
80 {
81  std::vector<T> v = v1;
82  v.insert(v.end(), v2.begin(), v2.end());
83  return v;
84 }
85 
86 #endif // BORNAGAIN_BASE_UTILS_ALGORITHMS_H
87 #endif // USER_API
Defines the macro ASSERT.
#define ASSERT(condition)
Definition: Assert.h:31
Some additions to standard library algorithms.
Definition: Algorithms.h:31
std::vector< T > concat(const std::vector< T > &v1, const std::vector< T > &v2)
Returns the concatenation of two std::vectors.
Definition: Algorithms.h:79
bool almostEqual(double a, double b)
Returns true if two doubles agree within machine epsilon.
Definition: Algorithms.h:34
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:69
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