mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: new/revised emplace_back() [for DynamicList/List/PtrDynList/PtrList]
- returns reference as per C++17 std::vector STYLE: drop unused, redundant DynamicField remove() method
This commit is contained in:
@ -259,6 +259,11 @@ public:
|
||||
template<int AnySizeMin>
|
||||
inline void transfer(DynamicList<T, AnySizeMin>& list);
|
||||
|
||||
//- Construct an element at the end of the list,
|
||||
//- return reference to the new list element
|
||||
template<class... Args>
|
||||
inline T& emplace_back(Args&&... args);
|
||||
|
||||
//- Copy append an element to the end of this list.
|
||||
inline void push_back(const T& val);
|
||||
|
||||
|
||||
@ -503,6 +503,23 @@ Foam::DynamicList<T, SizeMin>::transfer
|
||||
}
|
||||
|
||||
|
||||
template<class T, int SizeMin>
|
||||
template<class... Args>
|
||||
inline T& Foam::DynamicList<T, SizeMin>::emplace_back(Args&&... args)
|
||||
{
|
||||
// This could/should be better with inplace construction
|
||||
// (as per std::vector), but currently lacking the methods for that
|
||||
// so resize and move assign
|
||||
|
||||
const label idx = List<T>::size();
|
||||
resize(idx + 1);
|
||||
|
||||
// move assign element
|
||||
this->operator[](idx) = T(std::forward<Args>(args)...);
|
||||
return this->back();
|
||||
}
|
||||
|
||||
|
||||
template<class T, int SizeMin>
|
||||
inline void Foam::DynamicList<T, SizeMin>::push_back
|
||||
(
|
||||
|
||||
@ -240,6 +240,12 @@ public:
|
||||
|
||||
// Edit
|
||||
|
||||
//- Construct an element at the end of the list,
|
||||
//- return reference to the new list element.
|
||||
// If this is frequently required, consider a DynamicList instead.
|
||||
template<class... Args>
|
||||
inline T& emplace_back(Args&&... args);
|
||||
|
||||
//- Append an element at the end of the list
|
||||
// If this is frequently required, consider a DynamicList
|
||||
inline void push_back(const T& val);
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
Copyright (C) 2011-2016 OpenFOAM Foundation
|
||||
Copyright (C) 2017-2021 OpenCFD Ltd.
|
||||
Copyright (C) 2017-2023 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
@ -171,6 +171,23 @@ inline T& Foam::List<T>::newElmt(const label i)
|
||||
}
|
||||
|
||||
|
||||
template<class T>
|
||||
template<class... Args>
|
||||
inline T& Foam::List<T>::emplace_back(Args&&... args)
|
||||
{
|
||||
// This could/should be better with inplace construction
|
||||
// (as per std::vector), but currently lacking the methods for that
|
||||
// so resize and move assign
|
||||
|
||||
const label idx = this->size();
|
||||
resize(idx + 1);
|
||||
|
||||
// move assign element
|
||||
this->operator[](idx) = T(std::forward<Args>(args)...);
|
||||
return this->back();
|
||||
}
|
||||
|
||||
|
||||
template<class T>
|
||||
inline void Foam::List<T>::push_back(const T& val)
|
||||
{
|
||||
|
||||
@ -141,9 +141,10 @@ public:
|
||||
template<int AnySizeMin>
|
||||
inline void swap(PtrDynList<T, AnySizeMin>& other);
|
||||
|
||||
//- Construct an element at the end of the list
|
||||
//- Construct an element at the end of the list,
|
||||
//- return reference to the new list element
|
||||
template<class... Args>
|
||||
inline void emplace_back(Args&&... args);
|
||||
inline T& emplace_back(Args&&... args);
|
||||
|
||||
//- Append an element to the end of the list
|
||||
inline void push_back(T* ptr);
|
||||
|
||||
@ -218,9 +218,10 @@ inline void Foam::PtrDynList<T, SizeMin>::swap
|
||||
|
||||
template<class T, int SizeMin>
|
||||
template<class... Args>
|
||||
inline void Foam::PtrDynList<T, SizeMin>::emplace_back(Args&&... args)
|
||||
inline T& Foam::PtrDynList<T, SizeMin>::emplace_back(Args&&... args)
|
||||
{
|
||||
this->push_back(new T(std::forward<Args>(args)...));
|
||||
return this->back();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
Copyright (C) 2011-2016 OpenFOAM Foundation
|
||||
Copyright (C) 2018-2022 OpenCFD Ltd.
|
||||
Copyright (C) 2018-2023 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
@ -149,9 +149,10 @@ public:
|
||||
//- Same as resize()
|
||||
void setSize(const label newLen) { this->resize(newLen); }
|
||||
|
||||
//- Construct and append an element to the end of the list
|
||||
//- Construct and append an element to the end of the list,
|
||||
//- return reference to the new list element
|
||||
template<class... Args>
|
||||
inline void emplace_back(Args&&... args);
|
||||
inline T& emplace_back(Args&&... args);
|
||||
|
||||
//- Append an element to the end of the list
|
||||
inline void push_back(T* ptr);
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
Copyright (C) 2011-2016 OpenFOAM Foundation
|
||||
Copyright (C) 2018-2022 OpenCFD Ltd.
|
||||
Copyright (C) 2018-2023 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
@ -103,9 +103,10 @@ inline void Foam::PtrList<T>::clear()
|
||||
|
||||
template<class T>
|
||||
template<class... Args>
|
||||
inline void Foam::PtrList<T>::emplace_back(Args&&... args)
|
||||
inline T& Foam::PtrList<T>::emplace_back(Args&&... args)
|
||||
{
|
||||
UPtrList<T>::push_back(new T(std::forward<Args>(args)...));
|
||||
return this->back();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -279,21 +279,23 @@ public:
|
||||
template<int AnySizeMin>
|
||||
inline void transfer(DynamicField<T, AnySizeMin>& list);
|
||||
|
||||
//- Append an element at the end of the list
|
||||
//- Construct an element at the end of the list,
|
||||
//- return reference to the new list element
|
||||
template<class... Args>
|
||||
inline T& emplace_back(Args&&... args);
|
||||
|
||||
//- Copy append an element at the end of the list
|
||||
inline void push_back(const T& val);
|
||||
|
||||
//- Move append an element
|
||||
inline void push_back(T&& val);
|
||||
|
||||
//- Append a List at the end of this list
|
||||
//- Copy append another list to the end of this list
|
||||
inline void push_back(const UList<T>& list);
|
||||
|
||||
//- Reduce size by 1 or more elements. Can be called on an empty list.
|
||||
inline void pop_back(label n = 1);
|
||||
|
||||
//- Remove and return the last element. Fatal on an empty list.
|
||||
inline T remove();
|
||||
|
||||
|
||||
// Reading/writing
|
||||
|
||||
|
||||
@ -583,6 +583,23 @@ inline void Foam::DynamicField<T, SizeMin>::transfer
|
||||
}
|
||||
|
||||
|
||||
template<class T, int SizeMin>
|
||||
template<class... Args>
|
||||
inline T& Foam::DynamicField<T, SizeMin>::emplace_back(Args&&... args)
|
||||
{
|
||||
// This could/should be better with inplace construction
|
||||
// (as per std::vector), but currently lacking the methods for that
|
||||
// so resize and move assign
|
||||
|
||||
const label idx = List<T>::size();
|
||||
resize(idx + 1);
|
||||
|
||||
// move assign element
|
||||
this->operator[](idx) = T(std::forward<Args>(args)...);
|
||||
return this->back();
|
||||
}
|
||||
|
||||
|
||||
template<class T, int SizeMin>
|
||||
inline void Foam::DynamicField<T, SizeMin>::push_back
|
||||
(
|
||||
@ -646,26 +663,6 @@ inline void Foam::DynamicField<T, SizeMin>::pop_back(label n)
|
||||
}
|
||||
|
||||
|
||||
template<class T, int SizeMin>
|
||||
inline T Foam::DynamicField<T, SizeMin>::remove()
|
||||
{
|
||||
// Location of last element and simultaneously the new size
|
||||
const label idx = List<T>::size() - 1;
|
||||
|
||||
if (idx < 0)
|
||||
{
|
||||
FatalErrorInFunction
|
||||
<< "List is empty" << abort(FatalError);
|
||||
}
|
||||
|
||||
const T& val = List<T>::operator[](idx);
|
||||
|
||||
List<T>::setAddressableSize(idx);
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
|
||||
|
||||
template<class T, int SizeMin>
|
||||
|
||||
Reference in New Issue
Block a user