From c7e6ae30bfa2f57e41bb304be25ccd2b5a0d1418 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Tue, 1 Nov 2022 17:24:36 +0100 Subject: [PATCH] ENH: add front(), back() methods to List containers - traditionally used first(), last() methods, but front(), back() are well-known from std::vector etc which makes the access more familiar. - support push_back() method for containers that already had append(). This increases name familiar and can help when porting between different C++ code bases. - support pop_back() method for List containers. This is similar to std::vector --- .../test/IndirectList/Test-IndirectList.C | 3 + applications/test/PtrList/Test-PtrList.C | 12 +- .../containers/Bits/PackedList/PackedList.H | 15 ++- .../containers/Bits/PackedList/PackedListI.H | 18 ++- .../containers/Bits/PackedList/PackedListIO.C | 2 +- .../BiIndirectList/BiIndirectList.H | 71 ++++++----- .../BiIndirectList/BiIndirectListI.H | 108 +++-------------- .../IndirectListBase/IndirectListBase.H | 58 +++++---- .../IndirectListBase/IndirectListBaseI.H | 26 ++-- .../Lists/DynamicList/DynamicList.H | 113 +++++++++++++----- .../Lists/DynamicList/DynamicListI.H | 40 +++++-- .../containers/Lists/FixedList/FixedList.H | 34 ++++-- .../containers/Lists/FixedList/FixedListI.H | 8 +- src/OpenFOAM/containers/Lists/List/List.H | 91 +++++++++----- src/OpenFOAM/containers/Lists/List/ListI.H | 28 +++-- src/OpenFOAM/containers/Lists/List/UList.H | 35 ++++-- src/OpenFOAM/containers/Lists/List/UListI.H | 8 +- .../Lists/ListOps/ListOpsTemplates.C | 2 +- .../PtrLists/PtrDynList/PtrDynList.H | 66 +++++++--- .../PtrLists/PtrDynList/PtrDynListI.H | 42 ++++--- .../containers/PtrLists/PtrList/PtrList.H | 50 ++++++-- .../containers/PtrLists/PtrList/PtrListI.H | 28 ++--- .../containers/PtrLists/UPtrList/UPtrList.H | 47 ++++++-- .../containers/PtrLists/UPtrList/UPtrListI.H | 16 +-- .../fields/Fields/DynamicField/DynamicField.H | 28 ++++- .../Fields/DynamicField/DynamicFieldI.H | 22 +++- .../meshes/meshShapes/hexCell/hexCell.H | 11 +- .../meshes/meshShapes/tetCell/tetCell.H | 11 +- .../meshes/meshShapes/triFace/triFace.H | 9 ++ .../primitiveShapes/tetrahedron/tetrahedron.H | 6 + .../primitiveShapes/triangle/triangle.H | 6 + .../primitives/Vector/bools/boolVector.H | 6 + src/OpenFOAM/primitives/enums/Enum.C | 4 +- src/OpenFOAM/primitives/enums/Enum.H | 18 ++- .../primitives/strings/lists/hashedWordList.H | 18 ++- .../strings/lists/hashedWordListI.H | 13 +- src/OpenFOAM/primitives/tuples/Pair.H | 11 +- 37 files changed, 696 insertions(+), 388 deletions(-) diff --git a/applications/test/IndirectList/Test-IndirectList.C b/applications/test/IndirectList/Test-IndirectList.C index 399d43906c..92c7b413a2 100644 --- a/applications/test/IndirectList/Test-IndirectList.C +++ b/applications/test/IndirectList/Test-IndirectList.C @@ -101,6 +101,9 @@ int main(int argc, char *argv[]) printInfo(idl1); + Info<< "list() = "; + idl1.list().writeList(Info, 0) << endl; + for (const label val : { 10, 30, 40, 50, 90, 80, 120 } ) { testFind(val, idl1); diff --git a/applications/test/PtrList/Test-PtrList.C b/applications/test/PtrList/Test-PtrList.C index 51c7d59f36..a8f91df9f9 100644 --- a/applications/test/PtrList/Test-PtrList.C +++ b/applications/test/PtrList/Test-PtrList.C @@ -334,7 +334,7 @@ int main(int argc, char *argv[]) { listApp.append(new Scalar(1.3*i)); } - listApp.emplace_append(100); + listApp.emplace_back(100); Info<< nl @@ -580,8 +580,8 @@ int main(int argc, char *argv[]) } PtrList planes; - planes.emplace_append(vector::one, vector::one); - planes.emplace_append(vector(1,2,3), vector::one); + planes.emplace_back(vector::one, vector::one); + planes.emplace_back(vector(1,2,3), vector::one); Info<< nl << "appended values" << nl; for (const plane& p : planes) @@ -594,15 +594,15 @@ int main(int argc, char *argv[]) PtrDynList dynPlanes; { - dynPlanes.emplace_append(vector::one, vector::one); - dynPlanes.emplace_append(vector(1,2,3), vector::one); + dynPlanes.emplace_back(vector::one, vector::one); + dynPlanes.emplace_back(vector(1,2,3), vector::one); dynPlanes.append(nullptr); dynPlanes.set(6, new plane(vector(2,2,1), vector::one)); dynPlanes.set(10, new plane(vector(4,5,6), vector::one)); dynPlanes.emplace(12, vector(3,2,1), vector::one); - dynPlanes.emplace_append(Zero, vector::one); + dynPlanes.emplace_back(Zero, vector::one); } Info<< nl << "PtrDynList: "; diff --git a/src/OpenFOAM/containers/Bits/PackedList/PackedList.H b/src/OpenFOAM/containers/Bits/PackedList/PackedList.H index 9b281f158f..14150bc832 100644 --- a/src/OpenFOAM/containers/Bits/PackedList/PackedList.H +++ b/src/OpenFOAM/containers/Bits/PackedList/PackedList.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2017-2021 OpenCFD Ltd. + Copyright (C) 2017-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -437,7 +437,10 @@ public: // Member Operators //- Append a value at the end of the list - inline PackedList& append(const unsigned int val); + inline void push_back(const unsigned int val); + + //- 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 inline unsigned int remove(); @@ -556,6 +559,14 @@ public: //- Alias for resize() void setSize(const label n, unsigned int val = 0u) { resize(n, val); } + + //- Append a value at the end of the list + //FOAM_DEPRECATED_FOR(2022-10, "push_back()") + PackedList& append(const unsigned int val) + { + this->push_back(val); + return *this; + } }; diff --git a/src/OpenFOAM/containers/Bits/PackedList/PackedListI.H b/src/OpenFOAM/containers/Bits/PackedList/PackedListI.H index 5d3dbe1dd5..d4db13d316 100644 --- a/src/OpenFOAM/containers/Bits/PackedList/PackedListI.H +++ b/src/OpenFOAM/containers/Bits/PackedList/PackedListI.H @@ -693,15 +693,27 @@ inline bool Foam::PackedList::unset(const label i) template -inline Foam::PackedList& -Foam::PackedList::append(const unsigned int val) +inline void Foam::PackedList::push_back(const unsigned int val) { const label idx = size(); reserve(idx + 1); ++size_; reference(this, idx).set(val); - return *this; +} + + +template +inline void Foam::PackedList::pop_back(label n) +{ + if (n >= size()) + { + clear(); + } + else if (n > 0) + { + resize(size() - n); + } } diff --git a/src/OpenFOAM/containers/Bits/PackedList/PackedListIO.C b/src/OpenFOAM/containers/Bits/PackedList/PackedListIO.C index 70cf53d767..03943c5e3f 100644 --- a/src/OpenFOAM/containers/Bits/PackedList/PackedListIO.C +++ b/src/OpenFOAM/containers/Bits/PackedList/PackedListIO.C @@ -142,7 +142,7 @@ Foam::Istream& Foam::PackedList::readList(Istream& is) while (!tok.isPunctuation(token::END_LIST)) { is.putBack(tok); - list.append(list.readValue(is)); + list.push_back(list.readValue(is)); is >> tok; is.fatalCheck(FUNCTION_NAME); diff --git a/src/OpenFOAM/containers/IndirectLists/BiIndirectList/BiIndirectList.H b/src/OpenFOAM/containers/IndirectLists/BiIndirectList/BiIndirectList.H index 8584736338..cf0331da8d 100644 --- a/src/OpenFOAM/containers/IndirectLists/BiIndirectList/BiIndirectList.H +++ b/src/OpenFOAM/containers/IndirectLists/BiIndirectList/BiIndirectList.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2019 OpenCFD Ltd. + Copyright (C) 2019-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -35,8 +35,8 @@ SourceFiles \*---------------------------------------------------------------------------*/ -#ifndef BiIndirectList_H -#define BiIndirectList_H +#ifndef Foam_BiIndirectList_H +#define Foam_BiIndirectList_H #include "List.H" @@ -57,8 +57,7 @@ class BiIndirectList UList& posList_; UList& negList_; - labelList addressing_; - + labelList addr_; public: @@ -83,50 +82,56 @@ public: // Member Functions - // Access + // Access - //- The number of elements in the list - inline label size() const noexcept; + //- The number of elements in the list + label size() const noexcept { return addr_.size(); } - //- True if the list is empty (ie, size() is zero). - inline bool empty() const noexcept; + //- True if the list is empty (ie, size() is zero). + bool empty() const noexcept { return addr_.empty(); } - inline const UList& posList() const noexcept; - inline const UList& negList() const noexcept; + //- The list of positive values (without addressing) + const UList& posList() const noexcept { return posList_; } - //- Return the list addressing - inline const labelList& addressing() const noexcept; + //- The list of negative values (without addressing) + const UList& negList() const noexcept { return negList_; } - //- Calculate index given whether index is into posList or negList - inline static label posIndex(const label i); - inline static label negIndex(const label i); + //- The addressing used for the list + const labelList& addressing() const noexcept { return addr_; } + + //- Calculate index given whether index is into posList or negList + static label posIndex(const label i) noexcept { return i; } + static label negIndex(const label i) noexcept { return (-i-1); } - // Edit + // Edit - //- Copy reset addressing - inline void resetAddressing(const labelUList& addr); + //- Copy reset addressing + inline void resetAddressing(const labelUList& addr); - //- Move reset addressing - inline void resetAddressing(labelList&& addr); + //- Move reset addressing + inline void resetAddressing(labelList&& addr); + + //- Return the addressed elements as a List + inline List list() const; - // Member Operators + // Member Operators - //- Return the addressed elements as a List - inline List operator()() const; + //- Return the addressed elements as a List + List operator()() const { return this->list(); } - //- Return non-const access to an element - inline T& operator[](const label i); + //- Return non-const access to an element + inline T& operator[](const label i); - //- Return const access to an element - inline const T& operator[](const label i) const; + //- Return const access to an element + inline const T& operator[](const label i) const; - //- Assignment to UList of addressed elements - inline void operator=(const UList& ae); + //- Assignment to UList of addressed elements + inline void operator=(const UList& ae); - //- Assignment of all entries to the given value - inline void operator=(const T& val); + //- Assignment of all entries to the given value + inline void operator=(const T& val); }; diff --git a/src/OpenFOAM/containers/IndirectLists/BiIndirectList/BiIndirectListI.H b/src/OpenFOAM/containers/IndirectLists/BiIndirectList/BiIndirectListI.H index d9ac3ce68d..dd29ff917e 100644 --- a/src/OpenFOAM/containers/IndirectLists/BiIndirectList/BiIndirectListI.H +++ b/src/OpenFOAM/containers/IndirectLists/BiIndirectList/BiIndirectListI.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2019 OpenCFD Ltd. + Copyright (C) 2019-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -26,22 +26,6 @@ License \*---------------------------------------------------------------------------*/ -// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // - -template -inline Foam::label Foam::BiIndirectList::posIndex(const label i) -{ - return i; -} - - -template -inline Foam::label Foam::BiIndirectList::negIndex(const label i) -{ - return -i-1; -} - - // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // template @@ -54,7 +38,7 @@ inline Foam::BiIndirectList::BiIndirectList : posList_(const_cast&>(posList)), negList_(const_cast&>(negList)), - addressing_(addr) + addr_(addr) {} @@ -68,72 +52,28 @@ inline Foam::BiIndirectList::BiIndirectList : posList_(const_cast&>(posList)), negList_(const_cast&>(negList)), - addressing_(std::move(addr)) + addr_(std::move(addr)) {} // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // template -inline Foam::label Foam::BiIndirectList::size() const noexcept +inline void Foam::BiIndirectList::resetAddressing(const labelUList& addr) { - return addressing_.size(); + addr_ = addr; } template -inline bool Foam::BiIndirectList::empty() const noexcept +inline void Foam::BiIndirectList::resetAddressing(labelList&& addr) { - return addressing_.empty(); + addr_.transfer(addr); } template -inline const Foam::UList& Foam::BiIndirectList::posList() const noexcept -{ - return posList_; -} - - -template -inline const Foam::UList& Foam::BiIndirectList::negList() const noexcept -{ - return negList_; -} - - -template -inline const Foam::labelList& -Foam::BiIndirectList::addressing() const noexcept -{ - return addressing_; -} - - -template -inline void Foam::BiIndirectList::resetAddressing -( - const labelUList& addr -) -{ - addressing_ = addr; -} - - -template -inline void Foam::BiIndirectList::resetAddressing -( - labelList&& addr -) -{ - addressing_.transfer(addr); -} - - -// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // - -template -inline Foam::List Foam::BiIndirectList::operator()() const +inline Foam::List Foam::BiIndirectList::list() const { List result(size()); @@ -146,51 +86,39 @@ inline Foam::List Foam::BiIndirectList::operator()() const } +// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // + template inline T& Foam::BiIndirectList::operator[](const label i) { - const label index = addressing_[i]; + const label index = addr_[i]; - if (index >= 0) - { - return posList_[index]; - } - else - { - return negList_[-index-1]; - } + return (index >= 0 ? posList_[index] : negList_[-index-1]); } template inline const T& Foam::BiIndirectList::operator[](const label i) const { - const label index = addressing_[i]; + const label index = addr_[i]; - if (index >= 0) - { - return posList_[index]; - } - else - { - return negList_[-index-1]; - } + return (index >= 0 ? posList_[index] : negList_[-index-1]); } template inline void Foam::BiIndirectList::operator=(const UList& ae) { - if (addressing_.size() != ae.size()) + if (addr_.size() != ae.size()) { FatalErrorInFunction << "Addressing and list of addressed elements " "have different sizes: " - << addressing_.size() << " " << ae.size() + << addr_.size() << " " << ae.size() << abort(FatalError); } - forAll(addressing_, i) + forAll(addr_, i) { operator[](i) = ae[i]; } @@ -200,7 +128,7 @@ inline void Foam::BiIndirectList::operator=(const UList& ae) template inline void Foam::BiIndirectList::operator=(const T& val) { - forAll(addressing_, i) + forAll(addr_, i) { operator[](i) = val; } diff --git a/src/OpenFOAM/containers/IndirectLists/IndirectListBase/IndirectListBase.H b/src/OpenFOAM/containers/IndirectLists/IndirectListBase/IndirectListBase.H index 2c01a2c75c..de5a183582 100644 --- a/src/OpenFOAM/containers/IndirectLists/IndirectListBase/IndirectListBase.H +++ b/src/OpenFOAM/containers/IndirectLists/IndirectListBase/IndirectListBase.H @@ -123,49 +123,35 @@ public: // Access //- The number of elements in the list - inline label size() const noexcept - { - return addr_.size(); - } + label size() const noexcept { return addr_.size(); } //- True if the list is empty (ie, size() is zero). - inline bool empty() const noexcept - { - return addr_.empty(); - } + bool empty() const noexcept { return addr_.empty(); } //- The list of values (without addressing) - inline const UList& values() const noexcept - { - return values_; - } + const UList& values() const noexcept { return values_; } //- The list of values (without addressing) - inline UList& values() noexcept - { - return values_; - } + UList& values() noexcept { return values_; } //- The addressing used for the list - inline const Addr& addressing() const noexcept - { - return addr_; - } + const Addr& addressing() const noexcept { return addr_; } + //- True if all entries have identical values, and list is non-empty inline bool uniform() const; //- The first element of the list. - inline const T& first() const; + inline const T& front() const; //- The first element of the list. - inline T& first(); + inline T& front(); //- The last element of the list. - inline const T& last() const; + inline const T& back() const; //- The last element of the list. - inline T& last(); + inline T& back(); //- The forward circular index. The next index in the list //- which returns to the first at the end of the list @@ -187,6 +173,9 @@ public: //- Return reverse circular value (ie, previous value in the list) inline T& rcValue(const label i); + //- Return the addressed elements as a List + inline List list() const; + // Search @@ -211,7 +200,7 @@ public: // Member Operators //- Return the addressed elements as a List - inline List operator()() const; + List operator()() const { return this->list(); } //- Non-const access to an element in the list inline T& operator[](const label i); @@ -374,6 +363,25 @@ public: //- Write List, with line-breaks in ASCII when length exceeds shortLen. // Using '0' suppresses line-breaks entirely. Ostream& writeList(Ostream& os, const label shortLen=0) const; + + + // Housekeeping + + //- Access first element of the list, position [0] + //FOAM_DEPRECATED_FOR(2022-10, "front()") + T& first() { return front(); } + + //- Access first element of the list + //FOAM_DEPRECATED_FOR(2022-10, "front()") + const T& first() const { return front(); }; + + //- Access last element of the list, position [size()-1] + //FOAM_DEPRECATED_FOR(2022-10, "back()") + T& last() { return back(); } + + //- Access last element of the list, position [size()-1] + //FOAM_DEPRECATED_FOR(2022-10, "back()") + const T& last() const { return back(); }; }; diff --git a/src/OpenFOAM/containers/IndirectLists/IndirectListBase/IndirectListBaseI.H b/src/OpenFOAM/containers/IndirectLists/IndirectListBase/IndirectListBaseI.H index a6f87e688a..45afc104ce 100644 --- a/src/OpenFOAM/containers/IndirectLists/IndirectListBase/IndirectListBaseI.H +++ b/src/OpenFOAM/containers/IndirectLists/IndirectListBase/IndirectListBaseI.H @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2018-2021 OpenCFD Ltd. + Copyright (C) 2018-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -124,28 +124,28 @@ inline Foam::label Foam::IndirectListBase::rcIndex(const label i) const template -inline const T& Foam::IndirectListBase::first() const +inline const T& Foam::IndirectListBase::front() const { - return values_[addr_.first()]; + return values_[addr_.front()]; } template -inline T& Foam::IndirectListBase::first() +inline T& Foam::IndirectListBase::front() { - return values_[addr_.first()]; + return values_[addr_.front()]; } template -inline const T& Foam::IndirectListBase::last() const +inline const T& Foam::IndirectListBase::back() const { - return values_[addr_.last()]; + return values_[addr_.back()]; } template -inline T& Foam::IndirectListBase::last() +inline T& Foam::IndirectListBase::back() { - return values_[addr_.last()]; + return values_[addr_.back()]; } @@ -176,12 +176,8 @@ inline T& Foam::IndirectListBase::rcValue(const label i) return values_[this->rcIndex(i)]; } - -// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // - template -inline Foam::List -Foam::IndirectListBase::operator()() const +inline Foam::List Foam::IndirectListBase::list() const { const label len = addr_.size(); @@ -197,6 +193,8 @@ Foam::IndirectListBase::operator()() const } +// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // + template inline T& Foam::IndirectListBase::operator[](const label i) { diff --git a/src/OpenFOAM/containers/Lists/DynamicList/DynamicList.H b/src/OpenFOAM/containers/Lists/DynamicList/DynamicList.H index 57919c75bf..38b203c18e 100644 --- a/src/OpenFOAM/containers/Lists/DynamicList/DynamicList.H +++ b/src/OpenFOAM/containers/Lists/DynamicList/DynamicList.H @@ -178,7 +178,7 @@ public: //- Alter the size of the underlying storage. // The addressed size will be truncated if needed to fit, but will // remain otherwise untouched. - // Use this or reserve() in combination with append(). + // Use this or reserve() in combination with push_back(). inline void setCapacity(const label len); //- Alter the size of the underlying storage, @@ -245,47 +245,53 @@ public: // Edit - //- Swap content, independent of sizing parameter - template - inline void swap(DynamicList& other); + //- Swap content, independent of sizing parameter + template + inline void swap(DynamicList& other); - //- Transfer contents of the argument List into this. - inline void transfer(List& list); + //- Transfer contents of the argument List into this. + inline void transfer(List& list); - //- Transfer contents of any sized DynamicList into this. - template - inline void transfer(DynamicList& list); + //- Transfer contents of any sized DynamicList into this. + template + inline void transfer(DynamicList& list); - //- Copy append an element to the end of this list. - inline void append(const T& val); + //- Copy append an element to the end of this list. + inline void push_back(const T& val); - //- Move append an element - inline void append(T&& val); + //- Move append an element + inline void push_back(T&& val); - //- Append another list to the end of this list. - inline void append(const UList& lst); + //- Copy append another list to the end of this list. + inline void push_back(const UList& list); - //- Append a FixedList to the end of this list. - template - inline void append(const FixedList& lst); + //- Copy append a FixedList to the end of this list. + template + inline void push_back(const FixedList& list); - //- Append an initializer list at the end of this list. - inline void append(std::initializer_list lst); + //- Copy append an initializer list at the end of this list. + inline void push_back(std::initializer_list list); - //- Append a IndirectList at the end of this list - template - inline void append(const IndirectListBase& lst); + //- Copy append an IndirectList at the end of this list + template + inline void push_back(const IndirectListBase& lst); - //- Move append list - inline void append(List&& lst); + //- Move append list + inline void push_back(List&& list); - //- Move append list - template - inline void append(DynamicList&& list); + //- Move append list + template + inline void push_back(DynamicList&& list); - //- Append an element if not already in the list. - // \return the change in list length - inline label appendUniq(const T& val); + //- Append an element if not already in the list. + // \return the change in list length + inline label push_uniq(const T& val); + + //- Reduce size by 1 or more elements. Can be called on an empty list. + inline void pop_back(label n = 1); + + + // Edit //- Remove and return the last element. Fatal on an empty list. inline T remove(); @@ -381,6 +387,51 @@ public: Ostream& os, const DynamicList& list ); + + + // Housekeeping + + //- Copy append an element to the end of this list. + //FOAM_DEPRECATED_FOR(2022-10, "push_back()") + void append(const T& val) { this->push_back(val); } + + //- Move append an element + //FOAM_DEPRECATED_FOR(2022-10, "push_back()") + void append(T&& val) { this->push_back(std::move(val)); } + + //- Append another list to the end of this list. + void append(const UList& list) { this->push_back(list); } + + //- Append a FixedList to the end of this list. + //FOAM_DEPRECATED_FOR(2022-10, "push_back()") + template + void append(const FixedList& list) { this->push_back(list); } + + //- Append an initializer list at the end of this list. + //FOAM_DEPRECATED_FOR(2022-10, "push_back()") + void append(std::initializer_list list) { this->push_back(list); } + + //- Append a IndirectList at the end of this list + template + void append(const IndirectListBase& list) + { + this->push_back(list); + } + + //- Move append list + //FOAM_DEPRECATED_FOR(2022-10, "push_back()") + void append(List&& list) { this->push_back(std::move(list)); } + + //- Move append list + template + void append(DynamicList&& list) + { + this->push_back(std::move(list)); + } + + //- Append an element if not already in the list. + //FOAM_DEPRECATED_FOR(2022-10, "push_uniq()") + label appendUniq(const T& val) { return this->push_uniq(val); } }; diff --git a/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H b/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H index 610f7d69d8..2d2bc765b4 100644 --- a/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H +++ b/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H @@ -499,7 +499,7 @@ Foam::DynamicList::transfer template -inline void Foam::DynamicList::append +inline void Foam::DynamicList::push_back ( const T& val ) @@ -512,7 +512,7 @@ inline void Foam::DynamicList::append template -inline void Foam::DynamicList::append +inline void Foam::DynamicList::push_back ( T&& val ) @@ -525,7 +525,7 @@ inline void Foam::DynamicList::append template -inline void Foam::DynamicList::append +inline void Foam::DynamicList::push_back ( const UList& lst ) @@ -533,7 +533,7 @@ inline void Foam::DynamicList::append if (this == &lst) { FatalErrorInFunction - << "Attempted appending to self" + << "Attempted push_back to self" << abort(FatalError); } @@ -549,7 +549,7 @@ inline void Foam::DynamicList::append template template -inline void Foam::DynamicList::append +inline void Foam::DynamicList::push_back ( const FixedList& lst ) @@ -565,7 +565,7 @@ inline void Foam::DynamicList::append template -inline void Foam::DynamicList::append +inline void Foam::DynamicList::push_back ( std::initializer_list lst ) @@ -582,7 +582,7 @@ inline void Foam::DynamicList::append template template -inline void Foam::DynamicList::append +inline void Foam::DynamicList::push_back ( const IndirectListBase& lst ) @@ -600,7 +600,7 @@ inline void Foam::DynamicList::append template -inline void Foam::DynamicList::append +inline void Foam::DynamicList::push_back ( List&& list ) @@ -608,7 +608,7 @@ inline void Foam::DynamicList::append if (this == &list) { FatalErrorInFunction - << "Attempted appending to self" + << "Attempted push_back to self" << abort(FatalError); } @@ -626,18 +626,18 @@ inline void Foam::DynamicList::append template template -inline void Foam::DynamicList::append +inline void Foam::DynamicList::push_back ( DynamicList&& list ) { - append(std::move(static_cast&>(list))); + push_back(std::move(static_cast&>(list))); list.clearStorage(); // Ensure capacity=0 } template -inline Foam::label Foam::DynamicList::appendUniq(const T& val) +inline Foam::label Foam::DynamicList::push_uniq(const T& val) { if (this->found(val)) { @@ -645,12 +645,26 @@ inline Foam::label Foam::DynamicList::appendUniq(const T& val) } else { - this->append(val); + this->push_back(val); return 1; // Increased list length by one } } +template +inline void Foam::DynamicList::pop_back(label n) +{ + if (n >= this->size()) + { + clear(); + } + else if (n > 0) + { + resize(this->size() - n); + } +} + + template inline T Foam::DynamicList::remove() { diff --git a/src/OpenFOAM/containers/Lists/FixedList/FixedList.H b/src/OpenFOAM/containers/Lists/FixedList/FixedList.H index 17bf3a1e4d..0ff6b37164 100644 --- a/src/OpenFOAM/containers/Lists/FixedList/FixedList.H +++ b/src/OpenFOAM/containers/Lists/FixedList/FixedList.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2017-2021 OpenCFD Ltd. + Copyright (C) 2017-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -216,18 +216,17 @@ public: // \note Only meaningful for contiguous data inline char* data_bytes() noexcept; + //- Access first element of the list, position [0] + inline T& front() noexcept; - //- The first element of the list, position [0] - inline T& first() noexcept; + //- Access first element of the list, position [0] + inline const T& front() const noexcept; - //- The first element of the list, position [0] - inline const T& first() const noexcept; + //- Access last element of the list, position [N-1] + inline T& back() noexcept; - //- The last element of the list, position [N-1] - inline T& last() noexcept; - - //- The last element of the list, position [N-1] - inline const T& last() const noexcept; + //- Access last element of the list, position [N-1] + inline const T& back() const noexcept; //- Number of contiguous bytes for the list data, // \note Only meaningful for contiguous data @@ -501,6 +500,21 @@ public: { FOAM_DEPRECATED_FOR(2021-04, "hasher()") Hash() {} }; + + + // Housekeeping + + //- Access first element of the list, position [0] - front() + T& first() noexcept { return front(); } + + //- Access first element of the list, position [0] - front() + const T& first() const noexcept { return front(); } + + //- Access last element of the list, position [N-1] - back() + T& last() noexcept { return back(); } + + //- Access last element of the list, position [N-1] - back() + const T& last() const noexcept { return back(); } }; diff --git a/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H b/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H index b23bd86ba8..d6e290d18e 100644 --- a/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H +++ b/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H @@ -204,28 +204,28 @@ inline std::streamsize Foam::FixedList::size_bytes() noexcept template -inline T& Foam::FixedList::first() noexcept +inline T& Foam::FixedList::front() noexcept { return v_[0]; } template -inline const T& Foam::FixedList::first() const noexcept +inline const T& Foam::FixedList::front() const noexcept { return v_[0]; } template -inline T& Foam::FixedList::last() noexcept +inline T& Foam::FixedList::back() noexcept { return v_[N-1]; } template -inline const T& Foam::FixedList::last() const noexcept +inline const T& Foam::FixedList::back() const noexcept { return v_[N-1]; } diff --git a/src/OpenFOAM/containers/Lists/List/List.H b/src/OpenFOAM/containers/Lists/List/List.H index 8aa6711548..acd6a3e311 100644 --- a/src/OpenFOAM/containers/Lists/List/List.H +++ b/src/OpenFOAM/containers/Lists/List/List.H @@ -222,41 +222,47 @@ public: void setSize(const label n, const T& val) { this->resize(n, val); } - // Edit + // Edit - //- Append an element at the end of the list - // If this is frequently required, consider a DynamicList - inline void append(const T& val); + //- Transfer the contents of the argument List into this list + //- and annul the argument list + void transfer(List& list); - //- Move append an element at the end of the list - // If this is frequently required, consider a DynamicList - inline void append(T&& val); + //- Transfer the contents of the argument List into this list + //- and annul the argument list + template + void transfer(DynamicList& list); - //- Append a List to the end of this list - // If this is frequently required, consider a DynamicList - inline void append(const UList& list); + //- Return subscript-checked element of UList and resizing the list + //- if required. + inline T& newElmt(const label i); - //- Append IndirectList contents at the end of this list - // If this is frequently required, consider a DynamicList - template - inline void append(const IndirectListBase& list); - //- Append an element if not already in the list. - // \return the change in list length - inline label appendUniq(const T& val); + // Edit - //- Transfer the contents of the argument List into this list - //- and annul the argument list - void transfer(List& list); + //- Append an element at the end of the list + // If this is frequently required, consider a DynamicList + inline void push_back(const T& val); - //- Transfer the contents of the argument List into this list - //- and annul the argument list - template - void transfer(DynamicList& list); + //- Move append an element at the end of the list + // If this is frequently required, consider a DynamicList + inline void push_back(T&& val); - //- Return subscript-checked element of UList and resizing the list - //- if required. - inline T& newElmt(const label i); + //- Append a List to the end of this list + // If this is frequently required, consider a DynamicList + inline void push_back(const UList& list); + + //- Append IndirectList contents at the end of this list + // If this is frequently required, consider a DynamicList + template + inline void push_back(const IndirectListBase& list); + + //- Append an element if not already in the list. + // \return the change in list length + inline label push_uniq(const T& val); + + //- Reduce size by 1 or more elements. Can be called on an empty list. + inline void pop_back(label n = 1); // Member Operators @@ -346,6 +352,37 @@ public: (*this)[i] = val; return true; } + + + // Housekeeping + + //- Append an element at the end of the list + // If this is frequently required, consider a DynamicList + //FOAM_DEPRECATED_FOR(2022-10, "push_back()") + void append(const T& val) { this->push_back(val); } + + //- Move append an element at the end of the list + // If this is frequently required, consider a DynamicList + //FOAM_DEPRECATED_FOR(2022-10, "push_back()") + void append(T&& val) { this->push_back(std::move(val)); } + + //- Append a List to the end of this list + // If this is frequently required, consider a DynamicList + //FOAM_DEPRECATED_FOR(2022-10, "push_back()") + void append(const UList& list) { this->push_back(list); } + + //- Append IndirectList contents at the end of this list + // If this is frequently required, consider a DynamicList + //FOAM_DEPRECATED_FOR(2022-10, "push_back()") + template + void append(const IndirectListBase& list) + { + this->push_back(list); + } + + //- Append an element if not already in the list. + //FOAM_DEPRECATED_FOR(2022-10, "push_uniq()") + label appendUniq(const T& val) { return this->push_uniq(val); } }; diff --git a/src/OpenFOAM/containers/Lists/List/ListI.H b/src/OpenFOAM/containers/Lists/List/ListI.H index 4063c27bcf..c585e0b3e0 100644 --- a/src/OpenFOAM/containers/Lists/List/ListI.H +++ b/src/OpenFOAM/containers/Lists/List/ListI.H @@ -172,7 +172,7 @@ inline T& Foam::List::newElmt(const label i) template -inline void Foam::List::append(const T& val) +inline void Foam::List::push_back(const T& val) { const label idx = this->size(); resize(idx + 1); @@ -182,7 +182,7 @@ inline void Foam::List::append(const T& val) template -inline void Foam::List::append(T&& val) +inline void Foam::List::push_back(T&& val) { const label idx = this->size(); resize(idx + 1); @@ -192,12 +192,12 @@ inline void Foam::List::append(T&& val) template -inline void Foam::List::append(const UList& list) +inline void Foam::List::push_back(const UList& list) { if (this == &list) { FatalErrorInFunction - << "Attempted appending to self" << abort(FatalError); + << "Attempted push_back to self" << abort(FatalError); } label idx = this->size(); @@ -214,7 +214,7 @@ inline void Foam::List::append(const UList& list) template template -inline void Foam::List::append(const IndirectListBase& list) +inline void Foam::List::push_back(const IndirectListBase& list) { label idx = this->size(); const label n = list.size(); @@ -229,7 +229,7 @@ inline void Foam::List::append(const IndirectListBase& list) template -inline Foam::label Foam::List::appendUniq(const T& val) +inline Foam::label Foam::List::push_uniq(const T& val) { if (this->found(val)) { @@ -237,12 +237,26 @@ inline Foam::label Foam::List::appendUniq(const T& val) } else { - this->append(val); + this->push_back(val); return 1; // Increased list length by one } } +template +inline void Foam::List::pop_back(label n) +{ + if (n >= this->size()) + { + this->clear(); + } + else if (n > 0) + { + resize(this->size() - n); + } +} + + // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // template diff --git a/src/OpenFOAM/containers/Lists/List/UList.H b/src/OpenFOAM/containers/Lists/List/UList.H index 1587cdf809..fa511bc9d9 100644 --- a/src/OpenFOAM/containers/Lists/List/UList.H +++ b/src/OpenFOAM/containers/Lists/List/UList.H @@ -272,17 +272,17 @@ public: // \note Only meaningful for contiguous data inline char* data_bytes() noexcept; - //- Return the first element of the list - inline T& first(); + //- Access first element of the list, position [0] + inline T& front(); - //- Return first element of the list - inline const T& first() const; + //- Access first element of the list + inline const T& front() const; - //- Return the last element of the list - inline T& last(); + //- Access last element of the list, position [size()-1] + inline T& back(); - //- Return the last element of the list - inline const T& last() const; + //- Access last element of the list, position [size()-1] + inline const T& back() const; //- Number of contiguous bytes for the List data. // \note Only meaningful for contiguous data @@ -580,6 +580,25 @@ public: { FOAM_DEPRECATED_FOR(2021-04, "hasher()") Hash() {} }; + + + // Housekeeping + + //- Access first element of the list, position [0] + //FOAM_DEPRECATED_FOR(2022-10, "front()") + T& first() { return front(); } + + //- Access first element of the list + //FOAM_DEPRECATED_FOR(2022-10, "front()") + const T& first() const { return front(); }; + + //- Access last element of the list, position [size()-1] + //FOAM_DEPRECATED_FOR(2022-10, "back()") + T& last() { return back(); } + + //- Access last element of the list, position [size()-1] + //FOAM_DEPRECATED_FOR(2022-10, "back()") + const T& last() const { return back(); }; }; diff --git a/src/OpenFOAM/containers/Lists/List/UListI.H b/src/OpenFOAM/containers/Lists/List/UListI.H index 013a617a8b..4c91bbe336 100644 --- a/src/OpenFOAM/containers/Lists/List/UListI.H +++ b/src/OpenFOAM/containers/Lists/List/UListI.H @@ -199,28 +199,28 @@ inline bool Foam::UList::uniform() const template -inline T& Foam::UList::first() +inline T& Foam::UList::front() { return this->operator[](0); } template -inline const T& Foam::UList::first() const +inline const T& Foam::UList::front() const { return this->operator[](0); } template -inline T& Foam::UList::last() +inline T& Foam::UList::back() { return this->operator[](this->size()-1); } template -inline const T& Foam::UList::last() const +inline const T& Foam::UList::back() const { return this->operator[](this->size()-1); } diff --git a/src/OpenFOAM/containers/Lists/ListOps/ListOpsTemplates.C b/src/OpenFOAM/containers/Lists/ListOps/ListOpsTemplates.C index 9f990ec861..cb0c0db8ce 100644 --- a/src/OpenFOAM/containers/Lists/ListOps/ListOpsTemplates.C +++ b/src/OpenFOAM/containers/Lists/ListOps/ListOpsTemplates.C @@ -1097,7 +1097,7 @@ void Foam::ListOps::uniqueEqOp::operator() { if (!x.found(val)) { - x.append(val); + x.push_back(val); } } } diff --git a/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynList.H b/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynList.H index 51c445f45b..fac76f1846 100644 --- a/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynList.H +++ b/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynList.H @@ -143,33 +143,36 @@ public: template inline void swap(PtrDynList& other); - //- Construct and append an element to the end of the list + //- Construct an element at the end of the list template - inline void emplace_append(Args&&... args); + inline void emplace_back(Args&&... args); //- Append an element to the end of the list - inline void append(T* ptr); + inline void push_back(T* ptr); //- Move append an element to the end of the list - inline void append(std::unique_ptr&& ptr); + inline void push_back(std::unique_ptr&& ptr); //- Move append an element to the end of the list - inline void append(autoPtr&& ptr); + inline void push_back(autoPtr&& ptr); //- Move or clone append a tmp to the end of the list - inline void append(const refPtr& ptr); + inline void push_back(const refPtr& ptr); //- Move or clone append a tmp to the end of the list - inline void append(const tmp& ptr); + inline void push_back(const tmp& ptr); //- Move append another list to the end of this list. - inline void append(PtrList&& other); + inline void push_back(PtrList&& other); //- Move append another list to the end of this list. template - inline void append(PtrDynList&& other); + inline void push_back(PtrDynList&& other); - //- Remove and return the top element + //- 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 top element. Can be called on an empty list. inline autoPtr remove(); //- Construct and set an element @@ -225,17 +228,48 @@ public: // Housekeeping - //- Move append an element to the end of the list - void append(autoPtr& ptr) - { - this->append(std::move(ptr)); - } - //- Set element to given autoPtr and return old element autoPtr set(const label i, autoPtr& ptr) { return this->set(i, std::move(ptr)); } + + //- Move append an element to the end of the list + void append(autoPtr& ptr) { this->push_back(std::move(ptr)); } + + //- Append an element to the end of the list + //FOAM_DEPRECATED_FOR(2022-10, "push_back()") + void append(T* ptr) { this->push_back(ptr); } + + //- Move append an element to the end of the list + //FOAM_DEPRECATED_FOR(2022-10, "push_back()") + void append(std::unique_ptr&& ptr) + { + this->push_back(std::move(ptr)); + } + + //- Move append an element to the end of the list + //FOAM_DEPRECATED_FOR(2022-10, "push_back()") + void append(autoPtr&& ptr) { this->push_back(std::move(ptr)); } + + //- Move or clone append a tmp to the end of the list + //FOAM_DEPRECATED_FOR(2022-10, "push_back()") + void append(const refPtr& ptr) { this->push_back(ptr); } + + //- Move or clone append a tmp to the end of the list + //FOAM_DEPRECATED_FOR(2022-10, "push_back()") + void append(const tmp& ptr) { this->push_back(ptr); } + + //- Move append another list to the end of this list. + //FOAM_DEPRECATED_FOR(2022-10, "push_back()") + void append(PtrList&& other) { this->push_back(std::move(other)); } + + //- Move append another list to the end of this list. + template + void append(PtrDynList&& other) + { + this->push_back(other); + } }; diff --git a/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynListI.H b/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynListI.H index 11d5c2034d..f33a1e2f16 100644 --- a/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynListI.H +++ b/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynListI.H @@ -225,14 +225,14 @@ inline void Foam::PtrDynList::swap template template -inline void Foam::PtrDynList::emplace_append(Args&&... args) +inline void Foam::PtrDynList::emplace_back(Args&&... args) { - this->append(new T(std::forward(args)...)); + this->push_back(new T(std::forward(args)...)); } template -inline void Foam::PtrDynList::append(T* ptr) +inline void Foam::PtrDynList::push_back(T* ptr) { const label idx = this->size(); resize(idx + 1); @@ -241,35 +241,35 @@ inline void Foam::PtrDynList::append(T* ptr) template -inline void Foam::PtrDynList::append(std::unique_ptr&& ptr) +inline void Foam::PtrDynList::push_back(std::unique_ptr&& ptr) { - this->append(ptr.release()); + this->push_back(ptr.release()); } template -inline void Foam::PtrDynList::append(autoPtr&& ptr) +inline void Foam::PtrDynList::push_back(autoPtr&& ptr) { - this->append(ptr.release()); + this->push_back(ptr.release()); } template -inline void Foam::PtrDynList::append(const refPtr& ptr) +inline void Foam::PtrDynList::push_back(const refPtr& ptr) { - this->append(ptr.ptr()); // release or clone + this->push_back(ptr.ptr()); // release or clone } template -inline void Foam::PtrDynList::append(const tmp& ptr) +inline void Foam::PtrDynList::push_back(const tmp& ptr) { - this->append(ptr.ptr()); // release or clone + this->push_back(ptr.ptr()); // release or clone } template -inline void Foam::PtrDynList::append(PtrList&& other) +inline void Foam::PtrDynList::push_back(PtrList&& other) { const label idx = this->size(); const label len = other.size(); @@ -287,7 +287,7 @@ inline void Foam::PtrDynList::append(PtrList&& other) template template -inline void Foam::PtrDynList::append +inline void Foam::PtrDynList::push_back ( PtrDynList&& other ) @@ -299,7 +299,7 @@ inline void Foam::PtrDynList::append ) { FatalErrorInFunction - << "Attempted append to self" + << "Attempted push_back to self" << abort(FatalError); } @@ -317,6 +317,20 @@ inline void Foam::PtrDynList::append } +template +inline void Foam::PtrDynList::pop_back(label n) +{ + if (n >= this->size()) + { + this->clear(); + } + else if (n > 0) + { + this->resize(this->size() - n); + } +} + + template inline Foam::autoPtr Foam::PtrDynList::remove() { diff --git a/src/OpenFOAM/containers/PtrLists/PtrList/PtrList.H b/src/OpenFOAM/containers/PtrLists/PtrList/PtrList.H index 8997c05b68..1c0963a0b4 100644 --- a/src/OpenFOAM/containers/PtrLists/PtrList/PtrList.H +++ b/src/OpenFOAM/containers/PtrLists/PtrList/PtrList.H @@ -153,25 +153,25 @@ public: //- Construct and append an element to the end of the list template - inline void emplace_append(Args&&... args); + inline void emplace_back(Args&&... args); //- Append an element to the end of the list - inline void append(T* ptr); + inline void push_back(T* ptr); //- Move append an element to the end of the list - inline void append(std::unique_ptr&& ptr); + inline void push_back(std::unique_ptr&& ptr); //- Move append an element to the end of the list - inline void append(autoPtr&& ptr); + inline void push_back(autoPtr&& ptr); //- Move or clone append a refPtr to the end of the list - inline void append(const refPtr& ptr); + inline void push_back(const refPtr& ptr); //- Move or clone append a tmp to the end of the list - inline void append(const tmp& ptr); + inline void push_back(const tmp& ptr); //- Move append another list to the end of this list. - inline void append(PtrList&& other); + inline void push_back(PtrList&& other); //- Construct and set an element template @@ -220,17 +220,41 @@ public: // Housekeeping - //- Move append an element to the end of the list - void append(autoPtr& ptr) - { - this->append(std::move(ptr)); - } - //- Set element to given autoPtr and return old element autoPtr set(const label i, autoPtr& ptr) { return this->set(i, std::move(ptr)); } + + //- Move append an element to the end of the list + void append(autoPtr& ptr) { this->push_back(std::move(ptr)); } + + //- Append an element to the end of the list + //FOAM_DEPRECATED_FOR(2022-10, "push_back()") + void append(T* ptr) { this->push_back(ptr); } + + //- Move append an element to the end of the list + //FOAM_DEPRECATED_FOR(2022-10, "push_back()") + void append(std::unique_ptr&& ptr) + { + this->push_back(std::move(ptr)); + } + + //- Move append an element to the end of the list + //FOAM_DEPRECATED_FOR(2022-10, "push_back()") + void append(autoPtr&& ptr) { this->push_back(std::move(ptr)); } + + //- Move or clone append a tmp to the end of the list + //FOAM_DEPRECATED_FOR(2022-10, "push_back()") + void append(const refPtr& ptr) { this->push_back(ptr); } + + //- Move or clone append a tmp to the end of the list + //FOAM_DEPRECATED_FOR(2022-10, "push_back()") + void append(const tmp& ptr) { this->push_back(ptr); } + + //- Move append another list to the end of this list. + //FOAM_DEPRECATED_FOR(2022-10, "push_back()") + void append(PtrList&& other) { this->push_back(std::move(other)); } }; diff --git a/src/OpenFOAM/containers/PtrLists/PtrList/PtrListI.H b/src/OpenFOAM/containers/PtrLists/PtrList/PtrListI.H index fdfa4305d5..3985ccbc08 100644 --- a/src/OpenFOAM/containers/PtrLists/PtrList/PtrListI.H +++ b/src/OpenFOAM/containers/PtrLists/PtrList/PtrListI.H @@ -103,54 +103,54 @@ inline void Foam::PtrList::clear() template template -inline void Foam::PtrList::emplace_append(Args&&... args) +inline void Foam::PtrList::emplace_back(Args&&... args) { - UPtrList::append(new T(std::forward(args)...)); + UPtrList::push_back(new T(std::forward(args)...)); } template -inline void Foam::PtrList::append(T* ptr) +inline void Foam::PtrList::push_back(T* ptr) { - UPtrList::append(ptr); + UPtrList::push_back(ptr); } template -inline void Foam::PtrList::append(std::unique_ptr&& ptr) +inline void Foam::PtrList::push_back(std::unique_ptr&& ptr) { - UPtrList::append(ptr.release()); + UPtrList::push_back(ptr.release()); } template -inline void Foam::PtrList::append(autoPtr&& ptr) +inline void Foam::PtrList::push_back(autoPtr&& ptr) { - UPtrList::append(ptr.release()); + UPtrList::push_back(ptr.release()); } template -inline void Foam::PtrList::append(const refPtr& ptr) +inline void Foam::PtrList::push_back(const refPtr& ptr) { - UPtrList::append(ptr.ptr()); // release or clone + UPtrList::push_back(ptr.ptr()); // release or clone } template -inline void Foam::PtrList::append(const tmp& ptr) +inline void Foam::PtrList::push_back(const tmp& ptr) { - UPtrList::append(ptr.ptr()); // release or clone + UPtrList::push_back(ptr.ptr()); // release or clone } template -inline void Foam::PtrList::append(PtrList&& other) +inline void Foam::PtrList::push_back(PtrList&& other) { if (this == &other) { FatalErrorInFunction - << "Attempted append to self" + << "Attempted push_back to self" << abort(FatalError); } diff --git a/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrList.H b/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrList.H index c1d5d0d2df..88bf25242c 100644 --- a/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrList.H +++ b/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrList.H @@ -217,17 +217,17 @@ public: //- True if the list is empty (ie, size() is zero) inline bool empty() const noexcept; - //- Return reference to the first element of the list - inline T& first(); + //- Reference to the first element of the list + inline T& front(); - //- Return reference to first element of the list - inline const T& first() const; + //- Reference to first element of the list + inline const T& front() const; - //- Return reference to the last element of the list - inline T& last(); + //- Reference to the last element of the list + inline T& back(); - //- Return reference to the last element of the list - inline const T& last() const; + //- Reference to the last element of the list + inline const T& back() const; //- Return const pointer to element (can be nullptr), //- or nullptr for out-of-range access (ie, \em with bounds checking). @@ -267,10 +267,10 @@ public: label squeezeNull(); //- Append an element to the end of the list - inline void append(T* ptr); + inline void push_back(T* ptr); //- Move append another list to the end of this list. - inline void append(UPtrList&& other); + inline void push_back(UPtrList&& other); //- Swap content inline void swap(UPtrList& list); @@ -474,6 +474,33 @@ public: Ostream& os, const UPtrList& list ); + + + // Housekeeping + + //- Reference to the first element of the list + //FOAM_DEPRECATED_FOR(2022-10, "front()") + T& first() { return front(); } + + //- Return reference to first element of the list + //FOAM_DEPRECATED_FOR(2022-10, "front()") + const T& first() const { return front(); } + + //- Return reference to the last element of the list + //FOAM_DEPRECATED_FOR(2022-10, "back()") + T& last() { return back(); } + + //- Return reference to the last element of the list + //FOAM_DEPRECATED_FOR(2022-10, "back()") + const T& last() const{ return back(); } + + //- Append an element to the end of the list + //FOAM_DEPRECATED_FOR(2022-10, "push_back()") + void append(T* ptr) { this->push_back(ptr); } + + //- Move append another list to the end of this list. + //FOAM_DEPRECATED_FOR(2022-10, "push_back()") + void append(UPtrList&& other) { this->push_back(std::move(other)); } }; diff --git a/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrListI.H b/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrListI.H index ddb563e06c..781b26cc0d 100644 --- a/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrListI.H +++ b/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrListI.H @@ -179,28 +179,28 @@ inline void Foam::UPtrList::transfer(UPtrList& list) template -inline T& Foam::UPtrList::first() +inline T& Foam::UPtrList::front() { return this->operator[](0); } template -inline const T& Foam::UPtrList::first() const +inline const T& Foam::UPtrList::front() const { return this->operator[](0); } template -inline T& Foam::UPtrList::last() +inline T& Foam::UPtrList::back() { return this->operator[](this->size()-1); } template -inline const T& Foam::UPtrList::last() const +inline const T& Foam::UPtrList::back() const { return this->operator[](this->size()-1); } @@ -214,16 +214,16 @@ inline void Foam::UPtrList::resize(const label newLen) template -inline void Foam::UPtrList::append(T* ptr) +inline void Foam::UPtrList::push_back(T* ptr) { - ptrs_.append(ptr); + ptrs_.push_back(ptr); } template -inline void Foam::UPtrList::append(UPtrList&& other) +inline void Foam::UPtrList::push_back(UPtrList&& other) { - ptrs_.append(other.ptrs_); + ptrs_.push_back(other.ptrs_); other.ptrs_.clear(); } diff --git a/src/OpenFOAM/fields/Fields/DynamicField/DynamicField.H b/src/OpenFOAM/fields/Fields/DynamicField/DynamicField.H index 1ae483354c..8f10988ccf 100644 --- a/src/OpenFOAM/fields/Fields/DynamicField/DynamicField.H +++ b/src/OpenFOAM/fields/Fields/DynamicField/DynamicField.H @@ -196,7 +196,7 @@ public: //- Alter the size of the underlying storage. // The addressed size will be truncated if needed to fit, but will // remain otherwise untouched. - // Use this or reserve() in combination with append(). + // Use this or reserve() in combination with push_back(). inline void setCapacity(const label len); //- Alter the size of the underlying storage, @@ -283,15 +283,18 @@ public: inline void transfer(DynamicField& list); //- Append an element at the end of the list - inline void append(const T& val); + inline void push_back(const T& val); //- Move append an element - inline void append(T&& val); + inline void push_back(T&& val); //- Append a List at the end of this list - inline void append(const UList& list); + inline void push_back(const UList& list); - //- Remove and return the top element + //- 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(); @@ -349,6 +352,21 @@ public: Ostream& os, const DynamicField& rhs ); + + + // Housekeeping + + //- Append an element at the end of the list + //FOAM_DEPRECATED_FOR(2022-10, "push_back()") + void append(const T& val) { this->push_back(val); } + + //- Move append an element + //FOAM_DEPRECATED_FOR(2022-10, "push_back()") + void append(T&& val) { this->push_back(std::move(val)); } + + //- Append a List at the end of this list + //FOAM_DEPRECATED_FOR(2022-10, "push_back()") + void append(const UList& list) { this->push_back(list); } }; diff --git a/src/OpenFOAM/fields/Fields/DynamicField/DynamicFieldI.H b/src/OpenFOAM/fields/Fields/DynamicField/DynamicFieldI.H index 24fc97d075..e64fe817ee 100644 --- a/src/OpenFOAM/fields/Fields/DynamicField/DynamicFieldI.H +++ b/src/OpenFOAM/fields/Fields/DynamicField/DynamicFieldI.H @@ -591,7 +591,7 @@ inline void Foam::DynamicField::transfer template -inline void Foam::DynamicField::append +inline void Foam::DynamicField::push_back ( const T& val ) @@ -604,7 +604,7 @@ inline void Foam::DynamicField::append template -inline void Foam::DynamicField::append +inline void Foam::DynamicField::push_back ( T&& val ) @@ -617,7 +617,7 @@ inline void Foam::DynamicField::append template -inline void Foam::DynamicField::append +inline void Foam::DynamicField::push_back ( const UList& list ) @@ -625,7 +625,7 @@ inline void Foam::DynamicField::append if (this == &list) { FatalErrorInFunction - << "Attempted appending to self" + << "Attempted push_back to self" << abort(FatalError); } @@ -639,6 +639,20 @@ inline void Foam::DynamicField::append } +template +inline void Foam::DynamicField::pop_back(label n) +{ + if (n >= this->size()) + { + this->clear(); + } + else if (n > 0) + { + resize(this->size() - n); + } +} + + template inline T Foam::DynamicField::remove() { diff --git a/src/OpenFOAM/meshes/meshShapes/hexCell/hexCell.H b/src/OpenFOAM/meshes/meshShapes/hexCell/hexCell.H index 07817fa8ea..5765b7c6c4 100644 --- a/src/OpenFOAM/meshes/meshShapes/hexCell/hexCell.H +++ b/src/OpenFOAM/meshes/meshShapes/hexCell/hexCell.H @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2021 OpenCFD Ltd. + Copyright (C) 2021-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -79,6 +79,15 @@ class hexCell public: + // Generated Methods + + //- The front() accessor (from FixedList) has no purpose + void front() = delete; + + //- The back() accessor (from FixedList) has no purpose + void back() = delete; + + // Constructors //- Default construct, with invalid point labels (-1) diff --git a/src/OpenFOAM/meshes/meshShapes/tetCell/tetCell.H b/src/OpenFOAM/meshes/meshShapes/tetCell/tetCell.H index 427fc8bddd..694e9e2bd2 100644 --- a/src/OpenFOAM/meshes/meshShapes/tetCell/tetCell.H +++ b/src/OpenFOAM/meshes/meshShapes/tetCell/tetCell.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation - Copyright (C) 2019-2021 OpenCFD Ltd. + Copyright (C) 2019-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -77,6 +77,15 @@ class tetCell public: + // Generated Methods + + //- The front() accessor (from FixedList) has no purpose + void front() = delete; + + //- The back() accessor (from FixedList) has no purpose + void back() = delete; + + // Constructors //- Default construct, with invalid point labels (-1) diff --git a/src/OpenFOAM/meshes/meshShapes/triFace/triFace.H b/src/OpenFOAM/meshes/meshShapes/triFace/triFace.H index 957808e845..94d3f1b128 100644 --- a/src/OpenFOAM/meshes/meshShapes/triFace/triFace.H +++ b/src/OpenFOAM/meshes/meshShapes/triFace/triFace.H @@ -73,6 +73,15 @@ class triFace { public: + // Generated Methods + + //- The front() accessor (from FixedList) has no purpose + void front() = delete; + + //- The back() accessor (from FixedList) has no purpose + void back() = delete; + + // Constructors //- Default construct, with invalid point labels (-1) diff --git a/src/OpenFOAM/meshes/primitiveShapes/tetrahedron/tetrahedron.H b/src/OpenFOAM/meshes/primitiveShapes/tetrahedron/tetrahedron.H index c578722a53..f0f10e25db 100644 --- a/src/OpenFOAM/meshes/primitiveShapes/tetrahedron/tetrahedron.H +++ b/src/OpenFOAM/meshes/primitiveShapes/tetrahedron/tetrahedron.H @@ -91,6 +91,12 @@ public: //- Default construct tetPoints() = default; + //- The front() accessor (from FixedList) has no purpose + void front() = delete; + + //- The back() accessor (from FixedList) has no purpose + void back() = delete; + // Constructors diff --git a/src/OpenFOAM/meshes/primitiveShapes/triangle/triangle.H b/src/OpenFOAM/meshes/primitiveShapes/triangle/triangle.H index 4a05407d63..f571c896e1 100644 --- a/src/OpenFOAM/meshes/primitiveShapes/triangle/triangle.H +++ b/src/OpenFOAM/meshes/primitiveShapes/triangle/triangle.H @@ -85,6 +85,12 @@ public: //- Default construct triPoints() = default; + //- The front() accessor (from FixedList) has no purpose + void front() = delete; + + //- The back() accessor (from FixedList) has no purpose + void back() = delete; + // Constructors diff --git a/src/OpenFOAM/primitives/Vector/bools/boolVector.H b/src/OpenFOAM/primitives/Vector/bools/boolVector.H index 1275b782cd..237c17df42 100644 --- a/src/OpenFOAM/primitives/Vector/bools/boolVector.H +++ b/src/OpenFOAM/primitives/Vector/bools/boolVector.H @@ -83,6 +83,12 @@ public: //- Move assignment boolVector& operator=(boolVector&&) = default; + //- The front() accessor (from FixedList) has no purpose + void front() = delete; + + //- The back() accessor (from FixedList) has no purpose + void back() = delete; + // Constructors diff --git a/src/OpenFOAM/primitives/enums/Enum.C b/src/OpenFOAM/primitives/enums/Enum.C index 136171884b..503e3d499c 100644 --- a/src/OpenFOAM/primitives/enums/Enum.C +++ b/src/OpenFOAM/primitives/enums/Enum.C @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2017-2020 OpenCFD Ltd. + Copyright (C) 2017-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -52,7 +52,7 @@ Foam::Enum::Enum // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // template -void Foam::Enum::append +void Foam::Enum::push_back ( std::initializer_list> list ) diff --git a/src/OpenFOAM/primitives/enums/Enum.H b/src/OpenFOAM/primitives/enums/Enum.H index 8321f612c7..7427062310 100644 --- a/src/OpenFOAM/primitives/enums/Enum.H +++ b/src/OpenFOAM/primitives/enums/Enum.H @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2017-2021 OpenCFD Ltd. + Copyright (C) 2017-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -37,8 +37,8 @@ SourceFiles \*---------------------------------------------------------------------------*/ -#ifndef Enum_H -#define Enum_H +#ifndef Foam_Enum_H +#define Foam_Enum_H #include "wordList.H" #include @@ -131,7 +131,7 @@ public: //- Append value/key pairs to the lists of known enumerations // Does not check for duplicate entries - void append + void push_back ( std::initializer_list> list ); @@ -351,6 +351,16 @@ public: { return get(key, dict); } + + //- Append value/key pairs to the lists of known enumerations + // Does not check for duplicate entries + void append + ( + std::initializer_list> list + ) + { + push_back(list); + } }; diff --git a/src/OpenFOAM/primitives/strings/lists/hashedWordList.H b/src/OpenFOAM/primitives/strings/lists/hashedWordList.H index bf0553a9a3..1e2cc0c27a 100644 --- a/src/OpenFOAM/primitives/strings/lists/hashedWordList.H +++ b/src/OpenFOAM/primitives/strings/lists/hashedWordList.H @@ -104,13 +104,9 @@ public: //- Clear the list, i.e. set size to zero. inline void clear(); - //- Append an element if not already in the list. - FOAM_DEPRECATED_FOR(2022-05, "appendUniq method") - inline void append(const word& val); - //- Append an element if not already in the list. // \return the change in list length - inline label appendUniq(const word& val); + inline label push_uniq(const word& val); //- Return the hash of words/indices for inspection inline const HashTable