Commit d3c76d8e authored by mattijs's avatar mattijs
Browse files

ENH: ListOps: added findIndices with predicate

parent f1e950ce
...@@ -655,6 +655,16 @@ bool found ...@@ -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. //- Set various locations of the list with a specified value.
// //
// \param list the list to modify // \param list the list to modify
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2015-2019 OpenCFD Ltd. Copyright (C) 2015-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
...@@ -1170,6 +1170,55 @@ bool Foam::ListOps::found ...@@ -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> template<class T>
void Foam::ListOps::setValue void Foam::ListOps::setValue
( (
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment