ENH: ListOps - added uniqueSort function

This commit is contained in:
Andrew Heather
2022-03-09 11:32:00 +00:00
committed by Andrew Heather
parent 053727d2d9
commit ef14a415a9
2 changed files with 31 additions and 0 deletions

View File

@ -217,6 +217,10 @@ void uniqueOrder
);
//- Return sorted list with removal of duplicates
template<class T>
List<T> uniqueSort(const UList<T>& input);
//- Inplace sorting and removal of duplicates.
// Do not use FixedList for the input list, since it doesn't resize.
template<class ListType>

View File

@ -424,6 +424,33 @@ void Foam::uniqueOrder
}
template<class T>
Foam::List<T> Foam::uniqueSort(const UList<T>& input)
{
List<T> output(input);
const label len = output.size();
if (len > 1)
{
Foam::stableSort(output);
label count = 0;
for (label i = 1; i < len; ++i)
{
if (output[count] != output[i])
{
output[++count] = output[i];
}
}
output.resize(count+1);
}
return output;
}
template<class ListType>
void Foam::inplaceUniqueSort(ListType& input)
{