Commit bbb989f8 authored by Mark Olesen's avatar Mark Olesen
Browse files

SortableList changes

 - moved SortableList<T>::less class to UList<T>::less for better accessibility
 - add clear() method now also handles the indices
 - indices length is caught in sort()
 - indices remain (mostly) zero-sized until after sort()
 - allow non-const access to indices
parent 4e700e62
......@@ -24,53 +24,39 @@ License
\*---------------------------------------------------------------------------*/
#include "OSspecific.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
// Construct from List
template <class Type>
Foam::SortableList<Type>::SortableList(const UList<Type>& values)
:
List<Type>(values),
indices_(values.size())
List<Type>(values)
{
sort();
}
// Construct from List by transferring
template <class Type>
Foam::SortableList<Type>::SortableList(const xfer<List<Type> >& values)
:
List<Type>(),
indices_(values->size())
List<Type>(values)
{
List<Type>::transfer(values());
sort();
}
// Construct given size. Sort later on.
template <class Type>
Foam::SortableList<Type>::SortableList(const label size)
:
List<Type>(size),
indices_(size)
List<Type>(size)
{}
// Construct given size and initial value. Sort later on.
template <class Type>
Foam::SortableList<Type>::SortableList(const label size, const Type& val)
:
List<Type>(size, val),
indices_(size)
List<Type>(size, val)
{}
// Construct as copy.
template <class Type>
Foam::SortableList<Type>::SortableList(const SortableList<Type>& lst)
:
......@@ -85,29 +71,38 @@ template <class Type>
void Foam::SortableList<Type>::setSize(const label newSize)
{
List<Type>::setSize(newSize);
indices_.setSize(newSize);
indices_.setSize(newSize, -1);
}
template <class Type>
void Foam::SortableList<Type>::clear()
{
List<Type>::clear();
indices_.clear();
}
template <class Type>
void Foam::SortableList<Type>::sort()
{
// list lengths must be identical
indices_.setSize(this->size());
forAll(indices_, i)
{
indices_[i] = i;
}
// Foam::sort(indices_, less(*this));
Foam::stableSort(indices_, less(*this));
List<Type> tmpValues(this->size());
Foam::stableSort(indices_, typename UList<Type>::less(*this));
List<Type> lst(this->size());
forAll(indices_, i)
{
tmpValues[i] = this->operator[](indices_[i]);
lst[i] = this->operator[](indices_[i]);
}
List<Type>::transfer(tmpValues);
List<Type>::transfer(lst);
}
......@@ -120,7 +115,6 @@ void Foam::SortableList<Type>::operator=(const SortableList<Type>& rhs)
indices_ = rhs.indices();
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// ************************************************************************* //
......@@ -60,44 +60,22 @@ class SortableList
//- Original indices
labelList indices_;
public:
// Public classes
//- Less function class used by the sort function
class less
{
const UList<Type>& values_;
public:
less(const UList<Type>& values)
:
values_(values)
{}
bool operator()(const label a, const label b)
{
return values_[a] < values_[b];
}
};
// Constructors
//- Construct from List, sorting the elements.
// Starts with indices set to index in argument
//- Construct from UList, sorting immediately.
explicit SortableList(const UList<Type>&);
//- Construct from tranferred List, sorting the elements.
// Starts with indices set to index in argument
//- Construct from transferred List, sorting immediately.
explicit SortableList(const xfer<List<Type> >&);
//- Construct given size. Sort later on.
// The indices remain empty until the list is sorted
explicit SortableList(const label size);
//- Construct given size and initial value. Sort later on.
// The indices remain empty until the list is sorted
SortableList(const label size, const Type&);
//- Construct as copy.
......@@ -112,10 +90,20 @@ public:
return indices_;
}
//- Size the list. If grow can cause undefined indices (until next sort)
//- Return non-const access to the sorted indices. Updated every sort.
labelList& indices()
{
return indices_;
}
//- Size the list. Growing can cause undefined indices (until next sort)
void setSize(const label);
//- Clear the list and the indices
void clear();
//- (stable) sort the list (if changed after construction time)
// also resizes the indices if required
void sort();
// Member Operators
......
......@@ -92,6 +92,26 @@ public:
//- Declare friendship with the SubList class
friend class SubList<T>;
// Public classes
//- Less function class that can be used for sorting
class less
{
const UList<T>& values_;
public:
less(const UList<T>& values)
:
values_(values)
{}
bool operator()(const label a, const label b)
{
return values_[a] < values_[b];
}
};
// Constructors
......
......@@ -133,7 +133,7 @@ Foam::labelList Foam::simpleGeomDecomp::decompose(const pointField& points)
sort
(
pointIndices,
SortableList<scalar>::less(rotatedPoints.component(vector::X))
UList<scalar>::less(rotatedPoints.component(vector::X))
);
assignToProcessorGroup(processorGroups, n_.x());
......@@ -149,7 +149,7 @@ Foam::labelList Foam::simpleGeomDecomp::decompose(const pointField& points)
sort
(
pointIndices,
SortableList<scalar>::less(rotatedPoints.component(vector::Y))
UList<scalar>::less(rotatedPoints.component(vector::Y))
);
assignToProcessorGroup(processorGroups, n_.y());
......@@ -165,7 +165,7 @@ Foam::labelList Foam::simpleGeomDecomp::decompose(const pointField& points)
sort
(
pointIndices,
SortableList<scalar>::less(rotatedPoints.component(vector::Z))
UList<scalar>::less(rotatedPoints.component(vector::Z))
);
assignToProcessorGroup(processorGroups, n_.z());
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment