mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
SortableList improvements
- now works as expected with transfer() and assignment from other Lists - shrink() method : clears indices and returns underlying List
This commit is contained in:
@ -26,7 +26,7 @@ Description
|
||||
|
||||
\*---------------------------------------------------------------------------*/
|
||||
|
||||
#include "labelList.H"
|
||||
#include "SortableList.H"
|
||||
|
||||
using namespace Foam;
|
||||
|
||||
@ -35,16 +35,40 @@ using namespace Foam;
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
labelList a(5);
|
||||
a[0] = 7;
|
||||
a[1] = 4;
|
||||
a[2] = 1;
|
||||
a[3] = 2;
|
||||
a[4] = 9;
|
||||
labelList orig(5);
|
||||
orig[0] = 7;
|
||||
orig[1] = 4;
|
||||
orig[2] = 1;
|
||||
orig[3] = 2;
|
||||
orig[4] = 9;
|
||||
|
||||
labelList a(orig);
|
||||
Info << "before: " << a << endl;
|
||||
sort(a);
|
||||
Info << "after: " << a << endl;
|
||||
Info << "after: " << a << endl;
|
||||
|
||||
SortableList<label> b(orig);
|
||||
Info << "sorted: " << b << endl;
|
||||
Info << "indices: " << b.indices() << endl;
|
||||
|
||||
Info << "shrunk: " << b.shrink() << endl;
|
||||
Info << "indices: " << b.indices() << endl;
|
||||
|
||||
// repeat by assignment
|
||||
b = orig;
|
||||
Info << "unsorted: " << b << endl;
|
||||
b.sort();
|
||||
|
||||
Info << "sorted: " << b << endl;
|
||||
Info << "indices: " << b.indices() << endl;
|
||||
|
||||
// transfer assignment
|
||||
b.transfer(orig);
|
||||
Info << "unsorted: " << b << endl;
|
||||
b.sort();
|
||||
|
||||
Info << "sorted: " << b << endl;
|
||||
Info << "indices: " << b.indices() << endl;
|
||||
|
||||
Info << "End\n" << endl;
|
||||
|
||||
|
||||
@ -26,6 +26,11 @@ License
|
||||
|
||||
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
|
||||
|
||||
template <class Type>
|
||||
Foam::SortableList<Type>::SortableList()
|
||||
{}
|
||||
|
||||
|
||||
template <class Type>
|
||||
Foam::SortableList<Type>::SortableList(const UList<Type>& values)
|
||||
:
|
||||
@ -34,6 +39,7 @@ Foam::SortableList<Type>::SortableList(const UList<Type>& values)
|
||||
sort();
|
||||
}
|
||||
|
||||
|
||||
template <class Type>
|
||||
Foam::SortableList<Type>::SortableList(const xfer<List<Type> >& values)
|
||||
:
|
||||
@ -83,6 +89,14 @@ void Foam::SortableList<Type>::clear()
|
||||
}
|
||||
|
||||
|
||||
template <class Type>
|
||||
Foam::List<Type>& Foam::SortableList<Type>::shrink()
|
||||
{
|
||||
indices_.clear();
|
||||
return static_cast<List<Type>&>(*this);
|
||||
}
|
||||
|
||||
|
||||
template <class Type>
|
||||
void Foam::SortableList<Type>::sort()
|
||||
{
|
||||
@ -108,6 +122,14 @@ void Foam::SortableList<Type>::sort()
|
||||
|
||||
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
|
||||
|
||||
template <class Type>
|
||||
void Foam::SortableList<Type>::operator=(const UList<Type>& rhs)
|
||||
{
|
||||
List<Type>::operator=(rhs);
|
||||
indices_.clear();
|
||||
}
|
||||
|
||||
|
||||
template <class Type>
|
||||
void Foam::SortableList<Type>::operator=(const SortableList<Type>& rhs)
|
||||
{
|
||||
|
||||
@ -64,6 +64,9 @@ public:
|
||||
|
||||
// Constructors
|
||||
|
||||
//- Null constructor, sort later (eg, after assignment or transfer)
|
||||
SortableList();
|
||||
|
||||
//- Construct from UList, sorting immediately.
|
||||
explicit SortableList(const UList<Type>&);
|
||||
|
||||
@ -102,12 +105,19 @@ public:
|
||||
//- Clear the list and the indices
|
||||
void clear();
|
||||
|
||||
//- Clear the indices and return a reference to the underlying List
|
||||
List<Type>& shrink();
|
||||
|
||||
//- (stable) sort the list (if changed after construction time)
|
||||
// also resizes the indices if required
|
||||
// also resizes the indices as required
|
||||
void sort();
|
||||
|
||||
// Member Operators
|
||||
|
||||
//- Assignment from UList operator. Takes linear time.
|
||||
void operator=(const UList<Type>&);
|
||||
|
||||
//- Assignment operator. Takes linear time.
|
||||
void operator=(const SortableList<Type>&);
|
||||
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user