Commit 73e89f93 authored by Mark Olesen's avatar Mark Olesen
Browse files

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()
parent 8e0bd4bf
......@@ -488,6 +488,33 @@ int main(int argc, char *argv[])
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;
dynPlanes.free();
......
......@@ -106,7 +106,7 @@ public:
//- Alter the addressed list size.
inline void resize(const label newLen);
//- Alter the addressed list size.
//- Same as resize()
inline void setSize(const label newLen);
//- Reserve allocation space for at least this size.
......@@ -139,8 +139,9 @@ public:
//- Remove and return the top element
inline autoPtr<T> remove();
//- Return true if element is set (ie, not a nullptr)
inline bool set(const label i) const;
//- Return const pointer to element (if set) or nullptr.
// 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)
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>
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
void resize(const label newLen);
//- Adjust size of PtrList.
// New entries are initialized to nullptr, removed entries are deleted
//- Same as resize()
inline void setSize(const label newLen);
//- Append an element to the end of the list
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / 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
This file is part of OpenFOAM.
......@@ -38,6 +38,30 @@ Foam::UPtrList<T>::UPtrList(PtrList<T>& list)
// * * * * * * * * * * * * * * * 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>
void Foam::UPtrList<T>::reorder(const labelUList& oldToNew)
{
......
......@@ -132,7 +132,7 @@ public:
inline explicit UPtrList(UList<T>& list);
// Member functions
// Member Functions
// Access
......@@ -164,10 +164,13 @@ public:
// New entries are initialized to nullptr.
inline void resize(const label newLen);
//- Reset size of list.
// New entries are initialized to nullptr.
//- Same as resize()
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
inline void append(T* ptr);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment