Commit cc5f30f2 authored by Mark Olesen's avatar Mark Olesen
Browse files

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)
parent 3a4b92c4
......@@ -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<class T>
void printAddress(const UList<T>& list)
{
Info<< "list addr: " << long(&list)
<< " data addr: " << long(list.cdata()) << nl;
}
template<class T>
void printAddresses(const DLList<List<T>>& sll)
{
for (const auto& elem : sll)
{
printAddress(elem);
}
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
DLList<scalar> myList;
Info<< "DLList<scalar>" << nl;
DLList<scalar> 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);
Info<< "DLList<scalar>" << nl;
Info<< nl << "flat-output: " << flatOutput(myList) << nl;
Info<< nl << "range-for:" << nl;
for (const auto& val : myList)
{
Info<< " " << val << nl;
}
Info<< nl << "const_iterator:" << nl;
forAllConstIters(myList, iter)
{
Info<< "element:" << *iter << endl;
Info<< " " << *iter << endl;
}
// Test bi-directional movement
{
const label n2 = myList.size()/2;
Info<< nl << "test movement through " << flatOutput(myList) << nl;
DLList<scalar>::const_iterator citer = myList.begin();
for (label i=0; i<n2; ++i)
{
Info<< " forward " << i << " " << *citer << nl;
++citer;
}
for (label i=0; i<n2; ++i)
{
Info<< " backward " << i << " " << *citer << nl;
--citer;
}
// Verify - does not compile since it uses a delete method (good!)
DLList<scalar>::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 << "And again using the same STL iterator: " << nl << endl;
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;
{
myList.swapDown(myList.DLListBase::first());
myList.swapDown(myList.DLListBase::last());
Info<< nl << "swapUp" << endl;
Info<< nl << "swapDown => " << flatOutput(myList) << nl;
}
myList.swapUp(myList.DLListBase::first());
myList.swapUp(myList.DLListBase::last());
for (auto val : myList)
{
Info<< "element:" << val << endl;
}
Info<< nl << "Transfer: " << nl;
Info<< "original: " << flatOutput(myList) << endl;
DLList<scalar> newList;
newList.transfer(myList);
Info<< nl
<< "source: " << flatOutput(myList) << nl
<< "target: " << flatOutput(newList) << nl;
Info<< nl << "Move Construct: " << nl;
DLList<scalar> list2(std::move(newList));
Info<< nl
<< "in : " << flatOutput(newList) << nl
<< "out: " << flatOutput(list2) << nl;
Info<< nl << "swapDown" << endl;
// Move back
Info<< nl << "Move Assignment: " << nl;
myList.swapDown(myList.DLListBase::first());
myList.swapDown(myList.DLListBase::last());
newList = std::move(list2);
for (auto val : myList)
Info<< nl
<< "in : " << flatOutput(newList) << nl
<< "out: " << flatOutput(list2) << nl;
// Try delete data recovery
{
Info<< "element:" << val << endl;
}
DLList<List<label>> labList;
Info<< nl << "Testing transfer: " << nl << nl
<< "original: " << myList << endl;
for (int i = 0; i<5; i++)
{
labList.append(identity(6));
}
DLList<scalar> newList;
newList.transfer(myList);
Info<< nl
<< "DLList<labelList> : " << labList << nl;
printAddresses(labList);
auto elem = labList.removeHead();
Info<< " removed head" << nl;
printAddress(elem);
Info<< nl << "source: " << myList << nl
<< nl << "target: " << newList << endl;
elem = labList.removeHead();
Info<< " removed head" << nl;
printAddress(elem);
List<label> content1 = identity(10);
Info<< nl
<< " move append ";
printAddress(content1);
labList.append(std::move(content1));
Info<< " content " << flatOutput(content1) << nl
<< " list" << labList << nl;
printAddresses(labList);
// labList.append(content1);
}
Info<< nl << "Done." << endl;
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -28,9 +28,12 @@ Description
\*---------------------------------------------------------------------------*/
#include "OSspecific.H"
#include "IOstreams.H"
#include "ISLList.H"
#include "List.H"
#include "FlatOutput.H"
#include "ListOps.H"
#include "OSspecific.H"
using namespace Foam;
......@@ -66,7 +69,7 @@ public:
int main(int argc, char *argv[])
{
ISLList<Scalar> myList;
ISLList<Scalar> myList(new Scalar(0));
for (int i = 0; i<10; i++)
{
......@@ -76,31 +79,74 @@ int main(int argc, char *argv[])
myList.append(new Scalar(100.3));
myList.append(new Scalar(500.3));
Info<< nl << "And again using STL iterator: " << nl << endl;
Info<< "ISLList<scalar>" << myList << nl;
Info<< nl << "flat-output: " << flatOutput(myList) << nl;
forAllIters(myList, iter)
Info<< nl << "range-for:" << nl;
for (const auto& val : myList)
{
Info<< "element:" << *iter << endl;
Info<< " " << val << nl;
// Info<<" is " << typeid(val).name() << endl;
}
Info<< nl << "And again using STL const_iterator: " << nl << endl;
Info<< nl << "const_iterator:" << nl;
const ISLList<Scalar>& const_myList = myList;
forAllConstIters(const_myList, iter)
{
Info<< "element:" << *iter << endl;
Info<< " " << *iter << endl;
}
{
Info<< nl << "Remove element:" << nl;
Info<< nl << "Testing transfer: " << nl << endl;
Info<< "original: " << myList << endl;
Scalar *iter = myList.removeHead();
Info<< " remove " << *iter;
Info<< " => " << flatOutput(myList) << nl;
delete iter;
}
Info<< nl << "Transfer: " << nl;
Info<< "original: " << flatOutput(myList) << endl;
ISLList<Scalar> newList;
newList.transfer(myList);
Info<< nl << "source: " << myList << nl
<< nl << "target: " << newList << endl;
Info<< nl
<< "source: " << flatOutput(myList) << nl
<< "target: " << flatOutput(newList) << endl;
myList.swap(newList);
Info<< nl << "swap: " << nl;
Info<< nl
<< "source: " << flatOutput(myList) << nl
<< "target: " << flatOutput(newList) << endl;
myList.swap(newList);
Info<< nl << "Move Construct: " << nl;
ISLList<Scalar> 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 << "move assign: " << nl;
Info<< nl
<< "source: " << flatOutput(list2) << nl
<< "target: " << flatOutput(newList) << endl;
Info<< nl << "Bye." << endl;
return 0;
......
Test-List3.C
EXE = $(FOAM_USER_APPBIN)/Test-List3
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2017 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Application
Test-List3
Description
Test list construction
\*---------------------------------------------------------------------------*/
#include "argList.H"
#include "FixedList.H"
#include "labelList.H"
#include "vectorList.H"
#include "ListOps.H"
#include "IFstream.H"
#include "OFstream.H"
#include "cpuTime.H"
#include <initializer_list>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
using namespace Foam;
template<class T>
void printAddress(const UList<T>& list)
{
Info<< "list addr: " << long(&list)
<< " data addr: " << long(list.cdata()) << nl;
}
template<class T>
void printAddress(const SLList<T>& list)
{
Info<< "list addr: " << long(&list)
<< " data addr: ???" << nl;
}
template<class T>
void printAddresses(const List<List<T>>& list)
{
for (const auto& elem : list)
{
printAddress(elem);
}
}
template<class T>
void printAddresses(const SLList<List<T>>& list)
{
for (const auto& elem : list)
{
printAddress(elem);
}
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
int main(int argc, char *argv[])
{
argList::addBoolOption("labelListList");
argList args(argc, argv, false);
if (args.options().empty())
{
Info<< nl << "Specify an option! " << nl << endl;
}
if (args.optionFound("labelListList"))
{
for (label argi=1; argi < args.size(); ++argi)
{
if (true)
{
IFstream is(args[argi]);
Info<< nl << nl
<< "read from " << is.name() << nl << endl;
SLList<List<label>> sll(is);
Info<< "read " << sll.size() << " entries" << nl;
Info<< "sll" << nl;
for (const auto& elem : sll)
{
printAddress(elem);
}
// List<List<label>> list(std::move(sll));
List<List<label>> list;
Info<< "move to List" << nl;
list = std::move(sll);
Info<< "sll" << nl;
for (const auto& elem : sll)
{
printAddress(elem);
}
Info<< "list" << nl;
printAddresses(list);
}
if (true)
{
IFstream is(args[argi]);
Info<< nl << nl
<< "read from " << is.name() << nl << endl;
List<List<label>> list(is);
Info<< "list" << nl;
for (const auto& elem : list)
{
printAddress(elem);
}
}
}
}
Info<< nl << "Done" << nl << endl;
return 0;
}
// ************************************************************************* //
// List of labelList
(
(1 2 3 4)
(5 6 7 8)
(15 16 17 18)
)
......@@ -32,6 +32,8 @@ Description
#include "scalar.H"
#include "IOstreams.H"
#include "PtrList.H"
#include "DLPtrList.H"
#include "SLPtrList.H"
#include "plane.H"
#include "DynamicList.H"
......@@ -80,6 +82,36 @@ int main(int argc, char *argv[])
PtrList<Scalar> list2(15);
PtrList<Scalar> listApp;
{
DLPtrList<Scalar> llist1;
llist1.insert(new Scalar(100));
llist1.insert(new Scalar(200));
llist1.insert(new Scalar(300));
DLPtrList<Scalar>::const_iterator citer = llist1.begin();
Info<< *citer << endl;
Info<< typeid(*citer).name() << endl;
++citer;
++citer;
--citer;
Info<< typeid(llist1.begin()).name() << endl;
forAllIters(llist1, it)
{
Info<< typeid(*it).name() << endl;
Info<< "reversed: " << *it << endl;
}
for (const auto& it : llist1)
{
Info<< typeid(it).name() << endl;
Info<< "for-: " << it << endl;
}
}
forAll(list1, i)
{
list1.set(i, new Scalar(1.3*i));
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -28,12 +28,32 @@ Description
\*---------------------------------------------------------------------------*/
#include "OSspecific.H"
#include "IOstreams.H"
#include "SLList.H"
#include "List.H"
#include "FlatOutput.H"
#include "ListOps.H"
using namespace Foam;
template<class T>
void printAddress(const UList<T>& list)
{
Info<< "list addr: " << long(&list)