ListOps: Added count function to return the number of occurrences of a value in a list
This commit is contained in:
@ -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>
|
||||
|
||||
@ -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
|
||||
(
|
||||
|
||||
Reference in New Issue
Block a user