Commit 81ba5b77 authored by Henry Weller's avatar Henry Weller
Browse files

PtrList: Now derived from UPtrList to avoid unnecessary code duplication

consistency with UList/List and so that functions which take a UPtrList
argument can also be called for PtrList.
parent f779557d
......@@ -49,11 +49,12 @@ SourceFiles
namespace Foam
{
// Forward declaration of classes
class Istream;
class Ostream;
// Forward declaration of friend functions and operators
template<class T> class List;
template<class T> Istream& operator>>(Istream&, List<T>&);
......@@ -85,7 +86,7 @@ class List
protected:
//- Override size to be inconsistent with allocated storage.
// Use with care.
// Use with care
inline void size(const label);
......@@ -98,28 +99,28 @@ public:
// Constructors
//- Null constructor.
//- Null constructor
inline List();
//- Construct with given size.
//- Construct with given size
explicit List(const label);
//- Construct with given size and value for all elements.
//- Construct with given size and value for all elements
List(const label, const T&);
//- Construct with given size initializing all elements to zero.
//- Construct with given size initializing all elements to zero
List(const label, const zero);
//- Copy constructor.
//- Copy constructor
List(const List<T>&);
//- Construct by transferring the parameter contents
List(const Xfer<List<T>>&);
//- Construct as copy or re-use as specified.
//- Construct as copy or re-use as specified
List(List<T>&, bool reuse);
//- Construct as subset.
//- Construct as subset
List(const UList<T>&, const labelUList& mapAddressing);
//- Construct as copy of FixedList<T, Size>
......@@ -138,7 +139,7 @@ public:
//- Construct as copy of BiIndirectList<T>
explicit List(const BiIndirectList<T>&);
//- Construct from Istream.
//- Construct from Istream
List(Istream&);
//- Clone
......@@ -157,7 +158,7 @@ public:
// Member Functions
//- Return the number of elements in the UList.
//- Return the number of elements in the UList
inline label size() const;
......@@ -169,13 +170,13 @@ public:
//- Alias for setSize(const label, const T&)
inline void resize(const label, const T&);
//- Reset size of List.
//- Reset size of List
void setSize(const label);
//- Reset size of List and value for new elements.
//- Reset size of List and value for new elements
void setSize(const label, const T&);
//- Clear the list, i.e. set size to zero.
//- Clear the list, i.e. set size to zero
void clear();
//- Append an element at the end of the list
......@@ -188,22 +189,22 @@ public:
inline void append(const UIndirectList<T>&);
//- Transfer the contents of the argument List into this list
// and annul the argument list.
// and annul the argument list
void transfer(List<T>&);
//- Transfer the contents of the argument List into this list
// and annul the argument list.
// and annul the argument list
template<unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
void transfer(DynamicList<T, SizeInc, SizeMult, SizeDiv>&);
//- Transfer the contents of the argument List into this list
// and annul the argument list.
// and annul the argument list
void transfer(SortableList<T>&);
//- Transfer contents to the Xfer container
inline Xfer<List<T>> xfer();
//- Return subscript-checked element of UList.
//- Return subscript-checked element of UList
inline T& newElmt(const label);
......@@ -213,19 +214,19 @@ public:
// Member operators
//- Assignment from UList operator. Takes linear time.
//- Assignment from UList operator. Takes linear time
void operator=(const UList<T>&);
//- Assignment operator. Takes linear time.
//- Assignment operator. Takes linear time
void operator=(const List<T>&);
//- Assignment from SLList operator. Takes linear time.
//- Assignment from SLList operator. Takes linear time
void operator=(const SLList<T>&);
//- Assignment from UIndirectList operator. Takes linear time.
//- Assignment from UIndirectList operator. Takes linear time
void operator=(const UIndirectList<T>&);
//- Assignment from BiIndirectList operator. Takes linear time.
//- Assignment from BiIndirectList operator. Takes linear time
void operator=(const BiIndirectList<T>&);
//- Assignment of all entries to the given value
......@@ -237,19 +238,19 @@ public:
// Istream operator
//- Read List from Istream, discarding contents of existing List.
//- Read List from Istream, discarding contents of existing List
friend Istream& operator>> <T>
(Istream&, List<T>&);
};
//- Read a bracket-delimited list, or handle a single value as list of size 1.
//- Read a bracket-delimited list, or handle a single value as list of size 1
// For example,
// \code
// wList = readList<word>(IStringStream("(patch1 patch2 patch3)")());
// wList = readList<word>(IStringStream("patch0")());
// \endcode
// Mostly useful for handling command-line arguments.
// Mostly useful for handling command-line arguments
template<class T>
List<T> readList(Istream&);
......
......@@ -23,7 +23,6 @@ License
\*---------------------------------------------------------------------------*/
#include "error.H"
#include <climits>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......
......@@ -23,8 +23,6 @@ License
\*---------------------------------------------------------------------------*/
#include "error.H"
#include "PtrList.H"
#include "SLPtrList.H"
......@@ -33,25 +31,25 @@ License
template<class T>
Foam::PtrList<T>::PtrList()
:
ptrs_()
UPtrList<T>()
{}
template<class T>
Foam::PtrList<T>::PtrList(const label s)
:
ptrs_(s, reinterpret_cast<T*>(0))
UPtrList<T>(s)
{}
template<class T>
Foam::PtrList<T>::PtrList(const PtrList<T>& a)
:
ptrs_(a.size())
UPtrList<T>(a.size())
{
forAll(*this, i)
{
ptrs_[i] = (a[i]).clone().ptr();
this->ptrs_[i] = (a[i]).clone().ptr();
}
}
......@@ -60,11 +58,11 @@ template<class T>
template<class CloneArg>
Foam::PtrList<T>::PtrList(const PtrList<T>& a, const CloneArg& cloneArg)
:
ptrs_(a.size())
UPtrList<T>(a.size())
{
forAll(*this, i)
{
ptrs_[i] = (a[i]).clone(cloneArg).ptr();
this->ptrs_[i] = (a[i]).clone(cloneArg).ptr();
}
}
......@@ -79,13 +77,13 @@ Foam::PtrList<T>::PtrList(const Xfer<PtrList<T>>& lst)
template<class T>
Foam::PtrList<T>::PtrList(PtrList<T>& a, bool reuse)
:
ptrs_(a.ptrs_, reuse)
UPtrList<T>(a, reuse)
{
if (!reuse)
{
forAll(*this, i)
{
ptrs_[i] = (a[i]).clone().ptr();
this->ptrs_[i] = (a[i]).clone().ptr();
}
}
}
......@@ -94,7 +92,7 @@ Foam::PtrList<T>::PtrList(PtrList<T>& a, bool reuse)
template<class T>
Foam::PtrList<T>::PtrList(const SLPtrList<T>& sll)
:
ptrs_(sll.size())
UPtrList<T>(sll.size())
{
if (sll.size())
{
......@@ -106,7 +104,7 @@ Foam::PtrList<T>::PtrList(const SLPtrList<T>& sll)
++iter
)
{
ptrs_[i++] = (iter()).clone().ptr();
this->ptrs_[i++] = (iter()).clone().ptr();
}
}
}
......@@ -119,9 +117,9 @@ Foam::PtrList<T>::~PtrList()
{
forAll(*this, i)
{
if (ptrs_[i])
if (this->ptrs_[i])
{
delete ptrs_[i];
delete this->ptrs_[i];
}
}
}
......@@ -140,7 +138,7 @@ void Foam::PtrList<T>::setSize(const label newSize)
<< abort(FatalError);
}
label oldSize = size();
label oldSize = this->size();
if (newSize == 0)
{
......@@ -151,22 +149,22 @@ void Foam::PtrList<T>::setSize(const label newSize)
label i;
for (i=newSize; i<oldSize; i++)
{
if (ptrs_[i])
if (this->ptrs_[i])
{
delete ptrs_[i];
delete this->ptrs_[i];
}
}
ptrs_.setSize(newSize);
this->ptrs_.setSize(newSize);
}
else // newSize > oldSize
{
ptrs_.setSize(newSize);
this->ptrs_.setSize(newSize);
label i;
for (i=oldSize; i<newSize; i++)
{
ptrs_[i] = NULL;
this->ptrs_[i] = NULL;
}
}
}
......@@ -177,13 +175,13 @@ void Foam::PtrList<T>::clear()
{
forAll(*this, i)
{
if (ptrs_[i])
if (this->ptrs_[i])
{
delete ptrs_[i];
delete this->ptrs_[i];
}
}
ptrs_.clear();
this->ptrs_.clear();
}
......@@ -191,33 +189,33 @@ template<class T>
void Foam::PtrList<T>::transfer(PtrList<T>& a)
{
clear();
ptrs_.transfer(a.ptrs_);
this->ptrs_.transfer(a.ptrs_);
}
template<class T>
void Foam::PtrList<T>::reorder(const labelUList& oldToNew)
{
if (oldToNew.size() != size())
if (oldToNew.size() != this->size())
{
FatalErrorInFunction
<< "Size of map (" << oldToNew.size()
<< ") not equal to list size (" << size()
<< ") not equal to list size (" << this->size()
<< ") for type " << typeid(T).name()
<< abort(FatalError);
}
List<T*> newPtrs_(ptrs_.size(), reinterpret_cast<T*>(0));
List<T*> newPtrs_(this->ptrs_.size(), reinterpret_cast<T*>(0));
forAll(*this, i)
{
label newI = oldToNew[i];
if (newI < 0 || newI >= size())
if (newI < 0 || newI >= this->size())
{
FatalErrorInFunction
<< "Illegal index " << newI << nl
<< "Valid indices are 0.." << size()-1
<< "Valid indices are 0.." << this->size()-1
<< " for type " << typeid(T).name()
<< abort(FatalError);
}
......@@ -229,7 +227,7 @@ void Foam::PtrList<T>::reorder(const labelUList& oldToNew)
<< " already set for type " << typeid(T).name()
<< abort(FatalError);
}
newPtrs_[newI] = ptrs_[i];
newPtrs_[newI] = this->ptrs_[i];
}
forAll(newPtrs_, i)
......@@ -242,7 +240,7 @@ void Foam::PtrList<T>::reorder(const labelUList& oldToNew)
}
}
ptrs_.transfer(newPtrs_);
this->ptrs_.transfer(newPtrs_);
}
......@@ -258,16 +256,16 @@ void Foam::PtrList<T>::operator=(const PtrList<T>& a)
<< abort(FatalError);
}
if (size() == 0)
if (this->size() == 0)
{
setSize(a.size());
forAll(*this, i)
{
ptrs_[i] = (a[i]).clone().ptr();
this->ptrs_[i] = (a[i]).clone().ptr();
}
}
else if (a.size() == size())
else if (a.size() == this->size())
{
forAll(*this, i)
{
......
......@@ -41,83 +41,25 @@ SourceFiles
#ifndef PtrList_H
#define PtrList_H
#include "List.H"
#include "UPtrList.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// Forward declaration of friend functions and operators
// Forward declaration of classes
template<class T> class PtrList;
template<class T> class SLPtrList;
template<class T> class autoPtr;
template<class T> class tmp;
template<class T>
inline typename PtrList<T>::iterator operator+
(
const typename PtrList<T>::iterator&,
label
);
template<class T>
inline typename PtrList<T>::iterator operator+
(
label,
const typename PtrList<T>::iterator&
);
template<class T>
inline typename PtrList<T>::iterator operator-
(
const typename PtrList<T>::iterator&,
label
);
template<class T>
inline label operator-
(
const typename PtrList<T>::iterator&,
const typename PtrList<T>::iterator&
);
template<class T>
inline typename PtrList<T>::const_iterator operator+
(
const typename PtrList<T>::const_iterator&,
label
);
template<class T>
inline typename PtrList<T>::const_iterator operator+
(
label,
const typename PtrList<T>::const_iterator&
);
template<class T>
inline typename PtrList<T>::const_iterator operator-
(
const typename PtrList<T>::const_iterator&,
label
);
template<class T>
inline label operator-
(
const typename PtrList<T>::const_iterator&,
const typename PtrList<T>::const_iterator&
);
// Forward declaration of friend functions and operators
template<class T> class PtrList;
template<class T>
Istream& operator>>(Istream&, PtrList<T>&);
template<class T>
Ostream& operator<<(Ostream&, const PtrList<T>&);
template<class T> class autoPtr;
template<class T> class tmp;
/*---------------------------------------------------------------------------*\
Class PtrList Declaration
......@@ -125,11 +67,9 @@ template<class T> class tmp;
template<class T>
class PtrList
:
public UPtrList<T>
{
// Private data
List<T*> ptrs_;
protected:
......@@ -144,13 +84,13 @@ public:
// Constructors
//- Null Constructor.
//- Null Constructor
PtrList();
//- Construct with size specified.
//- Construct with size specified
explicit PtrList(const label);
//- Copy constructor.
//- Copy constructor
PtrList(const PtrList<T>&);
//- Copy constructor with additional argument for clone
......@@ -160,7 +100,7 @@ public:
//- Construct by transferring the parameter contents
PtrList(const Xfer<PtrList<T>>&);
//- Construct as copy or re-use as specified.
//- Construct as copy or re-use as specified
PtrList(PtrList<T>&, bool reuse);
//- Construct as copy of SLPtrList<T>
......@@ -180,38 +120,18 @@ public:
// Member functions
// Access
//- Return the number of elements in the PtrList
inline label size() const;
//- Return true if the PtrList is empty (ie, size() is zero).
inline bool empty() const;
//- Return reference to the first element of the list.
inline T& first();
//- Return reference to first element of the list.
inline const T& first() const;
//- Return reference to the last element of the list.
inline T& last();
//- Return reference to the last element of the list.
inline const T& last() const;
// Edit
//- Reset size of PtrList. If extending the PtrList, new entries are
// set to NULL. If truncating the PtrList, removed entries are
// deleted.
// deleted
void setSize(const label);
//- Alias for setSize(const label)
inline void resize(const label);
//- Clear the PtrList, i.e. set size to zero deleting all the
// allocated entries.
// allocated entries
void clear();
//- Append an element at the end of the list
......@@ -220,7 +140,7 @@ public:
inline void append(const tmp<T>&);
//- Transfer the contents of the argument PtrList into this PtrList
// and annul the argument list.
// and annul the argument list
void transfer(PtrList<T>&);
//- Transfer contents to the Xfer container
......@@ -229,10 +149,13 @@ public:
//- Is element set
inline bool set(const label) const;
//- Set element. Return old element (can be NULL).
// No checks on new element.
//- Set element to given T* and return old element (can be NULL)
inline autoPtr<T> set(const label, T*);
//- Set element to given autoPtr<T> and return old element
inline autoPtr<T> set(const label, const autoPtr<T>&);
//- Set element to given tmp<T> and return old element
inline autoPtr<T> set(const label, const tmp<T>&);
//- Reorders elements. Ordering does not have to be done in
......@@ -243,186 +166,14 @@ public:
// Member operators
//- Return element const reference.
inline const T& operator[](const label) const;
//- Return element reference.
inline T& operator[](const label);
//- Return element const pointer.
inline const T* operator()(const label) const;
//- Assignment.
//- Assignment
void operator=(const PtrList<T>&);
// STL type definitions
//- Type of values the PtrList contains.
typedef T value_type;
//- Type that can be used for storing into PtrList::value_type objects.
typedef T& reference;
//- Type that can be used for storing into constant PtrList::value_type
// objects.
typedef const T& const_reference;