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