Add protected member List::size(const label) and removed some of the old

usage of the public 'label& List::size()'.

IMO this method is much too dangerous to leave public - the different
signature is needed to avoid confusing the compiler.
This commit is contained in:
Mark Olesen
2008-10-10 10:20:18 +02:00
parent a82aa515b5
commit 8d63073b08
5 changed files with 43 additions and 13 deletions

View File

@ -33,7 +33,7 @@ inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::DynamicList()
List<T>(SizeInc), List<T>(SizeInc),
allocSize_(SizeInc) allocSize_(SizeInc)
{ {
List<T>::size() = 0; List<T>::size(0);
} }
@ -47,7 +47,7 @@ inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::DynamicList
List<T>(s), List<T>(s),
allocSize_(s) allocSize_(s)
{ {
List<T>::size() = 0; List<T>::size(0);
} }
@ -81,14 +81,14 @@ inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::setSize
{ {
if (s < List<T>::size()) if (s < List<T>::size())
{ {
List<T>::size() = s; List<T>::size(s);
} }
else else
{ {
label nextFree = List<T>::size(); label nextFree = List<T>::size();
allocSize_ = s; allocSize_ = s;
List<T>::setSize(allocSize_); List<T>::setSize(allocSize_);
List<T>::size() = nextFree; List<T>::size(nextFree);
} }
} }
@ -102,14 +102,14 @@ inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::setSize
{ {
if (s < List<T>::size()) if (s < List<T>::size())
{ {
List<T>::size() = s; List<T>::size(s);
} }
else else
{ {
label nextFree = List<T>::size(); label nextFree = List<T>::size();
allocSize_ = s; allocSize_ = s;
List<T>::setSize(allocSize_, t); List<T>::setSize(allocSize_, t);
List<T>::size() = nextFree; List<T>::size(nextFree);
} }
} }
@ -117,7 +117,7 @@ inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::setSize
template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv> template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::clear() inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::clear()
{ {
List<T>::size() = 0; List<T>::size(0);
} }
@ -191,7 +191,7 @@ inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::append(const T& e)
List<T>::setSize(allocSize_); List<T>::setSize(allocSize_);
} }
List<T>::size() = nextFree; List<T>::size(nextFree);
this->operator[](nextFree - 1) = e; this->operator[](nextFree - 1) = e;
} }
@ -212,7 +212,7 @@ inline T Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::remove()
const T& val = List<T>::operator[](nextFree); const T& val = List<T>::operator[](nextFree);
List<T>::size() = nextFree; List<T>::size(nextFree);
return val; return val;
} }
@ -240,7 +240,7 @@ inline T& Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::operator()
List<T>::setSize(allocSize_); List<T>::setSize(allocSize_);
} }
List<T>::size() = nextFree; List<T>::size(nextFree);
return this->operator[](i); return this->operator[](i);
} }

View File

@ -78,6 +78,12 @@ class List
public UList<T> public UList<T>
{ {
protected:
//- Override size to be inconsistent with allocated storage.
// Use with care.
inline void size(const label);
public: public:
// Constructors // Constructors

View File

@ -52,6 +52,13 @@ inline T& Foam::List<T>::newElmt(const label i)
} }
template<class T>
inline void Foam::List<T>::size(const label n)
{
UList<T>::size_ = n;
}
template<class T> template<class T>
inline Foam::label Foam::List<T>::size() const inline Foam::label Foam::List<T>::size() const
{ {

View File

@ -40,6 +40,17 @@ Foam::SortableList<Type>::SortableList(const List<Type>& values)
sort(); 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>::transfer(*values);
sort();
}
// Construct given size. Sort later on. // Construct given size. Sort later on.
template <class Type> template <class Type>
@ -86,7 +97,7 @@ void Foam::SortableList<Type>::sort()
indices_[i] = i; indices_[i] = i;
} }
//Foam::sort(indices_, less(*this)); // Foam::sort(indices_, less(*this));
Foam::stableSort(indices_, less(*this)); Foam::stableSort(indices_, less(*this));
List<Type> tmpValues(this->size()); List<Type> tmpValues(this->size());

View File

@ -29,6 +29,8 @@ Description
A list that is sorted upon construction or when explicitly requested A list that is sorted upon construction or when explicitly requested
with the sort() method. with the sort() method.
Uses the Foam::stableSort() algorithm.
SourceFiles SourceFiles
SortableList.C SortableList.C
@ -84,10 +86,14 @@ public:
// Constructors // Constructors
//- Construct from List, sorting the elements. Starts with indices set //- Construct from List, sorting the elements.
// to index in argument // Starts with indices set to index in argument
explicit SortableList(const List<Type>&); explicit SortableList(const List<Type>&);
//- Construct from tranferred List, sorting the elements.
// Starts with indices set to index in argument
explicit SortableList(const xfer<List<Type> >&);
//- Construct given size. Sort later on. //- Construct given size. Sort later on.
explicit SortableList(const label size); explicit SortableList(const label size);