diff --git a/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynListI.H b/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynListI.H index 1c929f57db..0320636bd6 100644 --- a/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynListI.H +++ b/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynListI.H @@ -187,7 +187,7 @@ template inline Foam::label Foam::PtrDynList::squeezeNull() { const label newLen = UPtrList::squeezeNull(); - resize(newLen); + PtrList::setAddressableSize(newLen); return newLen; } diff --git a/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrList.C b/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrList.C index 9ae4fcdec7..77b06feec8 100644 --- a/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrList.C +++ b/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrList.C @@ -65,6 +65,23 @@ Foam::label Foam::UPtrList::squeezeNull() } +template +void Foam::UPtrList::trimTrailingNull() +{ + label newLen = this->size(); + + for (label i = newLen-1; i >= 0 && !ptrs_[i]; --i) + { + --newLen; + } + + // Or mutable? + // const_cast&>(ptrs_).setAddressableSize(newLen); + + ptrs_.setAddressableSize(newLen); +} + + template void Foam::UPtrList::reorder(const labelUList& oldToNew, const bool check) { @@ -177,6 +194,22 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const UPtrList& list) // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * // +template +void Foam::sort(UPtrList& list) +{ + std::stable_sort + ( + list.begin_ptr(), + list.end_ptr(), + // Compare less, with nullptr protect and sort nullptr to end + [](const T* const a, const T* const b) -> bool + { + return (a && b) ? (*a < *b) : !b; + } + ); +} + + template void Foam::sort(UPtrList& list, const Compare& comp) { @@ -189,12 +222,4 @@ void Foam::sort(UPtrList& list, const Compare& comp) } -template -void Foam::sort(UPtrList& list) -{ - // ie, lessOp() or std::less() - Foam::sort(list, [](const T& a, const T& b) { return (a < b); }); -} - - // ************************************************************************* // diff --git a/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrList.H b/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrList.H index b546ac0541..fc6eb9eb23 100644 --- a/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrList.H +++ b/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrList.H @@ -129,7 +129,7 @@ public: //- Compare dereferenced pointers bool operator()(const T* const a, const T* const b) const { - return (a && b) ? comp(*a, *b) : bool(a); + return (a && b) ? comp(*a, *b) : !b; } }; @@ -150,7 +150,7 @@ public: const T* const a = values.get(ai); const T* const b = values.get(bi); - return (a && b) ? (*a < *b) : bool(a); + return (a && b) ? (*a < *b) : !b; } }; @@ -171,7 +171,7 @@ public: const T* const a = values.get(ai); const T* const b = values.get(bi); - return (a && b) ? (*b < *a) : bool(b); + return (a && b) ? (*b < *a) : !a; } }; @@ -262,10 +262,15 @@ public: //- Alias for resize() void setSize(const label n) { this->resize(n); } - //- Squeeze out intermediate nullptr entries in the list of pointers + //- Squeeze out nullptr entries in the list of pointers after which + //- any null pointers will be at the end of the list // \return the number of non-null entries label squeezeNull(); + //- Reduce addressable list size to ignore any trailing null pointers. + // The reduces the effective list length without reallocation + void trimTrailingNull(); + //- Append an element to the end of the list inline void push_back(T* ptr); @@ -343,7 +348,7 @@ public: // Member Functions //- Return pointer, can be nullptr. - inline pointer get() const; + pointer get() const { return *ptr_; } // Member Operators @@ -403,7 +408,7 @@ public: // Member Functions //- Return pointer, can be nullptr. - inline pointer get() const; + pointer get() const { return *ptr_; } // Member Operators @@ -507,6 +512,8 @@ public: // * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * // //- Inplace (stable) sorting of pointer list. +// This sort function includes null pointer guards and will also sort +// any null pointers to the end (eg, rubbish that can be truncated) template void sort(UPtrList& list); diff --git a/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrListI.H b/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrListI.H index d0c29ba2c1..652ebd872a 100644 --- a/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrListI.H +++ b/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrListI.H @@ -281,13 +281,6 @@ iterator(T** ptr) noexcept {} -template -inline T* Foam::UPtrList::iterator::get() const -{ - return *ptr_; -} - - template inline T* Foam::UPtrList::iterator::operator->() const { @@ -455,13 +448,6 @@ const_iterator(const iterator& iter) noexcept {} -template -inline const T* Foam::UPtrList::const_iterator::get() const -{ - return *ptr_; -} - - template inline const T* Foam::UPtrList::const_iterator::operator->() const {