Lists get first() and last() member functions

- this builds on Mattijs' commit 968f0bbd57 but with a first()
  as well.

- Added both to FixedList, IndirectList, UIndirectList and *PtrList and
  since they can certainly be useful there. Did not add to BiIndirectList,
  since I'm not sure what it should mean there. Did not add to PackedList,
  since it's not clear how useful they'd be yet in these contexts (and I'm
  not sure how it would interact with the iterator proxy implementation).

- Note: STL defines front() and back() for these methods.
This commit is contained in:
Mark Olesen
2009-11-25 14:47:48 +01:00
parent 1c6dd6de70
commit b2d7439b86
26 changed files with 284 additions and 52 deletions

View File

@ -144,7 +144,7 @@ public:
void clear(); void clear();
//- Transfer the contents of the argument into this DictionaryBase //- Transfer the contents of the argument into this DictionaryBase
// and annull the argument. // and annul the argument.
void transfer(DictionaryBase<IDLListType, T>&); void transfer(DictionaryBase<IDLListType, T>&);
// Member operators // Member operators

View File

@ -287,7 +287,7 @@ public:
void shrink(); void shrink();
//- Transfer the contents of the argument table into this table //- Transfer the contents of the argument table into this table
// and annull the argument table. // and annul the argument table.
void transfer(HashTable<T, Key, Hash>&); void transfer(HashTable<T, Key, Hash>&);
//- Transfer contents to the Xfer container //- Transfer contents to the Xfer container
@ -463,7 +463,7 @@ public:
inline iterator operator++(int); inline iterator operator++(int);
}; };
//- iterator set to the begining of the HashTable //- iterator set to the beginning of the HashTable
inline iterator begin(); inline iterator begin();

View File

@ -247,7 +247,7 @@ public:
void clearStorage(); void clearStorage();
//- Transfer the contents of the argument table into this table //- Transfer the contents of the argument table into this table
// and annull the argument table. // and annul the argument table.
void transfer(StaticHashTable<T, Key, Hash>&); void transfer(StaticHashTable<T, Key, Hash>&);
//- Transfer contents to the Xfer container //- Transfer contents to the Xfer container

View File

@ -140,7 +140,7 @@ public:
void clear(); void clear();
//- Transfer the contents of the argument into this List //- Transfer the contents of the argument into this List
// and annull the argument list. // and annul the argument list.
void transfer(ILList<LListBase, T>&); void transfer(ILList<LListBase, T>&);

View File

@ -202,7 +202,7 @@ public:
void clear(); void clear();
//- Transfer the contents of the argument into this List //- Transfer the contents of the argument into this List
// and annull the argument list. // and annul the argument list.
void transfer(LList<LListBase, T>&); void transfer(LList<LListBase, T>&);
// Member operators // Member operators

View File

@ -156,7 +156,7 @@ public:
void clear(); void clear();
//- Transfer the contents of the argument into this List //- Transfer the contents of the argument into this List
// and annull the argument list. // and annul the argument list.
void transfer(LPtrList<LListBase, T>&); void transfer(LPtrList<LListBase, T>&);

View File

@ -172,7 +172,7 @@ public:
inline void clear(); inline void clear();
//- Transfer the contents of the argument into this List //- Transfer the contents of the argument into this List
// and annull the argument list. // and annul the argument list.
inline void transfer(DLListBase&); inline void transfer(DLListBase&);
// STL iterator // STL iterator

View File

@ -157,7 +157,7 @@ public:
inline void clear(); inline void clear();
//- Transfer the contents of the argument into this List //- Transfer the contents of the argument into this List
// and annull the argument list. // and annul the argument list.
inline void transfer(SLListBase&); inline void transfer(SLListBase&);
// STL iterator // STL iterator

View File

@ -44,7 +44,7 @@ namespace Foam
{ {
/*---------------------------------------------------------------------------*\ /*---------------------------------------------------------------------------*\
Class BiIndirectList Declaration Class BiIndirectList Declaration
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
template<class T> template<class T>
@ -82,11 +82,16 @@ public:
// Access // Access
//- Return the number of elements in the list
inline label size() const; inline label size() const;
inline bool empty() const;
//- Return true if the list is empty (ie, size() is zero).
inline bool empty() const;
inline const UList<T>& posList() const; inline const UList<T>& posList() const;
inline const UList<T>& negList() const; inline const UList<T>& negList() const;
//- Return the list addressing
inline const List<label>& addressing() const; inline const List<label>& addressing() const;
//- Calculate index given whether index is into posList or negList //- Calculate index given whether index is into posList or negList

View File

@ -191,7 +191,7 @@ public:
labelList sizes() const; labelList sizes() const;
//- Transfer the contents of the argument CompactListList //- Transfer the contents of the argument CompactListList
// into this CompactListList and annull the argument list. // into this CompactListList and annul the argument list.
void transfer(CompactListList<T, Container>&); void transfer(CompactListList<T, Container>&);
//- Transfer the contents to the Xfer container //- Transfer the contents to the Xfer container

View File

@ -154,6 +154,18 @@ public:
// This can be used (with caution) when interfacing with C code. // This can be used (with caution) when interfacing with C code.
inline T* data(); inline T* data();
//- Return the first element of the list.
inline T& first();
//- Return first element of the list.
inline const T& first() const;
//- Return the last element of the list.
inline T& last();
//- Return the last element of the list.
inline const T& last() const;
// Check // Check

View File

@ -208,6 +208,34 @@ Foam::FixedList<T, Size>::data()
} }
template<class T, unsigned Size>
inline T& Foam::FixedList<T, Size>::first()
{
return v_[0];
}
template<class T, unsigned Size>
inline const T& Foam::FixedList<T, Size>::first() const
{
return v_[0];
}
template<class T, unsigned Size>
inline T& Foam::FixedList<T, Size>::last()
{
return v_[Size-1];
}
template<class T, unsigned Size>
inline const T& Foam::FixedList<T, Size>::last() const
{
return v_[Size-1];
}
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
// element access // element access

View File

@ -44,7 +44,7 @@ namespace Foam
{ {
/*---------------------------------------------------------------------------*\ /*---------------------------------------------------------------------------*\
Class IndirectList Declaration Class IndirectList Declaration
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
template<class T> template<class T>
@ -70,10 +70,28 @@ public:
// Access // Access
//- Return the number of elements in the list
inline label size() const; inline label size() const;
inline bool empty() const;
//- Return true if the list is empty (ie, size() is zero).
inline bool empty() const;
//- Return the first element of the list.
inline T& first();
//- Return first element of the list.
inline const T& first() const;
//- Return the last element of the list.
inline T& last();
//- Return the last element of the list.
inline const T& last() const;
//- Return the complete list
inline const UList<T>& completeList() const; inline const UList<T>& completeList() const;
//- Return the list addressing
inline const List<label>& addressing() const; inline const List<label>& addressing() const;
// Edit // Edit

View File

@ -66,6 +66,34 @@ inline bool Foam::IndirectList<T>::empty() const
} }
template<class T>
inline T& Foam::IndirectList<T>::first()
{
return completeList_[addressing_.first()];
}
template<class T>
inline const T& Foam::IndirectList<T>::first() const
{
return completeList_[addressing_.first()];
}
template<class T>
inline T& Foam::IndirectList<T>::last()
{
return completeList_[addressing_.last()];
}
template<class T>
inline const T& Foam::IndirectList<T>::last() const
{
return completeList_[addressing_.last()];
}
template<class T> template<class T>
inline const Foam::UList<T>& Foam::IndirectList<T>::completeList() const inline const Foam::UList<T>& Foam::IndirectList<T>::completeList() const
{ {

View File

@ -404,7 +404,7 @@ void Foam::List<T>::clear()
// Transfer the contents of the argument List into this List // Transfer the contents of the argument List into this List
// and anull the argument list // and annul the argument list
template<class T> template<class T>
void Foam::List<T>::transfer(List<T>& a) void Foam::List<T>::transfer(List<T>& a)
{ {
@ -418,7 +418,7 @@ void Foam::List<T>::transfer(List<T>& a)
// Transfer the contents of the argument DynamicList into this List // Transfer the contents of the argument DynamicList into this List
// and anull the argument list // and annul the argument list
template<class T> template<class T>
template<unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv> template<unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
void Foam::List<T>::transfer(DynamicList<T, SizeInc, SizeMult, SizeDiv>& a) void Foam::List<T>::transfer(DynamicList<T, SizeInc, SizeMult, SizeDiv>& a)
@ -431,7 +431,7 @@ void Foam::List<T>::transfer(DynamicList<T, SizeInc, SizeMult, SizeDiv>& a)
// Transfer the contents of the argument SortableList into this List // Transfer the contents of the argument SortableList into this List
// and anull the argument list // and annul the argument list
template<class T> template<class T>
void Foam::List<T>::transfer(SortableList<T>& a) void Foam::List<T>::transfer(SortableList<T>& a)
{ {

View File

@ -188,16 +188,16 @@ public:
inline void append(const UIndirectList<T>&); inline void append(const UIndirectList<T>&);
//- Transfer the contents of the argument List into this list //- Transfer the contents of the argument List into this list
// and annull the argument list. // and annul the argument list.
void transfer(List<T>&); void transfer(List<T>&);
//- Transfer the contents of the argument List into this list //- Transfer the contents of the argument List into this list
// and annull the argument list. // and annul the argument list.
template<unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv> template<unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
void transfer(DynamicList<T, SizeInc, SizeMult, SizeDiv>&); void transfer(DynamicList<T, SizeInc, SizeMult, SizeDiv>&);
//- Transfer the contents of the argument List into this list //- Transfer the contents of the argument List into this list
// and annull the argument list. // and annul the argument list.
void transfer(SortableList<T>&); void transfer(SortableList<T>&);
//- Transfer contents to the Xfer container //- Transfer contents to the Xfer container

View File

@ -253,7 +253,7 @@ public:
inline void shrink(); inline void shrink();
//- Transfer the contents of the argument list into this list //- Transfer the contents of the argument list into this list
// and annull the argument list. // and annul the argument list.
inline void transfer(PackedList<nBits>&); inline void transfer(PackedList<nBits>&);
//- Transfer contents to the Xfer container //- Transfer contents to the Xfer container

View File

@ -29,8 +29,8 @@ Description
A templated 1D list of pointers to objects of type \<T\>, where the A templated 1D list of pointers to objects of type \<T\>, where the
size of the array is known and used for subscript bounds checking, etc. size of the array is known and used for subscript bounds checking, etc.
The element operator [] returns a reference to the object The element operator [] returns a reference to the object rather than a
rather than to the pointer. pointer.
SourceFiles SourceFiles
PtrList.C PtrList.C
@ -119,7 +119,7 @@ public:
//- Null Constructor. //- Null Constructor.
PtrList(); PtrList();
//- Construct with length specified. //- Construct with size specified.
explicit PtrList(const label); explicit PtrList(const label);
//- Copy constructor. //- Copy constructor.
@ -130,7 +130,7 @@ public:
PtrList(const PtrList<T>&, const CloneArg&); PtrList(const PtrList<T>&, const CloneArg&);
//- Construct by transferring the parameter contents //- Construct by transferring the parameter contents
PtrList(const Xfer<PtrList<T> >&); 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); PtrList(PtrList<T>&, bool reUse);
@ -161,6 +161,17 @@ public:
//- Return true if the PtrList is empty (ie, size() is zero). //- Return true if the PtrList is empty (ie, size() is zero).
inline bool empty() const; 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 // Edit
@ -181,11 +192,11 @@ public:
void clear(); void clear();
//- Transfer the contents of the argument PtrList into this PtrList //- Transfer the contents of the argument PtrList into this PtrList
// and annull the argument list. // and annul the argument list.
void transfer(PtrList<T>&); void transfer(PtrList<T>&);
//- Transfer contents to the Xfer container //- Transfer contents to the Xfer container
inline Xfer<PtrList<T> > xfer(); inline Xfer< PtrList<T> > xfer();
//- Is element set //- Is element set
inline bool set(const label) const; inline bool set(const label) const;

View File

@ -45,6 +45,34 @@ inline bool Foam::PtrList<T>::empty() const
} }
template<class T>
inline T& Foam::PtrList<T>::first()
{
return this->operator[](0);
}
template<class T>
inline const T& Foam::PtrList<T>::first() const
{
return this->operator[](0);
}
template<class T>
inline T& Foam::PtrList<T>::last()
{
return this->operator[](this->size()-1);
}
template<class T>
inline const T& Foam::PtrList<T>::last() const
{
return this->operator[](this->size()-1);
}
template<class T> template<class T>
inline void Foam::PtrList<T>::resize(const label newSize) inline void Foam::PtrList<T>::resize(const label newSize)
{ {
@ -63,9 +91,7 @@ template<class T>
inline Foam::autoPtr<T> Foam::PtrList<T>::set(const label i, T* ptr) inline Foam::autoPtr<T> Foam::PtrList<T>::set(const label i, T* ptr)
{ {
autoPtr<T> old(ptrs_[i]); autoPtr<T> old(ptrs_[i]);
ptrs_[i] = ptr; ptrs_[i] = ptr;
return old; return old;
} }
@ -102,7 +128,7 @@ inline Foam::Xfer<Foam::PtrList<T> > Foam::PtrList<T>::xfer()
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
template<class T> template<class T>
const T& Foam::PtrList<T>::operator[](const label i) const inline const T& Foam::PtrList<T>::operator[](const label i) const
{ {
if (!ptrs_[i]) if (!ptrs_[i])
{ {
@ -116,7 +142,7 @@ const T& Foam::PtrList<T>::operator[](const label i) const
template<class T> template<class T>
T& Foam::PtrList<T>::operator[](const label i) inline T& Foam::PtrList<T>::operator[](const label i)
{ {
if (!ptrs_[i]) if (!ptrs_[i])
{ {
@ -130,7 +156,7 @@ T& Foam::PtrList<T>::operator[](const label i)
template<class T> template<class T>
const T* Foam::PtrList<T>::operator()(const label i) const inline const T* Foam::PtrList<T>::operator()(const label i) const
{ {
return ptrs_[i]; return ptrs_[i];
} }
@ -297,5 +323,4 @@ Foam::PtrList<T>::end()
} }
// ************************************************************************* // // ************************************************************************* //

View File

@ -27,6 +27,7 @@ Class
Description Description
A List with indirect addressing. A List with indirect addressing.
Like IndirectList but does not store addressing. Like IndirectList but does not store addressing.
SourceFiles SourceFiles
@ -73,8 +74,24 @@ public:
// Access // Access
//- Return the number of elements in the list
inline label size() const; inline label size() const;
inline bool empty() const;
//- Return true if the list is empty (ie, size() is zero).
inline bool empty() const;
//- Return the first element of the list.
inline T& first();
//- Return first element of the list.
inline const T& first() const;
//- Return the last element of the list.
inline T& last();
//- Return the last element of the list.
inline const T& last() const;
inline const UList<T>& completeList() const; inline const UList<T>& completeList() const;
inline const List<label>& addressing() const; inline const List<label>& addressing() const;

View File

@ -54,6 +54,34 @@ inline bool Foam::UIndirectList<T>::empty() const
} }
template<class T>
inline T& Foam::UIndirectList<T>::first()
{
return completeList_[addressing_.first()];
}
template<class T>
inline const T& Foam::UIndirectList<T>::first() const
{
return completeList_[addressing_.first()];
}
template<class T>
inline T& Foam::UIndirectList<T>::last()
{
return completeList_[addressing_.last()];
}
template<class T>
inline const T& Foam::UIndirectList<T>::last() const
{
return completeList_[addressing_.last()];
}
template<class T> template<class T>
inline const Foam::UList<T>& Foam::UIndirectList<T>::completeList() const inline const Foam::UList<T>& Foam::UIndirectList<T>::completeList() const
{ {

View File

@ -132,7 +132,7 @@ public:
inline label fcIndex(const label i) const; inline label fcIndex(const label i) const;
//- Return the reverse circular index, i.e. the previous index //- Return the reverse circular index, i.e. the previous index
// which returns to the last at the begining of the list // which returns to the last at the beginning of the list
inline label rcIndex(const label i) const; inline label rcIndex(const label i) const;
//- Return the binary size in number of characters of the UList //- Return the binary size in number of characters of the UList
@ -151,6 +151,18 @@ public:
// This can be used (with caution) when interfacing with C code. // This can be used (with caution) when interfacing with C code.
inline T* data(); inline T* data();
//- Return the first element of the list.
inline T& first();
//- Return first element of the list.
inline const T& first() const;
//- Return the last element of the list.
inline T& last();
//- Return the last element of the list.
inline const T& last() const;
// Check // Check
@ -184,12 +196,6 @@ public:
// an out-of-range element returns false without any ill-effects // an out-of-range element returns false without any ill-effects
inline const T& operator[](const label) const; inline const T& operator[](const label) const;
//- Return last element of UList.
inline T& last();
//- Return last element of UList.
inline const T& last() const;
//- Allow cast to a const List<T>& //- Allow cast to a const List<T>&
inline operator const Foam::List<T>&() const; inline operator const Foam::List<T>&() const;

View File

@ -114,6 +114,20 @@ inline void Foam::UList<T>::checkIndex(const label i) const
} }
template<class T>
inline T& Foam::UList<T>::first()
{
return this->operator[](0);
}
template<class T>
inline const T& Foam::UList<T>::first() const
{
return this->operator[](0);
}
template<class T> template<class T>
inline T& Foam::UList<T>::last() inline T& Foam::UList<T>::last()
{ {

View File

@ -93,8 +93,6 @@ void Foam::UPtrList<T>::clear()
} }
// Transfer the contents of the argument List into this List
// and anull the argument list
template<class T> template<class T>
void Foam::UPtrList<T>::transfer(UPtrList<T>& a) void Foam::UPtrList<T>::transfer(UPtrList<T>& a)
{ {

View File

@ -26,8 +26,8 @@ Class
Foam::UPtrList Foam::UPtrList
Description Description
A 1D array of pointers to objects of type \<T\>, where the size of the A templated 1D list of pointers to objects of type \<T\>, where the
array is known and used for subscript bounds checking, etc. size of the array is known and used for subscript bounds checking, etc.
The element operator [] returns a reference to the object rather than a The element operator [] returns a reference to the object rather than a
pointer. Storage is not allocated during construction or use but is pointer. Storage is not allocated during construction or use but is
@ -107,11 +107,11 @@ public:
//- Null Constructor. //- Null Constructor.
UPtrList(); UPtrList();
//- Construct with length specified. //- Construct with size specified.
explicit UPtrList(const label); explicit UPtrList(const label);
//- Construct by transferring the parameter contents //- Construct by transferring the parameter contents
UPtrList(const Xfer<UPtrList<T> >&); UPtrList(const Xfer< UPtrList<T> >&);
//- Construct as copy or re-use as specified. //- Construct as copy or re-use as specified.
UPtrList(UPtrList<T>&, bool reUse); UPtrList(UPtrList<T>&, bool reUse);
@ -127,6 +127,18 @@ public:
//- Return true if the UPtrList is empty (ie, size() is zero). //- Return true if the UPtrList is empty (ie, size() is zero).
inline bool empty() const; 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 // Edit
@ -144,11 +156,11 @@ public:
void clear(); void clear();
//- Transfer the contents of the argument UPtrList into this //- Transfer the contents of the argument UPtrList into this
// UPtrList and annull the argument list. // UPtrList and annul the argument list.
void transfer(UPtrList<T>&); void transfer(UPtrList<T>&);
//- Transfer contents to the Xfer container //- Transfer contents to the Xfer container
inline Xfer<UPtrList<T> > xfer(); inline Xfer< UPtrList<T> > xfer();
//- Is element set //- Is element set
inline bool set(const label) const; inline bool set(const label) const;

View File

@ -42,6 +42,34 @@ inline bool Foam::UPtrList<T>::empty() const
} }
template<class T>
inline T& Foam::UPtrList<T>::first()
{
return this->operator[](0);
}
template<class T>
inline const T& Foam::UPtrList<T>::first() const
{
return this->operator[](0);
}
template<class T>
inline T& Foam::UPtrList<T>::last()
{
return this->operator[](this->size()-1);
}
template<class T>
inline const T& Foam::UPtrList<T>::last() const
{
return this->operator[](this->size()-1);
}
template<class T> template<class T>
inline void Foam::UPtrList<T>::resize(const label newSize) inline void Foam::UPtrList<T>::resize(const label newSize)
{ {
@ -55,6 +83,7 @@ inline bool Foam::UPtrList<T>::set(const label i) const
return ptrs_[i] != NULL; return ptrs_[i] != NULL;
} }
template<class T> template<class T>
inline T* Foam::UPtrList<T>::set(const label i, T* ptr) inline T* Foam::UPtrList<T>::set(const label i, T* ptr)
{ {
@ -63,6 +92,7 @@ inline T* Foam::UPtrList<T>::set(const label i, T* ptr)
return old; return old;
} }
template<class T> template<class T>
inline Foam::Xfer<Foam::UPtrList<T> > Foam::UPtrList<T>::xfer() inline Foam::Xfer<Foam::UPtrList<T> > Foam::UPtrList<T>::xfer()
{ {
@ -73,7 +103,7 @@ inline Foam::Xfer<Foam::UPtrList<T> > Foam::UPtrList<T>::xfer()
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
template<class T> template<class T>
const T& Foam::UPtrList<T>::operator[](const label i) const inline const T& Foam::UPtrList<T>::operator[](const label i) const
{ {
if (!ptrs_[i]) if (!ptrs_[i])
{ {
@ -87,7 +117,7 @@ const T& Foam::UPtrList<T>::operator[](const label i) const
template<class T> template<class T>
T& Foam::UPtrList<T>::operator[](const label i) inline T& Foam::UPtrList<T>::operator[](const label i)
{ {
if (!ptrs_[i]) if (!ptrs_[i])
{ {
@ -101,7 +131,7 @@ T& Foam::UPtrList<T>::operator[](const label i)
template<class T> template<class T>
const T* Foam::UPtrList<T>::operator()(const label i) const inline const T* Foam::UPtrList<T>::operator()(const label i) const
{ {
return ptrs_[i]; return ptrs_[i];
} }