Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
D
dlib
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
钟尚武
dlib
Commits
fb3e8e4b
Commit
fb3e8e4b
authored
Jan 28, 2018
by
Davis King
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added max_scoring_element() and min_scoring_element()
parent
b1589d2b
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
82 additions
and
0 deletions
+82
-0
algs.h
dlib/algs.h
+82
-0
No files found.
dlib/algs.h
View file @
fb3e8e4b
...
...
@@ -1067,6 +1067,88 @@ namespace dlib
void
*
const
data
;
};
// ----------------------------------------------------------------------------------------
template
<
typename
T
,
typename
F
>
auto
max_scoring_element
(
const
T
&
container
,
F
score_func
)
->
decltype
(
std
::
make_pair
(
*
container
.
begin
(),
0
.
0
))
/*!
requires
- container has .begin() and .end(), allowing it to be enumerated.
- score_func() is a function that takes an element of the container and returns a double.
ensures
- This function finds the element of container that has the largest score,
according to score_func(), and returns a std::pair containing that maximal
element along with the score.
- If the container is empty then make_pair(a default initialized object, -infinity) is returned.
!*/
{
double
best_score
=
-
std
::
numeric_limits
<
double
>::
infinity
();
auto
best_i
=
container
.
begin
();
for
(
auto
i
=
container
.
begin
();
i
!=
container
.
end
();
++
i
)
{
auto
score
=
score_func
(
*
i
);
if
(
score
>
best_score
)
{
best_score
=
score
;
best_i
=
i
;
}
}
using
item_type
=
typename
std
::
remove_reference
<
decltype
(
*
best_i
)
>::
type
;
if
(
best_i
==
container
.
end
())
return
std
::
make_pair
(
item_type
(),
best_score
);
else
return
std
::
make_pair
(
*
best_i
,
best_score
);
}
// ----------------------------------------------------------------------------------------
template
<
typename
T
,
typename
F
>
auto
min_scoring_element
(
const
T
&
container
,
F
score_func
)
->
decltype
(
std
::
make_pair
(
*
container
.
begin
(),
0
.
0
))
/*!
requires
- container has .begin() and .end(), allowing it to be enumerated.
- score_func() is a function that takes an element of the container and returns a double.
ensures
- This function finds the element of container that has the smallest score,
according to score_func(), and returns a std::pair containing that minimal
element along with the score.
- If the container is empty then make_pair(a default initialized object, infinity) is returned.
!*/
{
double
best_score
=
std
::
numeric_limits
<
double
>::
infinity
();
auto
best_i
=
container
.
begin
();
for
(
auto
i
=
container
.
begin
();
i
!=
container
.
end
();
++
i
)
{
auto
score
=
score_func
(
*
i
);
if
(
score
<
best_score
)
{
best_score
=
score
;
best_i
=
i
;
}
}
using
item_type
=
typename
std
::
remove_reference
<
decltype
(
*
best_i
)
>::
type
;
if
(
best_i
==
container
.
end
())
return
std
::
make_pair
(
item_type
(),
best_score
);
else
return
std
::
make_pair
(
*
best_i
,
best_score
);
}
// ----------------------------------------------------------------------------------------
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment