From cc5f30f25e6a8cb188f8d036ce0431d2898b4ba8 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Mon, 27 Nov 2017 14:11:25 +0100 Subject: [PATCH] ENH: several improvements for linked-lists - support move construct/assignment for linked-lists themselves and when moving into a 'normal' list - better consistency with begin/end signatures and the various iterators. - for indirect linked-lists, provide iterator access to the underlying data element address: iter.get() vs &(iter()) - add standard '->' indirection for iterators (as per normal STL definitions) --- applications/test/DLList/Test-DLList.C | 192 ++++- applications/test/ISLList/Test-ISLList.C | 70 +- applications/test/List3/Make/files | 3 + applications/test/List3/Make/options | 0 applications/test/List3/Test-List3.C | 155 ++++ applications/test/List3/list1 | 8 + applications/test/PtrList/Test-PtrList.C | 32 + applications/test/SLList/Test-SLList.C | 109 ++- applications/test/UList/Make/files | 3 + applications/test/UList/Make/options | 0 applications/test/UList/Test-UList.C | 119 ++++ .../HashTables/HashPtrTable/HashPtrTable.H | 6 +- .../HashTables/HashPtrTable/HashPtrTableIO.C | 32 +- .../HashTables/HashTable/HashTableIO.C | 18 +- .../LinkedLists/accessTypes/ILList/ILList.C | 79 +-- .../LinkedLists/accessTypes/ILList/ILList.H | 69 +- .../LinkedLists/accessTypes/ILList/ILListIO.C | 56 +- .../LinkedLists/accessTypes/LList/LList.C | 28 +- .../LinkedLists/accessTypes/LList/LList.H | 660 ++++++++++++------ .../LinkedLists/accessTypes/LList/LListIO.C | 92 ++- .../accessTypes/LPtrList/LPtrList.C | 42 +- .../accessTypes/LPtrList/LPtrList.H | 376 +++++++--- .../accessTypes/LPtrList/LPtrListIO.C | 69 +- .../LinkedLists/accessTypes/UILList/UILList.C | 33 +- .../LinkedLists/accessTypes/UILList/UILList.H | 473 +++++++------ .../accessTypes/UILList/UILListIO.C | 63 +- .../linkTypes/DLListBase/DLListBase.C | 228 +++--- .../linkTypes/DLListBase/DLListBase.H | 313 +++++---- .../linkTypes/DLListBase/DLListBaseI.H | 476 ++++++------- .../linkTypes/SLListBase/SLListBase.C | 77 +- .../linkTypes/SLListBase/SLListBase.H | 234 ++++--- .../linkTypes/SLListBase/SLListBaseI.H | 302 ++++---- .../containers/LinkedLists/user/FIFOStack.H | 57 +- .../containers/LinkedLists/user/LIFOStack.H | 57 +- .../containers/LinkedLists/user/SLList.H | 12 +- .../containers/LinkedLists/user/SLListFwd.H | 52 ++ .../containers/LinkedLists/user/SLPtrList.H | 13 +- .../LinkedLists/user/SLPtrListFwd.H | 52 ++ .../containers/Lists/FixedList/FixedList.H | 11 +- .../containers/Lists/FixedList/FixedListIO.C | 42 +- src/OpenFOAM/containers/Lists/List/List.C | 31 +- src/OpenFOAM/containers/Lists/List/List.H | 17 +- src/OpenFOAM/containers/Lists/List/ListIO.C | 77 +- .../containers/Lists/PackedList/PackedList.C | 38 +- .../containers/Lists/PtrList/PtrList.C | 12 +- .../containers/Lists/PtrList/PtrList.H | 33 +- .../containers/Lists/PtrList/PtrListI.H | 6 +- .../containers/Lists/PtrList/PtrListIO.C | 73 +- .../Lists/UIndirectList/UIndirectListIO.C | 28 +- src/OpenFOAM/containers/Lists/UList/UList.C | 4 +- src/OpenFOAM/containers/Lists/UList/UListIO.C | 144 ++-- .../containers/Lists/UPtrList/UPtrListIO.C | 10 +- .../GeometricField/GeometricBoundaryField.C | 4 +- 53 files changed, 3147 insertions(+), 2043 deletions(-) create mode 100644 applications/test/List3/Make/files create mode 100644 applications/test/List3/Make/options create mode 100644 applications/test/List3/Test-List3.C create mode 100644 applications/test/List3/list1 create mode 100644 applications/test/UList/Make/files create mode 100644 applications/test/UList/Make/options create mode 100644 applications/test/UList/Test-UList.C create mode 100644 src/OpenFOAM/containers/LinkedLists/user/SLListFwd.H create mode 100644 src/OpenFOAM/containers/LinkedLists/user/SLPtrListFwd.H diff --git a/applications/test/DLList/Test-DLList.C b/applications/test/DLList/Test-DLList.C index 268c8b3ed1..7f6bcf0993 100644 --- a/applications/test/DLList/Test-DLList.C +++ b/applications/test/DLList/Test-DLList.C @@ -28,20 +28,39 @@ Description \*---------------------------------------------------------------------------*/ #include "OSspecific.H" - #include "IOstreams.H" #include "DLList.H" +#include "List.H" +#include "FlatOutput.H" +#include "ListOps.H" using namespace Foam; +template +void printAddress(const UList& list) +{ + Info<< "list addr: " << long(&list) + << " data addr: " << long(list.cdata()) << nl; +} + + +template +void printAddresses(const DLList>& sll) +{ + for (const auto& elem : sll) + { + printAddress(elem); + } +} + + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // Main program: int main(int argc, char *argv[]) { - DLList myList; - - Info<< "DLList" << nl; + DLList myList{2.1, 3.4}; + myList = {2.1, 3.4, 4.3}; for (int i = 0; i<10; i++) { @@ -51,58 +70,165 @@ int main(int argc, char *argv[]) myList.append(100.3); myList.append(500.3); - forAllConstIters(myList, iter) + Info<< "DLList" << nl; + Info<< nl << "flat-output: " << flatOutput(myList) << nl; + + Info<< nl << "range-for:" << nl; + for (const auto& val : myList) { - Info<< "element:" << *iter << endl; + Info<< " " << val << nl; } - Info<< nl << "And again using the same STL iterator: " << nl << endl; + Info<< nl << "const_iterator:" << nl; + + forAllConstIters(myList, iter) + { + Info<< " " << *iter << endl; + } + + // Test bi-directional movement + { + const label n2 = myList.size()/2; + + Info<< nl << "test movement through " << flatOutput(myList) << nl; + + DLList::const_iterator citer = myList.begin(); + + for (label i=0; i::iterator iter = myList.begin(); + ++iter; + ++iter; + ++iter; + + Info<<" now with " << *iter << nl; + myList.remove(iter); + Info<<" after remove " << *iter << nl; + ++iter; + Info<<" after incr " << *iter << nl; + --iter; + --iter; + + Info<<" after 2x decr " << *iter << nl; + } + + Info<< nl << "const_reverse_iterator:" << nl; + + forAllConstReverseIters(myList, iter) + { + Info<< " " << *iter << endl; + } + + + Info<< nl << "Remove elements:" << nl; forAllIters(myList, iter) { - Info<< "Removing " << myList.remove(iter) << endl; + Info<< " remove " << *iter; + myList.remove(iter); + + Info<< " => " << flatOutput(myList) << nl; } myList.append(500.3); myList.append(200.3); myList.append(100.3); - Info<< nl << "Using range-based for: " << nl << endl; - for (auto val : myList) + Info<< nl << "Testing swapUp and swapDown:" << nl; + Info<< " => " << flatOutput(myList) << nl; + { - Info<< "element:" << val << endl; + myList.swapUp(myList.DLListBase::first()); + myList.swapUp(myList.DLListBase::last()); + + Info<< nl << "swapUp => " << flatOutput(myList) << nl; } - Info<< nl << "Testing swapUp and swapDown: " << endl; - - Info<< nl << "swapUp" << endl; - - myList.swapUp(myList.DLListBase::first()); - myList.swapUp(myList.DLListBase::last()); - - for (auto val : myList) { - Info<< "element:" << val << endl; + myList.swapDown(myList.DLListBase::first()); + myList.swapDown(myList.DLListBase::last()); + + Info<< nl << "swapDown => " << flatOutput(myList) << nl; } - Info<< nl << "swapDown" << endl; - myList.swapDown(myList.DLListBase::first()); - myList.swapDown(myList.DLListBase::last()); - - for (auto val : myList) - { - Info<< "element:" << val << endl; - } - - Info<< nl << "Testing transfer: " << nl << nl - << "original: " << myList << endl; + Info<< nl << "Transfer: " << nl; + Info<< "original: " << flatOutput(myList) << endl; DLList newList; newList.transfer(myList); - Info<< nl << "source: " << myList << nl - << nl << "target: " << newList << endl; + Info<< nl + << "source: " << flatOutput(myList) << nl + << "target: " << flatOutput(newList) << nl; + + + Info<< nl << "Move Construct: " << nl; + + DLList list2(std::move(newList)); + + Info<< nl + << "in : " << flatOutput(newList) << nl + << "out: " << flatOutput(list2) << nl; + + // Move back + Info<< nl << "Move Assignment: " << nl; + + newList = std::move(list2); + + Info<< nl + << "in : " << flatOutput(newList) << nl + << "out: " << flatOutput(list2) << nl; + + // Try delete data recovery + { + DLList> labList; + + for (int i = 0; i<5; i++) + { + labList.append(identity(6)); + } + + Info<< nl + << "DLList : " << labList << nl; + + printAddresses(labList); + + auto elem = labList.removeHead(); + + Info<< " removed head" << nl; + printAddress(elem); + + elem = labList.removeHead(); + + Info<< " removed head" << nl; + printAddress(elem); + + List