Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
BornAgain
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mlz
BornAgain
Commits
39797889
Commit
39797889
authored
4 years ago
by
Wuttke, Joachim
Browse files
Options
Downloads
Patches
Plain Diff
provide generic functions min_value, max_value
parent
c83ce5e1
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
Core/Basics/Algorithms.h
+65
-0
65 additions, 0 deletions
Core/Basics/Algorithms.h
Tests/UnitTests/Core/Basics/MinMaxValueTest.cpp
+32
-0
32 additions, 0 deletions
Tests/UnitTests/Core/Basics/MinMaxValueTest.cpp
with
97 additions
and
0 deletions
Core/Basics/Algorithms.h
0 → 100644
+
65
−
0
View file @
39797889
// ************************************************************************** //
//
// 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
This diff is collapsed.
Click to expand it.
Tests/UnitTests/Core/Basics/MinMaxValueTest.cpp
0 → 100644
+
32
−
0
View file @
39797889
#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
);
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment