ListOps: Added count function to return the number of occurrences of a value in a list

This commit is contained in:
Will Bainbridge
2021-06-16 15:15:48 +01:00
parent fa766e8f3d
commit 25abd43a25
2 changed files with 23 additions and 0 deletions

View File

@ -168,6 +168,10 @@ List<OutList> invertManyToMany(const label len, const UList<InList>& in)
//- Create identity map (map[i] == i) of given length
labelList identity(const label len);
//- Count the number of occurrences of a value in a list
template<class ListType>
label count(const ListType& l, typename ListType::const_reference x);
//- Find first occurrence of given element and return index,
// return -1 if not found. Linear search.
template<class ListType>

View File

@ -475,6 +475,25 @@ void Foam::invertManyToMany
}
template<class ListType>
Foam::label Foam::count
(
const ListType& l,
typename ListType::const_reference x
)
{
label result = 0;
forAll(l, i)
{
if (l[i] == x)
{
++ result;
}
}
return result;
}
template<class ListType>
Foam::label Foam::findIndex
(