mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: add UPtrList method to squeeze out (remove) null pointers
- moves any nullptr to the end of the list where they can be eliminated in a second step with resize()
This commit is contained in:
@ -488,6 +488,33 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
report(Info, dynPlanes, true);
|
report(Info, dynPlanes, true);
|
||||||
|
|
||||||
|
{
|
||||||
|
// No clone for plane - do manual copy
|
||||||
|
PtrList<plane> stdPlanes(dynPlanes.size());
|
||||||
|
|
||||||
|
forAll(dynPlanes, i)
|
||||||
|
{
|
||||||
|
const plane* pln = dynPlanes.set(i);
|
||||||
|
if (pln)
|
||||||
|
{
|
||||||
|
stdPlanes.set(i, new plane(*pln));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
report(Info, stdPlanes);
|
||||||
|
printAddr(Info, stdPlanes);
|
||||||
|
|
||||||
|
stdPlanes.resize(stdPlanes.squeezeNull());
|
||||||
|
|
||||||
|
Info<<"After pruning nullptr entries" << endl;
|
||||||
|
printAddr(Info, stdPlanes);
|
||||||
|
}
|
||||||
|
|
||||||
|
dynPlanes.resize(dynPlanes.squeezeNull());
|
||||||
|
|
||||||
|
Info<<"After pruning nullptr entries" << endl;
|
||||||
|
report(Info, dynPlanes, true);
|
||||||
|
|
||||||
Info<<"free()" << endl;
|
Info<<"free()" << endl;
|
||||||
|
|
||||||
dynPlanes.free();
|
dynPlanes.free();
|
||||||
|
|||||||
@ -106,7 +106,7 @@ public:
|
|||||||
//- Alter the addressed list size.
|
//- Alter the addressed list size.
|
||||||
inline void resize(const label newLen);
|
inline void resize(const label newLen);
|
||||||
|
|
||||||
//- Alter the addressed list size.
|
//- Same as resize()
|
||||||
inline void setSize(const label newLen);
|
inline void setSize(const label newLen);
|
||||||
|
|
||||||
//- Reserve allocation space for at least this size.
|
//- Reserve allocation space for at least this size.
|
||||||
@ -139,8 +139,9 @@ public:
|
|||||||
//- Remove and return the top element
|
//- Remove and return the top element
|
||||||
inline autoPtr<T> remove();
|
inline autoPtr<T> remove();
|
||||||
|
|
||||||
//- Return true if element is set (ie, not a nullptr)
|
//- Return const pointer to element (if set) or nullptr.
|
||||||
inline bool set(const label i) const;
|
// The return value can be tested as a bool.
|
||||||
|
inline const T* set(const label i) const;
|
||||||
|
|
||||||
//- Set element to given pointer and return old element (can be null)
|
//- Set element to given pointer and return old element (can be null)
|
||||||
inline autoPtr<T> set(const label i, T* ptr);
|
inline autoPtr<T> set(const label i, T* ptr);
|
||||||
|
|||||||
@ -243,9 +243,9 @@ inline Foam::autoPtr<T> Foam::PtrDynList<T, SizeMin>::remove()
|
|||||||
|
|
||||||
|
|
||||||
template<class T, int SizeMin>
|
template<class T, int SizeMin>
|
||||||
inline bool Foam::PtrDynList<T, SizeMin>::set(const label i) const
|
inline const T* Foam::PtrDynList<T, SizeMin>::set(const label i) const
|
||||||
{
|
{
|
||||||
return (i >= 0 && i < PtrList<T>::size()) ? PtrList<T>::set(i) : false;
|
return (i >= 0 && i < PtrList<T>::size()) ? PtrList<T>::set(i) : nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -131,8 +131,7 @@ public:
|
|||||||
// New entries are initialized to nullptr, removed entries are deleted
|
// New entries are initialized to nullptr, removed entries are deleted
|
||||||
void resize(const label newLen);
|
void resize(const label newLen);
|
||||||
|
|
||||||
//- Adjust size of PtrList.
|
//- Same as resize()
|
||||||
// New entries are initialized to nullptr, removed entries are deleted
|
|
||||||
inline void setSize(const label newLen);
|
inline void setSize(const label newLen);
|
||||||
|
|
||||||
//- Append an element to the end of the list
|
//- Append an element to the end of the list
|
||||||
|
|||||||
@ -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-2018 OpenCFD Ltd.
|
\\/ M anipulation | Copyright (C) 2015-2019 OpenCFD Ltd.
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
License
|
License
|
||||||
This file is part of OpenFOAM.
|
This file is part of OpenFOAM.
|
||||||
@ -38,6 +38,30 @@ Foam::UPtrList<T>::UPtrList(PtrList<T>& list)
|
|||||||
|
|
||||||
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
|
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
Foam::label Foam::UPtrList<T>::squeezeNull()
|
||||||
|
{
|
||||||
|
const label len = this->size();
|
||||||
|
label newLen = 0;
|
||||||
|
|
||||||
|
for (label i=0; i < len; ++i)
|
||||||
|
{
|
||||||
|
T* ptr = ptrs_[i];
|
||||||
|
if (ptr)
|
||||||
|
{
|
||||||
|
if (i != newLen)
|
||||||
|
{
|
||||||
|
ptrs_[newLen] = ptr;
|
||||||
|
ptrs_[i] = nullptr;
|
||||||
|
}
|
||||||
|
++newLen;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return newLen;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
void Foam::UPtrList<T>::reorder(const labelUList& oldToNew)
|
void Foam::UPtrList<T>::reorder(const labelUList& oldToNew)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -132,7 +132,7 @@ public:
|
|||||||
inline explicit UPtrList(UList<T>& list);
|
inline explicit UPtrList(UList<T>& list);
|
||||||
|
|
||||||
|
|
||||||
// Member functions
|
// Member Functions
|
||||||
|
|
||||||
// Access
|
// Access
|
||||||
|
|
||||||
@ -164,10 +164,13 @@ public:
|
|||||||
// New entries are initialized to nullptr.
|
// New entries are initialized to nullptr.
|
||||||
inline void resize(const label newLen);
|
inline void resize(const label newLen);
|
||||||
|
|
||||||
//- Reset size of list.
|
//- Same as resize()
|
||||||
// New entries are initialized to nullptr.
|
|
||||||
inline void setSize(const label newLen);
|
inline void setSize(const label newLen);
|
||||||
|
|
||||||
|
//- Squeeze out intermediate nullptr entries in the list of pointers
|
||||||
|
// \return the number of non-null entries
|
||||||
|
label squeezeNull();
|
||||||
|
|
||||||
//- Append an element to the end of the list
|
//- Append an element to the end of the list
|
||||||
inline void append(T* ptr);
|
inline void append(T* ptr);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user