ENH: PtrList, UPtrList cleanup

- rationalize iterators, reduce code duplication,
  improve assignment behaviour, moveable construct and assignment.
This commit is contained in:
Mark Olesen
2018-01-09 13:15:50 +01:00
parent cc5f30f25e
commit 2f32b586b5
8 changed files with 627 additions and 684 deletions

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -21,10 +21,8 @@ License
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>. along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Application
Description Description
Test behaviour of UPtrList, PtrList
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#include "OSspecific.H" #include "OSspecific.H"
@ -102,13 +100,13 @@ int main(int argc, char *argv[])
forAllIters(llist1, it) forAllIters(llist1, it)
{ {
Info<< typeid(*it).name() << endl; Info<< typeid(*it).name() << nl
Info<< "reversed: " << *it << endl; << "reversed: " << *it << endl;
} }
for (const auto& it : llist1) for (const auto& it : llist1)
{ {
Info<< typeid(it).name() << endl; Info<< typeid(it).name() << nl
Info<< "for-: " << it << endl; << "for-: " << it << endl;
} }
} }
@ -127,9 +125,10 @@ int main(int argc, char *argv[])
listApp.append(new Scalar(1.3*i)); listApp.append(new Scalar(1.3*i));
} }
Info<<"list1: " << list1 << endl; Info<< nl
Info<<"list2: " << list2 << endl; <<"list1: " << list1 << nl
Info<<"listApp: " << listApp << endl; <<"list2: " << list2 << nl
<<"list-appended: " << listApp << endl;
Info<<"indirectly delete some items via set(.., 0) :" << endl; Info<<"indirectly delete some items via set(.., 0) :" << endl;
for (label i = 0; i < 3; i++) for (label i = 0; i < 3; i++)
@ -140,8 +139,8 @@ int main(int argc, char *argv[])
Info<<"transfer list2 -> list1:" << endl; Info<<"transfer list2 -> list1:" << endl;
list1.transfer(list2); list1.transfer(list2);
Info<<"list1: " << list1 << endl; Info<<"list1: " << list1 << nl
Info<<"list2: " << list2 << endl; <<"list2: " << list2 << endl;
Info<<"indirectly delete some items via setSize :" << endl; Info<<"indirectly delete some items via setSize :" << endl;
list1.setSize(4); list1.setSize(4);
@ -151,16 +150,71 @@ int main(int argc, char *argv[])
PtrList<Scalar> list3(list1.xfer()); PtrList<Scalar> list3(list1.xfer());
Info<< "Transferred via the xfer() method" << endl; Info<< "Transferred via the xfer() method" << endl;
Info<<"list1: " << list1 << endl; Info<<"list1: " << list1 << nl
Info<<"list2: " << list2 << endl; <<"list2: " << list2 << nl
Info<<"list3: " << list3 << endl; <<"list3: " << list3 << endl;
Info<<"Move construct:" << endl;
PtrList<Scalar> list4(std::move(list3));
Info<<"list3: " << list3 << nl
<<"list4: " << list4 << endl;
Info<<"Move assign:" << endl;
list3 = std::move(list4);
Info<<"list3: " << list3 << nl
<<"list4: " << list4 << endl;
Info<<"UPtrList from PtrList" << nl;
UPtrList<Scalar> ulist1(list3);
Info<<"ulist1: " << ulist1 << nl;
Info<<"Move construct:" << endl;
UPtrList<Scalar> ulist2(std::move(ulist1));
Info<<"ulist1: " << ulist1 << nl
<<"ulist2: " << ulist2 << nl;
Info<<"Copy assign:" << endl;
ulist1 = ulist2;
Info<<"ulist1: " << ulist1 << nl
<<"ulist2: " << ulist2 << nl;
Info<<"Move assign:" << endl;
ulist1 = std::move(ulist2);
Info<<"ulist1: " << ulist1 << nl
<<"ulist2: " << ulist2 << nl;
// Test iterator random access
{
auto iter1 = ulist1.begin();
auto iter2 = iter1 + 3;
Info<<"begin:" << *iter1 << " (+3):" << *iter2 << nl;
Info<< "diff= " << (iter1 - iter2) << nl;
Info<< "iter[2]=" << iter1[2] << nl;
Info<< "iter1 < iter2 : " << (iter1 < iter2) << nl;
Info<< "iter1 >= iter2 : " << (iter1 >= iter2) << nl;
}
PtrList<plane> planes; PtrList<plane> planes;
planes.append(new plane(vector::one, vector::one)); planes.append(new plane(vector::one, vector::one));
planes.append(new plane(vector(1,2,3), vector::one)); planes.append(new plane(vector(1,2,3), vector::one));
forAll(planes, p) Info<< nl << "appended values" << nl;
Info<< "plane " << planes[p] << endl; for (const plane& p : planes)
{
Info<< " plane " << p << endl;
}
Info<< nl << "Done." << endl; Info<< nl << "Done." << endl;
return 0; return 0;

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -29,40 +29,30 @@ License
// * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
template<class T> template<class T>
Foam::PtrList<T>::PtrList() Foam::PtrList<T>::PtrList(const PtrList<T>& lst)
: :
UPtrList<T>() UPtrList<T>(lst.size())
{}
template<class T>
Foam::PtrList<T>::PtrList(const label len)
:
UPtrList<T>(len)
{}
template<class T>
Foam::PtrList<T>::PtrList(const PtrList<T>& a)
:
UPtrList<T>(a.size())
{ {
forAll(*this, i) const label len = this->size();
for (label i=0; i<len; ++i)
{ {
this->ptrs_[i] = (a[i]).clone().ptr(); this->ptrs_[i] = (lst[i]).clone().ptr();
} }
} }
template<class T> template<class T>
template<class CloneArg> template<class CloneArg>
Foam::PtrList<T>::PtrList(const PtrList<T>& a, const CloneArg& cloneArg) Foam::PtrList<T>::PtrList(const PtrList<T>& lst, const CloneArg& cloneArg)
: :
UPtrList<T>(a.size()) UPtrList<T>(lst.size())
{ {
forAll(*this, i) const label len = this->size();
for (label i=0; i<len; ++i)
{ {
this->ptrs_[i] = (a[i]).clone(cloneArg).ptr(); this->ptrs_[i] = (lst[i]).clone(cloneArg).ptr();
} }
} }
@ -75,34 +65,31 @@ Foam::PtrList<T>::PtrList(const Xfer<PtrList<T>>& lst)
template<class T> template<class T>
Foam::PtrList<T>::PtrList(PtrList<T>& a, bool reuse) Foam::PtrList<T>::PtrList(PtrList<T>& lst, bool reuse)
: :
UPtrList<T>(a, reuse) UPtrList<T>(lst, reuse)
{ {
if (!reuse) if (!reuse)
{ {
forAll(*this, i) const label len = this->size();
for (label i=0; i<len; ++i)
{ {
this->ptrs_[i] = (a[i]).clone().ptr(); this->ptrs_[i] = (lst[i]).clone().ptr();
} }
} }
} }
template<class T> template<class T>
Foam::PtrList<T>::PtrList(const SLPtrList<T>& sll) Foam::PtrList<T>::PtrList(const SLPtrList<T>& lst)
: :
UPtrList<T>(sll.size()) UPtrList<T>(lst.size())
{ {
if (sll.size()) if (lst.size())
{ {
label i = 0; label i = 0;
for for (auto iter = lst.cbegin(); iter != lst.cend(); ++iter)
(
typename SLPtrList<T>::const_iterator iter = sll.cbegin();
iter != sll.cend();
++iter
)
{ {
this->ptrs_[i++] = (*iter).clone().ptr(); this->ptrs_[i++] = (*iter).clone().ptr();
} }
@ -115,7 +102,10 @@ Foam::PtrList<T>::PtrList(const SLPtrList<T>& sll)
template<class T> template<class T>
Foam::PtrList<T>::~PtrList() Foam::PtrList<T>::~PtrList()
{ {
forAll(*this, i) const label len = this->size();
// Free old pointers
for (label i=0; i<len; ++i)
{ {
if (this->ptrs_[i]) if (this->ptrs_[i])
{ {
@ -127,53 +117,12 @@ Foam::PtrList<T>::~PtrList()
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class T>
void Foam::PtrList<T>::setSize(const label newSize)
{
if (newSize < 0)
{
FatalErrorInFunction
<< "bad set size " << newSize
<< " for type " << typeid(T).name()
<< abort(FatalError);
}
label oldSize = this->size();
if (newSize == 0)
{
clear();
}
else if (newSize < oldSize)
{
label i;
for (i=newSize; i<oldSize; i++)
{
if (this->ptrs_[i])
{
delete this->ptrs_[i];
}
}
this->ptrs_.setSize(newSize);
}
else // newSize > oldSize
{
this->ptrs_.setSize(newSize);
label i;
for (i=oldSize; i<newSize; i++)
{
this->ptrs_[i] = nullptr;
}
}
}
template<class T> template<class T>
void Foam::PtrList<T>::clear() void Foam::PtrList<T>::clear()
{ {
forAll(*this, i) const label len = this->size();
for (label i=0; i<len; ++i)
{ {
if (this->ptrs_[i]) if (this->ptrs_[i])
{ {
@ -181,109 +130,82 @@ void Foam::PtrList<T>::clear()
} }
} }
this->ptrs_.clear(); UPtrList<T>::clear();
} }
template<class T> template<class T>
void Foam::PtrList<T>::transfer(PtrList<T>& a) void Foam::PtrList<T>::setSize(const label newLen)
{ {
clear(); if (newLen <= 0)
this->ptrs_.transfer(a.ptrs_);
}
template<class T>
void Foam::PtrList<T>::reorder(const labelUList& oldToNew)
{
if (oldToNew.size() != this->size())
{ {
FatalErrorInFunction clear();
<< "Size of map (" << oldToNew.size() return;
<< ") not equal to list size (" << this->size()
<< ") for type " << typeid(T).name()
<< abort(FatalError);
} }
List<T*> newPtrs_(this->ptrs_.size(), reinterpret_cast<T*>(0)); const label oldLen = this->size();
if (newLen < oldLen)
forAll(*this, i)
{ {
const label newI = oldToNew[i]; // Truncate - free old pointers
for (label i=newLen; i<oldLen; ++i)
if (newI < 0 || newI >= this->size())
{ {
FatalErrorInFunction if (this->ptrs_[i])
<< "Illegal index " << newI << nl {
<< "Valid indices are 0.." << this->size()-1 delete this->ptrs_[i];
<< " for type " << typeid(T).name() }
<< abort(FatalError);
} }
if (newPtrs_[newI]) this->ptrs_.setSize(newLen);
{
FatalErrorInFunction
<< "reorder map is not unique; element " << newI
<< " already set for type " << typeid(T).name()
<< abort(FatalError);
}
newPtrs_[newI] = this->ptrs_[i];
} }
else if (newLen > oldLen)
forAll(newPtrs_, i)
{ {
if (!newPtrs_[i]) // Extend - new elements initialized to nullptr
{ this->ptrs_.setSize(newLen, reinterpret_cast<T*>(0));
FatalErrorInFunction
<< "Element " << i << " not set after reordering with type "
<< typeid(T).name() << nl << abort(FatalError);
}
} }
this->ptrs_.transfer(newPtrs_);
} }
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
template<class T> template<class T>
void Foam::PtrList<T>::operator=(const PtrList<T>& a) void Foam::PtrList<T>::operator=(const PtrList<T>& lst)
{ {
if (this == &a) if (this == &lst)
{ {
FatalErrorInFunction FatalErrorInFunction
<< "attempted assignment to self for type " << typeid(T).name() << "attempted assignment to self for type " << typeid(T).name()
<< abort(FatalError); << abort(FatalError);
} }
if (this->size() == 0) const label oldLen = this->size();
{ const label newLen = lst.size();
setSize(a.size());
forAll(*this, i) // Truncate (frees old pointers) or extend the length
{ setSize(newLen);
this->ptrs_[i] = (a[i]).clone().ptr();
} if (newLen < oldLen)
}
else if (a.size() == this->size())
{ {
forAll(*this, i) // Copy values for existing entries
for (label i=0; i<newLen; ++i)
{ {
(*this)[i] = a[i]; (*this)[i] = lst[i];
} }
} }
else else
{ {
FatalErrorInFunction // Copy values for existing entries
<< "bad size: " << a.size() for (label i=0; i<oldLen; ++i)
<< " for type " << typeid(T).name() {
<< abort(FatalError); (*this)[i] = lst[i];
}
// Clone pointers for new entries
for (label i=oldLen; i<newLen; ++i)
{
this->ptrs_[i] = (lst[i]).clone().ptr();
}
} }
} }
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#include "PtrListIO.C"
// ************************************************************************* // // ************************************************************************* //

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -25,11 +25,12 @@ Class
Foam::PtrList Foam::PtrList
Description Description
A templated 1D list of pointers to objects of type \<T\>, where the A list of pointers to objects of type \<T\>, with allocation/deallocation
size of the array is known and used for subscript bounds checking, etc. management of the pointers.
The operator[] returns a reference to the object, not the pointer.
The element operator [] returns a reference to the object rather than a See Also
pointer. Foam::PtrList
SourceFiles SourceFiles
PtrListI.H PtrListI.H
@ -81,27 +82,30 @@ public:
// Constructors // Constructors
//- Null Constructor //- Construct null
PtrList(); PtrList() = default;
//- Construct with size specified //- Construct with specified size, each element initialized to nullptr
explicit PtrList(const label len); explicit inline PtrList(const label nElem);
//- Copy constructor, using 'clone()' method on each element //- Copy construct using 'clone()' method on each element
PtrList(const PtrList<T>& a); PtrList(const PtrList<T>& lst);
//- Copy constructor with additional argument for clone //- Move construct
inline PtrList(PtrList<T>&& lst);
//- Copy construct with additional argument for 'clone()'
template<class CloneArg> template<class CloneArg>
PtrList(const PtrList<T>& a, const CloneArg& cloneArg); PtrList(const PtrList<T>& lst, const CloneArg& cloneArg);
//- Construct by transferring the parameter contents //- Construct by transferring the parameter contents
PtrList(const Xfer<PtrList<T>>& lst); PtrList(const Xfer<PtrList<T>>& lst);
//- Construct as copy or re-use as specified //- Construct as copy or re-use as specified
PtrList(PtrList<T>& a, bool reuse); PtrList(PtrList<T>& lst, bool reuse);
//- Construct as copy of SLPtrList<T> //- Construct as copy of SLPtrList<T>
explicit PtrList(const SLPtrList<T>& sll); explicit PtrList(const SLPtrList<T>& lst);
//- Construct from Istream using given Istream constructor class //- Construct from Istream using given Istream constructor class
template<class INew> template<class INew>
@ -115,62 +119,62 @@ public:
~PtrList(); ~PtrList();
// Member functions // Member Functions
// Edit //- Clear the PtrList. Set size to zero and delete allocated entries
void clear();
//- Reset size of PtrList. If extending the PtrList, new entries are //- Reset size of PtrList.
// set to nullptr. If truncating the PtrList, removed entries are // New entries are initialized to nullptr, removed entries are deleted
// deleted void setSize(const label newLen);
void setSize(const label);
//- Alias for setSize(const label) //- Reset size of PtrList.
inline void resize(const label); // New entries are initialized to nullptr, removed entries are deleted
inline void resize(const label newLen);
//- Clear the PtrList, i.e. set size to zero deleting all the //- Append an element at the end of the list
// allocated entries using UPtrList<T>::append;
void clear();
//- Append an element at the end of the list //- Append an element at the end of the list
inline void append(T*); inline void append(const autoPtr<T>& aptr);
inline void append(const autoPtr<T>&);
inline void append(const tmp<T>&);
//- Transfer the contents of the argument PtrList into this PtrList //- Append an element at the end of the list
// and annul the argument list inline void append(const tmp<T>& tptr);
void transfer(PtrList<T>&);
//- Transfer contents to the Xfer container //- Transfer into this list and annul the argument list
inline Xfer<PtrList<T>> xfer(); inline void transfer(PtrList<T>& lst);
//- Is element set //- Transfer contents to the Xfer container
inline bool set(const label) const; inline Xfer<PtrList<T>> xfer();
//- Set element to given T* and return old element (can be nullptr) //- Return true if element is set (ie, not a nullptr)
inline autoPtr<T> set(const label, T*); inline bool set(const label i) const;
//- Set element to given autoPtr<T> and return old element //- Set element to given pointer and return old element (can be null)
inline autoPtr<T> set(const label, const autoPtr<T>&); inline autoPtr<T> set(const label i, T* ptr);
//- Set element to given tmp<T> and return old element //- Set element to given autoPtr and return old element
inline autoPtr<T> set(const label, const tmp<T>&); inline autoPtr<T> set(const label i, const autoPtr<T>& aptr);
//- Reorders elements. Ordering does not have to be done in //- Set element to given tmp and return old element
// ascending or descending order. Reordering has to be unique. inline autoPtr<T> set(const label i, const tmp<T>& tptr);
// (is shuffle)
void reorder(const labelUList&);
// Member operators // Member operators
//- Assignment //- Copy assignment.
void operator=(const PtrList<T>&); // For existing list entries, values are copied from the list.
// For new list entries, pointers are cloned from the list.
void operator=(const PtrList<T>& lst);
//- Move assignment
inline void operator=(PtrList<T>&& lst);
// IOstream operator // IOstream operator
//- Read PtrList from Istream, discarding contents of existing PtrList //- Read PtrList from Istream, discarding contents of existing PtrList
friend Istream& operator>> <T>(Istream&, PtrList<T>&); friend Istream& operator>> <T>(Istream& is, PtrList<T>& lst);
}; };
@ -186,6 +190,7 @@ public:
#ifdef NoRepository #ifdef NoRepository
#include "PtrList.C" #include "PtrList.C"
#include "PtrListIO.C"
#endif #endif
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -26,38 +26,42 @@ License
#include "autoPtr.H" #include "autoPtr.H"
#include "tmp.H" #include "tmp.H"
// * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
template<class T>
inline Foam::PtrList<T>::PtrList(const label nElem)
:
UPtrList<T>(nElem)
{}
template<class T>
inline Foam::PtrList<T>::PtrList(PtrList<T>&& lst)
:
UPtrList<T>(std::move(lst))
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class T> template<class T>
inline void Foam::PtrList<T>::resize(const label newSize) inline void Foam::PtrList<T>::resize(const label newLen)
{ {
this->setSize(newSize); this->setSize(newLen);
}
template<class T>
inline void Foam::PtrList<T>::append(T* ptr)
{
const label idx = this->size();
this->setSize(idx + 1);
this->ptrs_[idx] = ptr;
} }
template<class T> template<class T>
inline void Foam::PtrList<T>::append(const autoPtr<T>& aptr) inline void Foam::PtrList<T>::append(const autoPtr<T>& aptr)
{ {
return append(const_cast<autoPtr<T>&>(aptr).ptr()); return UPtrList<T>::append(const_cast<autoPtr<T>&>(aptr).ptr());
} }
template<class T> template<class T>
inline void Foam::PtrList<T>::append inline void Foam::PtrList<T>::append(const tmp<T>& tptr)
(
const tmp<T>& t
)
{ {
return append(const_cast<tmp<T>&>(t).ptr()); return UPtrList<T>::append(const_cast<tmp<T>&>(tptr).ptr());
} }
@ -92,10 +96,18 @@ template<class T>
inline Foam::autoPtr<T> Foam::PtrList<T>::set inline Foam::autoPtr<T> Foam::PtrList<T>::set
( (
const label i, const label i,
const tmp<T>& t const tmp<T>& tptr
) )
{ {
return set(i, const_cast<tmp<T>&>(t).ptr()); return set(i, const_cast<tmp<T>&>(tptr).ptr());
}
template<class T>
inline void Foam::PtrList<T>::transfer(PtrList<T>& lst)
{
this->clear();
UPtrList<T>::swap(lst);
} }
@ -106,4 +118,14 @@ inline Foam::Xfer<Foam::PtrList<T>> Foam::PtrList<T>::xfer()
} }
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
template<class T>
inline void Foam::PtrList<T>::operator=(PtrList<T>&& lst)
{
this->clear();
this->swap(lst);
}
// ************************************************************************* // // ************************************************************************* //

View File

@ -139,16 +139,11 @@ void Foam::PtrList<T>::read(Istream& is, const INew& inew)
setSize(sllPtrs.size()); setSize(sllPtrs.size());
// Pointers - can simply copy // A list of pointers - can simply copy
label i = 0; label i = 0;
for for (T* ptr : sllPtrs)
(
typename SLList<T*>::const_iterator iter = sllPtrs.cbegin();
iter != sllPtrs.cend();
++iter
)
{ {
set(i++, *iter); set(i++, ptr);
} }
return; return;

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd. \\/ M anipulation | Copyright (C) 2015-2018 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -27,137 +27,66 @@ License
// * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
template<class T>
Foam::UPtrList<T>::UPtrList()
:
ptrs_()
{}
template<class T>
Foam::UPtrList<T>::UPtrList(const label s)
:
ptrs_(s, reinterpret_cast<T*>(0))
{}
template<class T> template<class T>
Foam::UPtrList<T>::UPtrList(UList<T>& lst) Foam::UPtrList<T>::UPtrList(UList<T>& lst)
: :
ptrs_(lst.size()) ptrs_(lst.size())
{ {
forAll(lst, i) const label len = lst.size();
for (label i=0; i<len; ++i)
{ {
ptrs_[i] = &lst[i]; ptrs_[i] = &(lst[i]);
} }
} }
template<class T>
Foam::UPtrList<T>::UPtrList(PtrList<T>& lst)
:
ptrs_(lst.size())
{
forAll(lst, i)
{
ptrs_[i] = &lst[i];
}
}
template<class T>
Foam::UPtrList<T>::UPtrList(const Xfer<UPtrList<T>>& lst)
{
transfer(lst());
}
template<class T>
Foam::UPtrList<T>::UPtrList(UPtrList<T>& a, bool reuse)
:
ptrs_(a.ptrs_, reuse)
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class T>
void Foam::UPtrList<T>::setSize(const label newSize)
{
label oldSize = size();
if (newSize <= 0)
{
clear();
}
else if (newSize < oldSize)
{
ptrs_.setSize(newSize);
}
else if (newSize > oldSize)
{
ptrs_.setSize(newSize);
// set new elements to nullptr
for (label i=oldSize; i<newSize; i++)
{
ptrs_[i] = nullptr;
}
}
}
template<class T>
void Foam::UPtrList<T>::clear()
{
ptrs_.clear();
}
template<class T>
void Foam::UPtrList<T>::transfer(UPtrList<T>& a)
{
ptrs_.transfer(a.ptrs_);
}
template<class T> template<class T>
void Foam::UPtrList<T>::reorder(const labelUList& oldToNew) void Foam::UPtrList<T>::reorder(const labelUList& oldToNew)
{ {
if (oldToNew.size() != size()) const label len = this->size();
if (oldToNew.size() != len)
{ {
FatalErrorInFunction FatalErrorInFunction
<< "Size of map (" << oldToNew.size() << "Size of map (" << oldToNew.size()
<< ") not equal to list size (" << size() << ") not equal to list size (" << len
<< ")." << abort(FatalError); << ") for type " << typeid(T).name() << nl
<< abort(FatalError);
} }
List<T*> newPtrs_(ptrs_.size(), reinterpret_cast<T*>(0)); // New list of pointers
List<T*> ptrLst(len, reinterpret_cast<T*>(0));
forAll(*this, i) for (label i=0; i<len; ++i)
{ {
label newI = oldToNew[i]; const label idx = oldToNew[i];
if (newI < 0 || newI >= size()) if (idx < 0 || idx >= len)
{ {
FatalErrorInFunction FatalErrorInFunction
<< "Illegal index " << newI << nl << "Illegal index " << idx << nl
<< "Valid indices are 0.." << size()-1 << "Valid indices are 0.." << len-1
<< " for type " << typeid(T).name() << nl
<< abort(FatalError); << abort(FatalError);
} }
if (newPtrs_[newI]) if (ptrLst[idx])
{ {
FatalErrorInFunction FatalErrorInFunction
<< "reorder map is not unique; element " << newI << "reorder map is not unique; element " << idx
<< " already set." << abort(FatalError); << " already set for type " << typeid(T).name()
<< abort(FatalError);
} }
newPtrs_[newI] = ptrs_[i]; ptrLst[idx] = ptrs_[i];
} }
forAll(newPtrs_, i) // Verify that all pointers were set
for (label i=0; i<len; ++i)
{ {
if (!newPtrs_[i]) if (!ptrLst[i])
{ {
FatalErrorInFunction FatalErrorInFunction
<< "Element " << i << " not set after reordering." << nl << "Element " << i << " not set after reordering." << nl
@ -165,12 +94,8 @@ void Foam::UPtrList<T>::reorder(const labelUList& oldToNew)
} }
} }
ptrs_.transfer(newPtrs_); ptrs_.swap(ptrLst);
} }
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#include "UPtrListIO.C"
// ************************************************************************* // // ************************************************************************* //

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -25,14 +25,15 @@ Class
Foam::UPtrList Foam::UPtrList
Description Description
A templated 1D list of pointers to objects of type \<T\>, where the A list of pointers to objects of type \<T\>, without allocation/deallocation
size of the array is known and used for subscript bounds checking, etc. management of the pointers - this is to be done elsewhere.
The operator[] returns a reference to the object, not the pointer.
The element operator [] returns a reference to the object rather than a See Also
pointer. Storage is not allocated during construction or use but is Foam::PtrList
supplied to the constructor as an argument.
SourceFiles SourceFiles
UPtrListI.H
UPtrList.C UPtrList.C
UPtrListIO.C UPtrListIO.C
@ -43,337 +44,271 @@ SourceFiles
#include "List.H" #include "List.H"
#include "PtrList.H" #include "PtrList.H"
#include <iterator>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam namespace Foam
{ {
// Forward declaration of friend classes // Forward declarations
template<class T> class PtrList;
// Forward declaration of friend functions and operators template<class T> class PtrList;
template<class T> class UPtrList; template<class T> class UPtrList;
template<class T> template<class T> Ostream& operator<<(Ostream& os, const UPtrList<T>& lst);
inline typename UPtrList<T>::iterator operator+
(
const typename UPtrList<T>::iterator&,
label
);
template<class T>
inline typename UPtrList<T>::iterator operator+
(
label,
const typename UPtrList<T>::iterator&
);
template<class T>
inline typename UPtrList<T>::iterator operator-
(
const typename UPtrList<T>::iterator&,
label
);
template<class T>
inline label operator-
(
const typename UPtrList<T>::iterator&,
const typename UPtrList<T>::iterator&
);
template<class T>
inline typename UPtrList<T>::const_iterator operator+
(
const typename UPtrList<T>::const_iterator&,
label
);
template<class T>
inline typename UPtrList<T>::const_iterator operator+
(
label,
const typename UPtrList<T>::const_iterator&
);
template<class T>
inline typename UPtrList<T>::const_iterator operator-
(
const typename UPtrList<T>::const_iterator&,
label
);
template<class T>
inline label operator-
(
const typename UPtrList<T>::const_iterator&,
const typename UPtrList<T>::const_iterator&
);
template<class T>
Istream& operator>>(Istream&, UPtrList<T>&);
template<class T>
Ostream& operator<<(Ostream&, const UPtrList<T>&);
/*---------------------------------------------------------------------------*\ /*---------------------------------------------------------------------------*\
Class UPtrList Declaration Class UPtrList Declaration
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
template<class T> template<class T>
class UPtrList class UPtrList
{ {
// Private data protected:
// Protected data
List<T*> ptrs_; List<T*> ptrs_;
public: public:
// Related types // STL type definitions
//- Declare friendship with the UPtrList class //- Type of values the list contains
friend class PtrList<T>; typedef T value_type;
//- A non-const reference to the value_type
typedef T& reference;
//- A const reference to the value_type
typedef const T& const_reference;
//- Random-access iterator with non-const access
class iterator;
//- Random-access iterator with const access
class const_iterator;
// Constructors // Constructors
//- Null Constructor //- Construct null
UPtrList(); UPtrList() = default;
//- Construct with size specified //- Construct with specified size, each element initialized to nullptr
explicit UPtrList(const label); explicit inline UPtrList(const label nElem);
//- Construct from UList //- Construct from PtrList, copying addresses of each list element.
explicit UPtrList(UList<T>&); // The argument is non-const access since this UPtrList can be used
// to change its values.
explicit inline UPtrList(PtrList<T>& lst);
//- Construct from PtrList //- Construct from UList, taking addresses of each list element
explicit UPtrList(PtrList<T>&); explicit UPtrList(UList<T>& lst);
//- Construct by transferring the parameter contents //- Construct by transferring the parameter contents
UPtrList(const Xfer<UPtrList<T>>&); inline UPtrList(const Xfer<UPtrList<T>>& lst);
//- Construct as copy or re-use as specified //- Construct as copy or re-use as specified
UPtrList(UPtrList<T>&, bool reuse); inline UPtrList(UPtrList<T>& lst, bool reuse);
//- Copy construct
inline UPtrList(const UPtrList<T>& lst) = default;
//- Move construct
inline UPtrList(UPtrList<T>&& lst);
// Member functions // Member functions
// Access // Access
//- Return the number of elements in the UPtrList //- Return the number of elements in the UPtrList
inline label size() const; inline label size() const;
//- 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;
//- Swap content with another UPtrList //- Return reference to the first element of the list
inline void swap(UPtrList<T>& lst); inline T& first();
//- Return reference to the first element of the list //- Return reference to first element of the list
inline T& first(); inline const T& first() const;
//- Return reference to first element of the list //- Return reference to the last element of the list
inline const T& first() const; inline T& last();
//- Return reference to the last element of the list //- Return reference to the last element of the list
inline T& last(); inline const T& last() const;
//- Return reference to the last element of the list
inline const T& last() const;
// Edit // Edit
//- Reset size of UPtrList. This can only be used to set the size //- Set list size to zero.
// of an empty UPtrList, extend a UPtrList, remove entries from inline void clear();
// the end of a UPtrList
void setSize(const label);
//- Reset size of UPtrList. This can only be used to set the size //- Reset size of UPtrList.
// of an empty UPtrList, extend a UPtrList, remove entries from // New entries are initialized to nullptr.
// the end of a UPtrList inline void setSize(const label newLen);
inline void resize(const label);
//- Clear the UPtrList, i.e. set size to zero //- Reset size of UPtrList.
void clear(); // New entries are initialized to nullptr.
inline void resize(const label newLen);
//- Transfer the contents of the argument UPtrList into this //- Append an element to the end of the list
// UPtrList and annul the argument list inline void append(T* ptr);
void transfer(UPtrList<T>&);
//- Transfer contents to the Xfer container //- Swap content
inline Xfer<UPtrList<T>> xfer(); inline void swap(UPtrList<T>& lst);
//- Is element set //- Transfer contents into this list and annul the argument
inline bool set(const label) const; inline void transfer(UPtrList<T>& lst);
//- Set element. Return old element (can be nullptr). //- Transfer contents to the Xfer container
// No checks on new element inline Xfer<UPtrList<T>> xfer();
inline T* set(const label, T*);
//- Reorders elements. Ordering does not have to be done in //- Return true if element is set (ie, not a nullptr)
// ascending or descending order. Reordering has to be unique. inline bool set(const label i) const;
// (is shuffle)
void reorder(const labelUList&); //- Set element to given pointer and return old element (can be null).
// No checks on new element
inline T* set(const label i, T* ptr);
//- Reorder elements. Reordering must be unique (ie, shuffle).
void reorder(const labelUList& oldToNew);
// Member operators // Member operators
//- Return element const reference //- Return element const reference
inline const T& operator[](const label) const; inline const T& operator[](const label i) const;
//- Return element reference //- Return element reference
inline T& operator[](const label); inline T& operator[](const label i);
//- Return element const pointer //- Return element const pointer
inline const T* operator()(const label) const; inline const T* operator()(const label i) const;
//- Copy assignment
UPtrList<T>& operator=(const UPtrList<T>& lst) = default;
//- Move assignment
inline void operator=(UPtrList<T>&& lst);
// STL type definitions // Iterators
//- Type of values the UPtrList contains //- Random-access iterator with non-const access
typedef T value_type;
//- Type that can be used for storing into UPtrList::value_type objects
typedef T& reference;
//- Type that can be used for storing into constant UPtrList::value_type
// objects
typedef const T& const_reference;
// STL iterator
// Random access iterator for traversing UPtrList
class iterator;
friend class iterator;
//- An STL iterator
class iterator class iterator
{ {
T** ptr_; T** ptr_;
public: public:
//- Construct for a given UPtrList entry using iterator_category = std::random_access_iterator_tag;
inline iterator(T**); using value_type = T;
using difference_type = label;
using pointer = T*;
using reference = T&;
friend class const_iterator;
//- Construct for a given entry
inline iterator(T** ptr);
// Member operators // Member operators
inline bool operator==(const iterator&) const; inline bool operator==(const iterator& iter) const;
inline bool operator!=(const iterator&) const; inline bool operator!=(const iterator& iter) const;
inline T& operator*(); inline reference operator*() const;
inline T& operator()(); inline reference operator()() const;
inline iterator operator++(); // Forward iteration
inline iterator operator++(int); inline iterator& operator++();
inline iterator operator++(int);
inline iterator operator--(); inline iterator& operator--();
inline iterator operator--(int); inline iterator operator--(int);
inline iterator operator+=(label); // Random-access
inline iterator& operator+=(difference_type n);
inline iterator& operator-=(difference_type n);
inline iterator operator+(difference_type n) const;
inline iterator operator-(difference_type n) const;
friend iterator operator+ <T>(const iterator&, label); inline difference_type operator-(const iterator& iter) const;
friend iterator operator+ <T>(label, const iterator&);
inline iterator operator-=(label); inline reference operator[](difference_type n) const;
friend iterator operator- <T>(const iterator&, label); inline bool operator<(const iterator& iter) const;
inline bool operator>(const iterator& iter) const;
friend label operator- <T> inline bool operator<=(const iterator& iter) const;
( inline bool operator>=(const iterator& iter) const;
const iterator&,
const iterator&
);
inline T& operator[](label);
inline bool operator<(const iterator&) const;
inline bool operator>(const iterator&) const;
inline bool operator<=(const iterator&) const;
inline bool operator>=(const iterator&) const;
}; };
//- Return an iterator to begin traversing the UPtrList
inline iterator begin();
//- Return an iterator to end traversing the UPtrList //- Random-access iterator with const access
inline iterator end();
// STL const_iterator
// Random access iterator for traversing UPtrList
//- An STL-conforming const_iterator
class const_iterator class const_iterator
{ {
const T* const* ptr_; const T* const* ptr_;
public: public:
//- Construct for a given UPtrList entry using iterator_category = std::random_access_iterator_tag;
inline const_iterator(const T* const*); using value_type = const T;
using difference_type = label;
using pointer = const T*;
using reference = const T&;
//- Construct from an iterator //- Construct for a given entry
inline const_iterator(const iterator&); inline const_iterator(const T* const* ptr);
//- Copy construct from non-const iterator
inline const_iterator(const iterator& iter);
// Member operators // Member operators
inline bool operator==(const const_iterator&) const; inline bool operator==(const const_iterator& iter) const;
inline bool operator!=(const const_iterator&) const; inline bool operator!=(const const_iterator& iter) const;
typedef const T& Tref; inline reference operator*() const;
inline Tref operator*(); inline reference operator()() const;
inline Tref operator()();
inline const_iterator operator++(); // Forward iteration
inline const_iterator operator++(int); inline const_iterator& operator++();
inline const_iterator operator++(int);
inline const_iterator operator--(); inline const_iterator& operator--();
inline const_iterator operator--(int); inline const_iterator operator--(int);
inline const_iterator operator+=(label); // Random-access
inline const_iterator& operator+=(difference_type n);
inline const_iterator& operator-=(difference_type n);
inline const_iterator operator+(difference_type n) const;
inline const_iterator operator-(difference_type n) const;
friend const_iterator operator+ <T> inline difference_type operator-(const const_iterator& iter) const;
(
const const_iterator&,
label
);
friend const_iterator operator+ <T>
(
label,
const const_iterator&
);
inline const_iterator operator-=(label); inline reference operator[](difference_type n) const;
friend const_iterator operator- <T> inline bool operator<(const const_iterator& iter) const;
( inline bool operator>(const const_iterator& iter) const;
const const_iterator&,
label
);
friend label operator- <T> inline bool operator<=(const const_iterator& iter) const;
( inline bool operator>=(const const_iterator& iter) const;
const const_iterator&,
const const_iterator&
);
inline const T& operator[](label);
inline bool operator<(const const_iterator&) const;
inline bool operator>(const const_iterator&) const;
inline bool operator<=(const const_iterator&) const;
inline bool operator>=(const const_iterator&) const;
}; };
//- Return an iterator to begin traversing the UPtrList
inline iterator begin();
//- Return an iterator to end traversing the UPtrList
inline iterator end();
//- Return an const_iterator to begin traversing the UPtrList //- Return an const_iterator to begin traversing the UPtrList
inline const_iterator cbegin() const; inline const_iterator cbegin() const;
@ -390,7 +325,7 @@ public:
// IOstream operator // IOstream operator
//- Write UPtrList to Ostream //- Write UPtrList to Ostream
friend Ostream& operator<< <T>(Ostream&, const UPtrList<T>&); friend Ostream& operator<< <T>(Ostream& os, const UPtrList<T>& lst);
}; };
@ -406,6 +341,7 @@ public:
#ifdef NoRepository #ifdef NoRepository
#include "UPtrList.C" #include "UPtrList.C"
#include "UPtrListIO.C"
#endif #endif
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -23,6 +23,43 @@ License
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
// * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
template<class T>
inline Foam::UPtrList<T>::UPtrList(const label nElem)
:
ptrs_(nElem, reinterpret_cast<T*>(0))
{}
template<class T>
inline Foam::UPtrList<T>::UPtrList(PtrList<T>& lst)
:
ptrs_(lst.ptrs_)
{}
template<class T>
inline Foam::UPtrList<T>::UPtrList(UPtrList<T>&& lst)
:
ptrs_(std::move(lst.ptrs_))
{}
template<class T>
inline Foam::UPtrList<T>::UPtrList(const Xfer<UPtrList<T>>& lst)
{
transfer(lst());
}
template<class T>
inline Foam::UPtrList<T>::UPtrList(UPtrList<T>& lst, bool reuse)
:
ptrs_(lst.ptrs_, reuse)
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class T> template<class T>
@ -39,6 +76,13 @@ inline bool Foam::UPtrList<T>::empty() const
} }
template<class T>
inline void Foam::UPtrList<T>::clear()
{
ptrs_.clear();
}
template<class T> template<class T>
inline void Foam::UPtrList<T>::swap(UPtrList<T>& lst) inline void Foam::UPtrList<T>::swap(UPtrList<T>& lst)
{ {
@ -46,6 +90,13 @@ inline void Foam::UPtrList<T>::swap(UPtrList<T>& lst)
} }
template<class T>
inline void Foam::UPtrList<T>::transfer(UPtrList<T>& lst)
{
ptrs_.transfer(lst.ptrs_);
}
template<class T> template<class T>
inline T& Foam::UPtrList<T>::first() inline T& Foam::UPtrList<T>::first()
{ {
@ -75,9 +126,36 @@ inline const T& Foam::UPtrList<T>::last() const
template<class T> template<class T>
inline void Foam::UPtrList<T>::resize(const label newSize) inline void Foam::UPtrList<T>::setSize(const label newLen)
{ {
this->setSize(newSize); if (newLen <= 0)
{
clear();
return;
}
const label oldLen = this->size();
if (newLen != oldLen)
{
// Truncate or extend (new elements initialized to nullptr)
ptrs_.setSize(newLen, reinterpret_cast<T*>(0));
}
}
template<class T>
inline void Foam::UPtrList<T>::resize(const label nElem)
{
this->setSize(nElem);
}
template<class T>
inline void Foam::UPtrList<T>::append(T* ptr)
{
const label idx = this->size();
ptrs_.setSize(idx + 1);
ptrs_[idx] = ptr;
} }
@ -153,158 +231,149 @@ inline Foam::UPtrList<T>::iterator::iterator(T** ptr)
ptr_(ptr) ptr_(ptr)
{} {}
template<class T> template<class T>
inline bool Foam::UPtrList<T>::iterator::operator==(const iterator& iter) const inline bool Foam::UPtrList<T>::iterator::operator==(const iterator& iter) const
{ {
return ptr_ == iter.ptr_; return ptr_ == iter.ptr_;
} }
template<class T> template<class T>
inline bool Foam::UPtrList<T>::iterator::operator!=(const iterator& iter) const inline bool Foam::UPtrList<T>::iterator::operator!=(const iterator& iter) const
{ {
return ptr_ != iter.ptr_; return ptr_ != iter.ptr_;
} }
template<class T> template<class T>
inline T& Foam::UPtrList<T>::iterator::operator*() inline T& Foam::UPtrList<T>::iterator::operator*() const
{ {
return **ptr_; return **ptr_;
} }
template<class T>
inline T& Foam::UPtrList<T>::iterator::operator()()
{
return operator*();
}
template<class T> template<class T>
inline typename Foam::UPtrList<T>::iterator inline T& Foam::UPtrList<T>::iterator::operator()() const
{
return **ptr_;
}
template<class T>
inline typename Foam::UPtrList<T>::iterator&
Foam::UPtrList<T>::iterator::operator++() Foam::UPtrList<T>::iterator::operator++()
{ {
++ptr_; ++ptr_;
return *this; return *this;
} }
template<class T> template<class T>
inline typename Foam::UPtrList<T>::iterator inline typename Foam::UPtrList<T>::iterator
Foam::UPtrList<T>::iterator::operator++(int) Foam::UPtrList<T>::iterator::operator++(int)
{ {
iterator tmp = *this; iterator iter(*this);
++ptr_; ++ptr_;
return tmp; return iter;
} }
template<class T> template<class T>
inline typename Foam::UPtrList<T>::iterator inline typename Foam::UPtrList<T>::iterator&
Foam::UPtrList<T>::iterator::operator--() Foam::UPtrList<T>::iterator::operator--()
{ {
--ptr_; --ptr_;
return *this; return *this;
} }
template<class T> template<class T>
inline typename Foam::UPtrList<T>::iterator inline typename Foam::UPtrList<T>::iterator
Foam::UPtrList<T>::iterator::operator--(int) Foam::UPtrList<T>::iterator::operator--(int)
{ {
iterator tmp = *this; iterator iter(*this);
--ptr_; --ptr_;
return tmp; return iter;
} }
template<class T> template<class T>
inline typename Foam::UPtrList<T>::iterator inline typename Foam::UPtrList<T>::iterator&
Foam::UPtrList<T>::iterator::operator+=(label n) Foam::UPtrList<T>::iterator::operator+=(label n)
{ {
ptr_ += n; ptr_ += n;
return *this; return *this;
} }
template<class T>
inline typename Foam::UPtrList<T>::iterator
Foam::operator+(const typename UPtrList<T>::iterator& iter, label n)
{
typename UPtrList<T>::iterator tmp = iter;
return tmp += n;
}
template<class T> template<class T>
inline typename Foam::UPtrList<T>::iterator inline typename Foam::UPtrList<T>::iterator&
Foam::operator+(label n, const typename UPtrList<T>::iterator& iter)
{
typename UPtrList<T>::iterator tmp = iter;
return tmp += n;
}
template<class T>
inline typename Foam::UPtrList<T>::iterator
Foam::UPtrList<T>::iterator::operator-=(label n) Foam::UPtrList<T>::iterator::operator-=(label n)
{ {
ptr_ -= n; ptr_ -= n;
return *this; return *this;
} }
template<class T> template<class T>
inline typename Foam::UPtrList<T>::iterator inline typename Foam::UPtrList<T>::iterator
Foam::operator-(const typename UPtrList<T>::iterator& iter, label n) Foam::UPtrList<T>::iterator::operator+(label n) const
{ {
typename UPtrList<T>::iterator tmp = iter; return iterator(ptr_ + n);
return tmp -= n;
} }
template<class T>
inline Foam::label Foam::operator-
(
const typename UPtrList<T>::iterator& iter1,
const typename UPtrList<T>::iterator& iter2
)
{
return (iter1.ptr_ - iter2.ptr_)/sizeof(T*);
}
template<class T> template<class T>
inline T& Foam::UPtrList<T>::iterator::operator[](label n) inline typename Foam::UPtrList<T>::iterator
Foam::UPtrList<T>::iterator::operator-(label n) const
{
return iterator(ptr_ - n);
}
template<class T>
inline Foam::label
Foam::UPtrList<T>::iterator::operator-(const iterator& iter) const
{
return (ptr_ - iter.ptr_);
}
template<class T>
inline T& Foam::UPtrList<T>::iterator::operator[](label n) const
{ {
return *(*this + n); return *(*this + n);
} }
template<class T> template<class T>
inline bool Foam::UPtrList<T>::iterator::operator<(const iterator& iter) const inline bool Foam::UPtrList<T>::iterator::operator<(const iterator& iter) const
{ {
return ptr_ < iter.ptr_; return ptr_ < iter.ptr_;
} }
template<class T> template<class T>
inline bool Foam::UPtrList<T>::iterator::operator>(const iterator& iter) const inline bool Foam::UPtrList<T>::iterator::operator>(const iterator& iter) const
{ {
return ptr_ > iter.ptr_; return ptr_ > iter.ptr_;
} }
template<class T> template<class T>
inline bool Foam::UPtrList<T>::iterator::operator<=(const iterator& iter) const inline bool Foam::UPtrList<T>::iterator::operator<=(const iterator& iter) const
{ {
return ptr_ <= iter.ptr_; return ptr_ <= iter.ptr_;
} }
template<class T> template<class T>
inline bool Foam::UPtrList<T>::iterator::operator>=(const iterator& iter) const inline bool Foam::UPtrList<T>::iterator::operator>=(const iterator& iter) const
{ {
return ptr_ >= iter.ptr_; return ptr_ >= iter.ptr_;
} }
template<class T>
inline typename Foam::UPtrList<T>::iterator
Foam::UPtrList<T>::begin()
{
return ptrs_.begin();
}
template<class T>
inline typename Foam::UPtrList<T>::iterator
Foam::UPtrList<T>::end()
{
return ptrs_.end();
}
// * * * * * * * * * * * * * * * STL const_iterator * * * * * * * * * * * * // // * * * * * * * * * * * * * * * STL const_iterator * * * * * * * * * * * * //
@ -343,21 +412,21 @@ inline bool Foam::UPtrList<T>::const_iterator::operator!=
template<class T> template<class T>
inline const T& Foam::UPtrList<T>::const_iterator::operator*() inline const T& Foam::UPtrList<T>::const_iterator::operator*() const
{ {
return **ptr_; return **ptr_;
} }
template<class T> template<class T>
inline const T& Foam::UPtrList<T>::const_iterator::operator()() inline const T& Foam::UPtrList<T>::const_iterator::operator()() const
{ {
return operator*(); return **ptr_;
} }
template<class T> template<class T>
inline typename Foam::UPtrList<T>::const_iterator inline typename Foam::UPtrList<T>::const_iterator&
Foam::UPtrList<T>::const_iterator::operator++() Foam::UPtrList<T>::const_iterator::operator++()
{ {
++ptr_; ++ptr_;
@ -369,14 +438,14 @@ template<class T>
inline typename Foam::UPtrList<T>::const_iterator inline typename Foam::UPtrList<T>::const_iterator
Foam::UPtrList<T>::const_iterator::operator++(int) Foam::UPtrList<T>::const_iterator::operator++(int)
{ {
const_iterator tmp = *this; const_iterator iter(*this);
++ptr_; ++ptr_;
return tmp; return iter;
} }
template<class T> template<class T>
inline typename Foam::UPtrList<T>::const_iterator inline typename Foam::UPtrList<T>::const_iterator&
Foam::UPtrList<T>::const_iterator::operator--() Foam::UPtrList<T>::const_iterator::operator--()
{ {
--ptr_; --ptr_;
@ -388,14 +457,14 @@ template<class T>
inline typename Foam::UPtrList<T>::const_iterator inline typename Foam::UPtrList<T>::const_iterator
Foam::UPtrList<T>::const_iterator::operator--(int) Foam::UPtrList<T>::const_iterator::operator--(int)
{ {
const_iterator tmp = *this; const_iterator iter(*this);
--ptr_; --ptr_;
return tmp; return iter;
} }
template<class T> template<class T>
inline typename Foam::UPtrList<T>::const_iterator inline typename Foam::UPtrList<T>::const_iterator&
Foam::UPtrList<T>::const_iterator::operator+=(label n) Foam::UPtrList<T>::const_iterator::operator+=(label n)
{ {
ptr_ += n; ptr_ += n;
@ -404,25 +473,7 @@ Foam::UPtrList<T>::const_iterator::operator+=(label n)
template<class T> template<class T>
inline typename Foam::UPtrList<T>::const_iterator inline typename Foam::UPtrList<T>::const_iterator&
Foam::operator+(const typename UPtrList<T>::const_iterator& iter, label n)
{
typename UPtrList<T>::const_iterator tmp = iter;
return tmp += n;
}
template<class T>
inline typename Foam::UPtrList<T>::const_iterator
Foam::operator+(label n, const typename UPtrList<T>::const_iterator& iter)
{
typename UPtrList<T>::const_iterator tmp = iter;
return tmp += n;
}
template<class T>
inline typename Foam::UPtrList<T>::const_iterator
Foam::UPtrList<T>::const_iterator::operator-=(label n) Foam::UPtrList<T>::const_iterator::operator-=(label n)
{ {
ptr_ -= n; ptr_ -= n;
@ -432,26 +483,31 @@ Foam::UPtrList<T>::const_iterator::operator-=(label n)
template<class T> template<class T>
inline typename Foam::UPtrList<T>::const_iterator inline typename Foam::UPtrList<T>::const_iterator
Foam::operator-(const typename UPtrList<T>::const_iterator& iter, label n) Foam::UPtrList<T>::const_iterator::operator+(label n) const
{ {
typename UPtrList<T>::const_iterator tmp = iter; return const_iterator(ptr_ + n);
return tmp -= n;
} }
template<class T> template<class T>
inline Foam::label Foam::operator- inline typename Foam::UPtrList<T>::const_iterator
( Foam::UPtrList<T>::const_iterator::operator-(label n) const
const typename UPtrList<T>::const_iterator& iter1,
const typename UPtrList<T>::const_iterator& iter2
)
{ {
return (iter1.ptr_ - iter2.ptr_)/sizeof(T*); return const_iterator(ptr_ - n);
} }
template<class T> template<class T>
inline const T& Foam::UPtrList<T>::const_iterator::operator[](label n) inline Foam::label
Foam::UPtrList<T>::const_iterator::operator-(const const_iterator& iter) const
{
return (ptr_ - iter.ptr_);
}
template<class T>
inline const T&
Foam::UPtrList<T>::const_iterator::operator[](label n) const
{ {
return *(*this + n); return *(*this + n);
} }
@ -497,6 +553,40 @@ inline bool Foam::UPtrList<T>::const_iterator::operator>=
} }
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class T>
inline typename Foam::UPtrList<T>::iterator
Foam::UPtrList<T>::begin()
{
return ptrs_.begin();
}
template<class T>
inline typename Foam::UPtrList<T>::iterator
Foam::UPtrList<T>::end()
{
return ptrs_.end();
}
template<class T>
inline typename Foam::UPtrList<T>::const_iterator
Foam::UPtrList<T>::cbegin() const
{
return ptrs_.cbegin();
}
template<class T>
inline typename Foam::UPtrList<T>::const_iterator
Foam::UPtrList<T>::cend() const
{
return ptrs_.cend();
}
template<class T> template<class T>
inline typename Foam::UPtrList<T>::const_iterator inline typename Foam::UPtrList<T>::const_iterator
Foam::UPtrList<T>::begin() const Foam::UPtrList<T>::begin() const
@ -513,19 +603,13 @@ Foam::UPtrList<T>::end() const
} }
template<class T> // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
inline typename Foam::UPtrList<T>::const_iterator
Foam::UPtrList<T>::cbegin() const
{
return ptrs_.begin();
}
template<class T> template<class T>
inline typename Foam::UPtrList<T>::const_iterator inline void Foam::UPtrList<T>::operator=(UPtrList<T>&& lst)
Foam::UPtrList<T>::cend() const
{ {
return ptrs_.end(); this->clear();
this->swap(lst);
} }