Skip to content
Snippets Groups Projects
Commit 39797889 authored by Wuttke, Joachim's avatar Wuttke, Joachim
Browse files

provide generic functions min_value, max_value

parent c83ce5e1
No related branches found
No related tags found
No related merge requests found
// ************************************************************************** //
//
// BornAgain: simulate and fit scattering at grazing incidence
//
//! @file Core/Basics/Algorithms
//! @brief Defines and implements namespace algo with some algorithms
//!
//! @homepage http://www.bornagainproject.org
//! @license GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2018
//! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
// ************************************************************************** //
#ifndef ALGORITHMS_H
#define ALGORITHMS_H
#include <algorithm>
#include <functional>
#include <vector>
#include <cassert>
//! Some additions to standard library algorithms.
namespace algo {
//! Returns the minimum value of function evaluate as applied to the elements of an iterator range.
template<typename Evaluator, typename Iterator>
double min_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.
template<typename Evaluator, typename Iterator>
double max_value(const Iterator& begin, const Iterator& end, const Evaluator& evaluate);
} // namespace algo
// ************************************************************************** //
// Implementation
// ************************************************************************** //
template<typename Evaluator, typename Iterator>
double algo::min_value(const Iterator& begin, const Iterator& end, const Evaluator& evaluate)
{
assert(begin != end);
double ret = evaluate(*begin);
Iterator it = begin;
while (++it != end)
ret = std::min(ret, evaluate(*it));
return ret;
}
template<typename Evaluator, typename Iterator>
double algo::max_value(const Iterator& begin, const Iterator& end, const Evaluator& evaluate)
{
assert(begin != end);
double ret = evaluate(*begin);
Iterator it = begin;
while (++it != end)
ret = std::max(ret, evaluate(*it));
return ret;
}
#endif // ALGORITHMS_H
#include "Algorithms.h"
#include "google_test.h"
#include <cmath>
class MinMaxValueTest : public ::testing::Test
{
};
TEST_F(MinMaxValueTest, MinMaxValueAlmostEq)
{
double val;
std::vector<double> A{0.};
std::vector<int> C {1, 2, 3};
val = algo::min_value(A.begin(), A.end(), [](const double& x)->double { return x; });
EXPECT_EQ(val, 0.);
val = algo::max_value(A.begin(), A.end(), [](const double& x)->double { return 2+x; });
EXPECT_NEAR(val, 2., 1e-15);
val = algo::min_value(C.begin(), C.end(), [](const int& i)->double { return i; });
EXPECT_EQ(val, 1);
val = algo::min_value(C.begin(), C.end(), [](const int& i)->double { return -i; });
EXPECT_EQ(val, -3);
val = algo::min_value(C.begin(), C.end(), [](const int& i)->double { return pow(i-2.1,2); });
EXPECT_NEAR(val, 0.01, 1e-13);
val = algo::max_value(C.begin(), C.end(), [](const int& i)->double { return i; });
EXPECT_EQ(val, 3);
val = algo::max_value(C.begin(), C.end(), [](const int& i)->double { return -i; });
EXPECT_EQ(val, -1);
val = algo::max_value(C.begin(), C.end(), [](const int& i)->double { return -pow(i-2.1,2); });
EXPECT_NEAR(val, -0.01, 1e-13);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment