mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: add list operation findMinMax to return label pair.
- when both min and max are required, this is more efficient than traversing the list twice.
This commit is contained in:
@ -43,6 +43,7 @@ SourceFiles
|
|||||||
#define ListOps_H
|
#define ListOps_H
|
||||||
|
|
||||||
#include "FlatOutput.H"
|
#include "FlatOutput.H"
|
||||||
|
#include "labelPair.H"
|
||||||
#include "labelList.H"
|
#include "labelList.H"
|
||||||
#include "HashSet.H"
|
#include "HashSet.H"
|
||||||
#include "Map.H"
|
#include "Map.H"
|
||||||
@ -360,7 +361,21 @@ labelList findIndices
|
|||||||
label start=0
|
label start=0
|
||||||
);
|
);
|
||||||
|
|
||||||
//- Linear search for the index of the max element.
|
|
||||||
|
//- Linear search for the index of the min element,
|
||||||
|
//- similar to std::min_element but for lists and returns the index.
|
||||||
|
//
|
||||||
|
// \tparam ListType The input list type
|
||||||
|
//
|
||||||
|
// \param input The list to search
|
||||||
|
// \param start The start index in the list (default: 0)
|
||||||
|
//
|
||||||
|
// \return The min index or -1 on error.
|
||||||
|
template<class ListType>
|
||||||
|
label findMin(const ListType& input, label start=0);
|
||||||
|
|
||||||
|
//- Linear search for the index of the max element,
|
||||||
|
//- similar to std::max_element but for lists and returns the index.
|
||||||
//
|
//
|
||||||
// \tparam ListType The input list type
|
// \tparam ListType The input list type
|
||||||
//
|
//
|
||||||
@ -372,16 +387,18 @@ template<class ListType>
|
|||||||
label findMax(const ListType& input, label start=0);
|
label findMax(const ListType& input, label start=0);
|
||||||
|
|
||||||
|
|
||||||
//- Linear search for the index of the min element.
|
//- Linear search for the index of the min/max element,
|
||||||
|
//- similar to std::minmax_element but for lists and returns the index.
|
||||||
//
|
//
|
||||||
// \tparam ListType The input list type
|
// \tparam ListType The input list type
|
||||||
//
|
//
|
||||||
// \param input The list to search
|
// \param input The list to search
|
||||||
// \param start The start index in the list (default: 0)
|
// \param start The start index in the list (default: 0)
|
||||||
//
|
//
|
||||||
// \return The min index or -1 on error.
|
// \return The min/max indices as a Pair (min is first max is second)
|
||||||
|
// or (-1,-1) on error.
|
||||||
template<class ListType>
|
template<class ListType>
|
||||||
label findMin(const ListType& input, label start=0);
|
labelPair findMinMax(const ListType& input, label start=0);
|
||||||
|
|
||||||
|
|
||||||
//- Binary search to find the index of the last element in a sorted list
|
//- Binary search to find the index of the last element in a sorted list
|
||||||
|
|||||||
@ -671,6 +671,32 @@ Foam::labelList Foam::findIndices
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<class ListType>
|
||||||
|
Foam::label Foam::findMin
|
||||||
|
(
|
||||||
|
const ListType& input,
|
||||||
|
label start
|
||||||
|
)
|
||||||
|
{
|
||||||
|
const label len = input.size();
|
||||||
|
|
||||||
|
if (start < 0 || start >= len)
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (label i = start+1; i < len; ++i)
|
||||||
|
{
|
||||||
|
if (input[i] < input[start])
|
||||||
|
{
|
||||||
|
start = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return start;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
template<class ListType>
|
template<class ListType>
|
||||||
Foam::label Foam::findMax
|
Foam::label Foam::findMax
|
||||||
(
|
(
|
||||||
@ -698,7 +724,7 @@ Foam::label Foam::findMax
|
|||||||
|
|
||||||
|
|
||||||
template<class ListType>
|
template<class ListType>
|
||||||
Foam::label Foam::findMin
|
Foam::labelPair Foam::findMinMax
|
||||||
(
|
(
|
||||||
const ListType& input,
|
const ListType& input,
|
||||||
label start
|
label start
|
||||||
@ -708,18 +734,25 @@ Foam::label Foam::findMin
|
|||||||
|
|
||||||
if (start < 0 || start >= len)
|
if (start < 0 || start >= len)
|
||||||
{
|
{
|
||||||
return -1;
|
return labelPair(-1,-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
label minIdx = start;
|
||||||
|
label maxIdx = start;
|
||||||
|
|
||||||
for (label i = start+1; i < len; ++i)
|
for (label i = start+1; i < len; ++i)
|
||||||
{
|
{
|
||||||
if (input[i] < input[start])
|
if (input[i] < input[minIdx])
|
||||||
{
|
{
|
||||||
start = i;
|
minIdx = i;
|
||||||
|
}
|
||||||
|
if (input[maxIdx] < input[i])
|
||||||
|
{
|
||||||
|
maxIdx = i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return start;
|
return labelPair(minIdx, maxIdx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user