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