mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: ListOps: added findIndices with predicate
This commit is contained in:
@ -655,6 +655,16 @@ bool found
|
||||
);
|
||||
|
||||
|
||||
//- Linear search to find all occurences of given element.
|
||||
template<class ListType, class UnaryPredicate>
|
||||
labelList findIndices
|
||||
(
|
||||
const ListType& input,
|
||||
const UnaryPredicate& pred,
|
||||
label start=0
|
||||
);
|
||||
|
||||
|
||||
//- Set various locations of the list with a specified value.
|
||||
//
|
||||
// \param list the list to modify
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
Copyright (C) 2011-2016 OpenFOAM Foundation
|
||||
Copyright (C) 2015-2019 OpenCFD Ltd.
|
||||
Copyright (C) 2015-2020 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
@ -1170,6 +1170,55 @@ bool Foam::ListOps::found
|
||||
}
|
||||
|
||||
|
||||
template<class ListType, class UnaryPredicate>
|
||||
Foam::labelList Foam::ListOps::findIndices
|
||||
(
|
||||
const ListType& input,
|
||||
const UnaryPredicate& pred,
|
||||
label start
|
||||
)
|
||||
{
|
||||
const label len = input.size();
|
||||
|
||||
// Pass 1: count occurrences
|
||||
label count = 0;
|
||||
|
||||
if (start >= 0)
|
||||
{
|
||||
for (label i = start; i < len; ++i)
|
||||
{
|
||||
if (pred(input[i]))
|
||||
{
|
||||
if (!count) start = i; // adjust start for second pass
|
||||
++count;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
labelList indices(count);
|
||||
|
||||
// Pass 2: fill content
|
||||
if (count)
|
||||
{
|
||||
const label total = count;
|
||||
count = 0;
|
||||
for (label i = start; i < len; ++i)
|
||||
{
|
||||
if (pred(input[i]))
|
||||
{
|
||||
indices[count] = i;
|
||||
if (++count == total) // early termination
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return indices;
|
||||
}
|
||||
|
||||
|
||||
template<class T>
|
||||
void Foam::ListOps::setValue
|
||||
(
|
||||
|
||||
Reference in New Issue
Block a user