mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: ensure indices are properly reset in SortList
- use more ListOps functions, add uniqueSort() method
This commit is contained in:
@ -6,7 +6,7 @@
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
Copyright (C) 2011 OpenFOAM Foundation
|
||||
Copyright (C) 2017-2020 OpenCFD Ltd.
|
||||
Copyright (C) 2017-2022 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
@ -196,19 +196,23 @@ int main(int argc, char *argv[])
|
||||
|
||||
SortList<scalar> sorter1(list1);
|
||||
|
||||
Info<< nl << "Sorted: " << flatOutput(sorter1) << nl;
|
||||
Info<< nl << "Sort indices: " << flatOutput(sorter1.indices()) << nl;
|
||||
|
||||
Info<< nl << "Reverse indices: " << flatOutput(sorter1.indices()) << nl;
|
||||
|
||||
// Reverses indices
|
||||
sorter1.reverse();
|
||||
|
||||
Info<< nl << "Again indices: " << flatOutput(sorter1.indices()) << nl;
|
||||
Info<< nl << "Reverse: " << flatOutput(sorter1.indices()) << nl;
|
||||
|
||||
sorter1.reset();
|
||||
|
||||
Info<< nl << "Reset indices: " << flatOutput(sorter1.indices()) << nl;
|
||||
|
||||
sorter1.reverseSort();
|
||||
|
||||
Info<< nl << "Reverse indices: " << flatOutput(sorter1.indices()) << nl;
|
||||
|
||||
Info<< nl << "Sorted : " << flatOutput(sorter1) << nl;
|
||||
Info<< nl << "Reverse sorted: " << flatOutput(sorter1) << nl;
|
||||
|
||||
sorter1.sort(std::greater<scalar>());
|
||||
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
\\ / A nd | www.openfoam.com
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
Copyright (C) 2019 OpenCFD Ltd.
|
||||
Copyright (C) 2019-2022 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
@ -37,8 +37,8 @@ SourceFiles
|
||||
|
||||
\*---------------------------------------------------------------------------*/
|
||||
|
||||
#ifndef SortList_H
|
||||
#define SortList_H
|
||||
#ifndef Foam_SortList_H
|
||||
#define Foam_SortList_H
|
||||
|
||||
#include "IndirectList.H"
|
||||
|
||||
@ -72,13 +72,13 @@ public:
|
||||
|
||||
// Member Functions
|
||||
|
||||
//- Return the list of sorted indices. Updated every sort
|
||||
//- Return the list of sorted indices (updated every sort).
|
||||
// Same as addressing()
|
||||
inline const labelUList& indices() const;
|
||||
inline const labelUList& indices() const noexcept;
|
||||
|
||||
//- Return the list of indices. Updated every sort
|
||||
//- Return the list of indices (updated every sort).
|
||||
// Same as addressing()
|
||||
inline labelList& indices();
|
||||
inline labelList& indices() noexcept;
|
||||
|
||||
//- Reverse the indices
|
||||
inline void reverse();
|
||||
@ -94,9 +94,13 @@ public:
|
||||
//- Functionally identical to sort with std::less\<T\>()
|
||||
inline void sort();
|
||||
|
||||
//- Reverse (stable) sort the list
|
||||
//- Reverse (stable) sort the list.
|
||||
//- Functionally identical to sort with std::greater\<T\>()
|
||||
inline void reverseSort();
|
||||
|
||||
//- Sort the list, only retaining unique entries
|
||||
inline void uniqueSort();
|
||||
|
||||
|
||||
// Member Operators
|
||||
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
\\ / A nd | www.openfoam.com
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
Copyright (C) 2019 OpenCFD Ltd.
|
||||
Copyright (C) 2019-2022 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
@ -32,7 +32,7 @@ License
|
||||
template<class T>
|
||||
inline Foam::SortList<T>::SortList(const UList<T>& values)
|
||||
:
|
||||
IndirectList<T>(values, identity(values.size()))
|
||||
IndirectList<T>(values, labelList())
|
||||
{
|
||||
sort();
|
||||
}
|
||||
@ -42,7 +42,7 @@ template<class T>
|
||||
template<class Compare>
|
||||
inline Foam::SortList<T>::SortList(const UList<T>& values, const Compare& comp)
|
||||
:
|
||||
IndirectList<T>(values, identity(values.size()))
|
||||
IndirectList<T>(values, labelList())
|
||||
{
|
||||
sort<Compare>(comp);
|
||||
}
|
||||
@ -51,14 +51,14 @@ inline Foam::SortList<T>::SortList(const UList<T>& values, const Compare& comp)
|
||||
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
|
||||
|
||||
template<class T>
|
||||
inline const Foam::labelUList& Foam::SortList<T>::indices() const
|
||||
inline const Foam::labelUList& Foam::SortList<T>::indices() const noexcept
|
||||
{
|
||||
return this->addressing();
|
||||
}
|
||||
|
||||
|
||||
template<class T>
|
||||
inline Foam::labelList& Foam::SortList<T>::indices()
|
||||
inline Foam::labelList& Foam::SortList<T>::indices() noexcept
|
||||
{
|
||||
return this->addressing();
|
||||
}
|
||||
@ -74,10 +74,9 @@ inline void Foam::SortList<T>::reverse()
|
||||
template<class T>
|
||||
inline void Foam::SortList<T>::reset()
|
||||
{
|
||||
const label len = this->values().size();
|
||||
auto& addr = this->indices();
|
||||
|
||||
labelList& addr = this->indices();
|
||||
addr.resize(len);
|
||||
addr.resize_nocopy(this->values().size());
|
||||
ListOps::identity(addr);
|
||||
}
|
||||
|
||||
@ -86,14 +85,11 @@ template<class T>
|
||||
template<class Compare>
|
||||
inline void Foam::SortList<T>::sort(const Compare& comp)
|
||||
{
|
||||
UList<T>& vals = this->values();
|
||||
labelList& addr = this->indices();
|
||||
auto& vals = this->values();
|
||||
auto& addr = this->indices();
|
||||
|
||||
if (addr.size() != vals.size())
|
||||
{
|
||||
addr.resize(vals.size());
|
||||
addr.resize_nocopy(vals.size());
|
||||
ListOps::identity(addr);
|
||||
}
|
||||
|
||||
std::stable_sort
|
||||
(
|
||||
@ -107,43 +103,26 @@ inline void Foam::SortList<T>::sort(const Compare& comp)
|
||||
template<class T>
|
||||
inline void Foam::SortList<T>::sort()
|
||||
{
|
||||
UList<T>& vals = this->values();
|
||||
labelList& addr = this->indices();
|
||||
|
||||
if (addr.size() != vals.size())
|
||||
{
|
||||
addr.resize(vals.size());
|
||||
ListOps::identity(addr);
|
||||
Foam::sortedOrder(this->values(), this->indices());
|
||||
}
|
||||
|
||||
// Forward sort of indices
|
||||
std::stable_sort
|
||||
(
|
||||
addr.begin(),
|
||||
addr.end(),
|
||||
[&](label a, label b) -> bool { return vals[a] < vals[b]; }
|
||||
);
|
||||
|
||||
template<class T>
|
||||
inline void Foam::SortList<T>::uniqueSort()
|
||||
{
|
||||
Foam::uniqueOrder(this->values(), this->indices());
|
||||
}
|
||||
|
||||
|
||||
template<class T>
|
||||
inline void Foam::SortList<T>::reverseSort()
|
||||
{
|
||||
UList<T>& vals = this->values();
|
||||
labelList& addr = this->indices();
|
||||
|
||||
if (addr.size() != vals.size())
|
||||
{
|
||||
addr.resize(vals.size());
|
||||
ListOps::identity(addr);
|
||||
}
|
||||
|
||||
// Reverse sort of indices
|
||||
std::stable_sort
|
||||
// Reverse sorted order for indices
|
||||
Foam::sortedOrder
|
||||
(
|
||||
addr.begin(),
|
||||
addr.end(),
|
||||
[&](label a, label b) -> bool { return vals[b] < vals[a]; }
|
||||
this->values(),
|
||||
this->indices(),
|
||||
typename UList<T>::greater(this->values())
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
Copyright (C) 2011-2016 OpenFOAM Foundation
|
||||
Copyright (C) 2017-2021 OpenCFD Ltd.
|
||||
Copyright (C) 2017-2022 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
@ -143,7 +143,7 @@ void Foam::SortableList<T>::reverseSort()
|
||||
template<class T>
|
||||
void Foam::SortableList<T>::partialSort(label n, label start)
|
||||
{
|
||||
indices_.resize(this->size());
|
||||
indices_.resize_nocopy(this->size());
|
||||
ListOps::identity(indices_);
|
||||
|
||||
// Forward partial sort of indices
|
||||
@ -163,7 +163,7 @@ void Foam::SortableList<T>::partialSort(label n, label start)
|
||||
template<class T>
|
||||
void Foam::SortableList<T>::partialReverseSort(label n, label start)
|
||||
{
|
||||
indices_.resize(this->size());
|
||||
indices_.resize_nocopy(this->size());
|
||||
ListOps::identity(indices_);
|
||||
|
||||
// Reverse partial sort of indices
|
||||
|
||||
Reference in New Issue
Block a user