diff --git a/src/OpenFOAM/containers/Lists/ListOps/ListOps.H b/src/OpenFOAM/containers/Lists/ListOps/ListOps.H index 2314801ad6b89de35f16900dad846ded6a4ee079..ab2009edb9f27c31b20bd0bb688a067b00fb148e 100644 --- a/src/OpenFOAM/containers/Lists/ListOps/ListOps.H +++ b/src/OpenFOAM/containers/Lists/ListOps/ListOps.H @@ -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 diff --git a/src/OpenFOAM/containers/Lists/ListOps/ListOpsTemplates.C b/src/OpenFOAM/containers/Lists/ListOps/ListOpsTemplates.C index bc334bc786d2adad548a0b6d75742cdd61c2a8a5..37edefa42c8504e61082681b8525f3425564a1a8 100644 --- a/src/OpenFOAM/containers/Lists/ListOps/ListOpsTemplates.C +++ b/src/OpenFOAM/containers/Lists/ListOps/ListOpsTemplates.C @@ -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 (