diff --git a/applications/test/CompactListList/Test-CompactListList.C b/applications/test/CompactListList/Test-CompactListList.C index 265bb7646ffb413d078943df55b9722caa0500c5..587fa9df8208a8e3d279109f814e3ae6fc00b98b 100644 --- a/applications/test/CompactListList/Test-CompactListList.C +++ b/applications/test/CompactListList/Test-CompactListList.C @@ -47,8 +47,8 @@ int main(int argc, char *argv[]) Info<< "cll1:" << cll1 << endl; // Resize and assign row by row - labelList row0(2, 0); - labelList row1(3, 1); + labelList row0(2, label(0)); + labelList row1(3, label(1)); labelList rowSizes(2); rowSizes[0] = row0.size(); @@ -140,8 +140,8 @@ int main(int argc, char *argv[]) { faceList fcs(2); - fcs[0] = face(labelList(1, 111)); - fcs[1] = face(labelList(2, 222)); + fcs[0] = face(labelList(1, label(111))); + fcs[1] = face(labelList(2, label(222))); CompactListList<label, face> compactFcs(fcs); Info<< "comactFcs:" << compactFcs << endl; diff --git a/applications/test/DLList/Test-DLList.C b/applications/test/DLList/Test-DLList.C index 268c8b3ed1e0c89c1291a7862b2ecec511f4e826..7f6bcf09931e29a825b6071bd8e422b67c0c9110 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<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; diff --git a/applications/test/FixedList/Test-FixedList.C b/applications/test/FixedList/Test-FixedList.C index c501b77bc4cf70cd10b8eec4a14f1907653d7a25..1e3703b2b818dc426358a684bff021aa0228087f 100644 --- a/applications/test/FixedList/Test-FixedList.C +++ b/applications/test/FixedList/Test-FixedList.C @@ -25,7 +25,7 @@ Application Test-FixedList Description - Simple tests and examples of use of FixedList + Simple tests and examples for FixedList See also Foam::FixedList @@ -42,47 +42,72 @@ See also using namespace Foam; -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -// Main program: +template<class T, unsigned Size> +Ostream& printInfo(const FixedList<List<T>, Size>& list) +{ + Info<< list << " addresses:"; + for (unsigned i = 0; i < Size; ++i) + { + Info<< " " << long(list[i].cdata()); + } + Info<< nl; + return Info; +} -int main(int argc, char *argv[]) + +template<class T, unsigned Size> +Ostream& printInfo +( + const FixedList<List<T>, Size>& list1, + const FixedList<List<T>, Size>& list2 +) { - argList args(argc, argv); + Info<< "llist1:"; printInfo(list1); + Info<< "llist2:"; printInfo(list2); + return Info; +} - if (false) - { - FixedList<string, 1> ident; - auto iter = ident.begin(); +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +// Main program: - Info << iter->size() << endl; +int main(int argc, char *argv[]) +{ + argList::addBoolOption("assign"); + argList::addBoolOption("iter"); + argList::addBoolOption("swap"); + argList::addBoolOption("default", "reinstate default tests"); + argList::addNote("runs default tests or specified ones only"); - auto riter = ident.rbegin(); - Info << riter->size() << endl; + #include "setRootCase.H" - auto iter2 = ident.rbegin(); + // Run default tests, unless only specific tests are requested + const bool defaultTests = + args.found("default") || args.options().empty(); - iter2 = iter; - } + if (defaultTests || args.found("iter")) { + Info<< nl + << "Test iterators" << nl; + FixedList<label, 15> ident; std::iota(ident.begin(), ident.end(), 0); // auto iter = ident.begin(); // // iter += 5; - // Info << *iter << "< " << endl; + // Info << *iter << "< " << nl; // iter -= 2; - // Info << *iter << "< " << endl; + // Info << *iter << "< " << nl; // Don't yet bother with making reverse iterators random access // auto riter = ident.crbegin(); // riter += 5; - // Info << *riter << "< " << endl; + // Info << *riter << "< " << nl; // riter += 2; - // Info << *riter << "< " << endl; + // Info << *riter << "< " << nl; Info<<"Ident:"; forAllConstIters(ident, iter) @@ -106,65 +131,121 @@ int main(int argc, char *argv[]) Info<< nl; } + if (defaultTests || args.found("swap")) { - FixedList<label, 4> list1{1, 2, 3, 4}; + Info<< nl + << "Test swap" << nl; + + FixedList<label, 4> list1{2, 3, 4, 5}; Info<< "list1:" << list1 - << " hash:" << FixedList<label, 4>::Hash<>()(list1) << endl; + << " hash:" << FixedList<label, 4>::Hash<>()(list1) << nl; label a[4] = {0, 1, 2, 3}; FixedList<label, 4> list2(a); Info<< "list2:" << list2 - << " hash:" << FixedList<label, 4>::Hash<>()(list2) << endl; + << " hash:" << FixedList<label, 4>::Hash<>()(list2) << nl; // Using FixedList for content too { List<FixedList<label, 4>> twolists{list1, list2}; - Info<<"List of FixedList: " << flatOutput(twolists) << endl; + Info<<"List of FixedList: " << flatOutput(twolists) << nl; sort(twolists); // outer-sort only - Info<<"sorted FixedList : " << flatOutput(twolists) << endl; + Info<<"sorted FixedList : " << flatOutput(twolists) << nl; } Info<< "====" << nl << "Test swap" << nl; Info<< "list1: " << list1 << nl - << "list2: " << list2 << endl; + << "list2: " << list2 << nl; + + // Addresses don't change with swap + Info<< "mem: " << long(list1.data()) << " " << long(list2.data()) << nl; + list1.swap(list2); - Info<< "The swap() method" << endl; + Info<< "The swap() method" << nl; Info<< "list1: " << list1 << nl - << "list2: " << list2 << endl; + << "list2: " << list2 << nl; + + Info<< "mem: " << long(list1.data()) << " " << long(list2.data()) << nl; Swap(list1, list2); - Info<< "The Swap() function" << endl; + Info<< "The Swap() function" << nl; Info<< "list1: " << list1 << nl - << "list2: " << list2 << endl; + << "list2: " << list2 << nl; + + Info<< "mem: " << long(list1.data()) << " " << long(list2.data()) << nl; Info<< "====" << nl; + + + Info<< nl + << "Test of swap with other container content" << nl; + + FixedList<labelList, 4> llist1; + FixedList<labelList, 4> llist2; + + { + label i = 1; + for (auto& item : llist1) + { + item = identity(1 + 1.5*i); + ++i; + } + } + + Info<< nl + << "initial lists" << nl; + printInfo(llist1, llist2); + + llist2.transfer(llist1); + Info<< nl + << "After transfer" << nl; + printInfo(llist1, llist2); + + llist2.swap(llist1); + Info<< nl + << "After swap" << nl; + printInfo(llist1, llist2); + + llist2 = llist1; + Info<< nl + << "After copy assignment" << nl; + printInfo(llist1, llist2); + + llist2 = std::move(llist1); + Info<< nl + << "After move assignment" << nl; + printInfo(llist1, llist2); } + Info<< nl + << "Test construct and assignment" << nl; + + List<label> list3{0, 1, 2, 3}; FixedList<label, 4> list4(list3.begin(), list3.end()); Info<< "list3: " << list3 << nl - << "list4: " << list4 << endl; + << "list4: " << list4 << nl; list4 = {1, 2, 3, 5}; Info<< "list4: " << list4 << nl; FixedList<label, 5> list5{0, 1, 2, 3, 4}; - Info<< "list5: " << list5 << endl; + Info<< "list5: " << list5 << nl; List<FixedList<label, 2>> list6{{0, 1}, {2, 3}}; - Info<< "list6: " << list6 << endl; + Info<< "list6: " << list6 << nl; if (Pstream::parRun()) { if (Pstream::myProcNo() != Pstream::masterNo()) { Serr<< "slave sending to master " - << Pstream::masterNo() << endl; + << Pstream::masterNo() << nl; OPstream toMaster ( diff --git a/applications/test/HashTable1/Test-HashTable1.C b/applications/test/HashTable1/Test-HashTable1.C index 82dd7151d65fabc68110e220ecb6d0462e69191f..26171deee04c37a2ecae21d689a286bc3662ac3b 100644 --- a/applications/test/HashTable1/Test-HashTable1.C +++ b/applications/test/HashTable1/Test-HashTable1.C @@ -98,11 +98,12 @@ int main() } - HashTable<scalar> table2(table1); - HashTable<scalar> table3(std::move(table1)); + HashTable<scalar> table2(table1); // Copy + HashTable<scalar> table3(std::move(table1)); // Move - Info<< "\ncopy table1 -> table2" << nl - << "transfer table1 -> table3 via the xfer() method" << nl; + Info<< nl + << "copy table1 -> table2" << nl + << "move table1 -> table3" << nl; Info<< "\ntable1" << table1 << nl << "\ntable2" << table2 << nl diff --git a/applications/test/ISLList/Test-ISLList.C b/applications/test/ISLList/Test-ISLList.C index 38ba2a1424deb19a04328179c1f14bce4c8f375e..12626f33cedb9ec72c122e52586d2dc97401605d 100644 --- a/applications/test/ISLList/Test-ISLList.C +++ b/applications/test/ISLList/Test-ISLList.C @@ -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; diff --git a/applications/test/List3/Make/files b/applications/test/List3/Make/files new file mode 100644 index 0000000000000000000000000000000000000000..640cc113d780dace56186ebe58fb2bbfda648bf0 --- /dev/null +++ b/applications/test/List3/Make/files @@ -0,0 +1,3 @@ +Test-List3.C + +EXE = $(FOAM_USER_APPBIN)/Test-List3 diff --git a/applications/test/xfer/Make/options b/applications/test/List3/Make/options similarity index 100% rename from applications/test/xfer/Make/options rename to applications/test/List3/Make/options diff --git a/applications/test/List3/Test-List3.C b/applications/test/List3/Test-List3.C new file mode 100644 index 0000000000000000000000000000000000000000..21b8b8407d540d1d58d8bc8d4ede7e938bb665a8 --- /dev/null +++ b/applications/test/List3/Test-List3.C @@ -0,0 +1,155 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / 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; +} + + +// ************************************************************************* // diff --git a/applications/test/List3/list1 b/applications/test/List3/list1 new file mode 100644 index 0000000000000000000000000000000000000000..39777e581512971aebe5dd6510014ecdadf4978e --- /dev/null +++ b/applications/test/List3/list1 @@ -0,0 +1,8 @@ +// List of labelList + +( + (1 2 3 4) + (5 6 7 8) + (15 16 17 18) +) + diff --git a/applications/test/PackedList4/Test-PackedList4.C b/applications/test/PackedList4/Test-PackedList4.C index e50eee04f4d6dae8bcf77141a36cdc7280f4be71..cb94feb1b9f22fb548036769324ada10a59dc2f3 100644 --- a/applications/test/PackedList4/Test-PackedList4.C +++ b/applications/test/PackedList4/Test-PackedList4.C @@ -144,18 +144,18 @@ int main(int argc, char *argv[]) Info<< "\ntest Istream constructor\n"; list4.printInfo(Info, true); - Info<< list4 << " indices: " << list4.used()() << nl; + Info<< list4 << " indices: " << list4.used() << nl; Info<< "\nassign from labelList\n"; list4 = labelList{0, 1, 2, 3, 12, 13, 14, 19, 20, 21}; list4.printInfo(Info, true); - Info<< list4 << " indices: " << list4.used()() << nl; + Info<< list4 << " indices: " << list4.used() << nl; // Not yet: // PackedBoolList list5{0, 1, 2, 3, 12, 13, 14, 19, 20, 21}; // list5.printInfo(Info, true); - // Info<< list5 << " indices: " << list5.used()() << nl; + // Info<< list5 << " indices: " << list5.used() << nl; Info<< "\nassign from indices\n"; list4.read @@ -168,7 +168,7 @@ int main(int argc, char *argv[]) list4.printInfo(Info, true); - Info<< list4 << " indices: " << list4.used()() << nl; + Info<< list4 << " indices: " << list4.used() << nl; boolList bools(list4.size()); forAll(list4, i) diff --git a/applications/test/PtrList/Test-PtrList.C b/applications/test/PtrList/Test-PtrList.C index 664dd5b501ef35dca4b3bbd063eb080b9eaf3431..043cfe5c37645f7cc80586b539cd7654d06db70b 100644 --- a/applications/test/PtrList/Test-PtrList.C +++ b/applications/test/PtrList/Test-PtrList.C @@ -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) 2018 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -21,10 +21,8 @@ License You should have received a copy of the GNU General Public License along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>. -Application - Description - + Test behaviour of UPtrList, PtrList \*---------------------------------------------------------------------------*/ #include "OSspecific.H" @@ -32,6 +30,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 +80,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() << nl + << "reversed: " << *it << endl; + } + for (const auto& it : llist1) + { + Info<< typeid(it).name() << nl + << "for-: " << it << endl; + } + } + forAll(list1, i) { list1.set(i, new Scalar(1.3*i)); @@ -95,9 +125,10 @@ int main(int argc, char *argv[]) listApp.append(new Scalar(1.3*i)); } - Info<<"list1: " << list1 << endl; - Info<<"list2: " << list2 << endl; - Info<<"listApp: " << listApp << endl; + Info<< nl + <<"list1: " << list1 << nl + <<"list2: " << list2 << nl + <<"list-appended: " << listApp << endl; Info<<"indirectly delete some items via set(.., 0) :" << endl; for (label i = 0; i < 3; i++) @@ -108,8 +139,8 @@ int main(int argc, char *argv[]) Info<<"transfer list2 -> list1:" << endl; list1.transfer(list2); - Info<<"list1: " << list1 << endl; - Info<<"list2: " << list2 << endl; + Info<<"list1: " << list1 << nl + <<"list2: " << list2 << endl; Info<<"indirectly delete some items via setSize :" << endl; list1.setSize(4); @@ -119,16 +150,71 @@ int main(int argc, char *argv[]) PtrList<Scalar> list3(list1.xfer()); Info<< "Transferred via the xfer() method" << endl; - Info<<"list1: " << list1 << endl; - Info<<"list2: " << list2 << endl; - Info<<"list3: " << list3 << endl; + Info<<"list1: " << list1 << nl + <<"list2: " << list2 << nl + <<"list3: " << list3 << endl; + + + Info<<"Move construct:" << endl; + + PtrList<Scalar> list4(std::move(list3)); + + Info<<"list3: " << list3 << nl + <<"list4: " << list4 << endl; + + Info<<"Move assign:" << endl; + list3 = std::move(list4); + + Info<<"list3: " << list3 << nl + <<"list4: " << list4 << endl; + + + Info<<"UPtrList from PtrList" << nl; + + UPtrList<Scalar> ulist1(list3); + + Info<<"ulist1: " << ulist1 << nl; + + Info<<"Move construct:" << endl; + + UPtrList<Scalar> ulist2(std::move(ulist1)); + + Info<<"ulist1: " << ulist1 << nl + <<"ulist2: " << ulist2 << nl; + + Info<<"Copy assign:" << endl; + ulist1 = ulist2; + + Info<<"ulist1: " << ulist1 << nl + <<"ulist2: " << ulist2 << nl; + + Info<<"Move assign:" << endl; + ulist1 = std::move(ulist2); + + Info<<"ulist1: " << ulist1 << nl + <<"ulist2: " << ulist2 << nl; + + // Test iterator random access + { + auto iter1 = ulist1.begin(); + auto iter2 = iter1 + 3; + + Info<<"begin:" << *iter1 << " (+3):" << *iter2 << nl; + Info<< "diff= " << (iter1 - iter2) << nl; + Info<< "iter[2]=" << iter1[2] << nl; + Info<< "iter1 < iter2 : " << (iter1 < iter2) << nl; + Info<< "iter1 >= iter2 : " << (iter1 >= iter2) << nl; + } PtrList<plane> planes; planes.append(new plane(vector::one, vector::one)); planes.append(new plane(vector(1,2,3), vector::one)); - forAll(planes, p) - Info<< "plane " << planes[p] << endl; + Info<< nl << "appended values" << nl; + for (const plane& p : planes) + { + Info<< " plane " << p << endl; + } Info<< nl << "Done." << endl; return 0; diff --git a/applications/test/SLList/Test-SLList.C b/applications/test/SLList/Test-SLList.C index 2a6ef7a245bf5c93313b53ab7e7fddfe06cb290e..7b6bb749bdf4b4ce572ea82f220216a68897e7f3 100644 --- a/applications/test/SLList/Test-SLList.C +++ b/applications/test/SLList/Test-SLList.C @@ -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) + << " data addr: " << long(list.cdata()) << nl; +} + + +template<class T> +void printAddresses(const SLList<List<T>>& sll) +{ + for (const auto& elem : sll) + { + printAddress(elem); + } +} + + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // Main program: @@ -50,34 +70,34 @@ int main(int argc, char *argv[]) myList.append(100.3); myList.append(500.3); - Info<< nl << "And again using STL iterator: " << nl << endl; + Info<< "SLList<scalar>" << myList << nl; + Info<< nl << "flat-output: " << flatOutput(myList) << nl; + Info<< nl << "range-for:" << nl; for (const auto& val : myList) { - Info<< "element:" << val << endl; + Info<< " " << val << nl; } - Info<< nl << "And again using STL const_iterator: " << nl << endl; + Info<< nl << "const_iterator:" << nl; const SLList<scalar>& const_myList = myList; forAllConstIters(const_myList, iter) { - Info<< "element:" << *iter << endl; + Info<< " " << *iter << endl; } + Info<< nl << "Remove elements:" << nl; + forAllIters(myList, iter) { - Info<< "Removing element:" << *iter << endl; + Info<< " remove " << *iter; myList.remove(iter); - } - for (const auto& val : const_myList) - { - Info<< "element:" << val << endl; + Info<< " => " << flatOutput(myList) << nl; } - for (int i = 0; i<10; i++) { myList.append(1.3*i); @@ -86,15 +106,72 @@ int main(int argc, char *argv[]) myList.append(100.3); myList.append(500.3); - Info<< nl << "Testing transfer: " << nl << endl; - Info<< "original: " << myList << endl; + Info<< nl << "Transfer: " << nl; + Info<< "original: " << flatOutput(myList) << endl; SLList<scalar> 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; + + SLList<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 + << "in : " << flatOutput(newList) << nl + << "out: " << flatOutput(list2) << nl; + + + // Try delete data recovery + { + SLList<List<label>> labList; + + for (int i = 0; i<5; i++) + { + labList.append(identity(6)); + } + + Info<< nl + << "SLList<labelList> : " << labList << nl; + + printAddresses(labList); + + auto elem = labList.removeHead(); + + Info<< " removed head" << nl; + printAddress(elem); + + 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; return 0; diff --git a/applications/test/UList/Make/files b/applications/test/UList/Make/files new file mode 100644 index 0000000000000000000000000000000000000000..0e82a10baef74d062221948224b3fd677c5474b0 --- /dev/null +++ b/applications/test/UList/Make/files @@ -0,0 +1,3 @@ +Test-UList.C + +EXE = $(FOAM_USER_APPBIN)/Test-UList diff --git a/applications/test/UList/Make/options b/applications/test/UList/Make/options new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/applications/test/UList/Test-UList.C b/applications/test/UList/Test-UList.C new file mode 100644 index 0000000000000000000000000000000000000000..9f928b05dd598054616a78bac1861f5c6e81928b --- /dev/null +++ b/applications/test/UList/Test-UList.C @@ -0,0 +1,119 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / 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-UList + +Description + Simple tests for UList constructors + +See also + Foam::List + +\*---------------------------------------------------------------------------*/ + +#include "OSspecific.H" +#include "IOstreams.H" +#include "StringStream.H" + +#include "labelList.H" +#include "ListOps.H" +#include "SubList.H" +#include "FlatOutput.H" + +using namespace Foam; + +template<class ListType> +void print(const ListType& list) +{ + Info << flatOutput(list) << " data addr: " << long(list.cdata()) << nl; +} + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +// Main program: + +int main(int argc, char *argv[]) +{ + List<label> source = identity(7); + List<label> other = identity(7); + + // Text for reading as a SLList" + string inputSLList("(10 20 30 40 50 60 70)"); + + // Text for reading as a SLList" + string inputCompound("List<label> (-1 -2 -3 -4 -5 -6 -7)"); + + reverse(other); + + UList<label> ulist(source.data(), source.size()); + + Info<<"source: "; print(source); + Info<<"other: "; print(other); + Info<<"UList: "; print(ulist); + + { + Info<<"shallow copy" << nl; + ulist.shallowCopy(other); + + Info<<"source: "; print(source); + Info<<"other: "; print(other); + Info<<"UList: "; print(ulist); + } + + { + Info<<"deep copy" << nl; + ulist.deepCopy(source); + + Info<<"source: "; print(source); + Info<<"other: "; print(other); + Info<<"UList: "; print(ulist); + } + + { + Info<<"Read from " << inputSLList << nl; + + IStringStream is(inputSLList); + is >> ulist; + +// Info<<"source: "; print(source); +// Info<<"other: "; print(other); + Info<<"UList: "; print(ulist); + } + + + { + Info<<"Read from " << inputCompound << nl; + + IStringStream is(inputCompound); + is >> ulist; + +// Info<<"source: "; print(source); +// Info<<"other: "; print(other); + Info<<"UList: "; print(ulist); + } + + return 0; +} + +// ************************************************************************* // diff --git a/applications/test/fileName/Test-fileName.C b/applications/test/fileName/Test-fileName.C index 3863a698f88791b2e2ded42cae951c2a02adfa50..42b3fe4d7a22071d253f5944cff3fde9abf6d0c1 100644 --- a/applications/test/fileName/Test-fileName.C +++ b/applications/test/fileName/Test-fileName.C @@ -194,6 +194,7 @@ int main(int argc, char *argv[]) argList::addBoolOption("ext", "test handing of file extensions"); argList::addBoolOption("construct", "test constructors"); argList::addBoolOption("relative", "test relative operations"); + argList::addBoolOption("system", "test filesystem operations"); argList::addBoolOption("default", "reinstate default tests"); argList::addNote("runs default tests or specified ones only"); @@ -485,72 +486,8 @@ int main(int argc, char *argv[]) } - if (!defaultTests) - { - return 0; - } - - DynamicList<word> wrdList - { - "hello", - "hello1", - "hello2", - "hello3", - "hello4.hmm" - }; - - fileName pathName(wrdList); - - Info<< "pathName = " << pathName << nl - << "pathName.name() = >" << pathName.name() << "<\n" - << "pathName.path() = " << pathName.path() << nl - << "pathName.ext() = >" << pathName.ext() << "<\n" - << "pathName.name(true) = >" << pathName.name(true) << "<\n"; - - Info<< "pathName.components() = " << pathName.components() << nl - << "pathName.component(2) = " << pathName.component(2) << nl - << endl; - - // try with different combination - // The final one should emit warnings - for (label start = 0; start <= wrdList.size(); ++start) - { - fileName instance, local; - word name; - - fileName path(SubList<word>(wrdList, wrdList.size()-start, start)); - fileName path2 = "."/path; - - IOobject::fileNameComponents - ( - path, - instance, - local, - name - ); - - Info<< "IOobject::fileNameComponents for " << path << nl - << " instance = " << instance << nl - << " local = " << local << nl - << " name = " << name << endl; - - IOobject::fileNameComponents - ( - path2, - instance, - local, - name - ); - - Info<< "IOobject::fileNameComponents for " << path2 << nl - << " instance = " << instance << nl - << " local = " << local << nl - << " name = " << name << endl; - - } - - // Test some copying and deletion + if (args.found("system")) { const fileName dirA("dirA"); const fileName lnA("lnA"); @@ -663,6 +600,71 @@ int main(int argc, char *argv[]) } + if (!defaultTests) + { + return 0; + } + + DynamicList<word> wrdList + { + "hello", + "hello1", + "hello2", + "hello3", + "hello4.hmm" + }; + + fileName pathName(wrdList); + + Info<< "pathName = " << pathName << nl + << "pathName.name() = >" << pathName.name() << "<\n" + << "pathName.path() = " << pathName.path() << nl + << "pathName.ext() = >" << pathName.ext() << "<\n" + << "pathName.name(true) = >" << pathName.name(true) << "<\n"; + + Info<< "pathName.components() = " << pathName.components() << nl + << "pathName.component(2) = " << pathName.component(2) << nl + << endl; + + // try with different combination + // The final one should emit warnings + for (label start = 0; start <= wrdList.size(); ++start) + { + fileName instance, local; + word name; + + fileName path(SubList<word>(wrdList, wrdList.size()-start, start)); + fileName path2 = "."/path; + + IOobject::fileNameComponents + ( + path, + instance, + local, + name + ); + + Info<< "IOobject::fileNameComponents for " << path << nl + << " instance = " << instance << nl + << " local = " << local << nl + << " name = " << name << endl; + + IOobject::fileNameComponents + ( + path2, + instance, + local, + name + ); + + Info<< "IOobject::fileNameComponents for " << path2 << nl + << " instance = " << instance << nl + << " local = " << local << nl + << " name = " << name << endl; + + } + + // test findEtcFile Info<< "\n\nfindEtcFile tests:" << nl << " controlDict => " << findEtcFile("controlDict") << nl diff --git a/applications/test/fileNameClean/Test-fileNameClean.C b/applications/test/fileNameClean/Test-fileNameClean.C index 47e3572894d08105f7472316602a789daa7e75d6..e87636cc7d7e0a7d28153534daabbb138ccbee27 100644 --- a/applications/test/fileNameClean/Test-fileNameClean.C +++ b/applications/test/fileNameClean/Test-fileNameClean.C @@ -46,6 +46,9 @@ void printCleaning(fileName& pathName) << " name() = " << pathName.name() << nl << " joined = " << pathName.path()/pathName.name() << nl << nl; + Info<< "components = " << flatOutput(pathName.components()) << nl; + Info<< "component 2 = " << pathName.component(2) << nl; + pathName.clean(); Info<< "cleaned = " << pathName << nl diff --git a/applications/test/hashedWordList/Make/files b/applications/test/hashedWordList/Make/files new file mode 100644 index 0000000000000000000000000000000000000000..e92564415ee4cb6cae5bb9f29541b72fbde4b0ca --- /dev/null +++ b/applications/test/hashedWordList/Make/files @@ -0,0 +1,3 @@ +Test-hashedWordList.C + +EXE = $(FOAM_USER_APPBIN)/Test-hashedWordList diff --git a/applications/test/hashedWordList/Make/options b/applications/test/hashedWordList/Make/options new file mode 100644 index 0000000000000000000000000000000000000000..6a9e9810b3d5ce6684bdaf03143933480ff45e42 --- /dev/null +++ b/applications/test/hashedWordList/Make/options @@ -0,0 +1,2 @@ +/* EXE_INC = -I$(LIB_SRC)/cfdTools/include */ +/* EXE_LIBS = -lfiniteVolume */ diff --git a/applications/test/hashedWordList/Test-hashedWordList.C b/applications/test/hashedWordList/Test-hashedWordList.C new file mode 100644 index 0000000000000000000000000000000000000000..e7126e6a8edc1ca7e2d853bae82d80843757a130 --- /dev/null +++ b/applications/test/hashedWordList/Test-hashedWordList.C @@ -0,0 +1,181 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2018 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/>. + +Description + +\*---------------------------------------------------------------------------*/ + +#include "IOstreams.H" +#include "ITstream.H" +#include "FlatOutput.H" +#include "hashedWordList.H" + +using namespace Foam; + +Ostream& printInfo(const hashedWordList& list, bool withAddr=false) +{ + Info<< flatOutput(list) << nl << list.lookup() << nl; + if (withAddr) + { + Info<< "addr=" << long(list.cdata()) << nl; + } + + return Info; +} + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +// Main program: + +int main(int argc, char *argv[]) +{ + Info<< "Test hashedWordList" << nl; + + hashedWordList list1 + { + "this", + "is", + "a", + "list", + "of", + "words", + }; + + Info<<nl << "From initializer_list" << nl; + printInfo(list1, true); + + list1.sort(); + + Info<<nl << "sorted" << nl; + printInfo(list1, true); + + // Copy construct + hashedWordList list2(list1); + + Info<<nl << "Copy construct" << nl; + Info<<"list1: "; + printInfo(list1, true); + Info<<"list2: "; + printInfo(list2, true); + + // Move construct + hashedWordList list3(std::move(list1)); + + Info<<nl << "Move construct" << nl; + Info<<"list1: "; + printInfo(list1, true); + Info<<"list3: "; + printInfo(list3, true); + + // Move assign + list1 = std::move(list3); + + Info<<nl << "Move assign" << nl; + Info<<"list1: "; + printInfo(list1, true); + Info<<"list3: "; + printInfo(list3, true); + + list1.swap(list3); + Info<<nl << "Swap" << nl; + Info<<"list1: "; + printInfo(list1, true); + Info<<"list3: "; + printInfo(list3, true); + + wordList wlist1 + { + "plain", "list", "with", "some", "with", "list", "duplicates" + }; + + + // Copy construct unique + hashedWordList list4(wlist1, true); + + Info<<nl << "Copy construct unique" << nl; + Info<<"words: " << flatOutput(wlist1) << nl; + Info<<"list4: "; + printInfo(list4, false); + + // Move construct unique + hashedWordList list5(std::move(wlist1), true); + + Info<<nl << "Move construct unique" << nl; + Info<<"words: " << flatOutput(wlist1) << nl; + Info<<"list5: "; + printInfo(list5, false); + + // Move back. Leaves lookup() with some rubbish, but clean that later + wlist1 = std::move(list5); + + Info<<nl << "Move to wordList" << nl; + Info<<"words: " << flatOutput(wlist1) << nl; + Info<<"list5: "; + printInfo(list5, false); + + // Move back. Leaves lookup() with some rubbish, but clean that later + list5 = std::move(wlist1); + + Info<<nl << "Moved from wordList" << nl; + Info<<"words: " << flatOutput(wlist1) << nl; + Info<<"list5: "; + printInfo(list5, false); + + + // Test access: + + Info<<nl << "Access" << nl; + Info<<"list: " << flatOutput(list5) << nl; + + for (const auto str : { "some", "list", "of", "words" }) + { + Info<<"index of " << str << " = " << list5[str] << nl; + } + + // Stream construct + { + ITstream input + ( + "input", + "(plain list with some with list duplicates)" + ); + + hashedWordList list6(input); + + Info<<nl << "Construct from stream" << nl; + Info<<"list: " << flatOutput(list6) << nl; + + input.rewind(); + + input >> list4; + Info<<nl << "re-read from stream" << nl; + Info<<"list: " << flatOutput(list4) << nl; + } + + + Info<< "\nEnd\n"; + + return 0; +} + + +// ************************************************************************* // diff --git a/applications/test/xfer/Make/files b/applications/test/xfer/Make/files deleted file mode 100644 index bbe20a4c7ef003a845f11774d089102f5f3dbf43..0000000000000000000000000000000000000000 --- a/applications/test/xfer/Make/files +++ /dev/null @@ -1,3 +0,0 @@ -Test-xferList.C - -EXE = $(FOAM_USER_APPBIN)/Test-xferList diff --git a/applications/test/xfer1/Make/files b/applications/test/xfer1/Make/files new file mode 100644 index 0000000000000000000000000000000000000000..10a272c027317cc5a5be6cc3ec959f654fdf6461 --- /dev/null +++ b/applications/test/xfer1/Make/files @@ -0,0 +1,3 @@ +Test-xfer1.C + +EXE = $(FOAM_USER_APPBIN)/Test-xfer1 diff --git a/applications/test/xfer1/Make/options b/applications/test/xfer1/Make/options new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/applications/test/xfer/Test-xferList.C b/applications/test/xfer1/Test-xfer1.C similarity index 67% rename from applications/test/xfer/Test-xferList.C rename to applications/test/xfer1/Test-xfer1.C index 897e4270724bbfae63f792a7190d46a6e8faa6c3..e12d5fc48f4cb7f1e61c22405474e18e16e1ee12 100644 --- a/applications/test/xfer/Test-xferList.C +++ b/applications/test/xfer1/Test-xfer1.C @@ -39,61 +39,83 @@ Description using namespace Foam; - // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // Main program: int main(int argc, char *argv[]) { - List<label> lstA(10); - List<label> lstC + labelList lstA(10); + labelList lstC { 1, 2, 3, 4 }; forAll(lstA, i) { - lstA[i] = i; + lstA[i] = 5 - i; } - Info<< "lstA: " << lstA << endl; - Info<< "lstC: " << lstC << endl; + Info<< "lstA: " << lstA << nl + << "lstC: " << lstC << nl; + + Xfer<labelList> xA = xferMove(lstA); + Xfer<labelList> xB; - Xfer<List<label>> xA = xferMove(lstA); - Xfer<List<label>> xB; + labelList lstB( xA ); - List<label> lstB( xA ); + Info<< "xA: " << xA() << nl + << "xB: " << xB() << nl + << "lstA: " << lstA << nl + << "lstB: " << lstB << nl + << "lstC: " << lstC << nl; - Info<< "xA: " << xA() << endl; - Info<< "xB: " << xB() << endl; - Info<< "lstA: " << lstA << endl; - Info<< "lstB: " << lstB << endl; - Info<< "lstC: " << lstC << endl; + // Now illegal: xA = lstB; - xA = lstB; + xA->transfer(lstB); - Info<< "xA: " << xA() << endl; - Info<< "xB: " << xB() << endl; - Info<< "lstA: " << lstA << endl; - Info<< "lstB: " << lstB << endl; - Info<< "lstC: " << lstC << endl; + Info<< "xA: " << xA() << nl + << "xB: " << xB() << nl + << "lstA: " << lstA << nl + << "lstB: " << lstB << nl + << "lstC: " << lstC << nl; xB = xA; - List<label> lstD(xferCopy(lstC)); - List<label> lstE(xferMove(lstC)); + // Construct with forwarding. For this example, truly ugly. + Xfer<labelList> xFwdA = + Xfer<labelList>::New + ( + std::initializer_list<label> + { + 1, 2, 10, 20, 15, 24, 200 + } + ); + + Xfer<labelList> xFwdB = Xfer<labelList>::New(label(8), 123); + Xfer<labelList> xFwdC = Xfer<labelList>::New(); + + Info<< nl + << "Constructed with forwarding: " << nl + << *xFwdA << nl + << *xFwdB << nl + << *xFwdC << nl + << nl; + + + labelList lstD(xferCopy(lstC)); + labelList lstE(xferMove(lstC)); // this must be empty - List<label> lstF = xferCopy(lstC); + labelList lstF = xferCopy(lstC); - Info<< "xA: " << xA() << endl; - Info<< "xB: " << xB() << endl; - Info<< "lstA: " << lstA << endl; - Info<< "lstB: " << lstB << endl; - Info<< "lstC: " << lstC << endl; - Info<< "lstD: " << lstD << endl; - Info<< "lstE: " << lstE << endl; - Info<< "lstF: " << lstF << endl; + Info<< "xA: " << xA() << nl + << "xB: " << xB() << nl + << "lstA: " << lstA << nl + << "lstB: " << lstB << nl + << "lstC: " << lstC << nl + << "lstD: " << lstD << nl + << "lstE: " << lstE << nl + << "lstF: " << lstF << nl; Info<< "xB[" << xB->size() << "]\n"; @@ -102,7 +124,7 @@ int main(int argc, char *argv[]) Info<< "xB[" << xB->size() << "]\n"; - DynamicList<label> dl(10); + DynamicList<label> dl; for (label i = 0; i < 5; ++i) { dl.append(i); @@ -111,9 +133,9 @@ int main(int argc, char *argv[]) face f1(dl); face f2(xferCopy<labelList>(dl)); - Info<< "dl[" << dl.size() << "/" << dl.capacity() << "] " << dl << endl; - Info<< "f1: " << f1 << endl; - Info<< "f2: " << f2 << endl; + Info<< "dl[" << dl.size() << "/" << dl.capacity() << "] " << dl << nl; + Info<< "f1: " << f1 << nl; + Info<< "f2: " << f2 << nl; // add some more labels for (label i = 5; i < 8; ++i) @@ -123,18 +145,18 @@ int main(int argc, char *argv[]) // note: xfer() method returns a plain labelList face f3(dl.xfer()); - Info<< "dl[" << dl.size() << "/" << dl.capacity() << "] " << dl << endl; - Info<< "f3: " << f3 << endl; + Info<< "dl[" << dl.size() << "/" << dl.capacity() << "] " << dl << nl; + Info<< "f3: " << f3 << nl; - Info<<"\nflip faces:" << endl; + Info<<"\nflip faces:" << nl; f1.flip(); f3.flip(); - Info<< "f1: " << f1 << endl; - Info<< "f3: " << f3 << endl; + Info<< "f1: " << f1 << nl; + Info<< "f3: " << f3 << nl; { - Info<<"\nTest xfer with fields:" << endl; + Info<<"\nTest xfer with fields:" << nl; List<point> list1 { { 0, 1, 2 }, @@ -180,7 +202,6 @@ int main(int argc, char *argv[]) <<"xfer copy construct from Field (as Field): " << nl <<"input (field) = " << field4 << nl <<"output (dyn-field) = " << dyfield1 << nl; - } return 0; diff --git a/applications/utilities/preProcessing/faceAgglomerate/faceAgglomerate.C b/applications/utilities/preProcessing/faceAgglomerate/faceAgglomerate.C index 99caaf375b63e3366d20d0e8fd472ab40fe154b7..eff776563f4ec6fce575cb1283613618db346c6d 100644 --- a/applications/utilities/preProcessing/faceAgglomerate/faceAgglomerate.C +++ b/applications/utilities/preProcessing/faceAgglomerate/faceAgglomerate.C @@ -41,7 +41,6 @@ SeeAlso #include "fvMesh.H" #include "Time.H" #include "volFields.H" -#include "CompactListList.H" #include "unitConversion.H" #include "pairPatchAgglomeration.H" #include "labelListIOList.H" diff --git a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.H b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.H index 548066d19f6c96449d65c36b457213c679e83f8c..b705c27c9fab0e54cd281b44c63a53f6df23008a 100644 --- a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.H +++ b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.H @@ -70,11 +70,11 @@ class HashPtrTable //- Read from Istream using given Istream constructor class template<class INew> - void read(Istream& is, const INew& inewt); + void read(Istream& is, const INew& inew); //- Read from dictionary using given dictionary constructor class template<class INew> - void read(const dictionary& dict, const INew& inewt); + void read(const dictionary& dict, const INew& inew); public: @@ -99,7 +99,7 @@ public: //- Construct from Istream using given Istream constructor class template<class INew> - HashPtrTable(Istream& is, const INew& inewt); + HashPtrTable(Istream& is, const INew& inew); //- Construct from Istream using default Istream constructor class HashPtrTable(Istream& is); diff --git a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTableIO.C b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTableIO.C index bbc7351857e23dad60bcd34dfca08cf7efda1418..fdf3e554a0234a54953f2a7ef12bda9d445acb63 100644 --- a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTableIO.C +++ b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTableIO.C @@ -33,7 +33,7 @@ License template<class T, class Key, class Hash> template<class INew> -void Foam::HashPtrTable<T, Key, Hash>::read(Istream& is, const INew& inewt) +void Foam::HashPtrTable<T, Key, Hash>::read(Istream& is, const INew& inew) { is.fatalCheck(FUNCTION_NAME); @@ -47,25 +47,25 @@ void Foam::HashPtrTable<T, Key, Hash>::read(Istream& is, const INew& inewt) if (firstToken.isLabel()) { - const label s = firstToken.labelToken(); + const label len = firstToken.labelToken(); // Read beginning of contents const char delimiter = is.readBeginList("HashPtrTable"); - if (s) + if (len) { - if (2*s > this->capacity()) + if (2*len > this->capacity()) { - this->resize(2*s); + this->resize(2*len); } if (delimiter == token::BEGIN_LIST) { - for (label i=0; i<s; ++i) + for (label i=0; i<len; ++i) { Key key; is >> key; - this->insert(key, inewt(key, is).ptr()); + this->insert(key, inew(key, is).ptr()); is.fatalCheck ( @@ -110,7 +110,7 @@ void Foam::HashPtrTable<T, Key, Hash>::read(Istream& is, const INew& inewt) is.putBack(lastToken); Key key; is >> key; - this->insert(key, inewt(key, is).ptr()); + this->insert(key, inew(key, is).ptr()); is.fatalCheck ( @@ -140,14 +140,14 @@ template<class INew> void Foam::HashPtrTable<T, Key, Hash>::read ( const dictionary& dict, - const INew& inewt + const INew& inew ) { forAllConstIter(dictionary, dict, iter) { const word& k = iter().keyword(); - this->insert(k, inewt(dict.subDict(k)).ptr()); + this->insert(k, inew(dict.subDict(k)).ptr()); } } @@ -170,9 +170,9 @@ void Foam::HashPtrTable<T, Key, Hash>::write(Ostream& os) const template<class T, class Key, class Hash> template<class INew> -Foam::HashPtrTable<T, Key, Hash>::HashPtrTable(Istream& is, const INew& inewt) +Foam::HashPtrTable<T, Key, Hash>::HashPtrTable(Istream& is, const INew& inew) { - this->read(is, inewt); + this->read(is, inew); } @@ -209,12 +209,12 @@ Foam::Ostream& Foam::operator<< const HashPtrTable<T, Key, Hash>& tbl ) { - const label sz = tbl.size(); + const label len = tbl.size(); - if (sz) + if (len) { // Size and start list delimiter - os << nl << sz << nl << token::BEGIN_LIST << nl; + os << nl << len << nl << token::BEGIN_LIST << nl; // Contents for (auto iter = tbl.cbegin(); iter != tbl.cend(); ++iter) @@ -233,7 +233,7 @@ Foam::Ostream& Foam::operator<< else { // Empty hash table - os << sz << token::BEGIN_LIST << token::END_LIST; + os << len << token::BEGIN_LIST << token::END_LIST; } os.check(FUNCTION_NAME); diff --git a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.C b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.C index 575dda7903054cb1bc4df3f693461ea47a74b4cd..9fc7370d103a7f292d288726f5312ecedc926002 100644 --- a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.C +++ b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.C @@ -170,28 +170,27 @@ inline bool Foam::HashSet<Key, Hash>::operator[](const Key& key) const template<class Key, class Hash> -void Foam::HashSet<Key, Hash>::operator=(const UList<Key>& lst) +void Foam::HashSet<Key, Hash>::operator=(const UList<Key>& rhs) { - assignMultiple(lst.begin(), lst.end(), 2*lst.size()); + assignMultiple(rhs.begin(), rhs.end(), 2*rhs.size()); } template<class Key, class Hash> template<unsigned Size> -void Foam::HashSet<Key, Hash>::operator=(const FixedList<Key, Size>& lst) +void Foam::HashSet<Key, Hash>::operator=(const FixedList<Key, Size>& rhs) { - assignMultiple(lst.begin(), lst.end(), 2*lst.size()); + assignMultiple(rhs.begin(), rhs.end(), 2*rhs.size()); } template<class Key, class Hash> -void Foam::HashSet<Key, Hash>::operator=(std::initializer_list<Key> lst) +void Foam::HashSet<Key, Hash>::operator=(std::initializer_list<Key> rhs) { - assignMultiple(lst.begin(), lst.end(), 2*lst.size()); + assignMultiple(rhs.begin(), rhs.end(), 2*rhs.size()); } - template<class Key, class Hash> bool Foam::HashSet<Key, Hash>::operator==(const HashSet<Key, Hash>& rhs) const { @@ -283,24 +282,10 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const HashSet<Key, Hash>& tbl) } -// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * // - -template<class Key, class Hash> -inline void Foam::Swap -( - HashSet<Key, Hash>& a, - HashSet<Key, Hash>& b -) -{ - a.swap(b); -} - - /* * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * */ template<class Key, class Hash> -Foam::HashSet<Key, Hash> -Foam::operator| +Foam::HashSet<Key, Hash> Foam::operator| ( const HashSet<Key, Hash>& hash1, const HashSet<Key, Hash>& hash2 @@ -313,8 +298,7 @@ Foam::operator| template<class Key, class Hash> -Foam::HashSet<Key, Hash> -Foam::operator& +Foam::HashSet<Key, Hash> Foam::operator& ( const HashSet<Key, Hash>& hash1, const HashSet<Key, Hash>& hash2 @@ -327,8 +311,7 @@ Foam::operator& template<class Key, class Hash> -Foam::HashSet<Key, Hash> -Foam::operator^ +Foam::HashSet<Key, Hash> Foam::operator^ ( const HashSet<Key, Hash>& hash1, const HashSet<Key, Hash>& hash2 diff --git a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H index abf1731ceefe534322226a160113675a31212925..da024138b6d5294e655d188c4ad68f00282f8f7c 100644 --- a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H +++ b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H @@ -334,10 +334,10 @@ public: //- Assignment from a FixedList of keys template<unsigned Size> - void operator=(const FixedList<Key, Size>& lst); + void operator=(const FixedList<Key, Size>& rhs); //- Assignment from an initializer list of keys - void operator=(std::initializer_list<Key> lst); + void operator=(std::initializer_list<Key> rhs); // Logical and set operations @@ -372,17 +372,6 @@ public: }; -// Global Functions - -// Exchange contents of HashSets - see HashSet::swap(). -template<class Key, class Hash> -inline void Swap -( - HashSet<Key, Hash>& a, - HashSet<Key, Hash>& b -); - - // Global Operators //- Combine entries from HashSets diff --git a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C index e4344770abffcfc5445f814ad72a17c29ccf3bd3..e96d1311e95fca2c5f7c2d3ee5661260593dfb1e 100644 --- a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C +++ b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C @@ -100,11 +100,16 @@ Foam::HashTable<T, Key, Hash>::HashTable(const HashTable<T, Key, Hash>& ht) template<class T, class Key, class Hash> -Foam::HashTable<T, Key, Hash>::HashTable(HashTable<T, Key, Hash>&& ht) +Foam::HashTable<T, Key, Hash>::HashTable(HashTable<T, Key, Hash>&& rhs) : - HashTable<T, Key, Hash>(0) + HashTableCore(), + size_(rhs.size_), + capacity_(rhs.capacity_), + table_(rhs.table_) { - transfer(ht); + rhs.size_ = 0; + rhs.capacity_ = 0; + rhs.table_ = nullptr; } @@ -116,9 +121,9 @@ Foam::HashTable<T, Key, Hash>::HashTable : HashTable<T, Key, Hash>(2*lst.size()) { - for (const auto& pair : lst) + for (const auto& keyval : lst) { - insert(pair.first, pair.second); + insert(keyval.first, keyval.second); } } @@ -141,25 +146,25 @@ Foam::HashTable<T, Key, Hash>::~HashTable() template<class T, class Key, class Hash> Foam::List<Key> Foam::HashTable<T, Key, Hash>::toc() const { - List<Key> keyLst(size_); + List<Key> list(size_); label count = 0; for (const_iterator iter = cbegin(); iter != cend(); ++iter) { - keyLst[count++] = iter.key(); + list[count++] = iter.key(); } - return keyLst; + return list; } template<class T, class Key, class Hash> Foam::List<Key> Foam::HashTable<T, Key, Hash>::sortedToc() const { - List<Key> keyLst(this->toc()); - Foam::sort(keyLst); + List<Key> list(this->toc()); + Foam::sort(list); - return keyLst; + return list; } @@ -170,10 +175,10 @@ Foam::List<Key> Foam::HashTable<T, Key, Hash>::sortedToc const Compare& comp ) const { - List<Key> keyLst(this->toc()); - Foam::sort(keyLst, comp); + List<Key> list(this->toc()); + Foam::sort(list, comp); - return keyLst; + return list; } @@ -185,21 +190,21 @@ Foam::List<Key> Foam::HashTable<T, Key, Hash>::tocKeys const bool invert ) const { - List<Key> keyLst(size_); + List<Key> list(size_); label count = 0; for (const_iterator iter = cbegin(); iter != cend(); ++iter) { if ((pred(iter.key()) ? !invert : invert)) { - keyLst[count++] = iter.key(); + list[count++] = iter.key(); } } - keyLst.setSize(count); - Foam::sort(keyLst); + list.setSize(count); + Foam::sort(list); - return keyLst; + return list; } @@ -211,21 +216,21 @@ Foam::List<Key> Foam::HashTable<T, Key, Hash>::tocValues const bool invert ) const { - List<Key> keyLst(size_); + List<Key> list(size_); label count = 0; for (const_iterator iter = cbegin(); iter != cend(); ++iter) { if ((pred(iter.object()) ? !invert : invert)) { - keyLst[count++] = iter.key(); + list[count++] = iter.key(); } } - keyLst.setSize(count); - Foam::sort(keyLst); + list.setSize(count); + Foam::sort(list); - return keyLst; + return list; } @@ -237,21 +242,21 @@ Foam::List<Key> Foam::HashTable<T, Key, Hash>::tocEntries const bool invert ) const { - List<Key> keyLst(size_); + List<Key> list(size_); label count = 0; for (const_iterator iter = cbegin(); iter != cend(); ++iter) { if ((pred(iter.key(), iter.object()) ? !invert : invert)) { - keyLst[count++] = iter.key(); + list[count++] = iter.key(); } } - keyLst.setSize(count); - Foam::sort(keyLst); + list.setSize(count); + Foam::sort(list); - return keyLst; + return list; } @@ -647,33 +652,19 @@ void Foam::HashTable<T, Key, Hash>::clearStorage() template<class T, class Key, class Hash> -void Foam::HashTable<T, Key, Hash>::swap(HashTable<T, Key, Hash>& ht) +void Foam::HashTable<T, Key, Hash>::swap(HashTable<T, Key, Hash>& rhs) { - Foam::Swap(size_, ht.size_); - Foam::Swap(capacity_, ht.capacity_); - Foam::Swap(table_, ht.table_); + Foam::Swap(size_, rhs.size_); + Foam::Swap(capacity_, rhs.capacity_); + Foam::Swap(table_, rhs.table_); } template<class T, class Key, class Hash> -void Foam::HashTable<T, Key, Hash>::transfer(HashTable<T, Key, Hash>& ht) +void Foam::HashTable<T, Key, Hash>::transfer(HashTable<T, Key, Hash>& rhs) { - // As per destructor - if (table_) - { - clear(); - delete[] table_; - } - - size_ = ht.size_; - ht.size_ = 0; - - capacity_ = ht.capacity_; - ht.capacity_ = 0; - - table_ = ht.table_; - ht.table_ = nullptr; - + clear(); + swap(rhs); } @@ -794,22 +785,22 @@ void Foam::HashTable<T, Key, Hash>::operator= template<class T, class Key, class Hash> void Foam::HashTable<T, Key, Hash>::operator= ( - std::initializer_list<std::pair<Key, T>> lst + std::initializer_list<std::pair<Key, T>> rhs ) { // Could be zero-sized from a previous transfer() if (!capacity_) { - resize(2*lst.size()); + resize(2*rhs.size()); } else { clear(); } - for (const auto& pair : lst) + for (const auto& keyval : rhs) { - insert(pair.first, pair.second); + insert(keyval.first, keyval.second); } } diff --git a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H index 35621df78dea9cc7158df1c7260132e4002e9453..922fe917cedb43db58a825e7a7bbe6929695079b 100644 --- a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H +++ b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H @@ -336,11 +336,11 @@ public: //- Construct from Istream with default table capacity HashTable(Istream& is, const label size = 128); - //- Construct as copy + //- Copy construct HashTable(const this_type& ht); //- Move construct - HashTable(this_type&& ht); + HashTable(this_type&& rhs); //- Construct from an initializer list HashTable(std::initializer_list<std::pair<Key, T>> lst); @@ -591,12 +591,11 @@ public: // Equivalent to clear() followed by resize(0) void clearStorage(); - //- Swap contents of the argument table into this table - void swap(HashTable<T, Key, Hash>& ht); + //- Swap contents into this table + void swap(HashTable<T, Key, Hash>& rhs); - //- Transfer the contents of the argument table into this table - // and annul the argument table. - void transfer(HashTable<T, Key, Hash>& ht); + //- Transfer contents into this table. + void transfer(HashTable<T, Key, Hash>& rhs); // Member Operators @@ -615,11 +614,11 @@ public: //- Return existing entry or insert a new entry. inline T& operator()(const Key& key, const T& deflt); - //- Assignment + //- Copy assignment void operator=(const HashTable<T, Key, Hash>& rhs); - //- Assignment from an initializer list - void operator=(std::initializer_list<std::pair<Key, T>> lst); + //- Copy assignment from an initializer list + void operator=(std::initializer_list<std::pair<Key, T>> rhs); //- Move assign void operator=(HashTable<T, Key, Hash>&& rhs); @@ -973,17 +972,6 @@ public: }; -// Global Functions - -// Exchange contents of hash tables - see HashTable::swap(). -template<class T, class Key, class Hash> -inline void Swap -( - HashTable<T, Key, Hash>& a, - HashTable<T, Key, Hash>& b -); - - // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // } // End namespace Foam diff --git a/src/OpenFOAM/containers/HashTables/HashTable/HashTableI.H b/src/OpenFOAM/containers/HashTables/HashTable/HashTableI.H index 0ac2749278d00b33a8a97cfdcd69eae93a487f38..424d3db57de50a454021a018a2a6b9848c468cc0 100644 --- a/src/OpenFOAM/containers/HashTables/HashTable/HashTableI.H +++ b/src/OpenFOAM/containers/HashTables/HashTable/HashTableI.H @@ -218,17 +218,4 @@ inline T& Foam::HashTable<T, Key, Hash>::operator() } -// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * // - -template<class T, class Key, class Hash> -inline void Foam::Swap -( - HashTable<T, Key, Hash>& a, - HashTable<T, Key, Hash>& b -) -{ - a.swap(b); -} - - // ************************************************************************* // diff --git a/src/OpenFOAM/containers/HashTables/HashTable/HashTableIO.C b/src/OpenFOAM/containers/HashTables/HashTable/HashTableIO.C index 34392da5ee48b4cc4e091a14466ebd5513c4d509..0ee42dff4dfd630fc0a1d995da147bfb06eead27 100644 --- a/src/OpenFOAM/containers/HashTables/HashTable/HashTableIO.C +++ b/src/OpenFOAM/containers/HashTables/HashTable/HashTableIO.C @@ -160,21 +160,21 @@ Foam::Istream& Foam::operator>> if (firstToken.isLabel()) { - const label s = firstToken.labelToken(); + const label len = firstToken.labelToken(); // Read beginning of contents const char delimiter = is.readBeginList("HashTable"); - if (s) + if (len) { - if (2*s > L.capacity_) + if (2*len > L.capacity_) { - L.resize(2*s); + L.resize(2*len); } if (delimiter == token::BEGIN_LIST) { - for (label i=0; i<s; ++i) + for (label i=0; i<len; ++i) { Key key; is >> key; @@ -258,12 +258,12 @@ Foam::Ostream& Foam::operator<< const HashTable<T, Key, Hash>& tbl ) { - const label sz = tbl.size(); + const label len = tbl.size(); - if (sz) + if (len) { // Size and start list delimiter - os << nl << sz << nl << token::BEGIN_LIST << nl; + os << nl << len << nl << token::BEGIN_LIST << nl; // Contents for (auto iter = tbl.cbegin(); iter != tbl.cend(); ++iter) @@ -276,7 +276,7 @@ Foam::Ostream& Foam::operator<< else { // Empty hash table - os << sz << token::BEGIN_LIST << token::END_LIST; + os << len << token::BEGIN_LIST << token::END_LIST; } os.check(FUNCTION_NAME); diff --git a/src/OpenFOAM/containers/Identifiers/Keyed/Keyed.H b/src/OpenFOAM/containers/Identifiers/Keyed/Keyed.H index df695531de15a6405cb36920fada8ea49efc1ef6..c7cdb3ce97c7bad90d841be95818da0c0422731a 100644 --- a/src/OpenFOAM/containers/Identifiers/Keyed/Keyed.H +++ b/src/OpenFOAM/containers/Identifiers/Keyed/Keyed.H @@ -71,14 +71,14 @@ public: //- Add labels to a list of values inline static List<Keyed<T>> createList ( - const List<T>&, + const UList<T>& lst, const label key=0 ); //- Add labels to a list of values inline static List<Keyed<T>> createList ( - const List<T>&, + const UList<T>& lst, const labelUList& keys ); @@ -88,14 +88,14 @@ public: //- Construct null inline Keyed(); - //- Construct as a copy of item, with a key + //- Copy construct item, with a key inline Keyed(const T& item, const label key=0); - //- Construct by transferring the item, with a key - inline Keyed(const Xfer<T>& item, const label key=0); + //- Move construct item, with a key + inline Keyed(T&& item, const label key=0); //- Construct from Istream - inline Keyed(Istream&); + inline Keyed(Istream& is); // Member Functions diff --git a/src/OpenFOAM/containers/Identifiers/Keyed/KeyedI.H b/src/OpenFOAM/containers/Identifiers/Keyed/KeyedI.H index 59f49406871d3dfcb1fb43d5adbd59a23c00be8d..25598ea1c76a4768b6bf7fdc4779744ca7fb1fcc 100644 --- a/src/OpenFOAM/containers/Identifiers/Keyed/KeyedI.H +++ b/src/OpenFOAM/containers/Identifiers/Keyed/KeyedI.H @@ -25,8 +25,6 @@ License #include "IOstreams.H" -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // template<class T> @@ -45,9 +43,9 @@ inline Foam::Keyed<T>::Keyed(const T& item, const label key) template<class T> -inline Foam::Keyed<T>::Keyed(const Xfer<T>& item, const label key) +inline Foam::Keyed<T>::Keyed(T&& item, const label key) : - T(item), + T(std::move(item)), key_(key) {} @@ -76,7 +74,7 @@ inline Foam::label& Foam::Keyed<T>::key() template<class T> inline Foam::List<Foam::Keyed<T>> -Foam::Keyed<T>::createList(const List<T>& lst, const label key) +Foam::Keyed<T>::createList(const UList<T>& lst, const label key) { List<Keyed<T>> newList(lst.size()); @@ -90,7 +88,7 @@ Foam::Keyed<T>::createList(const List<T>& lst, const label key) template<class T> inline Foam::List<Foam::Keyed<T>> -Foam::Keyed<T>::createList(const List<T>& lst, const labelUList& keys) +Foam::Keyed<T>::createList(const UList<T>& lst, const labelUList& keys) { if (lst.size() != keys.size()) { @@ -141,6 +139,5 @@ inline Foam::Ostream& Foam::operator<<(Ostream& os, const Keyed<T>& item) return os; } -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // ************************************************************************* // diff --git a/src/OpenFOAM/containers/LinkedLists/accessTypes/ILList/ILList.C b/src/OpenFOAM/containers/LinkedLists/accessTypes/ILList/ILList.C index 460498e7d56dde480608a597eb7259afc1ac9eca..8207c6184d874653afe9310c149b2d0ab6dc9266 100644 --- a/src/OpenFOAM/containers/LinkedLists/accessTypes/ILList/ILList.C +++ b/src/OpenFOAM/containers/LinkedLists/accessTypes/ILList/ILList.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -32,18 +32,22 @@ Foam::ILList<LListBase, T>::ILList(const ILList<LListBase, T>& lst) : UILList<LListBase, T>() { - for - ( - typename UILList<LListBase, T>::const_iterator iter = lst.begin(); - iter != lst.end(); - ++iter - ) + for (const auto& item : lst) { - this->append(iter().clone().ptr()); + this->append(item.clone().ptr()); } } +template<class LListBase, class T> +Foam::ILList<LListBase, T>::ILList(ILList<LListBase, T>&& lst) +: + UILList<LListBase, T>() +{ + LListBase::transfer(lst); +} + + template<class LListBase, class T> template<class CloneArg> Foam::ILList<LListBase, T>::ILList @@ -54,14 +58,9 @@ Foam::ILList<LListBase, T>::ILList : UILList<LListBase, T>() { - for - ( - typename UILList<LListBase, T>::const_iterator iter = lst.begin(); - iter != lst.end(); - ++iter - ) + for (const auto& item :lst) { - this->append(iter().clone(cloneArg).ptr()); + this->append(item.clone(cloneArg).ptr()); } } @@ -80,39 +79,38 @@ Foam::ILList<LListBase, T>::~ILList() template<class LListBase, class T> bool Foam::ILList<LListBase, T>::eraseHead() { - T* tPtr; - if ((tPtr = this->removeHead())) + T* p = this->removeHead(); + + if (p) { - delete tPtr; + delete p; return true; } - else - { - return false; - } + + return false; } template<class LListBase, class T> -bool Foam::ILList<LListBase, T>::erase(T* p) +bool Foam::ILList<LListBase, T>::erase(T* item) { - T* tPtr; - if ((tPtr = remove(p))) + T* p = remove(item); + + if (p) { - delete tPtr; + delete p; return true; } - else - { - return false; - } + + return false; } template<class LListBase, class T> void Foam::ILList<LListBase, T>::clear() { - label oldSize = this->size(); - for (label i=0; i<oldSize; ++i) + const label len = this->size(); + + for (label i=0; i<len; ++i) { eraseHead(); } @@ -136,20 +134,19 @@ void Foam::ILList<LListBase, T>::operator=(const ILList<LListBase, T>& lst) { this->clear(); - for - ( - typename UILList<LListBase, T>::const_iterator iter = lst.begin(); - iter != lst.end(); - ++iter - ) + for (const auto& item : lst) { - this->append(iter().clone().ptr()); + this->append(item.clone().ptr()); } } -// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * // -#include "ILListIO.C" +template<class LListBase, class T> +void Foam::ILList<LListBase, T>::operator=(ILList<LListBase, T>&& lst) +{ + clear(); + LListBase::transfer(lst); +} // ************************************************************************* // diff --git a/src/OpenFOAM/containers/LinkedLists/accessTypes/ILList/ILList.H b/src/OpenFOAM/containers/LinkedLists/accessTypes/ILList/ILList.H index 1af13f0a11421b3da33b46ae19ded3dfe40867e3..d4a9e5c50d10a34d922fcebbb68becd41eaa7a6e 100644 --- a/src/OpenFOAM/containers/LinkedLists/accessTypes/ILList/ILList.H +++ b/src/OpenFOAM/containers/LinkedLists/accessTypes/ILList/ILList.H @@ -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. @@ -43,17 +43,16 @@ SourceFiles namespace Foam { +// Forward declarations + class Istream; class Ostream; - -// Forward declaration of friend functions and operators - template<class LListBase, class T> class ILList; template<class LListBase, class T> Istream& operator>> ( - Istream&, - ILList<LListBase, T>& + Istream& is, + ILList<LListBase, T>& lst ); @@ -70,7 +69,7 @@ class ILList //- Read from Istream using given Istream constructor class template<class INew> - void read(Istream&, const INew&); + void read(Istream& is, const INew& inew); public: @@ -78,28 +77,30 @@ public: // Constructors //- Null construct - ILList() - {} + ILList() = default; - //- Construct given initial T - ILList(T* a) + //- Construct and insert the initial T item pointer + explicit ILList(T* item) : - UILList<LListBase, T>(a) + UILList<LListBase, T>(item) {} //- Construct from Istream - ILList(Istream&); + ILList(Istream& is); - //- Construct as copy - ILList(const ILList<LListBase, T>&); + //- Copy construct using the 'clone()' method for each element + ILList(const ILList<LListBase, T>& lst); - //- Copy constructor with additional argument for clone + //- Move construct + ILList(ILList<LListBase, T>&& lst); + + //- Copy constructor with additional argument for clone 'clone()' template<class CloneArg> ILList(const ILList<LListBase, T>& lst, const CloneArg& cloneArg); //- Construct from Istream using given Istream constructor class template<class INew> - ILList(Istream&, const INew&); + ILList(Istream& is, const INew& inew); //- Destructor @@ -108,35 +109,36 @@ public: // Member Functions - // Edit - - //- Remove the head element specified from the list and delete it - bool eraseHead(); + //- Remove the head element specified from the list and delete it + bool eraseHead(); - //- Remove the specified element from the list and delete it - bool erase(T* p); + //- Remove the specified element from the list and delete it + bool erase(T* item); - //- Clear the contents of the list - void clear(); + //- Clear the contents of the list + void clear(); - //- Transfer the contents of the argument into this List - // and annul the argument list. - void transfer(ILList<LListBase, T>&); + //- Transfer the contents of the argument into this List + //- and annul the argument list. + void transfer(ILList<LListBase, T>& lst); // Member operators - //- Assignment operator - void operator=(const ILList<LListBase, T>&); + //- Copy assignment using the 'clone()' method for each element + void operator=(const ILList<LListBase, T>& lst); + + //- Move assignment + void operator=(ILList<LListBase, T>&& lst); // Istream operator - //- Read List from Istream, discarding contents of existing List. + //- Read from Istream, discarding existing contents. friend Istream& operator>> <LListBase, T> ( - Istream&, - ILList<LListBase, T>& + Istream& is, + ILList<LListBase, T>& lst ); }; @@ -149,6 +151,7 @@ public: #ifdef NoRepository #include "ILList.C" + #include "ILListIO.C" #endif // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/OpenFOAM/containers/LinkedLists/accessTypes/ILList/ILListIO.C b/src/OpenFOAM/containers/LinkedLists/accessTypes/ILList/ILListIO.C index 961569aea775a457d904ba3a9a9978c76bb40449..4312699f166b8921a545ee3a36cf72dd009ace37 100644 --- a/src/OpenFOAM/containers/LinkedLists/accessTypes/ILList/ILListIO.C +++ b/src/OpenFOAM/containers/LinkedLists/accessTypes/ILList/ILListIO.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -31,59 +31,49 @@ License template<class LListBase, class T> template<class INew> -void Foam::ILList<LListBase, T>::read(Istream& is, const INew& iNew) +void Foam::ILList<LListBase, T>::read(Istream& is, const INew& inew) { is.fatalCheck(FUNCTION_NAME); token firstToken(is); - is.fatalCheck - ( - "operator>>(Istream&, ILList<LListBase, T>&) : reading first token" - ); + is.fatalCheck("ILList::readList : reading first token"); if (firstToken.isLabel()) { - const label s = firstToken.labelToken(); + const label len = firstToken.labelToken(); // Read beginning of contents - const char delimiter = is.readBeginList("ILList<LListBase, T>"); + const char delimiter = is.readBeginList("ILList"); - if (s) + if (len) { if (delimiter == token::BEGIN_LIST) { - for (label i=0; i<s; ++i) + for (label i=0; i<len; ++i) { - this->append(iNew(is).ptr()); + T* p = inew(is).ptr(); + this->append(p); - is.fatalCheck - ( - "operator>>(Istream&, ILList<LListBase, T>&) : " - "reading entry" - ); + is.fatalCheck("ILList::readList : reading entry"); } } else { - T* tPtr = iNew(is).ptr(); - this->append(tPtr); + T* p = inew(is).ptr(); + this->append(p); - is.fatalCheck - ( - "operator>>(Istream&, ILList<LListBase, T>&) : " - "reading entry" - ); + is.fatalCheck("ILList::readList : reading entry"); - for (label i=1; i<s; ++i) + for (label i=1; i<len; ++i) { - this->append(new T(*tPtr)); + this->append(new T(*p)); // Copy construct } } } // Read end of contents - is.readEndList("ILList<LListBase, T>"); + is.readEndList("ILList"); } else if (firstToken.isPunctuation()) { @@ -108,7 +98,9 @@ void Foam::ILList<LListBase, T>::read(Istream& is, const INew& iNew) ) { is.putBack(lastToken); - this->append(iNew(is).ptr()); + + T* p = inew(is).ptr(); + this->append(p); is >> lastToken; is.fatalCheck(FUNCTION_NAME); @@ -128,9 +120,9 @@ void Foam::ILList<LListBase, T>::read(Istream& is, const INew& iNew) template<class LListBase, class T> template<class INew> -Foam::ILList<LListBase, T>::ILList(Istream& is, const INew& iNew) +Foam::ILList<LListBase, T>::ILList(Istream& is, const INew& inew) { - this->read(is, iNew); + this->read(is, inew); } @@ -144,10 +136,10 @@ Foam::ILList<LListBase, T>::ILList(Istream& is) // * * * * * * * * * * * * * * * Istream Operator * * * * * * * * * * * * * // template<class LListBase, class T> -Foam::Istream& Foam::operator>>(Istream& is, ILList<LListBase, T>& L) +Foam::Istream& Foam::operator>>(Istream& is, ILList<LListBase, T>& lst) { - L.clear(); - L.read(is, INew<T>()); + lst.clear(); + lst.read(is, INew<T>()); return is; } diff --git a/src/OpenFOAM/containers/LinkedLists/accessTypes/LList/LList.C b/src/OpenFOAM/containers/LinkedLists/accessTypes/LList/LList.C index 7854cc9d84cdd5c554e8e08c9bfd1eecd3661fe9..05e3c521edda21d9ab03f6be71d3a19d9c56bfa5 100644 --- a/src/OpenFOAM/containers/LinkedLists/accessTypes/LList/LList.C +++ b/src/OpenFOAM/containers/LinkedLists/accessTypes/LList/LList.C @@ -39,6 +39,15 @@ Foam::LList<LListBase, T>::LList(const LList<LListBase, T>& lst) } +template<class LListBase, class T> +Foam::LList<LListBase, T>::LList(LList<LListBase, T>&& lst) +: + LListBase() +{ + LListBase::transfer(lst); +} + + template<class LListBase, class T> Foam::LList<LListBase, T>::LList(std::initializer_list<T> lst) : @@ -51,6 +60,8 @@ Foam::LList<LListBase, T>::LList(std::initializer_list<T> lst) } +// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // + template<class LListBase, class T> Foam::LList<LListBase, T>::~LList() { @@ -63,8 +74,8 @@ Foam::LList<LListBase, T>::~LList() template<class LListBase, class T> void Foam::LList<LListBase, T>::clear() { - label oldSize = this->size(); - for (label i=0; i<oldSize; ++i) + const label len = this->size(); + for (label i=0; i<len; ++i) { this->removeHead(); } @@ -95,6 +106,15 @@ void Foam::LList<LListBase, T>::operator=(const LList<LListBase, T>& lst) } +template<class LListBase, class T> +void Foam::LList<LListBase, T>::operator=(LList<LListBase, T>&& lst) +{ + this->clear(); + + LListBase::transfer(lst); +} + + template<class LListBase, class T> void Foam::LList<LListBase, T>::operator=(std::initializer_list<T> lst) { @@ -107,8 +127,4 @@ void Foam::LList<LListBase, T>::operator=(std::initializer_list<T> lst) } -// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * // - -#include "LListIO.C" - // ************************************************************************* // diff --git a/src/OpenFOAM/containers/LinkedLists/accessTypes/LList/LList.H b/src/OpenFOAM/containers/LinkedLists/accessTypes/LList/LList.H index f16cf03d14d7f22ba8d291e5af66e0e47715a874..d964202b8261f32eb7cb5271846647411412917a 100644 --- a/src/OpenFOAM/containers/LinkedLists/accessTypes/LList/LList.H +++ b/src/OpenFOAM/containers/LinkedLists/accessTypes/LList/LList.H @@ -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. @@ -44,25 +44,25 @@ SourceFiles namespace Foam { +// Forward declarations + class Istream; class Ostream; -// Forward declaration of friend functions and operators - template<class LListBase, class T> class LList; template<class LListBase, class T> Istream& operator>> ( - Istream&, - LList<LListBase, T>& + Istream& is, + LList<LListBase, T>& lst ); template<class LListBase, class T> Ostream& operator<< ( - Ostream&, - const LList<LListBase, T>& + Ostream& os, + const LList<LListBase, T>& lst ); @@ -75,19 +75,42 @@ class LList : public LListBase { - public: + // STL type definitions + + //- Type of values stored. + typedef T value_type; + + //- Pointer for value_type + typedef T* pointer; + + //- Const pointer for value_type + typedef const T* const_pointer; + + //- Reference for value_type + typedef T& reference; + + //- Const reference for value_type + typedef const T& const_reference; + + //- The type that can represent the container size + typedef label size_type; + + //- The difference between iterator objects + typedef label difference_type; + + // Forward declaration of STL iterators class iterator; - friend class iterator; - class const_iterator; - friend class const_iterator; + using base_iterator = typename LListBase::iterator; + using const_base_iterator = typename LListBase::const_iterator; - //- Link structure + + //- The storage of T with linked nodes struct link : public LListBase::link @@ -95,34 +118,73 @@ public: //- Stored object T obj_; - //- Construct given object - link(T a) + //- Copy construct from given object + link(const T& obj) + : + obj_(obj) + {} + + //- Move construct from given object + link(T&& obj) : - obj_(a) + obj_(std::move(obj)) {} + + + //- Dereference LListBase::link to obtain address of stored object + static constexpr T* ptr(typename LListBase::link* node) + { + return &(static_cast<link*>(node)->obj_); + } + + //- Dereference LListBase::link to obtain address of stored object + static constexpr const T* ptr(const typename LListBase::link* node) + { + return &(static_cast<const link*>(node)->obj_); + } + + //- Dereference LListBase::link to obtain the stored object + static constexpr T& ref(typename LListBase::link* node) + { + return static_cast<link*>(node)->obj_; + } + + //- Dereference LListBase::link to obtain the stored object + static constexpr const T& ref(const typename LListBase::link* node) + { + return static_cast<const link*>(node)->obj_; + } }; // Constructors //- Null construct - LList() - {} + LList() = default; - //- Construct given initial T - explicit LList(T a) - : - LListBase(new link(a)) - {} + //- Construct and copy insert the initial T item + explicit LList(const T& item) + { + this->insert(item); + } + + //- Construct and move insert the initial T item + explicit LList(T&& item) + { + this->insert(std::move(item)); + } //- Construct from Istream - explicit LList(Istream&); + explicit LList(Istream& is); + + //- Copy construct + LList(const LList<LListBase, T>& lst); - //- Construct as copy - LList(const LList<LListBase, T>&); + //- Move construct + LList(LList<LListBase, T>&& lst); - //- Construct from an initializer list - LList(std::initializer_list<T>); + //- Copy construct from an initializer list + LList(std::initializer_list<T> lst); //- Destructor @@ -131,240 +193,375 @@ public: // Member Functions - // Access + //- The first entry in the list + reference first() + { + return link::ref(LListBase::first()); + } - //- Return the first entry added - T& first() - { - return static_cast<link*>(LListBase::first())->obj_; - } + //- The first entry in the list (const access) + const_reference first() const + { + return link::ref(LListBase::first()); + } - //- Return const access to the first entry added - const T& first() const - { - return static_cast<const link*>(LListBase::first())->obj_; - } + //- The last entry in the list + reference last() + { + return link::ref(LListBase::last()); + } - //- Return the last entry added - T& last() - { - return static_cast<link*>(LListBase::last())->obj_; - } + //- The last entry in the list (const access) + const_reference last() const + { + return link::ref(LListBase::last()); + } - //- Return const access to the last entry added - const T& last() const - { - return static_cast<const link*>(LListBase::last())->obj_; - } + //- Add copy at head of list + void insert(const T& item) + { + LListBase::insert(new link(item)); + } - // Edit + //- Move construct at head of list + void insert(T&& item) + { + LListBase::insert(new link(std::move(item))); + } - //- Add at head of list - void insert(const T& a) - { - LListBase::insert(new link(a)); - } - //- Add at tail of list - void append(const T& a) - { - LListBase::append(new link(a)); - } + //- Add copy at tail of list + void append(const T& item) + { + LListBase::append(new link(item)); + } - //- Remove and return head - T removeHead() - { - link* elmtPtr = static_cast<link*>(LListBase::removeHead()); - T data = elmtPtr->obj_; - delete elmtPtr; - return data; - } + //- Move construct at tail of list + void append(T&& item) + { + LListBase::append(new link(std::move(item))); + } - //- Remove and return element - T remove(link* l) - { - link* elmtPtr = static_cast<link*>(LListBase::remove(l)); - T data = elmtPtr->obj_; - delete elmtPtr; - return data; - } - //- Remove and return element specified by iterator - T remove(iterator& it) - { - link* elmtPtr = static_cast<link*>(LListBase::remove(it)); - T data = elmtPtr->obj_; - delete elmtPtr; - return data; - } + //- Remove and return head + T removeHead() + { + auto p = LListBase::removeHead(); + T obj(std::move(link::ref(p))); + delete p; + return obj; + } + + //- Remove and return element + T remove(link* item) + { + auto p = LListBase::remove(item); + T obj(std::move(link::ref(p))); + delete p; + return obj; + } + + //- Remove and return element specified by iterator + T remove(iterator& iter) + { + auto p = LListBase::remove(iter); + T obj(std::move(link::ref(p))); + delete p; + return obj; + } - //- Delete contents of list - void clear(); - //- Transfer the contents of the argument into this List - // and annul the argument list. - void transfer(LList<LListBase, T>&); + //- Delete contents of list + void clear(); + + //- Transfer the contents of the argument into this List + // and annul the argument list. + void transfer(LList<LListBase, T>& lst); // Member operators - //- Assignment operator - void operator=(const LList<LListBase, T>&); + //- Copy assignment + void operator=(const LList<LListBase, T>& lst); - //- Assignment to an initializer list - void operator=(std::initializer_list<T>); + //- Move assignment + void operator=(LList<LListBase, T>&& lst); + //- Copy assignment from an initializer list + void operator=(std::initializer_list<T> lst); - // STL type definitions - //- Type of values the LList contains. - typedef T value_type; + // IOstream operators - //- Type that can be used for storing into value_type - // objects. - typedef T& reference; + //- Write LList with line-breaks when its length exceeds + //- shortListLen. + // Using '0' suppresses line-breaks entirely. + Ostream& writeList(Ostream& os, const label shortListLen=0) const; - //- Type that can be used for storing into constant - // LList::value_type objects. - typedef const T& const_reference; + //- Read list from Istream + friend Istream& operator>> <LListBase, T> + ( + Istream&, + LList<LListBase, T>& lst + ); - //- The type that can represent the size of a LList. - typedef label size_type; + //- Write LList to Ostream with line breaks, + //- as per writeList() with shortListLen=-1 + friend Ostream& operator<< <LListBase, T> + ( + Ostream& os, + const LList<LListBase, T>& lst + ); // STL iterator - typedef typename LListBase::iterator LListBase_iterator; - //- An STL-conforming iterator class iterator : - public LListBase_iterator + public base_iterator { - public: //- Construct from base iterator - iterator(LListBase_iterator baseIter) + iterator(base_iterator iter) : - LListBase_iterator(baseIter) + base_iterator(iter) {} + reference operator*() const + { + return link::ref(this->get_node()); + } - // Member operators + pointer operator->() const + { + return link::ptr(this->get_node()); + } - T& operator*() const - { - return - static_cast<link&> - (LListBase_iterator::operator*()).obj_; - } + reference operator()() const + { + return operator*(); + } - T& operator()() const - { - return operator*(); - } + iterator& operator++() + { + this->next(); + return *this; + } - iterator& operator++() - { - LListBase_iterator::operator++(); - return *this; - } + iterator& operator--() + { + this->prev(); // May not be implemented + return *this; + } }; - inline iterator begin() - { - return LListBase::begin(); - } - - inline const iterator& end() - { - return static_cast<const iterator&>(LListBase::end()); - } - // STL const_iterator - typedef typename LListBase::const_iterator LListBase_const_iterator; - //- An STL-conforming const_iterator class const_iterator : - public LListBase_const_iterator + public const_base_iterator { - public: - //- Construct from base const_iterator - const_iterator(LListBase_const_iterator baseIter) + //- Construct from base iterator + const_iterator(const_base_iterator iter) : - LListBase_const_iterator(baseIter) + const_base_iterator(iter) {} + //- Construct from base iterator + const_iterator(base_iterator iter) + : + const_base_iterator(iter) + {} + + const_reference operator*() const + { + return link::ref(this->get_node()); + } + + const_pointer operator->() const + { + return link::ptr(this->get_node()); + } + + const_reference operator()() const + { + return operator*(); + } + + const_iterator& operator++() + { + this->next(); + return *this; + } + + const_iterator& operator--() + { + this->prev(); // May not be implemented + return *this; + } + }; + + + // STL reverse_iterator + + //- A reverse_iterator, for LListBase classes that support + //- reverse iteration + class reverse_iterator + : + public base_iterator + { + public: //- Construct from base iterator - const_iterator(LListBase_iterator baseIter) + reverse_iterator(base_iterator iter) : - LListBase_const_iterator(baseIter) + base_iterator(iter) {} + reference operator*() const + { + return link::ref(this->get_node()); + } + + pointer operator->() const + { + return link::ptr(this->get_node()); + } + + reverse_iterator& operator++() + { + this->prev(); // Only if base iterator is bidirectional + return *this; + } + + reverse_iterator& operator--() + { + this->next(); + return *this; + } + }; + + + // STL const_reverse_iterator + + //- A const_reverse_iterator, for LListBase classes that support + //- reverse iteration + class const_reverse_iterator + : + public const_base_iterator + { + public: + + //- Construct from base iterator + const_reverse_iterator(const_base_iterator iter) + : + const_base_iterator(iter) + {} - // Member operators + const_reference operator*() const + { + return link::ref(this->get_node()); + } - const T& operator*() const - { - return - static_cast<const link&> - (LListBase_const_iterator::operator*()).obj_; - } + const_pointer operator->() const + { + return link::ptr(this->get_node()); + } - const T& operator()() const - { - return operator*(); - } + const_reverse_iterator& operator++() + { + this->prev(); // Only if base iterator is bidirectional + return *this; + } - const_iterator& operator++() - { - LListBase_const_iterator::operator++(); - return *this; - } + const_reverse_iterator& operator--() + { + this->next(); + return *this; + } }; + + //- Iterator to first item in list with non-const access + inline iterator begin() + { + return LListBase::template iterator_first<base_iterator>(); + } + + //- Iterator to first item in list with const access inline const_iterator cbegin() const { - return LListBase::cbegin(); + return LListBase::template iterator_first<const_base_iterator>(); } - inline const const_iterator& cend() const + //- Iterator to last item in list with non-const access + inline reverse_iterator rbegin() + { + return LListBase::template iterator_last<base_iterator>(); + } + + //- Iterator to last item in list with const access + inline const_reverse_iterator crbegin() const { - return static_cast<const const_iterator&>(LListBase::cend()); + return LListBase::template iterator_last<const_base_iterator>(); } + //- Iterator to first item in list with const access inline const_iterator begin() const { - return LListBase::begin(); + return LListBase::cbegin(); } - inline const const_iterator& end() const + //- Iterator to last item in list with const access + inline const_reverse_iterator rbegin() const { - return static_cast<const const_iterator&>(LListBase::end()); + return crbegin(); } - // IOstream operators + //- End of list for forward iterators + inline const iterator& end() + { + return LListBase::template iterator_end<iterator>(); + } - friend Istream& operator>> <LListBase, T> - ( - Istream&, - LList<LListBase, T>& - ); + //- End of list for forward iterators + inline const const_iterator& cend() const + { + return LListBase::template iterator_end<const_iterator>(); + } + + //- End of list for reverse iterators + inline const reverse_iterator& rend() + { + return LListBase::template iterator_rend<reverse_iterator>(); + } + + //- End of list for reverse iterators + inline const const_reverse_iterator& crend() const + { + return LListBase::template iterator_rend<const_reverse_iterator>(); + } + + //- End of list for forward iterators + inline const const_iterator& end() const + { + return cend(); + } + + //- End of list for reverse iterators + inline const const_reverse_iterator& rend() const + { + return crend(); + } - friend Ostream& operator<< <LListBase, T> - ( - Ostream&, - const LList<LListBase, T>& - ); }; @@ -376,6 +573,7 @@ public: #ifdef NoRepository #include "LList.C" + #include "LListIO.C" #endif // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/OpenFOAM/containers/LinkedLists/accessTypes/LList/LListIO.C b/src/OpenFOAM/containers/LinkedLists/accessTypes/LList/LListIO.C index 11ce9dd852816ea4ce4435ca8e91dd5cb7340758..2ff55fb6862cfff3b30b1b823e580969216d472f 100644 --- a/src/OpenFOAM/containers/LinkedLists/accessTypes/LList/LListIO.C +++ b/src/OpenFOAM/containers/LinkedLists/accessTypes/LList/LListIO.C @@ -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. @@ -36,39 +36,36 @@ Foam::LList<LListBase, T>::LList(Istream& is) } -// * * * * * * * * * * * * * * * Istream Operator * * * * * * * * * * * * * // +// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // template<class LListBase, class T> -Foam::Istream& Foam::operator>>(Istream& is, LList<LListBase, T>& L) +Foam::Istream& Foam::operator>>(Istream& is, LList<LListBase, T>& lst) { // Anull list - L.clear(); + lst.clear(); is.fatalCheck(FUNCTION_NAME); token firstToken(is); - is.fatalCheck - ( - " operator>>(Istream&, LList<LListBase, T>&) : reading first token" - ); + is.fatalCheck("LList::readList : reading first token"); if (firstToken.isLabel()) { - const label s = firstToken.labelToken(); + const label len = firstToken.labelToken(); // Read beginning of contents - const char delimiter = is.readBeginList("LList<LListBase, T>"); + const char delimiter = is.readBeginList("LList"); - if (s) + if (len) { if (delimiter == token::BEGIN_LIST) { - for (label i=0; i<s; ++i) + for (label i=0; i<len; ++i) { T element; is >> element; - L.append(element); + lst.append(element); } } else @@ -76,9 +73,9 @@ Foam::Istream& Foam::operator>>(Istream& is, LList<LListBase, T>& L) T element; is >> element; - for (label i=0; i<s; ++i) + for (label i=0; i<len; ++i) { - L.append(element); + lst.append(element); } } } @@ -109,9 +106,10 @@ Foam::Istream& Foam::operator>>(Istream& is, LList<LListBase, T>& L) ) { is.putBack(lastToken); + T element; is >> element; - L.append(element); + lst.append(element); is >> lastToken; is.fatalCheck(FUNCTION_NAME); @@ -125,36 +123,66 @@ Foam::Istream& Foam::operator>>(Istream& is, LList<LListBase, T>& L) << exit(FatalIOError); } - // Check state of IOstream is.fatalCheck(FUNCTION_NAME); - return is; } -// * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * // - template<class LListBase, class T> -Foam::Ostream& Foam::operator<<(Ostream& os, const LList<LListBase, T>& lst) +Foam::Ostream& Foam::LList<LListBase, T>::writeList +( + Ostream& os, + const label shortListLen +) const { - // Write size - os << nl << lst.size(); - - // Write beginning of contents - os << nl << token::BEGIN_LIST << nl; + const label len = this->size(); - // Write contents - for (const T& val : lst) + if + ( + len <= 1 || !shortListLen + || (len <= shortListLen) + ) { - os << val << nl; + // Size and start delimiter + os << len << token::BEGIN_LIST; + + // Contents + bool space = false; + for (const T& val : *this) + { + if (space) os << token::SPACE; + os << val; + space = true; + } + + // End delimiter + os << token::END_LIST; } + else + { + // Size and start delimiter + os << nl << len << nl << token::BEGIN_LIST << nl; + + // Contents + for (const T& val : *this) + { + os << val << nl; + } - // Write end of contents - os << token::END_LIST; + // End delimiter + os << token::END_LIST; + } os.check(FUNCTION_NAME); return os; } +template<class LListBase, class T> +Foam::Ostream& Foam::operator<<(Ostream& os, const LList<LListBase, T>& lst) +{ + return lst.writeList(os, -1); // always with line breaks +} + + // ************************************************************************* // diff --git a/src/OpenFOAM/containers/LinkedLists/accessTypes/LPtrList/LPtrList.C b/src/OpenFOAM/containers/LinkedLists/accessTypes/LPtrList/LPtrList.C index d64d9f2d5177e0343811d543719a2b6c782a1ab7..40df96f018deaa7a33109561287cca143246472d 100644 --- a/src/OpenFOAM/containers/LinkedLists/accessTypes/LPtrList/LPtrList.C +++ b/src/OpenFOAM/containers/LinkedLists/accessTypes/LPtrList/LPtrList.C @@ -32,13 +32,22 @@ Foam::LPtrList<LListBase, T>::LPtrList(const LPtrList<LListBase, T>& lst) : LList<LListBase, T*>() { - for (const_iterator iter = lst.begin(); iter != lst.end(); ++iter) + for (auto iter = lst.cbegin(); iter != lst.cend(); ++iter) { - this->append(iter().clone().ptr()); + this->append((*iter).clone().ptr()); } } +template<class LListBase, class T> +Foam::LPtrList<LListBase, T>::LPtrList(LPtrList<LListBase, T>&& lst) +: + LList<LListBase, T*>() +{ + LList<LListBase, T*>::transfer(lst); +} + + // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // template<class LListBase, class T> @@ -53,24 +62,23 @@ Foam::LPtrList<LListBase, T>::~LPtrList() template<class LListBase, class T> bool Foam::LPtrList<LListBase, T>::eraseHead() { - T* tPtr; - if ((tPtr = this->removeHead())) + T* p = this->removeHead(); + + if (p) { - delete tPtr; + delete p; return true; } - else - { - return false; - } + + return false; } template<class LListBase, class T> void Foam::LPtrList<LListBase, T>::clear() { - const label oldSize = this->size(); - for (label i=0; i<oldSize; ++i) + const label len = this->size(); + for (label i=0; i<len; ++i) { eraseHead(); } @@ -94,16 +102,18 @@ void Foam::LPtrList<LListBase, T>::operator=(const LPtrList<LListBase, T>& lst) { clear(); - for (const_iterator iter = lst.begin(); iter != lst.end(); ++iter) + for (auto iter = lst.cbegin(); iter != lst.cend(); ++iter) { - this->append(iter().clone().ptr()); + this->append((*iter).clone().ptr()); } } -// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * // - -#include "LPtrListIO.C" +template<class LListBase, class T> +void Foam::LPtrList<LListBase, T>::operator=(LPtrList<LListBase, T>&& lst) +{ + transfer(lst); +} // ************************************************************************* // diff --git a/src/OpenFOAM/containers/LinkedLists/accessTypes/LPtrList/LPtrList.H b/src/OpenFOAM/containers/LinkedLists/accessTypes/LPtrList/LPtrList.H index a0e22917f9c7fc1b29a17e653c9ab838cb871497..fa7312cbd5072a7cc180b001aac4f00a1cb00d3d 100644 --- a/src/OpenFOAM/containers/LinkedLists/accessTypes/LPtrList/LPtrList.H +++ b/src/OpenFOAM/containers/LinkedLists/accessTypes/LPtrList/LPtrList.H @@ -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. @@ -43,22 +43,22 @@ SourceFiles namespace Foam { -// Forward declaration of friend functions and operators +// Forward declarations template<class LListBase, class T> class LPtrList; template<class LListBase, class T> Istream& operator>> ( - Istream&, - LPtrList<LListBase, T>& + Istream& is, + LPtrList<LListBase, T>& lst ); template<class LListBase, class T> Ostream& operator<< ( - Ostream&, - const LPtrList<LListBase, T>& + Ostream& os, + const LPtrList<LListBase, T>& lst ); @@ -71,45 +71,67 @@ class LPtrList : public LList<LListBase, T*> { +private: + // Private Member Functions //- Read from Istream using given Istream constructor class template<class INew> - void read(Istream&, const INew&); + void read(Istream& is, const INew& inew); public: + // STL type definitions + + //- Pointer for LPtrList::value_type objects. + typedef T* pointer; + + //- Const pointer for LPtrList::value_type objects. + typedef const T* const_pointer; + + //- Reference for LPtrList::value_type objects. + typedef T& reference; + + //- Const reference for LPtrList::value_type objects. + typedef const T& const_reference; + + // Forward declaration of STL iterators class iterator; - friend class iterator; - class const_iterator; - friend class const_iterator; + + using base_iterator = typename LListBase::iterator; + using const_base_iterator = typename LListBase::const_iterator; + + //- The parent list storage + typedef LList<LListBase, T*> parent_type; // Constructors //- Null construct - LPtrList() - {} + LPtrList() = default; - //- Construct given initial T - LPtrList(T* a) - : - LList<LListBase, T*>(a) - {} + //- Construct and insert the initial T item + explicit LPtrList(T* item) + { + this->insert(item); + } + + //- Copy construct by using 'clone()' for each element + LPtrList(const LPtrList& lst); + + //- Move construct + LPtrList(LPtrList&& lst); //- Construct from Istream using given Istream constructor class template<class INew> - LPtrList(Istream&, const INew&); + LPtrList(Istream& is, const INew& inew); //- Construct from Istream using default Istream constructor class - LPtrList(Istream&); - - //- Construct as copy - LPtrList(const LPtrList&); + LPtrList(Istream& is); //- Destructor @@ -118,147 +140,292 @@ public: // Member Functions - // Access + //- The first entry in the list + T& first() + { + return *(parent_type::first()); + } + + //- The first entry in the list (const access) + const T& first() const + { + return *(parent_type::first()); + } + + //- The last entry in the list + T& last() + { + return *(parent_type::last()); + } + + //- The last entry in the list (const access) + const T& last() const + { + return *(parent_type::last()); + } + + + //- Remove the head element from the list and delete the pointer + bool eraseHead(); + + //- Clear the contents of the list + void clear(); + + //- Transfer the contents of the argument into this List + //- and annul the argument list. + void transfer(LPtrList<LListBase, T>& lst); + + + // Member operators - //- Return the first entry added - T& first() + //- Copy assign by using 'clone()' for each element + void operator=(const LPtrList<LListBase, T>& lst); + + //- Move assign + void operator=(LPtrList<LListBase, T>&& lst); + + + // STL iterator + + //- An STL-conforming iterator + class iterator + : + public parent_type::iterator + { + public: + + iterator(base_iterator iter) + : + parent_type::iterator(iter) + {} + + //- Return the address of the object being referenced + pointer get() const { - return *LList<LListBase, T*>::first(); + return parent_type::iterator::operator*(); } - //- Return const access to the first entry added - const T& first() const + reference operator*() const { - return *LList<LListBase, T*>::first(); + return *(this->get()); } - //- Return the last entry added - T& last() + pointer operator->() const { - return *LList<LListBase, T*>::last(); + return this->get(); } - //- Return const access to the last entry added - const T& last() const + reference operator()() const { - return *LList<LListBase, T*>::last(); + return operator*(); } + }; - // Edit + // STL const_iterator - //- Remove the head element from the list and delete the pointer - bool eraseHead(); + //- An STL-conforming const_iterator + class const_iterator + : + public parent_type::const_iterator + { + public: - //- Clear the contents of the list - void clear(); + const_iterator(const_base_iterator iter) + : + parent_type::const_iterator(iter) + {} - //- Transfer the contents of the argument into this List - // and annul the argument list. - void transfer(LPtrList<LListBase, T>&); + const_iterator(base_iterator iter) + : + parent_type::const_iterator(iter) + {} + //- Return the address of the object being referenced + const_pointer get() const + { + return parent_type::const_iterator::operator*(); + } - // Member operators + const_reference operator*() const + { + return *(this->get()); + } - //- Assign copy - void operator=(const LPtrList<LListBase, T>&); + const_pointer operator->() const + { + return this->get(); + } + const_reference operator()() const + { + return operator*(); + } + }; - // STL type definitions - //- Type that can be used for storing into LPtrList::value_type - // objects. - typedef T& reference; + // STL reverse_iterator - //- Type that can be used for storing into constant - // LPtrList::value_type objects. - typedef T& const_reference; + //- A reverse_iterator, for base classes that support + //- reverse iteration + class reverse_iterator + : + public parent_type::reverse_iterator + { + public: + reverse_iterator(base_iterator iter) + : + parent_type::reverse_iterator(iter) + {} - // STL iterator + //- Return the address of the object being referenced + pointer get() const + { + return parent_type::reverse_iterator::operator*(); + } - typedef typename LListBase::iterator LListBase_iterator; + reference operator*() const + { + return *(this->get()); + } - //- An STL-conforming iterator - class iterator + pointer operator->() const + { + return this->get(); + } + + reference operator()() const + { + return operator*(); + } + }; + + + // STL const_reverse_iterator + + //- A const_reverse_iterator, for base classes that support + //- reverse iteration + class const_reverse_iterator : - public LList<LListBase, T*>::iterator + public parent_type::const_reverse_iterator { - public: - //- Construct from base iterator - iterator(LListBase_iterator baseIter) + const_reverse_iterator(const_base_iterator iter) : - LList<LListBase, T*>::iterator(baseIter) + parent_type::const_reverse_iterator(iter) {} + //- Return the address of the object being referenced + const_pointer get() const + { + return parent_type::const_reverse_iterator::operator*(); + } - // Member operators + const_reference operator*() const + { + return *(this->get()); + } - T& operator*() const - { - return *(LList<LListBase, T*>::iterator::operator*()); - } + const_pointer operator->() const + { + return this->get(); + } - T& operator()() const - { - return operator*(); - } + const_reference operator()() const + { + return operator*(); + } }; - // STL const_iterator + //- Iterator to first item in list with non-const access + inline iterator begin() + { + return LListBase::template iterator_first<base_iterator>(); + } - typedef typename LListBase::const_iterator LListBase_const_iterator; + //- Iterator to first item in list with const access + inline const_iterator cbegin() const + { + return LListBase::template iterator_first<const_base_iterator>(); + } - //- An STL-conforming const_iterator - class const_iterator - : - public LList<LListBase, T*>::const_iterator + //- Iterator to last item in list with non-const access + inline reverse_iterator rbegin() { + return LListBase::template iterator_last<base_iterator>(); + } - public: + //- Iterator to last item in list with const access + inline const_reverse_iterator crbegin() const + { + return LListBase::template iterator_last<const_base_iterator>(); + } - //- Construct from base const_iterator - const_iterator(LListBase_const_iterator baseIter) - : - LList<LListBase, T*>::const_iterator(baseIter) - {} + //- Iterator to first item in list with const access + inline const_iterator begin() const + { + return LListBase::cbegin(); + } - //- Construct from base iterator - const_iterator(LListBase_iterator baseIter) - : - LList<LListBase, T*>::const_iterator(baseIter) - {} + //- Iterator to last item in list with const access + inline const_reverse_iterator rbegin() const + { + return crbegin(); + } - // Member operators + //- End of list for forward iterators + inline const iterator& end() + { + return LListBase::template iterator_end<iterator>(); + } - const T& operator*() const - { - return *(LList<LListBase, T*>::const_iterator::operator*()); - } + //- End of list for forward iterators + inline const const_iterator& cend() const + { + return LListBase::template iterator_end<const_iterator>(); + } - const T& operator()() const - { - return operator*(); - } - }; + //- End of list for reverse iterators + inline const reverse_iterator& rend() + { + return LListBase::template iterator_rend<reverse_iterator>(); + } + + //- End of list for reverse iterators + inline const const_reverse_iterator& crend() const + { + return LListBase::template iterator_rend<const_reverse_iterator>(); + } + + //- End of list for forward iterators + inline const const_iterator& end() const + { + return cend(); + } + + //- End of list for reverse iterators + inline const const_reverse_iterator& rend() const + { + return crend(); + } // IOstream operators friend Istream& operator>> <LListBase, T> ( - Istream&, - LPtrList<LListBase, T>& + Istream& is, + LPtrList<LListBase, T>& lst ); friend Ostream& operator<< <LListBase, T> ( - Ostream&, - const LPtrList<LListBase, T>& + Ostream& os, + const LPtrList<LListBase, T>& lst ); }; @@ -271,6 +438,7 @@ public: #ifdef NoRepository #include "LPtrList.C" + #include "LPtrListIO.C" #endif // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/OpenFOAM/containers/LinkedLists/accessTypes/LPtrList/LPtrListIO.C b/src/OpenFOAM/containers/LinkedLists/accessTypes/LPtrList/LPtrListIO.C index 1db2a59a50ef9adc8d794ac59966bc0ac1005d7c..0421dce84f7e055149e231e42a5471a2f516a14f 100644 --- a/src/OpenFOAM/containers/LinkedLists/accessTypes/LPtrList/LPtrListIO.C +++ b/src/OpenFOAM/containers/LinkedLists/accessTypes/LPtrList/LPtrListIO.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -32,60 +32,49 @@ License template<class LListBase, class T> template<class INew> -void Foam::LPtrList<LListBase, T>::read(Istream& is, const INew& iNew) +void Foam::LPtrList<LListBase, T>::read(Istream& is, const INew& inew) { is.fatalCheck(FUNCTION_NAME); token firstToken(is); - is.fatalCheck - ( - "LPtrList<LListBase, T>::read(Istream&, const INew&) : " - "reading first token" - ); + is.fatalCheck("LPtrList::readList : reading first token"); if (firstToken.isLabel()) { - const label s = firstToken.labelToken(); + const label len = firstToken.labelToken(); // Read beginning of contents - const char delimiter = is.readBeginList("LPtrList<LListBase, T>"); + const char delimiter = is.readBeginList("LPtrList"); - if (s) + if (len) { if (delimiter == token::BEGIN_LIST) { - for (label i=0; i<s; ++i) + for (label i=0; i<len; ++i) { - this->append(iNew(is).ptr()); + T* p = inew(is).ptr(); + this->append(p); - is.fatalCheck - ( - "LPtrList<LListBase, T>::read(Istream&, const INew&) : " - "reading entry" - ); + is.fatalCheck("LPtrList::readList : reading entry"); } } else { - T* tPtr = iNew(is).ptr(); - this->append(tPtr); + T* p = inew(is).ptr(); + this->append(p); - is.fatalCheck - ( - "LPtrList<LListBase, T>::read(Istream&, const INew&) : " - "reading entry" - ); + is.fatalCheck("LPtrList::readList : reading entry"); - for (label i=1; i<s; ++i) + for (label i=1; i<len; ++i) { - this->append(tPtr->clone().ptr()); + this->append(p->clone().ptr()); } } } // Read end of contents - is.readEndList("LPtrList<LListBase, T>"); + is.readEndList("LPtrList"); } else if (firstToken.isPunctuation()) { @@ -110,7 +99,7 @@ void Foam::LPtrList<LListBase, T>::read(Istream& is, const INew& iNew) ) { is.putBack(lastToken); - this->append(iNew(is).ptr()); + this->append(inew(is).ptr()); is >> lastToken; is.fatalCheck(FUNCTION_NAME); @@ -134,9 +123,9 @@ void Foam::LPtrList<LListBase, T>::read(Istream& is, const INew& iNew) template<class LListBase, class T> template<class INew> -Foam::LPtrList<LListBase, T>::LPtrList(Istream& is, const INew& iNew) +Foam::LPtrList<LListBase, T>::LPtrList(Istream& is, const INew& inew) { - this->read(is, iNew); + this->read(is, inew); } @@ -164,24 +153,16 @@ Foam::Istream& Foam::operator>>(Istream& is, LPtrList<LListBase, T>& L) template<class LListBase, class T> Foam::Ostream& Foam::operator<<(Ostream& os, const LPtrList<LListBase, T>& lst) { - // Write size - os << nl << lst.size(); - - // Write beginning of contents - os << nl << token::BEGIN_LIST << nl; - - // Write contents - for - ( - typename LPtrList<LListBase, T>::const_iterator iter = lst.begin(); - iter != lst.end(); - ++iter - ) + // Size and start delimiter + os << nl << lst.size() << nl << token::BEGIN_LIST << nl; + + // Contents + for (auto iter = lst.cbegin(); iter != lst.cend(); ++iter) { - os << iter() << nl; + os << *iter << nl; } - // Write end of contents + // End delimiter os << token::END_LIST; os.check(FUNCTION_NAME); diff --git a/src/OpenFOAM/containers/LinkedLists/accessTypes/UILList/UILList.C b/src/OpenFOAM/containers/LinkedLists/accessTypes/UILList/UILList.C index 9c7ee2d550be11bafba19459085ef57dd9717e3d..5266fb0cc4f359819825c75ec217c688b7c1cf28 100644 --- a/src/OpenFOAM/containers/LinkedLists/accessTypes/UILList/UILList.C +++ b/src/OpenFOAM/containers/LinkedLists/accessTypes/UILList/UILList.C @@ -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. @@ -30,9 +30,9 @@ License template<class LListBase, class T> Foam::UILList<LListBase, T>::UILList(const UILList<LListBase, T>& lst) { - for (const_iterator iter = lst.begin(); iter != lst.end(); ++iter) + for (auto iter = lst.cbegin(); iter != lst.cend(); ++iter) { - this->append(&iter()); + this->append(&(*iter)); } } @@ -40,13 +40,13 @@ Foam::UILList<LListBase, T>::UILList(const UILList<LListBase, T>& lst) // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // template<class LListBase, class T> -void Foam::UILList<LListBase, T>::operator=(const UILList<LListBase, T>& rhs) +void Foam::UILList<LListBase, T>::operator=(const UILList<LListBase, T>& lst) { LListBase::clear(); - for (const_iterator iter = rhs.begin(); iter != rhs.end(); ++iter) + for (auto iter = lst.cbegin(); iter != lst.cend(); ++iter) { - this->append(&iter()); + this->append(&(*iter)); } } @@ -57,22 +57,22 @@ bool Foam::UILList<LListBase, T>::operator== const UILList<LListBase, T>& rhs ) const { - bool equal = (this->size() == rhs.size()); - if (!equal) + if (this->size() != rhs.size()) { return false; } - const_iterator iter1 = this->begin(); - const_iterator iter2 = rhs.begin(); + auto iter2 = rhs.cbegin(); - for (; iter1 != this->end(); ++iter1, ++iter2) + for (auto iter1 = this->cbegin(); iter1 != this->cend(); ++iter1, ++iter2) { - equal = (iter1() == iter2()); - if (!equal) break; + if (!(*iter1 == *iter2)) + { + return false; + } } - return equal; + return true; } @@ -86,9 +86,4 @@ bool Foam::UILList<LListBase, T>::operator!= } -// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * // - -#include "UILListIO.C" - - // ************************************************************************* // diff --git a/src/OpenFOAM/containers/LinkedLists/accessTypes/UILList/UILList.H b/src/OpenFOAM/containers/LinkedLists/accessTypes/UILList/UILList.H index 1f5b01b326424bf317df0a87e20bacc1cb8125c4..b2c27aaf0b9639c5ac635668818100f64ce41002 100644 --- a/src/OpenFOAM/containers/LinkedLists/accessTypes/UILList/UILList.H +++ b/src/OpenFOAM/containers/LinkedLists/accessTypes/UILList/UILList.H @@ -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. @@ -44,18 +44,17 @@ SourceFiles namespace Foam { -class Ostream; +// Forward declarations -// Forward declaration of friend functions and operators +class Ostream; -template<class LListBase, class T> -class UILList; +template<class LListBase, class T> class UILList; template<class LListBase, class T> Ostream& operator<< ( - Ostream&, - const UILList<LListBase, T>& + Ostream& os, + const UILList<LListBase, T>& lst ); @@ -68,308 +67,385 @@ class UILList : public LListBase { - public: + // STL type definitions + + //- Type of values stored + typedef T value_type; + + //- Pointer for value_type + typedef T* pointer; + + //- Const pointer for value_type + typedef const T* const_pointer; + + //- Reference for value_type + typedef T& reference; + + //- Const reference for value_type + typedef const T& const_reference; + + //- The type that can represent the container size + typedef label size_type; + + //- The difference between iterator objects + typedef label difference_type; + + // Forward declaration of STL iterators class iterator; - friend class iterator; - class const_iterator; - friend class const_iterator; - class const_reverse_iterator; - friend class const_reverse_iterator; + using base_iterator = typename LListBase::iterator; + using const_base_iterator = typename LListBase::const_iterator; // Constructors //- Null construct - UILList() - {} + UILList() = default; - //- Construct given initial T - UILList(T* a) - : - LListBase(a) - {} + //- Construct and insert the initial T item + explicit UILList(T* item) + { + this->insert(item); + } //- Construct as copy - UILList(const UILList<LListBase, T>&); + UILList(const UILList<LListBase, T>& lst); // Member Functions - // Access - - //- Return the first entry - T* first() - { - return static_cast<T*>(LListBase::first()); - } - - //- Return the first entry - const T* first() const - { - return static_cast<const T*>(LListBase::first()); - } + //- The first entry in the list + T* first() + { + return static_cast<T*>(LListBase::first()); + } - //- Return the last entry - T* last() - { - return static_cast<T*>(LListBase::last()); - } + //- The first entry in the list (const access) + const T* first() const + { + return static_cast<const T*>(LListBase::first()); + } - //- Return the last entry - const T* last() const - { - return static_cast<const T*>(LListBase::last()); - } + //- The last entry in the list + T* last() + { + return static_cast<T*>(LListBase::last()); + } + //- The last entry in the list (const access) + const T* last() const + { + return static_cast<const T*>(LListBase::last()); + } - // Edit - //- Remove and return head - T* removeHead() - { - return static_cast<T*>(LListBase::removeHead()); - } + //- Remove and return head + T* removeHead() + { + return static_cast<T*>(LListBase::removeHead()); + } - //- Remove and return element - T* remove(T* p) - { - return static_cast<T*>(LListBase::remove(p)); - } + //- Remove and return element + T* remove(T* item) + { + return static_cast<T*>(LListBase::remove(item)); + } - //- Remove and return specified by iterator - T* remove(iterator& it) - { - return static_cast<T*>(LListBase::remove(it)); - } + //- Remove and return item specified by iterator + T* remove(iterator& iter) + { + return static_cast<T*>(LListBase::remove(iter)); + } // Member operators - void operator=(const UILList<LListBase, T>&); + //- Copy assignment + void operator=(const UILList<LListBase, T>& lst); + //- Equality. True both lists are element-wise equal + // (using value_type::operator==). Takes linear time. + bool operator==(const UILList<LListBase, T>& lst) const; - // STL type definitions + //- The opposite of the equality operation. Takes linear time. + bool operator!=(const UILList<LListBase, T>& lst) const; - //- Type of values the DLList contains. - typedef T value_type; - //- Type that can be used for storing into DLList::value_type - // objects. - typedef T& reference; + // IOstream operators - //- Type that can be used for storing into constant - // DLList::value_type objects. - typedef const T& const_reference; + //- Write UILList with line-breaks when its length exceeds + //- shortListLen. + // Using '0' suppresses line-breaks entirely. + Ostream& writeList(Ostream& os, const label shortListLen=0) const; - //- The type that can represent the size of a DLList. - typedef label size_type; + //- Write UILList to Ostream with line breaks, + //- as per writeList() with shortListLen=-1 + friend Ostream& operator<< <LListBase, T> + ( + Ostream& os, + const UILList<LListBase, T>& lst + ); // STL iterator - typedef typename LListBase::iterator LListBase_iterator; - - //- An STL-conforming iterator + //- A non-const iterator class iterator : - public LListBase_iterator + public base_iterator { - public: - //- Construct from base iterator - iterator(LListBase_iterator baseIter) + iterator(base_iterator iter) : - LListBase_iterator(baseIter) + base_iterator(iter) {} + //- Return the address of the object being referenced + pointer get() const + { + return static_cast<T*>(base_iterator::get_node()); + } - // Member operators + reference operator*() const + { + return *(this->get()); + } - T& operator*() const - { - return static_cast<T&>(LListBase_iterator::operator*()); - } + pointer operator->() const + { + return this->get(); + } - T& operator()() const - { - return operator*(); - } + reference operator()() const + { + return operator*(); + } - iterator& operator++() - { - LListBase_iterator::operator++(); - return *this; - } + iterator& operator++() + { + this->next(); + return *this; + } }; - inline iterator begin() - { - return LListBase::begin(); - } - - inline const iterator& end() - { - return static_cast<const iterator&>(LListBase::end()); - } - // STL const_iterator - typedef typename LListBase::const_iterator LListBase_const_iterator; - - //- An STL-conforming const_iterator + //- A const_iterator class const_iterator : - public LListBase_const_iterator + public const_base_iterator { - public: //- Construct from base const_iterator - const_iterator(LListBase_const_iterator baseIter) + const_iterator(const_base_iterator iter) : - LListBase_const_iterator(baseIter) + const_base_iterator(iter) {} //- Construct from base iterator - const_iterator(LListBase_iterator baseIter) + const_iterator(base_iterator iter) : - LListBase_const_iterator(baseIter) + const_base_iterator(iter) {} + //- Return the address of the object being referenced + const_pointer get() const + { + return static_cast<const T*>(const_base_iterator::get_node()); + } - // Member operators + const_reference operator*() const + { + return *(this->get()); + } - const T& operator*() const - { - return - static_cast<const T&> - (LListBase_const_iterator::operator*()); - } + const_pointer operator->() const + { + return this->get(); + } - const T& operator()() const - { - return operator*(); - } + const_reference operator()() const + { + return operator*(); + } - const_iterator& operator++() - { - LListBase_const_iterator::operator++(); - return *this; - } + const_iterator& operator++() + { + this->next(); + return *this; + } }; - inline const_iterator cbegin() const - { - return LListBase::cbegin(); - } - inline const const_iterator& cend() const - { - return static_cast<const const_iterator&>(LListBase::cend()); - } + // STL reverse_iterator - inline const_iterator begin() const + //- A reverse_iterator, for LListBase classes that support + //- reverse iteration + class reverse_iterator + : + public base_iterator { - return LListBase::begin(); - } + public: - inline const const_iterator& end() const - { - return static_cast<const const_iterator&>(LListBase::end()); - } + reverse_iterator(base_iterator iter) + : + base_iterator(iter) + {} + + //- Return the address of the object being referenced + pointer get() const + { + return static_cast<T*>(base_iterator::get_node()); + } + + reference operator*() const + { + return *(this->get()); + } + + pointer operator->() const + { + return this->get(); + } + + reference operator()() const + { + return operator*(); + } + + reverse_iterator& operator++() + { + this->prev(); // Only if base iterator is bidirectional + return *this; + } + }; // STL const_reverse_iterator - //- An STL-conforming const_reverse_iterator + //- A const_reverse_iterator, for LListBase classes that support + //- reverse iteration class const_reverse_iterator : - public LListBase::const_reverse_iterator + public const_base_iterator { - public: - //- Construct from base const_reverse_iterator - const_reverse_iterator - ( - typename LListBase::const_reverse_iterator baseIter - ) + const_reverse_iterator(const_base_iterator iter) : - LListBase::const_reverse_iterator(baseIter) + const_base_iterator(iter) {} + //- Return the address of the object being referenced + const_pointer get() const + { + return static_cast<const T*>(const_base_iterator::get_node()); + } - // Member operators + const_reference operator*() const + { + return *(this->get()); + } - const T& operator*() const - { - return - static_cast<const T&> - (LListBase::const_reverse_iterator::operator*()); - } + const_pointer operator->() const + { + return this->get(); + } - const T& operator()() const - { - return operator*(); - } + const_reference operator()() const + { + return operator*(); + } - const_reverse_iterator& operator++() - { - LListBase::const_reverse_iterator::operator++(); - return *this; - } + const_reverse_iterator& operator++() + { + this->prev(); // Only if base iterator is bidirectional + return *this; + } }; - inline const_reverse_iterator crbegin() const + + //- Iterator to first item in list with non-const access + inline iterator begin() { - return LListBase::crbegin(); + return LListBase::template iterator_first<base_iterator>(); } - inline const const_reverse_iterator& crend() const + //- Iterator to first item in list with const access + inline const_iterator cbegin() const { - return - static_cast<const const_reverse_iterator&>(LListBase::crend()); + return LListBase::template iterator_first<const_base_iterator>(); } + //- Iterator to last item in list with non-const access + inline reverse_iterator rbegin() + { + return LListBase::template iterator_last<base_iterator>(); + } - inline const_reverse_iterator rbegin() const + //- Iterator to last item in list with const access + inline const_reverse_iterator crbegin() const { - return LListBase::rbegin(); + return LListBase::template iterator_last<const_base_iterator>(); } - inline const const_reverse_iterator& rend() const + //- Iterator to first item in list with const access + inline const_iterator begin() const { - return - static_cast<const const_reverse_iterator&>(LListBase::rend()); + return LListBase::cbegin(); } + //- Iterator to last item in list with const access + inline const_reverse_iterator rbegin() const + { + return crbegin(); + } - // STL member operators - //- Equality operation on ULists of the same type. - // Returns true when the ULists are element-wise equal - // (using UList::value_type::operator==). Takes linear time. - bool operator==(const UILList<LListBase, T>&) const; + //- End of list for forward iterators + inline const iterator& end() + { + return LListBase::template iterator_end<iterator>(); + } - //- The opposite of the equality operation. Takes linear time. - bool operator!=(const UILList<LListBase, T>&) const; + //- End of list for forward iterators + inline const const_iterator& cend() const + { + return LListBase::template iterator_end<const_iterator>(); + } + //- End of list for reverse iterators + inline const reverse_iterator& rend() + { + return LListBase::template iterator_rend<reverse_iterator>(); + } - // Ostream operator + //- End of list for reverse iterators + inline const const_reverse_iterator& crend() const + { + return LListBase::template iterator_rend<const_reverse_iterator>(); + } + + //- End of list for forward iterators + inline const const_iterator& end() const + { + return cend(); + } + + //- End of list for reverse iterators + inline const const_reverse_iterator& rend() const + { + return crend(); + } - friend Ostream& operator<< <LListBase, T> - ( - Ostream&, - const UILList<LListBase, T>& - ); }; @@ -381,6 +457,7 @@ public: #ifdef NoRepository #include "UILList.C" + #include "UILListIO.C" #endif // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/OpenFOAM/containers/LinkedLists/accessTypes/UILList/UILListIO.C b/src/OpenFOAM/containers/LinkedLists/accessTypes/UILList/UILListIO.C index 3f34eb83116c287fc865c409f57ea290c33d457f..843f75e39b8ee5992c58f3256e5f1cb7477913b4 100644 --- a/src/OpenFOAM/containers/LinkedLists/accessTypes/UILList/UILListIO.C +++ b/src/OpenFOAM/containers/LinkedLists/accessTypes/UILList/UILListIO.C @@ -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. @@ -30,30 +30,61 @@ License // * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * // template<class LListBase, class T> -Foam::Ostream& Foam::operator<<(Ostream& os, const UILList<LListBase, T>& lst) +Foam::Ostream& Foam::UILList<LListBase, T>::writeList +( + Ostream& os, + const label shortListLen +) const { - // Write size - os << nl << lst.size(); - - // Write beginning of contents - os << nl << token::BEGIN_LIST << nl; + const label len = this->size(); - // Write contents - for + if ( - typename UILList<LListBase, T>::const_iterator iter = lst.begin(); - iter != lst.end(); - ++iter + len <= 1 || !shortListLen + || (len <= shortListLen) ) { - os << iter() << nl; + // Size and start delimiter + os << len << token::BEGIN_LIST; + + // Contents + bool space = false; + for (const T& val : *this) + { + if (space) os << token::SPACE; + space = true; + os << val; + } + + // End delimiter + os << token::END_LIST; } + else + { + // Size and start delimiter + os << nl << len << nl << token::BEGIN_LIST << nl; + + // Contents + for (const T& val : *this) + { + os << val << nl; + } - // Write end of contents - os << token::END_LIST; + // End delimiter + os << token::END_LIST; + } os.check(FUNCTION_NAME); return os; + } + +template<class LListBase, class T> +Foam::Ostream& Foam::operator<<(Ostream& os, const UILList<LListBase, T>& lst) +{ + return lst.writeList(os, -1); +} + + // ************************************************************************* // diff --git a/src/OpenFOAM/containers/LinkedLists/linkTypes/DLListBase/DLListBase.C b/src/OpenFOAM/containers/LinkedLists/linkTypes/DLListBase/DLListBase.C index f428836f43bab04790383273b1d2f8d127124852..25228b2a6a73e97594f9aaa1892e852e57ae2a27 100644 --- a/src/OpenFOAM/containers/LinkedLists/linkTypes/DLListBase/DLListBase.C +++ b/src/OpenFOAM/containers/LinkedLists/linkTypes/DLListBase/DLListBase.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -23,158 +23,142 @@ License \*---------------------------------------------------------------------------*/ -#include "error.H" - #include "DLListBase.H" -#include "IOstreams.H" - -// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // - -Foam::DLListBase::iterator Foam::DLListBase::endIter_ -( - const_cast<DLListBase&>(static_cast<const DLListBase&>(DLListBase())) -); - -Foam::DLListBase::const_iterator Foam::DLListBase::endConstIter_ -( - static_cast<const DLListBase&>(DLListBase()), - reinterpret_cast<const link*>(0) -); - -Foam::DLListBase::const_reverse_iterator Foam::DLListBase::endConstRevIter_ -( - static_cast<const DLListBase&>(DLListBase()), - reinterpret_cast<const link*>(0) -); - +#include "error.H" // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // -void Foam::DLListBase::insert(DLListBase::link* a) +void Foam::DLListBase::insert(DLListBase::link* item) { - nElmts_++; + if (!item) + { + return; + } + + ++size_; if (!first_) { - a->prev_ = a; - a->next_ = a; - first_ = last_ = a; + item->prev_ = item; + item->next_ = item; + first_ = last_ = item; } else { - a->prev_ = a; - a->next_ = first_; - first_->prev_ = a; - first_ = a; + item->prev_ = item; + item->next_ = first_; + first_->prev_ = item; + first_ = item; } } -void Foam::DLListBase::append(DLListBase::link* a) +void Foam::DLListBase::append(DLListBase::link* item) { - nElmts_++; + if (!item) + { + return; + } + + ++size_; if (!first_) { - a->prev_ = a; - a->next_ = a; - first_ = last_ = a; + item->prev_ = item; + item->next_ = item; + first_ = last_ = item; } else { - last_->next_ = a; - a->prev_ = last_; - a->next_ = a; - last_ = a; + last_->next_ = item; + item->prev_ = last_; + item->next_ = item; + last_ = item; } } bool Foam::DLListBase::swapUp(DLListBase::link* a) { - if (first_ != a) + if (first_ == a) { - link* ap = a->prev_; - - if (ap == first_) - { - first_ = a; - ap->prev_ = a; - } - else - { - ap->prev_->next_ = a; - } - - if (a == last_) - { - last_ = ap; - a->next_ = ap; - } - else - { - a->next_->prev_ = ap; - } - - a->prev_ = ap->prev_; + return false; + } + + DLListBase::link *ap = a->prev_; + + if (ap == first_) + { + first_ = a; ap->prev_ = a; + } + else + { + ap->prev_->next_ = a; + } - ap->next_ = a->next_; + if (a == last_) + { + last_ = ap; a->next_ = ap; - - return true; } else { - return false; + a->next_->prev_ = ap; } + + a->prev_ = ap->prev_; + ap->prev_ = a; + + ap->next_ = a->next_; + a->next_ = ap; + + return true; } bool Foam::DLListBase::swapDown(DLListBase::link* a) { - if (last_ != a) + if (last_ == a) + { + return false; + } + + DLListBase::link *an = a->next_; + + if (a == first_) { - link* an = a->next_; - - if (a == first_) - { - first_ = an; - a->prev_ = an; - } - else - { - a->prev_->next_ = an; - } - - if (an == last_) - { - last_ = a; - an->next_ = a; - } - else - { - an->next_->prev_ = a; - } - - an->prev_ = a->prev_; + first_ = an; a->prev_ = an; + } + else + { + a->prev_->next_ = an; + } - a->next_ = an->next_; + if (an == last_) + { + last_ = a; an->next_ = a; - - return true; } else { - return false; + an->next_->prev_ = a; } + + an->prev_ = a->prev_; + a->prev_ = an; + + a->next_ = an->next_; + an->next_ = a; + + return true; } Foam::DLListBase::link* Foam::DLListBase::removeHead() { - nElmts_--; + --size_; if (!first_) { @@ -183,44 +167,44 @@ Foam::DLListBase::link* Foam::DLListBase::removeHead() << abort(FatalError); } - DLListBase::link* f = first_; - first_ = f->next_; + DLListBase::link *ret = first_; + first_ = first_->next_; if (!first_) { last_ = nullptr; } - f->deregister(); - return f; + ret->deregister(); + return ret; } -Foam::DLListBase::link* Foam::DLListBase::remove(DLListBase::link* l) +Foam::DLListBase::link* Foam::DLListBase::remove(DLListBase::link* item) { - nElmts_--; + --size_; - link* ret = l; + DLListBase::link *ret = item; - if (l == first_ && first_ == last_) + if (item == first_ && first_ == last_) { first_ = nullptr; last_ = nullptr; } - else if (l == first_) + else if (item == first_) { first_ = first_->next_; first_->prev_ = first_; } - else if (l == last_) + else if (item == last_) { last_ = last_->prev_; last_->next_ = last_; } else { - l->next_->prev_ = l->prev_; - l->prev_->next_ = l->next_; + item->next_->prev_ = item->prev_; + item->prev_->next_ = item->next_; } ret->deregister(); @@ -234,7 +218,7 @@ Foam::DLListBase::link* Foam::DLListBase::replace DLListBase::link* newLink ) { - link* ret = oldLink; + DLListBase::link *ret = oldLink; newLink->prev_ = oldLink->prev_; newLink->next_ = oldLink->next_; diff --git a/src/OpenFOAM/containers/LinkedLists/linkTypes/DLListBase/DLListBase.H b/src/OpenFOAM/containers/LinkedLists/linkTypes/DLListBase/DLListBase.H index d65363113eafaa3991bbc35040fda8dc8099e71c..4a9cd19e6d3d41e5aeb296193c518ad76b1d67c5 100644 --- a/src/OpenFOAM/containers/LinkedLists/linkTypes/DLListBase/DLListBase.H +++ b/src/OpenFOAM/containers/LinkedLists/linkTypes/DLListBase/DLListBase.H @@ -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. @@ -27,6 +27,13 @@ Class Description Base for doubly-linked lists. + The iterators associated with the list only have a core functionality + for navigation, with additional functionality to be added by inheriting + classes. The node iterators always have a node-pointer as the + first member data, which allows reinterpret_cast from anything else with + a nullptr as its first data member. + The nullObject is such an item (with a nullptr data member). + SourceFiles DLListBaseI.H DLListBase.C @@ -36,9 +43,9 @@ SourceFiles #ifndef DLListBase_H #define DLListBase_H -#include "bool.H" #include "label.H" #include "uLabel.H" +#include <utility> // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -51,35 +58,40 @@ namespace Foam class DLListBase { - public: - //- Link structure + //- The structure for a doubly-linked storage node struct link { + //- Pointer to prev entry in list + link* prev_ = nullptr; + //- Pointer to next entry in list - link *prev_, *next_; + link* next_ = nullptr; //- Null construct - inline link(); + link() = default; - //- Check if the link is registered with the DLListBase + //- Check if the node is registered with the list inline bool registered() const; - //- Deregister the link after removal + //- Deregister the node after removal inline void deregister(); }; private: - // Private data + // Private Member Data - //- first_ points to first element and last_ points to last element. - link *first_, *last_; + //- Pointer to first element + link *first_ = nullptr; - //- Number of elements in in list - label nElmts_; + //- Pointer to last element + link *last_ = nullptr; + + //- Number of elements in the list + label size_ = 0; // Private Member Functions @@ -91,9 +103,34 @@ private: void operator=(const DLListBase&) = delete; +protected: + + // Protected Member Functions + + //- Factory method to return an iterator end + // Simply reinterprets a NullObject as a DLListBase iterator. + template<class IteratorType> + inline static const IteratorType& iterator_end(); + + //- Factory method to return an iterator reverse end + // Simply reinterprets a NullObject as a DLListBase iterator. + template<class IteratorType> + inline static const IteratorType& iterator_rend(); + + //- Return iterator to first item or end-iterator if list is empty + // Removes constness which the caller promises to manage. + template<class IteratorType> + inline IteratorType iterator_first() const; + + //- Return iterator to last item or end-iterator if list is empty + // Removes constness which the caller promises to manage. + template<class IteratorType> + inline IteratorType iterator_last() const; + + public: - // Forward declaration of STL iterators + // Forward declaration of iterators class iterator; friend class iterator; @@ -101,213 +138,185 @@ public: class const_iterator; friend class const_iterator; - class const_reverse_iterator; - friend class const_reverse_iterator; - // Constructors //- Null construct - inline DLListBase(); - - //- Construct given initial entry - inline DLListBase(link*); + DLListBase() = default; //- Destructor - ~DLListBase(); + ~DLListBase() = default; // Member Functions - // Access + //- Return number of elements in list + inline label size() const; - //- Return number of elements in list - inline label size() const; + //- Return true if the list is empty + inline bool empty() const; - //- Return true if the list is empty - inline bool empty() const; + //- Return first entry + inline link* first(); - //- Return first entry - inline link* first(); + //- Return const access to first entry + inline const link* first() const; - //- Return const access to first entry - inline const link* first() const; + //- Return last entry + inline link* last(); - //- Return last entry - inline link* last(); + //- Return const access to last entry + inline const link* last() const; - //- Return const access to last entry - inline const link* last() const; + //- Add at head of list + void insert(link* item); - // Edit + //- Add at tail of list + void append(link* item); - //- Add at head of list - void insert(link*); + //- Swap this element with the one above unless it is at the top + bool swapUp(link* item); - //- Add at tail of list - void append(link*); + //- Swap this element with the one below unless it is at the bottom + bool swapDown(link* item); - //- Swap this element with the one above unless it is at the top - bool swapUp(link*); + //- Remove and return head + link* removeHead(); - //- Swap this element with the one below unless it is at the bottom - bool swapDown(link*); + //- Remove and return element + link* remove(link* item); - //- Remove and return head - link* removeHead(); + // Remove and return element specified by iterator + inline link* remove(iterator& iter); - //- Remove and return element - link* remove(link*); + //- Replace oldLink with newLink and return element + link* replace(link* oldLink, link* newLink); - // Remove and return element specified by iterator - inline link* remove(iterator&); + //- Replace oldIter with newItem and return element + inline link* replace(iterator& oldIter, link* newitem); - //- Replace oldLink with newLink and return element - link* replace(link* oldLink, link* newLink); + //- Clear the list + inline void clear(); - //- Replace oldIter with newLink and return element - inline link* replace(iterator& oldIter, link* newLink); + //- Swap the contents of the list + inline void swap(DLListBase& lst); - //- Clear the list - inline void clear(); + //- Transfer the contents of the argument into this list + //- and annul the argument list. + inline void transfer(DLListBase& lst); - //- Transfer the contents of the argument into this List - // and annul the argument list. - inline void transfer(DLListBase&); - // STL iterator + // iterator - //- An STL-conforming iterator + //- A primitive non-const node iterator. + // Needs to be extended by inheriting classes. class iterator { friend class DLListBase; friend class const_iterator; - //- Reference to the list this is an iterator for - DLListBase& curList_; - - //- Current element - link* curElmt_; + //- The selected node. + // MUST be the first member for easy comparison between iterators + // and for reinterpret_cast from nullObject + link* node_; - //- Copy of the link - link curLink_; + //- The list being iterated on + DLListBase* list_; - //- Construct for a given SLListBase with nullptr element and link. - // Only used to create endIter - inline iterator(DLListBase&); + //- Copy of the node prev/next pointers (to use after removal) + link copy_; public: - //- Construct for a given DLListBase and link - inline iterator(DLListBase&, link*); + //- Construct for a node on a list + inline iterator(DLListBase* list, link* item); - //- Currently pointing at a valid entry + //- The storage node + inline link* get_node() const; + + //- Pointing at a valid storage node inline bool found() const; - inline void operator=(const iterator& iter); + //- Move backward through list + inline void prev(); - inline bool operator==(const iterator& iter) const; - inline bool operator!=(const iterator& iter) const; + //- Move forward through list + inline void next(); - inline link& operator*() const; + inline void operator=(const iterator& iter); - inline iterator& operator++(); - inline iterator operator++(int); + inline bool operator==(const iterator&) const; + inline bool operator!=(const iterator&) const; }; - inline iterator begin(); - inline const iterator& end(); + // const_iterator - // STL const_iterator - - //- An STL-conforming const_iterator + //- A primitive const node iterator (bidirectional). + // Must normally be extended by inheriting classes. + // Since this iterator works bidirectionally, it can be used as the + // basis for a derived const_reverse_iterator class const_iterator { - //- Reference to the list this is an iterator for - const DLListBase& curList_; + //- The selected node. + // MUST be the first member for easy comparison between iterators + // and for reinterpret_cast from nullObject + const link* node_; - //- Current element - const link* curElmt_; + //- The list being iterated on (as pointer for bitwise copy) + const DLListBase* list_; public: - //- Construct for a given DLListBase and link - inline const_iterator(const DLListBase&, const link*); + //- Construct for a node on a list + inline const_iterator(const DLListBase* list, const link* item); - //- Construct from a non-const iterator + //- Copy construct from a non-const iterator inline const_iterator(const DLListBase::iterator& iter); - //- Currently pointing at a valid entry - inline bool found() const; - - inline void operator=(const const_iterator& iter); - - inline bool operator==(const const_iterator& iter) const; - inline bool operator!=(const const_iterator& iter) const; - - inline const link& operator*() const; + //- Copy construct + const_iterator(const const_iterator&) = default; - inline const_iterator& operator++(); - inline const_iterator operator++(int); - }; + //- The storage node + inline const link* get_node() const; - inline const_iterator cbegin() const; - inline const const_iterator& cend() const; - - inline const_iterator begin() const; - inline const const_iterator& end() const; - - - // STL const_reverse_iterator - - //- An STL-conforming const_reverse_iterator - class const_reverse_iterator - { - //- Reference to the list this is an reverse_iterator for - const DLListBase& curList_; - - //- Current element - const link* curElmt_; - - public: - - //- Construct for a given DLListBase and link - inline const_reverse_iterator(const DLListBase& lst, const link*); - - //- Currently pointing at a valid entry + //- Pointing at a valid storage node inline bool found() const; - inline void operator=(const const_reverse_iterator& iter); + //- Move backward through list + inline void prev(); - inline bool operator==(const const_reverse_iterator& iter) const; - inline bool operator!=(const const_reverse_iterator& iter) const; + //- Move forward through list + inline void next(); - inline const link& operator*() const; + const_iterator& operator=(const const_iterator&) = default; - inline const_reverse_iterator& operator++(); - inline const_reverse_iterator operator++(int); + inline bool operator==(const const_iterator&) const; + inline bool operator!=(const const_iterator&) const; }; - inline const_reverse_iterator crbegin() const; - inline const const_reverse_iterator& crend() const; - inline const_reverse_iterator rbegin() const; - inline const const_reverse_iterator& rend() const; + //- Iterator to first item in list with non-const access + inline iterator begin(); + //- Iterator to first item in list with const access + inline const_iterator cbegin() const; -private: + //- Iterator to last item in list with const access + // Note that this is not a const_reverse_iterator, this is the + // responsibilty of any derived classes. + inline const_iterator crbegin() const; - //- Iterator returned by end() - static iterator endIter_; + //- End of list for iterators + inline const iterator& end(); - //- const_iterator returned by end() - static const_iterator endConstIter_; + //- End of list for iterators + inline const const_iterator& cend() const; - //- const_reverse_iterator returned by end() - static const_reverse_iterator endConstRevIter_; + //- End of list for reverse iterators + inline const const_iterator& crend() const; }; diff --git a/src/OpenFOAM/containers/LinkedLists/linkTypes/DLListBase/DLListBaseI.H b/src/OpenFOAM/containers/LinkedLists/linkTypes/DLListBase/DLListBaseI.H index 6dc080777a9b39d4c9a0a9a973023b3d5b9e9de0..6f072108218ec851018abef9c414a16d44025708 100644 --- a/src/OpenFOAM/containers/LinkedLists/linkTypes/DLListBase/DLListBaseI.H +++ b/src/OpenFOAM/containers/LinkedLists/linkTypes/DLListBase/DLListBaseI.H @@ -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. @@ -24,39 +24,74 @@ License \*---------------------------------------------------------------------------*/ #include "error.H" +#include "nullObject.H" -// * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * // +// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * // -inline Foam::DLListBase::link::link() -: - prev_(nullptr), - next_(nullptr) -{} +template<class IteratorType> +inline const IteratorType& Foam::DLListBase::iterator_end() +{ + return *reinterpret_cast<const IteratorType*>(nullObjectPtr); +} -inline Foam::DLListBase::DLListBase() -: - first_(nullptr), - last_(nullptr), - nElmts_(0) -{} +template<class IteratorType> +inline const IteratorType& Foam::DLListBase::iterator_rend() +{ + return *reinterpret_cast<const IteratorType*>(nullObjectPtr); +} -inline Foam::DLListBase::DLListBase(link* a) -: - first_(a), - last_(a), - nElmts_(1) +template<class IteratorType> +inline IteratorType Foam::DLListBase::iterator_first() const { - a->prev_ = a; - a->next_ = a; + DLListBase* list = const_cast<DLListBase*>(this); + + if (size()) + { + return IteratorType(list, const_cast<DLListBase::link*>(first_)); + } + + // Return an end iterator + return IteratorType(list, nullptr); } -// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // +template<class IteratorType> +inline IteratorType Foam::DLListBase::iterator_last() const +{ + DLListBase* list = const_cast<DLListBase*>(this); -inline Foam::DLListBase::~DLListBase() -{} + if (size()) + { + return IteratorType(list, const_cast<DLListBase::link*>(last_)); + } + + // Return an end iterator + return IteratorType(list, nullptr); +} + + +// * * * * * * * * * * * * * * * Iterator ends * * * * * * * * * * * * * * * // + +inline const Foam::DLListBase::iterator& Foam::DLListBase::end() +{ + return iterator_end<DLListBase::iterator>(); +} + + +inline const Foam::DLListBase::const_iterator& +Foam::DLListBase::cend() const +{ + return iterator_end<DLListBase::const_iterator>(); +} + + +inline const Foam::DLListBase::const_iterator& +Foam::DLListBase::crend() const +{ + return iterator_rend<DLListBase::const_iterator>(); +} // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // @@ -69,27 +104,26 @@ inline bool Foam::DLListBase::link::registered() const inline void Foam::DLListBase::link::deregister() { - prev_ = nullptr; - next_ = nullptr; + prev_ = next_ = nullptr; } inline Foam::label Foam::DLListBase::size() const { - return nElmts_; + return size_; } inline bool Foam::DLListBase::empty() const { - return !nElmts_; + return !size_; } inline Foam::DLListBase::link* Foam::DLListBase::first() { - if (!nElmts_) + if (!size_) { FatalErrorInFunction << "list is empty" @@ -102,7 +136,7 @@ Foam::DLListBase::first() inline const Foam::DLListBase::link* Foam::DLListBase::first() const { - if (!nElmts_) + if (!size_) { FatalErrorInFunction << "list is empty" @@ -115,7 +149,7 @@ Foam::DLListBase::first() const inline Foam::DLListBase::link* Foam::DLListBase::last() { - if (!nElmts_) + if (!size_) { FatalErrorInFunction << "list is empty" @@ -128,7 +162,7 @@ Foam::DLListBase::last() inline const Foam::DLListBase::link* Foam::DLListBase::last() const { - if (!nElmts_) + if (!size_) { FatalErrorInFunction << "list is empty" @@ -142,15 +176,23 @@ inline void Foam::DLListBase::clear() { first_ = nullptr; last_ = nullptr; - nElmts_ = 0; + size_ = 0; +} + + +inline void Foam::DLListBase::swap(DLListBase& lst) +{ + std::swap(first_, lst.first_); + std::swap(last_, lst.last_); + std::swap(size_, lst.size_); } inline void Foam::DLListBase::transfer(DLListBase& lst) { - first_ = lst.first_; - last_ = lst.last_; - nElmts_ = lst.nElmts_; + first_ = lst.first_; + last_ = lst.last_; + size_ = lst.size_; lst.clear(); } @@ -159,10 +201,10 @@ inline void Foam::DLListBase::transfer(DLListBase& lst) inline Foam::DLListBase::link* Foam::DLListBase::remove ( - DLListBase::iterator& it + DLListBase::iterator& iter ) { - return remove(it.curElmt_); + return remove(iter.node_); } @@ -170,88 +212,100 @@ inline Foam::DLListBase::link* Foam::DLListBase::replace ( DLListBase::iterator& oldIter, - DLListBase::link* newLink + DLListBase::link* newItem ) { - return replace(oldIter.curElmt_, newLink); + return replace(oldIter.node_, newItem); } // * * * * * * * * * * * * * * * STL iterator * * * * * * * * * * * * * * * // -inline Foam::DLListBase::iterator::iterator(DLListBase& s, link* elmt) -: - curList_(s), - curElmt_(elmt), - curLink_(*curElmt_) -{} - - -inline Foam::DLListBase::iterator::iterator(DLListBase& s) +inline Foam::DLListBase::iterator::iterator +( + DLListBase* list, + DLListBase::link* item +) : - curList_(s), - curElmt_(nullptr), - curLink_() -{} + node_(item), + list_(list), + copy_() +{ + if (node_ != nullptr) + { + copy_ = *node_; + } +} -inline bool Foam::DLListBase::iterator::found() const +inline Foam::DLListBase::link* +Foam::DLListBase::iterator::get_node() const { - return (curElmt_ != nullptr); + return node_; } -inline void Foam::DLListBase::iterator::operator=(const iterator& iter) +inline bool Foam::DLListBase::iterator::found() const { - curElmt_ = iter.curElmt_; - curLink_ = iter.curLink_; + return (node_ != nullptr); } -inline bool Foam::DLListBase::iterator::operator==(const iterator& iter) const +inline void Foam::DLListBase::iterator::prev() { - return curElmt_ == iter.curElmt_; + if (list_) + { + // Check if the node_ is the first element (points to itself) + // or if the list is empty because last element was removed + if (node_ == copy_.prev_ || list_->first_ == nullptr) + { + node_ = nullptr; + } + else + { + node_ = copy_.prev_; + copy_ = *node_; + } + } } -inline bool Foam::DLListBase::iterator::operator!=(const iterator& iter) const +inline void Foam::DLListBase::iterator::next() { - return curElmt_ != iter.curElmt_; + if (list_) + { + // Check if the node_ is the last element (points to itself) + // or if the list is empty because last element was removed + if (node_ == copy_.next_ || list_->last_ == nullptr) + { + node_ = nullptr; + } + else + { + node_ = copy_.next_; + copy_ = *node_; + } + } } -inline Foam::DLListBase::link& -Foam::DLListBase::iterator::operator*() const +inline void Foam::DLListBase::iterator::operator=(const iterator& iter) { - return *curElmt_; + node_ = iter.node_; + list_ = iter.list_; + copy_ = iter.copy_; } -inline Foam::DLListBase::iterator& -Foam::DLListBase::iterator::operator++() +inline bool Foam::DLListBase::iterator::operator==(const iterator& iter) const { - // Check if the curElmt_ is the last element (if it points to itself) - // or if the list is empty because the last element may have been removed - if (curLink_.next_ == curElmt_ || curList_.last_ == nullptr) - { - curElmt_ = nullptr; - } - else - { - curElmt_ = curLink_.next_; - curLink_ = *curElmt_; - } - - return *this; + return node_ == iter.node_; } -inline Foam::DLListBase::iterator -Foam::DLListBase::iterator::operator++(int) +inline bool Foam::DLListBase::iterator::operator!=(const iterator& iter) const { - iterator tmp = *this; - ++*this; - return tmp; + return node_ != iter.node_; } @@ -260,18 +314,10 @@ Foam::DLListBase::begin() { if (size()) { - return iterator(*this, first()); - } - else - { - return endIter_; + return iterator_first<iterator>(); } -} - -inline const Foam::DLListBase::iterator& Foam::DLListBase::end() -{ - return endIter_; + return end(); } @@ -279,12 +325,12 @@ inline const Foam::DLListBase::iterator& Foam::DLListBase::end() inline Foam::DLListBase::const_iterator::const_iterator ( - const DLListBase& s, - const link* elmt + const DLListBase* list, + const DLListBase::link* item ) : - curList_(s), - curElmt_(elmt) + node_(item), + list_(list) {} @@ -293,221 +339,95 @@ inline Foam::DLListBase::const_iterator::const_iterator const DLListBase::iterator& iter ) : - curList_(iter.curList_), - curElmt_(iter.curElmt_) + node_(iter.node_), + list_(iter.list_) {} -inline bool Foam::DLListBase::const_iterator::found() const -{ - return (curElmt_ != nullptr); -} - - -inline void Foam::DLListBase::const_iterator::operator= -( - const const_iterator& iter -) -{ - curElmt_ = iter.curElmt_; -} - - -inline bool Foam::DLListBase::const_iterator::operator== -( - const const_iterator& iter -) const -{ - return curElmt_ == iter.curElmt_; -} - - -inline bool Foam::DLListBase::const_iterator::operator!= -( - const const_iterator& iter -) const +inline const Foam::DLListBase::link* +Foam::DLListBase::const_iterator::get_node() const { - return curElmt_ != iter.curElmt_; + return node_; } -inline const Foam::DLListBase::link& -Foam::DLListBase::const_iterator::operator*() const +inline bool Foam::DLListBase::const_iterator::found() const { - return *curElmt_; + return (node_ != nullptr); } -inline Foam::DLListBase::const_iterator& -Foam::DLListBase::const_iterator::operator++() +inline void Foam::DLListBase::const_iterator::prev() { - if (curElmt_ == curList_.last_) - { - curElmt_ = nullptr; - } - else + if (list_ && node_) { - curElmt_ = curElmt_->next_; + if (node_ == list_->first_) + { + node_ = nullptr; + } + else + { + node_ = node_->prev_; + } } - - return *this; } -inline Foam::DLListBase::const_iterator -Foam::DLListBase::const_iterator::operator++(int) -{ - const_iterator tmp = *this; - ++*this; - return tmp; -} - - -inline Foam::DLListBase::const_iterator -Foam::DLListBase::cbegin() const +inline void Foam::DLListBase::const_iterator::next() { - if (size()) - { - return const_iterator(*this, first()); - } - else + if (list_ && node_) { - return endConstIter_; + if (node_ == list_->last_) + { + node_ = nullptr; + } + else + { + node_ = node_->next_; + } } } -inline const Foam::DLListBase::const_iterator& -Foam::DLListBase::cend() const -{ - return endConstIter_; -} - - -inline Foam::DLListBase::const_iterator -Foam::DLListBase::begin() const -{ - return this->cbegin(); -} - - -inline const Foam::DLListBase::const_iterator& -Foam::DLListBase::end() const -{ - return endConstIter_; -} - - -// * * * * * * * * * * STL const_reverse_iterator * * * * * * * * * * * * * // - -inline Foam::DLListBase::const_reverse_iterator::const_reverse_iterator -( - const DLListBase& lst, - const link* elmt -) -: - curList_(lst), - curElmt_(elmt) -{} - - -inline bool Foam::DLListBase::const_reverse_iterator::found() const -{ - return (curElmt_ != nullptr); -} - - -inline void Foam::DLListBase::const_reverse_iterator::operator= -( - const const_reverse_iterator& iter -) -{ - curElmt_ = iter.curElmt_; -} - - -inline bool Foam::DLListBase::const_reverse_iterator::operator== +inline bool Foam::DLListBase::const_iterator::operator== ( - const const_reverse_iterator& iter + const const_iterator& iter ) const { - return curElmt_ == iter.curElmt_; + return node_ == iter.node_; } -inline bool Foam::DLListBase::const_reverse_iterator::operator!= +inline bool Foam::DLListBase::const_iterator::operator!= ( - const const_reverse_iterator& iter + const const_iterator& iter ) const { - return curElmt_ != iter.curElmt_; + return node_ != iter.node_; } -inline const Foam::DLListBase::link& -Foam::DLListBase::const_reverse_iterator::operator*() const -{ - return *curElmt_; -} - - -inline Foam::DLListBase::const_reverse_iterator& -Foam::DLListBase::const_reverse_iterator::operator++() +inline Foam::DLListBase::const_iterator +Foam::DLListBase::cbegin() const { - if (curElmt_ == curList_.first_) - { - curElmt_ = nullptr; - } - else + if (size()) { - curElmt_ = curElmt_->prev_; + return iterator_first<const_iterator>(); } - return *this; + return cend(); } -inline Foam::DLListBase::const_reverse_iterator -Foam::DLListBase::const_reverse_iterator::operator++(int) -{ - const_reverse_iterator tmp = *this; - ++*this; - return tmp; -} - - -inline Foam::DLListBase::const_reverse_iterator +inline Foam::DLListBase::const_iterator Foam::DLListBase::crbegin() const { if (size()) { - return const_reverse_iterator(*this, this->last()); + return iterator_last<const_iterator>(); } - else - { - return endConstRevIter_; - } -} - -inline const Foam::DLListBase::const_reverse_iterator& -Foam::DLListBase::crend() const -{ - return endConstRevIter_; -} - - -inline Foam::DLListBase::const_reverse_iterator -Foam::DLListBase::rbegin() const -{ - return this->crbegin(); -} - - -inline const Foam::DLListBase::const_reverse_iterator& -Foam::DLListBase::rend() const -{ - return endConstRevIter_; + return crend(); } diff --git a/src/OpenFOAM/containers/LinkedLists/linkTypes/SLListBase/SLListBase.C b/src/OpenFOAM/containers/LinkedLists/linkTypes/SLListBase/SLListBase.C index 693530d595a026350b94b87cb71c2f7b066f3ef8..ba2b81f32d000ba6b57a7a3b81fcb1e732828a0c 100644 --- a/src/OpenFOAM/containers/LinkedLists/linkTypes/SLListBase/SLListBase.C +++ b/src/OpenFOAM/containers/LinkedLists/linkTypes/SLListBase/SLListBase.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -23,61 +23,57 @@ License \*---------------------------------------------------------------------------*/ -#include "error.H" #include "SLListBase.H" - -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - -Foam::SLListBase::iterator Foam::SLListBase::endIter_ -( - const_cast<SLListBase&>(static_cast<const SLListBase&>(SLListBase())) -); - -Foam::SLListBase::const_iterator Foam::SLListBase::endConstIter_ -( - static_cast<const SLListBase&>(SLListBase()), - reinterpret_cast<const link*>(0) -); - +#include "error.H" // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // -void Foam::SLListBase::insert(SLListBase::link* a) +void Foam::SLListBase::insert(SLListBase::link* item) { - nElmts_++; + if (!item) + { + return; + } + + ++size_; if (last_) { - a->next_ = last_->next_; + item->next_ = last_->next_; } else { - last_ = a; + last_ = item; } - last_->next_ = a; + last_->next_ = item; } -void Foam::SLListBase::append(SLListBase::link* a) +void Foam::SLListBase::append(SLListBase::link* item) { - nElmts_++; + if (!item) + { + return; + } + + ++size_; if (last_) { - a->next_ = last_->next_; - last_ = last_->next_ = a; + item->next_ = last_->next_; + last_ = last_->next_ = item; } else { - last_ = a->next_ = a; + last_ = item->next_ = item; } } Foam::SLListBase::link* Foam::SLListBase::removeHead() { - nElmts_--; + --size_; if (last_ == nullptr) { @@ -86,39 +82,39 @@ Foam::SLListBase::link* Foam::SLListBase::removeHead() << abort(FatalError); } - SLListBase::link* f = last_->next_; + SLListBase::link *ret = last_->next_; - if (f == last_) + if (ret == last_) { last_ = nullptr; } else { - last_->next_ = f->next_; + last_->next_ = ret->next_; } - return f; + return ret; } -Foam::SLListBase::link* Foam::SLListBase::remove(SLListBase::link* it) +Foam::SLListBase::link* Foam::SLListBase::remove(SLListBase::link* item) { SLListBase::iterator iter = begin(); - SLListBase::link *prev = &(*iter); + SLListBase::link *prev = iter.get_node(); - if (it == prev) + if (item == prev) { return removeHead(); } - nElmts_--; - - for (++iter; iter != end(); ++iter) + for (iter.next(); iter != end(); iter.next()) { - SLListBase::link *p = &(*iter); + SLListBase::link *p = iter.get_node(); - if (p == it) + if (p == item) { + --size_; + prev->next_ = p->next_; if (p == last_) @@ -126,12 +122,13 @@ Foam::SLListBase::link* Foam::SLListBase::remove(SLListBase::link* it) last_ = prev; } - return it; + return item; } prev = p; } + // Did not remove return nullptr; } diff --git a/src/OpenFOAM/containers/LinkedLists/linkTypes/SLListBase/SLListBase.H b/src/OpenFOAM/containers/LinkedLists/linkTypes/SLListBase/SLListBase.H index 421922d3b867ff33eb3cd5b8b04bdc2d3bb8f70c..f74e29c504ad141288dd9f70e9412989d2bb9f44 100644 --- a/src/OpenFOAM/containers/LinkedLists/linkTypes/SLListBase/SLListBase.H +++ b/src/OpenFOAM/containers/LinkedLists/linkTypes/SLListBase/SLListBase.H @@ -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. @@ -27,6 +27,13 @@ Class Description Base for singly-linked lists. + The iterators associated with the list only have a core functionality + for navigation, with additional functionality to be added by inheriting + classes. The node iterators always have a node-pointer as the + first member data, which allows reinterpret_cast from anything else with + a nullptr as its first data member. + The nullObject is such an item (with a nullptr data member). + SourceFiles SLListBaseI.H SLListBase.C @@ -36,9 +43,9 @@ SourceFiles #ifndef SLListBase_H #define SLListBase_H -#include "bool.H" #include "label.H" #include "uLabel.H" +#include <utility> // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -51,33 +58,30 @@ namespace Foam class SLListBase { - public: - //- Link structure + //- The structure for a singly-linked storage node struct link { //- Pointer to next entry in list - link* next_; + link* next_ = nullptr; //- Null construct - inline link(); - - //- Construct given pointer to another link - inline link(link* p); + link() = default; }; private: - // Private data + // Private Member Data - //- last_ points to last element + //- A pointer to the last element. // last_->next_ points to first element, i.e. circular storage - link* last_; + link* last_ = nullptr; + + //- Number of elements in the list + label size_ = 0; - //- Number of elements in in list - label nElmts_; // Private Member Functions @@ -88,9 +92,33 @@ private: void operator=(const SLListBase&) = delete; +protected: + + // Protected Member Functions + + //- Factory method to return an iterator end + // Simply reinterprets a NullObject as a SLListBase iterator. + template<class IteratorType> + inline static const IteratorType& iterator_end(); + + //- Factory method to return an iterator rend + // Deleted for SLListBase + template<class IteratorType> + static const IteratorType& iterator_rend() = delete; + + //- Return iterator to first item or end-iterator if list is empty + // Removes constness which the caller promises to manage. + template<class IteratorType> + inline IteratorType iterator_first() const; + + //- Return iterator to last item or end-iterator if list is empty + // Removes constness which the caller promises to manage. + template<class IteratorType> + inline IteratorType iterator_last() const; + public: - // Forward declaration of STL iterators + // Forward declaration of iterators class iterator; friend class iterator; @@ -102,154 +130,166 @@ public: // Constructors //- Null construct - inline SLListBase(); - - //- Construct given initial entry - inline SLListBase(link*); + SLListBase() = default; //- Destructor - ~SLListBase(); + ~SLListBase() = default; // Member Functions - // Access + //- Return number of elements in list + inline label size() const; - //- Return number of elements in list - inline label size() const; + //- Return true if the list is empty + inline bool empty() const; - //- Return true if the list is empty - inline bool empty() const; + //- Return first entry + inline link* first(); - //- Return first entry - inline link* first(); + //- Return const access to first entry + inline const link* first() const; - //- Return const access to first entry - inline const link* first() const; + //- Return last entry + inline link* last(); - //- Return last entry - inline link* last(); + //- Return const access to last entry + inline const link* last() const; - //- Return const access to last entry - inline const link* last() const; + //- Add at head of list + void insert(link* item); - // Edit + //- Add at tail of list + void append(link* item); - //- Add at head of list - void insert(link*); + //- Remove and return head + link* removeHead(); - //- Add at tail of list - void append(link*); + // Remove and return element + link* remove(link* item); - //- Remove and return head - link* removeHead(); + // Remove and return element specified by iterator + inline link* remove(iterator& iter); - // Remove and return element - link* remove(link*); + //- Clear the list + inline void clear(); - // Remove and return element specified by iterator - inline link* remove(iterator&); + //- Swap the contents of list + inline void swap(SLListBase& lst); - //- Clear the list - inline void clear(); + //- Transfer the contents of the argument into this list + //- and annul the argument list. + inline void transfer(SLListBase& lst); - //- Transfer the contents of the argument into this List - // and annul the argument list. - inline void transfer(SLListBase&); - // STL iterator + // iterator - //- An STL-conforming iterator + //- A primitive non-const node iterator. + // Must normally be extended by inheriting classes. class iterator { friend class SLListBase; friend class const_iterator; - //- Reference to the list this is an iterator for - SLListBase& curList_; - - //- Current element - link* curElmt_; + //- The selected node. + // MUST be the first member for easy comparison between iterators + // and for reinterpret_cast from nullObject + link* node_; - //- Copy of the link - link curLink_; + //- The list being iterated on (as pointer for bitwise copy) + SLListBase* list_; - //- Construct for a given SLListBase with nullptr element and link. - // Only used to create endIter - inline iterator(SLListBase&); + //- Copy of the node next pointer (to use after removal) + link copy_; public: - //- Construct for a given SLListBase and link - inline iterator(SLListBase&, link*); + //- Construct for a node on the list + inline iterator(SLListBase* list, link* item); - //- Currently pointing at a valid entry + //- The storage node + inline link* get_node() const; + + //- Pointing at a valid storage node inline bool found() const; + //- Cannot move backward through list + inline void prev() = delete; + + //- Move forward through list + inline void next(); + inline void operator=(const iterator& iter); inline bool operator==(const iterator& iter) const; inline bool operator!=(const iterator& iter) const; - - inline link& operator*() const; - - inline iterator& operator++(); - inline iterator operator++(int); }; - inline iterator begin(); - inline const iterator& end(); - // STL const_iterator - //- An STL-conforming const_iterator + //- A primitive const node iterator. + // Must normally be extended by inheriting classes. class const_iterator { - //- Reference to the list this is an iterator for - const SLListBase& curList_; + //- The selected node. + // MUST be the first member for easy comparison between iterators + // and for reinterpret_cast from nullObject + const link* node_; - //- Current element - const link* curElmt_; + //- The list being iterated on (as pointer for bitwise copy) + const SLListBase* list_; public: - //- Construct for a given SLListBase and link - inline const_iterator(const SLListBase&, const link*); + //- Construct for a node on the list + inline const_iterator(const SLListBase* list, const link* item); //- Construct from a non-const iterator inline const_iterator(const SLListBase::iterator& iter); - //- Currently pointing at a valid entry + //- Copy construct + const_iterator(const const_iterator&) = default; + + //- The storage node + inline const link* get_node() const; + + //- Pointing at a valid storage node inline bool found() const; - inline void operator=(const const_iterator& iter); + //- Cannot move backward through list + inline void prev() = delete; + + //- Move forward through list + inline void next(); + + const_iterator& operator=(const const_iterator&) = default; inline bool operator==(const const_iterator& iter) const; inline bool operator!=(const const_iterator& iter) const; + }; - inline const link& operator*() const; - inline const_iterator& operator++(); - inline const_iterator operator++(int); - }; + //- Iterator to first item in list with non-const access + inline iterator begin(); + //- Iterator to first item in list with const access inline const_iterator cbegin() const; - inline const const_iterator& cend() const; - inline const_iterator begin() const; - inline const const_iterator& end() const; + //- No reverse iteration + const_iterator crbegin() const = delete; + //- End of list for iterators + inline const iterator& end(); -private: + //- End of list for iterators + inline const const_iterator& cend() const; - //- Iterator returned by end() - static iterator endIter_; + //- No reverse iteration + const const_iterator& crend() const = delete; - //- const_iterator returned by end() - static const_iterator endConstIter_; }; diff --git a/src/OpenFOAM/containers/LinkedLists/linkTypes/SLListBase/SLListBaseI.H b/src/OpenFOAM/containers/LinkedLists/linkTypes/SLListBase/SLListBaseI.H index 8c4ebdb7c372a2f1b38972123126918c32be2360..be940f3ab2f51528cddad30c17d4bbcd2d7d4967 100644 --- a/src/OpenFOAM/containers/LinkedLists/linkTypes/SLListBase/SLListBaseI.H +++ b/src/OpenFOAM/containers/LinkedLists/linkTypes/SLListBase/SLListBaseI.H @@ -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. @@ -24,65 +24,65 @@ License \*---------------------------------------------------------------------------*/ #include "error.H" +#include "nullObject.H" -// * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * // +// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * // -inline Foam::SLListBase::link::link() -: - next_(nullptr) -{} +template<class IteratorType> +inline const IteratorType& Foam::SLListBase::iterator_end() +{ + return *reinterpret_cast<const IteratorType*>(nullObjectPtr); +} -inline Foam::SLListBase::link::link(link* p) -: - next_(p) -{} +template<class IteratorType> +inline IteratorType Foam::SLListBase::iterator_first() const +{ + SLListBase* list = const_cast<SLListBase*>(this); + if (size()) + { + return IteratorType(list, const_cast<SLListBase::link*>(last_->next_)); + } -inline Foam::SLListBase::SLListBase() -: - last_(nullptr), - nElmts_(0) -{} + // Return an end iterator + return IteratorType(list, nullptr); +} -inline Foam::SLListBase::SLListBase(link* a) -: - last_(a), - nElmts_(0) +template<class IteratorType> +inline IteratorType Foam::SLListBase::iterator_last() const { - if (a) // protect against nullptr + SLListBase* list = const_cast<SLListBase*>(this); + + if (size()) { - a->next_ = a; - nElmts_ = 1; + return IteratorType(list, const_cast<SLListBase::link*>(last_)); } -} - -// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // - -inline Foam::SLListBase::~SLListBase() -{} + // Return an end iterator + return IteratorType(list, nullptr); +} // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // inline Foam::label Foam::SLListBase::size() const { - return nElmts_; + return size_; } inline bool Foam::SLListBase::empty() const { - return !nElmts_; + return !size_; } inline Foam::SLListBase::link* Foam::SLListBase::first() { - if (!nElmts_) + if (!size_) { FatalErrorInFunction << "list is empty" @@ -95,7 +95,7 @@ Foam::SLListBase::first() inline const Foam::SLListBase::link* Foam::SLListBase::first() const { - if (!nElmts_) + if (!size_) { FatalErrorInFunction << "list is empty" @@ -108,7 +108,7 @@ Foam::SLListBase::first() const inline Foam::SLListBase::link* Foam::SLListBase::last() { - if (!nElmts_) + if (!size_) { FatalErrorInFunction << "list is empty" @@ -121,7 +121,7 @@ Foam::SLListBase::last() inline const Foam::SLListBase::link* Foam::SLListBase::last() const { - if (!nElmts_) + if (!size_) { FatalErrorInFunction << "list is empty" @@ -134,14 +134,21 @@ Foam::SLListBase::last() const inline void Foam::SLListBase::clear() { last_ = nullptr; - nElmts_ = 0; + size_ = 0; +} + + +inline void Foam::SLListBase::swap(SLListBase& lst) +{ + std::swap(last_, lst.last_); + std::swap(size_, lst.size_); } inline void Foam::SLListBase::transfer(SLListBase& lst) { - last_ = lst.last_; - nElmts_ = lst.nElmts_; + last_ = lst.last_; + size_ = lst.size_; lst.clear(); } @@ -149,84 +156,79 @@ inline void Foam::SLListBase::transfer(SLListBase& lst) inline Foam::SLListBase::link* Foam::SLListBase::remove ( - SLListBase::iterator& it + SLListBase::iterator& iter ) { - return remove(it.curElmt_); + return remove(iter.node_); } // * * * * * * * * * * * * * * * STL iterator * * * * * * * * * * * * * * * // -inline Foam::SLListBase::iterator::iterator(SLListBase& s, link* elmt) -: - curList_(s), - curElmt_(elmt), - curLink_(*curElmt_) -{} - - -inline Foam::SLListBase::iterator::iterator(SLListBase& s) +inline Foam::SLListBase::iterator::iterator +( + SLListBase* list, + SLListBase::link* item +) : - curList_(s), - curElmt_(nullptr), - curLink_() -{} - - -inline bool Foam::SLListBase::iterator::found() const + node_(item), + list_(list), + copy_() { - return (curElmt_ != nullptr); + if (node_ != nullptr) + { + copy_ = *node_; + } } -inline void Foam::SLListBase::iterator::operator=(const iterator& iter) +inline Foam::SLListBase::link* +Foam::SLListBase::iterator::get_node() const { - curElmt_ = iter.curElmt_; - curLink_ = iter.curLink_; + return node_; } -inline bool Foam::SLListBase::iterator::operator==(const iterator& iter) const +inline bool Foam::SLListBase::iterator::found() const { - return curElmt_ == iter.curElmt_; + return (node_ != nullptr); } -inline bool Foam::SLListBase::iterator::operator!=(const iterator& iter) const +inline void Foam::SLListBase::iterator::next() { - return curElmt_ != iter.curElmt_; + if (list_) + { + if (node_ == list_->last_ || list_->last_ == nullptr) + { + node_ = nullptr; + } + else + { + node_ = copy_.next_; + copy_ = *node_; + } + } } -inline Foam::SLListBase::link& Foam::SLListBase::iterator::operator*() const +inline void Foam::SLListBase::iterator::operator=(const iterator& iter) { - return *curElmt_; + node_ = iter.node_; + list_ = iter.list_; + copy_ = iter.copy_; } -inline Foam::SLListBase::iterator& Foam::SLListBase::iterator::operator++() +inline bool Foam::SLListBase::iterator::operator==(const iterator& iter) const { - if (curElmt_ == curList_.last_ || curList_.last_ == nullptr) - { - curElmt_ = nullptr; - } - else - { - curElmt_ = curLink_.next_; - curLink_ = *curElmt_; - } - - return *this; + return node_ == iter.node_; } -inline Foam::SLListBase::iterator -Foam::SLListBase::iterator::operator++(int) +inline bool Foam::SLListBase::iterator::operator!=(const iterator& iter) const { - iterator tmp = *this; - ++*this; - return tmp; + return node_ != iter.node_; } @@ -235,19 +237,24 @@ Foam::SLListBase::begin() { if (size()) { - return iterator(*this, first()); - } - else - { - return endIter_; + return iterator_first<iterator>(); } + + return end(); } inline const Foam::SLListBase::iterator& Foam::SLListBase::end() { - return endIter_; + return iterator_end<SLListBase::iterator>(); +} + + +inline const Foam::SLListBase::const_iterator& +Foam::SLListBase::cend() const +{ + return iterator_end<SLListBase::const_iterator>(); } @@ -255,12 +262,12 @@ Foam::SLListBase::end() inline Foam::SLListBase::const_iterator::const_iterator ( - const SLListBase& s, - const link* elmt + const SLListBase* list, + const SLListBase::link* item ) : - curList_(s), - curElmt_(elmt) + node_(item), + list_(list) {} @@ -269,23 +276,37 @@ inline Foam::SLListBase::const_iterator::const_iterator const SLListBase::iterator& iter ) : - curList_(iter.curList_), - curElmt_(iter.curElmt_) + node_(iter.node_), + list_(iter.list_) {} +inline const Foam::SLListBase::link* +Foam::SLListBase::const_iterator::get_node() const +{ + return node_; +} + + inline bool Foam::SLListBase::const_iterator::found() const { - return (curElmt_ != nullptr); + return (node_ != nullptr); } -inline void Foam::SLListBase::const_iterator::operator= -( - const const_iterator& iter -) +inline void Foam::SLListBase::const_iterator::next() { - curElmt_ = iter.curElmt_; + if (list_) + { + if (node_ == list_->last_) + { + node_ = nullptr; + } + else + { + node_ = node_->next_; + } + } } @@ -294,7 +315,7 @@ inline bool Foam::SLListBase::const_iterator::operator== const const_iterator& iter ) const { - return curElmt_ == iter.curElmt_; + return node_ == iter.node_; } @@ -303,39 +324,7 @@ inline bool Foam::SLListBase::const_iterator::operator!= const const_iterator& iter ) const { - return curElmt_ != iter.curElmt_; -} - - -inline const Foam::SLListBase::link& -Foam::SLListBase::const_iterator::operator*() const -{ - return *curElmt_; -} - - -inline Foam::SLListBase::const_iterator& -Foam::SLListBase::const_iterator::operator++() -{ - if (curElmt_ == curList_.last_) - { - curElmt_ = nullptr; - } - else - { - curElmt_ = curElmt_->next_; - } - - return *this; -} - - -inline Foam::SLListBase::const_iterator -Foam::SLListBase::const_iterator::operator++(int) -{ - const_iterator tmp = *this; - ++*this; - return tmp; + return node_ != iter.node_; } @@ -344,33 +333,10 @@ Foam::SLListBase::cbegin() const { if (size()) { - return const_iterator(*this, first()); - } - else - { - return endConstIter_; + return iterator_first<const_iterator>(); } -} - -inline const Foam::SLListBase::const_iterator& -Foam::SLListBase::cend() const -{ - return endConstIter_; -} - - -inline Foam::SLListBase::const_iterator -Foam::SLListBase::begin() const -{ - return this->cbegin(); -} - - -inline const Foam::SLListBase::const_iterator& -Foam::SLListBase::end() const -{ - return endConstIter_; + return cend(); } diff --git a/src/OpenFOAM/containers/LinkedLists/user/FIFOStack.H b/src/OpenFOAM/containers/LinkedLists/user/FIFOStack.H index e7331d0a8760cc7bb26e533162d4223f9442e3a7..4961f84d40eb09753e7b6d3f4f9cb5915a83a366 100644 --- a/src/OpenFOAM/containers/LinkedLists/user/FIFOStack.H +++ b/src/OpenFOAM/containers/LinkedLists/user/FIFOStack.H @@ -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. @@ -27,10 +27,7 @@ Class Description A FIFO stack based on a singly-linked list. - Operations are push(), pop(), top(), bottom() and empty(). - -SourceFiles - FIFOStack.C + Stack operations are push(), pop(), top(), bottom(). \*---------------------------------------------------------------------------*/ @@ -53,7 +50,6 @@ class FIFOStack : public SLList<T> { - public: // Constructors @@ -64,34 +60,35 @@ public: // Member Functions - // Access - - //- Return a copy of the top element - T top() const - { - return this->last(); - } - - //- Return a copy of the bottom element - T bottom() const - { - return this->first(); - } - - - // Edit - - //- Push an element onto the stack - void push(const T& a) - { - this->append(a); - } - - //- Pop the bottom element off the stack - T pop() - { - return this->removeHead(); - } + //- Return a copy of the top element + T top() const + { + return this->last(); + } + + //- Return a copy of the bottom element + T bottom() const + { + return this->first(); + } + + //- Push an element onto the back of the stack + void push(const T& element) + { + this->append(element); + } + + //- Move an element onto the back of the stack + void push(T&& element) + { + this->append(std::move(element)); + } + + //- Pop the bottom element off the stack + T pop() + { + return this->removeHead(); + } }; diff --git a/src/OpenFOAM/containers/LinkedLists/user/LIFOStack.H b/src/OpenFOAM/containers/LinkedLists/user/LIFOStack.H index 03135735dec54570b435db0549f7f51d306efcc6..d65eaa34954d5ace9012ff886077b0ce66e2fdc2 100644 --- a/src/OpenFOAM/containers/LinkedLists/user/LIFOStack.H +++ b/src/OpenFOAM/containers/LinkedLists/user/LIFOStack.H @@ -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. @@ -27,10 +27,7 @@ Class Description A LIFO stack based on a singly-linked list. - Operations are push(), pop(), top(), bottom() and empty(). - -SourceFiles - LIFOStack.C + Stack operations are push(), pop(), top(), bottom(). \*---------------------------------------------------------------------------*/ @@ -53,7 +50,6 @@ class LIFOStack : public SLList<T> { - public: // Constructors @@ -64,34 +60,35 @@ public: // Member Functions - // Access - - //- Return a copy of the top element - T top() const - { - return this->first(); - } - - //- Return a copy of the bottom element - T bottom() const - { - return this->last(); - } - - - // Edit - - //- Push an element onto the stack - void push(const T& a) - { - this->insert(a); - } - - //- Pop the top element off the stack - T pop() - { - return this->removeHead(); - } + //- Return a copy of the top element + T top() const + { + return this->first(); + } + + //- Return a copy of the bottom element + T bottom() const + { + return this->last(); + } + + //- Push an element onto the front of the stack + void push(const T& element) + { + this->insert(element); + } + + //- Move an element onto the front of the stack + void push(T&& element) + { + this->insert(std::move(element)); + } + + //- Pop the top element off the stack + T pop() + { + return this->removeHead(); + } }; diff --git a/src/OpenFOAM/containers/LinkedLists/user/SLList.H b/src/OpenFOAM/containers/LinkedLists/user/SLList.H index 8be3c5561eac1948969a677a50dc8f69c3e1fbd9..8fe0e9695801ade215206d01bad427c4201ea310 100644 --- a/src/OpenFOAM/containers/LinkedLists/user/SLList.H +++ b/src/OpenFOAM/containers/LinkedLists/user/SLList.H @@ -32,18 +32,10 @@ Description #ifndef SLList_H #define SLList_H -#include "LList.H" #include "SLListBase.H" +#include "LList.H" -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - -namespace Foam -{ - template<class T> - using SLList = LList<SLListBase, T>; -} - -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +#include "SLListFwd.H" #endif diff --git a/src/OpenFOAM/containers/LinkedLists/user/SLListFwd.H b/src/OpenFOAM/containers/LinkedLists/user/SLListFwd.H new file mode 100644 index 0000000000000000000000000000000000000000..efe9a4e430d148245b5d0697b898a62c16310dd6 --- /dev/null +++ b/src/OpenFOAM/containers/LinkedLists/user/SLListFwd.H @@ -0,0 +1,52 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / 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/>. + +InClass + Foam::SLList + +Description + Forward declarations for SLList + +\*---------------------------------------------------------------------------*/ + +#ifndef SLListFwd_H +#define SLListFwd_H + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + // Forward declarations + class SLListBase; + template<class LListBase, class T> class LList; + + // Alias + template<class T> + using SLList = LList<SLListBase, T>; +} + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/OpenFOAM/containers/LinkedLists/user/SLPtrList.H b/src/OpenFOAM/containers/LinkedLists/user/SLPtrList.H index fb9bb05cd7a9d0c568ff7232aba779809db074b2..e5c4c695d9edbe2052d8ca3675b970a88504194a 100644 --- a/src/OpenFOAM/containers/LinkedLists/user/SLPtrList.H +++ b/src/OpenFOAM/containers/LinkedLists/user/SLPtrList.H @@ -32,18 +32,9 @@ Description #ifndef SLPtrList_H #define SLPtrList_H -#include "LPtrList.H" #include "SLListBase.H" - -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - -namespace Foam -{ - template<class T> - using SLPtrList = LPtrList<SLListBase, T>; -} - -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +#include "LPtrList.H" +#include "SLPtrListFwd.H" #endif diff --git a/src/OpenFOAM/containers/LinkedLists/user/SLPtrListFwd.H b/src/OpenFOAM/containers/LinkedLists/user/SLPtrListFwd.H new file mode 100644 index 0000000000000000000000000000000000000000..ef998349cdcf492df78e26c3fdadb444e9fa69bf --- /dev/null +++ b/src/OpenFOAM/containers/LinkedLists/user/SLPtrListFwd.H @@ -0,0 +1,52 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / 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/>. + +InClass + Foam::SLPtrList + +Description + Forward declarations for SLPtrList + +\*---------------------------------------------------------------------------*/ + +#ifndef SLPtrListFwd_H +#define SLPtrListFwd_H + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + // Forward declarations + class SLListBase; + template<class LListBase, class T> class LPtrList; + + // Alias + template<class T> + using SLPtrList = LPtrList<SLListBase, T>; +} + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/OpenFOAM/containers/Lists/CompactListList/CompactListList.C b/src/OpenFOAM/containers/Lists/CompactListList/CompactListList.C index f2ad0802b08b45e0936c367e6d0bf6c4a4c1ad7f..2532ac7e4ac7ed397b40581707a41847736b71e9 100644 --- a/src/OpenFOAM/containers/Lists/CompactListList/CompactListList.C +++ b/src/OpenFOAM/containers/Lists/CompactListList/CompactListList.C @@ -81,7 +81,7 @@ template<class T, class Container> Foam::CompactListList<T, Container>::CompactListList ( const labelUList& rowSizes, - const T& t + const T& val ) : size_(rowSizes.size()), @@ -95,7 +95,7 @@ Foam::CompactListList<T, Container>::CompactListList offsets_[i+1] = sumSize; } - m_.setSize(sumSize, t); + m_.setSize(sumSize, val); } @@ -109,19 +109,6 @@ Foam::CompactListList<T, Container>::CompactListList } -template<class T, class Container> -Foam::CompactListList<T, Container>::CompactListList -( - CompactListList<T, Container>& lst, - bool reuse -) -: - size_(lst.size()), - offsets_(lst.offsets_, reuse), - m_(lst.m_, reuse) -{} - - // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // template<class T, class Container> @@ -218,15 +205,27 @@ void Foam::CompactListList<T, Container>::clear() } +template<class T, class Container> +void Foam::CompactListList<T, Container>::swap +( + CompactListList<T, Container>& lst +) +{ + Foam::Swap(size_, lst.size_); + offsets_.swap(lst.offsets_); + m_.swap(lst.m_); +} + + template<class T, class Container> void Foam::CompactListList<T, Container>::transfer ( - CompactListList<T, Container>& a + CompactListList<T, Container>& lst ) { - size_ = a.size_; - offsets_.transfer(a.offsets_); - m_.transfer(a.m_); + size_ = lst.size_; + offsets_.transfer(lst.offsets_); + m_.transfer(lst.m_); } diff --git a/src/OpenFOAM/containers/Lists/CompactListList/CompactListList.H b/src/OpenFOAM/containers/Lists/CompactListList/CompactListList.H index fcb7ea2ba557c573aa8e8fa1171b79eeeba3ee34..5c1115e5af3bc399baf75b7888809670abb9f768 100644 --- a/src/OpenFOAM/containers/Lists/CompactListList/CompactListList.H +++ b/src/OpenFOAM/containers/Lists/CompactListList/CompactListList.H @@ -119,11 +119,17 @@ public: //- Construct given list of row-sizes CompactListList(const labelUList& rowSizes, const T&); + //- Copy construct + inline CompactListList(const CompactListList<T, Container>& lst); + + //- Move construct + inline CompactListList(CompactListList<T, Container>&& lst); + //- Construct by transferring the parameter contents explicit CompactListList(const Xfer<CompactListList<T, Container>>&); //- Construct as copy or re-use as specified. - CompactListList(CompactListList<T, Container>&, bool reuse); + inline CompactListList(CompactListList<T, Container>& lst, bool reuse); //- Construct from Istream. CompactListList(Istream&); @@ -189,13 +195,17 @@ public: //- Return sizes (to be used e.g. for construction) labelList sizes() const; + //- Swap contents + void swap(CompactListList<T, Container>& lst); + //- Transfer the contents of the argument CompactListList // into this CompactListList and annul the argument list. - void transfer(CompactListList<T, Container>&); + void transfer(CompactListList<T, Container>& lst); //- Transfer the contents to the Xfer container inline Xfer<CompactListList<T, Container>> xfer(); + // Other //- Return index into m @@ -226,7 +236,13 @@ public: List<Container> operator()() const; //- Assignment of all entries to the given value - inline void operator=(const T&); + inline void operator=(const T& val); + + //- Copy assignment + inline void operator=(const CompactListList<T, Container>& lst); + + //- Move assignment + inline void operator=(CompactListList<T, Container>&& lst); // Istream operator diff --git a/src/OpenFOAM/containers/Lists/CompactListList/CompactListListI.H b/src/OpenFOAM/containers/Lists/CompactListList/CompactListListI.H index 2072887cc5040a5969bfcc039d8c5d2904fd7d1c..65ae222f6e6933c11f93a7c69b50586a4f53351c 100644 --- a/src/OpenFOAM/containers/Lists/CompactListList/CompactListListI.H +++ b/src/OpenFOAM/containers/Lists/CompactListList/CompactListListI.H @@ -53,12 +53,47 @@ inline Foam::CompactListList<T, Container>::CompactListList ( const label mRows, const label nData, - const T& t + const T& val ) : size_(mRows), offsets_(mRows+1, 0), - m_(nData, t) + m_(nData, val) +{} + + +template<class T, class Container> +inline Foam::CompactListList<T, Container>::CompactListList +( + const CompactListList<T, Container>& lst +) +: + size_(lst.size()), + offsets_(lst.offsets_), + m_(lst.m_) +{} + + +template<class T, class Container> +inline Foam::CompactListList<T, Container>::CompactListList +( + CompactListList<T, Container>&& lst +) +{ + transfer(lst); +} + + +template<class T, class Container> +inline Foam::CompactListList<T, Container>::CompactListList +( + CompactListList<T, Container>& lst, + bool reuse +) +: + size_(lst.size()), + offsets_(lst.offsets_, reuse), + m_(lst.m_, reuse) {} @@ -220,7 +255,7 @@ inline Foam::UList<T> Foam::CompactListList<T, Container>::operator[] const label i ) { - label start = offsets_[i]; + const label start = offsets_[i]; return UList<T>(m_.begin() + start, offsets_[i+1] - start); } @@ -232,7 +267,7 @@ Foam::CompactListList<T, Container>::operator[] const label i ) const { - label start = offsets_[i]; + const label start = offsets_[i]; return UList<T> ( const_cast<T*>(m_.begin() + start), @@ -264,9 +299,31 @@ inline const T& Foam::CompactListList<T, Container>::operator() template<class T, class Container> -inline void Foam::CompactListList<T, Container>::operator=(const T& t) +inline void Foam::CompactListList<T, Container>::operator=(const T& val) +{ + m_ = val; +} + + +template<class T, class Container> +inline void Foam::CompactListList<T, Container>::operator= +( + const CompactListList<T, Container>& lst +) +{ + size_ = lst.size_; + offsets_ = lst.offsets_, + m_ = lst.m_; +} + + +template<class T, class Container> +inline void Foam::CompactListList<T, Container>::operator= +( + CompactListList<T, Container>&& lst +) { - m_ = t; + transfer(lst); } diff --git a/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H b/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H index 81ec482e2121082d29e1356045b3001b02fae4cb..ed2b0b6b84564940b9d5cfc6676a8a6a302191ab 100644 --- a/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H +++ b/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H @@ -418,11 +418,7 @@ inline void Foam::DynamicList<T, SizeMin>::swap const label oldSize2 = lst.expandStorage(); // Swap storage - Foam::Swap - ( - static_cast<UList<T>&>(cur), - static_cast<UList<T>&>(lst) - ); + UList<T>::swap(lst); // Match capacity to the underlying allocated list size cur.setCapacity(cur.size()); diff --git a/src/OpenFOAM/containers/Lists/FixedList/FixedList.C b/src/OpenFOAM/containers/Lists/FixedList/FixedList.C index 2a10efe4116ad07673afde138ab08ec778c8b188..e0cd4afae6e22df3dae0e77c6fcdb2e05f4713e3 100644 --- a/src/OpenFOAM/containers/Lists/FixedList/FixedList.C +++ b/src/OpenFOAM/containers/Lists/FixedList/FixedList.C @@ -52,13 +52,6 @@ Foam::label Foam::FixedList<T, Size>::find } -template<class T, unsigned Size> -void Foam::FixedList<T, Size>::swap(FixedList<T, Size>& lst) -{ - Foam::Swap(v_, lst.v_); -} - - template<class T, unsigned Size> void Foam::FixedList<T, Size>::moveFirst(const label i) { @@ -112,72 +105,74 @@ void Foam::FixedList<T, Size>::swapLast(const label i) // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // template<class T, unsigned Size> -bool Foam::FixedList<T, Size>::operator==(const FixedList<T, Size>& a) const +bool Foam::FixedList<T, Size>::operator==(const FixedList<T, Size>& list) const { - bool equal = true; - - List_CONST_ACCESS(T, (*this), vp); - List_CONST_ACCESS(T, (a), ap); + List_CONST_ACCESS(T, *this, lhs); + List_CONST_ACCESS(T, (list), rhs); + // List sizes are identical by definition (template parameter) for (unsigned i = 0; i < Size; ++i) { - equal = (vp[i] == ap[i]); - if (!equal) break; + if (!(lhs[i] == rhs[i])) + { + return false; + } } - return equal; + // Contents appear to be identical. + return true; } template<class T, unsigned Size> -bool Foam::FixedList<T, Size>::operator!=(const FixedList<T, Size>& a) const +bool Foam::FixedList<T, Size>::operator<(const FixedList<T, Size>& list) const { - return !operator==(a); -} - - -template<class T, unsigned Size> -bool Foam::FixedList<T, Size>::operator<(const FixedList<T, Size>& a) const -{ - List_CONST_ACCESS(T, *this, ptr1); - List_CONST_ACCESS(T, a, ptr2); + List_CONST_ACCESS(T, *this, lhs); + List_CONST_ACCESS(T, (list), rhs); + // List sizes are identical by definition (template parameter) for (unsigned i=0; i<Size; ++i) { - if (ptr1[i] < ptr2[i]) + if (lhs[i] < rhs[i]) { return true; } - else if (ptr1[i] > ptr2[i]) + else if (rhs[i] < lhs[i]) { return false; } } - // Contents look to be identical. - // The sizes are identical by definition (template parameter) + // Contents appear to be identical. return false; } template<class T, unsigned Size> -bool Foam::FixedList<T, Size>::operator>(const FixedList<T, Size>& a) const +bool Foam::FixedList<T, Size>::operator!=(const FixedList<T, Size>& list) const +{ + return !operator==(list); +} + + +template<class T, unsigned Size> +bool Foam::FixedList<T, Size>::operator>(const FixedList<T, Size>& list) const { - return a.operator<(*this); + return list.operator<(*this); } template<class T, unsigned Size> -bool Foam::FixedList<T, Size>::operator<=(const FixedList<T, Size>& a) const +bool Foam::FixedList<T, Size>::operator<=(const FixedList<T, Size>& list) const { - return !operator>(a); + return !list.operator<(*this); } template<class T, unsigned Size> -bool Foam::FixedList<T, Size>::operator>=(const FixedList<T, Size>& a) const +bool Foam::FixedList<T, Size>::operator>=(const FixedList<T, Size>& list) const { - return !operator<(a); + return !operator<(list); } diff --git a/src/OpenFOAM/containers/Lists/FixedList/FixedList.H b/src/OpenFOAM/containers/Lists/FixedList/FixedList.H index 6b1a1836cb8d3965c9b01cd8712b6c2fa8482a55..ea43d305f31948b96e03c53b9177218e87319e95 100644 --- a/src/OpenFOAM/containers/Lists/FixedList/FixedList.H +++ b/src/OpenFOAM/containers/Lists/FixedList/FixedList.H @@ -43,16 +43,18 @@ SourceFiles #include "Hash.H" #include "autoPtr.H" #include "Swap.H" +#include "SLListFwd.H" #include <type_traits> #include <initializer_list> +#include <iterator> // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // namespace Foam { -// Forward declaration of friend functions and operators +// Forward declarations template<class T, unsigned Size> class FixedList; @@ -64,10 +66,6 @@ Ostream& operator<<(Ostream&, const FixedList<T, Size>&); template<class T> class UList; -class SLListBase; -template<class LListBase, class T> class LList; -template<class T> -using SLList = LList<SLListBase, T>; /*---------------------------------------------------------------------------*\ Class FixedList Declaration @@ -98,6 +96,42 @@ protected: public: + // STL type definitions + + //- The value type the FixedList contains + typedef T value_type; + + //- The pointer type for non-const access to value_type items + typedef T* pointer; + + //- The pointer type for const access to value_type items + typedef const T* const_pointer; + + //- The type used for storing into value_type objects + typedef T& reference; + + //- The type used for reading from constant value_type objects. + typedef const T& const_reference; + + //- Random access iterator for traversing FixedList + typedef T* iterator; + + //- Random access iterator for traversing FixedList + typedef const T* const_iterator; + + //- The type to represent the size of a FixedList + typedef label size_type; + + //- The difference between iterator objects + typedef label difference_type; + + //- Reverse iterator (non-const access) + typedef std::reverse_iterator<iterator> reverse_iterator; + + //- Reverse iterator (const access) + typedef std::reverse_iterator<const_iterator> const_reverse_iterator; + + //- Hashing function class. // Use Hasher directly for contiguous data. Otherwise hash incrementally. template<class HashT=Hash<T>> @@ -105,7 +139,7 @@ public: { inline unsigned operator() ( - const FixedList<T, Size>&, + const FixedList<T, Size>& lst, unsigned seed = 0 ) const; }; @@ -120,7 +154,7 @@ public: // Constructors //- Null constructor - inline FixedList(); + inline FixedList() = default; //- Construct from value explicit inline FixedList(const T& val); @@ -131,7 +165,8 @@ public: //- Copy constructor inline FixedList(const FixedList<T, Size>& lst); - //- Move constructor + //- Move construct by using move assignment for the individual + //- list elements inline FixedList(FixedList<T, Size>&& lst); //- Construct given begin/end iterators @@ -230,11 +265,11 @@ public: //- Dummy resize function // needed to make FixedList consistent with List - inline void resize(const label s); + inline void resize(const label n); //- Dummy setSize function // needed to make FixedList consistent with List - inline void setSize(const label s); + inline void setSize(const label n); //- Move element to the first position. void moveFirst(const label i); @@ -248,9 +283,9 @@ public: //- Swap element with the last element. void swapLast(const label i); - //- Copy (not transfer) the argument contents - // needed to make FixedList consistent with List - void transfer(const FixedList<T, Size>& lst); + //- Transfer by swapping using a move assignment for the content + //- of the individual list elements + inline void transfer(FixedList<T, Size>& lst); // Member operators @@ -283,29 +318,7 @@ public: inline void operator=(FixedList<T, Size>&& lst); - // STL type definitions - - //- Type of values the FixedList contains - typedef T value_type; - - //- The type used for storing into value_type objects - typedef T& reference; - - //- The type used for reading from constant value_type objects. - typedef const T& const_reference; - - //- The type that can represent the difference between any two - //- FixedList iterator objects - typedef label difference_type; - - //- The type that can represent the size of a FixedList - typedef label size_type; - - - // STL iterator - - //- Random access iterator for traversing FixedList - typedef T* iterator; + // Random access iterator (non-const) //- Return an iterator to begin traversing the FixedList inline iterator begin(); @@ -314,10 +327,7 @@ public: inline iterator end(); - // STL const_iterator - - //- Random access iterator for traversing FixedList - typedef const T* const_iterator; + // Random access iterator (const) //- Return const_iterator to begin traversing the constant FixedList inline const_iterator cbegin() const; @@ -332,86 +342,7 @@ public: inline const_iterator end() const; - // Reverse iterators - - //- Generic const/non-const reverse iterator - template<bool Const> - class reverse_iterator_base - { - public: - //- The const/non-const type for entries - typedef typename std::conditional - <Const, const T, T>::type value_type; - - //- A pointer to a const/non-const entry - typedef value_type* pointer; - - //- A reference to a const/non-const entry - typedef value_type& reference; - - - private: - - //- The element pointer - pointer ptr_; - - public: - - //- Construct null or from list element pointer - inline reverse_iterator_base(pointer ptr = nullptr) - : - ptr_(ptr) - {} - - //- Copy construct - inline reverse_iterator_base(const reverse_iterator_base& iter) - : - ptr_(iter.ptr_) - {} - - - //- Reverse increment - inline void operator++() - { - --ptr_; - } - - //- Reverse increment - inline reverse_iterator_base operator++(int) - { - reverse_iterator_base old(*this); - --ptr_; - return old; - } - - //- Dereference iterator - reference operator*() const - { - return *ptr_; - } - - //- Dereference iterator - pointer operator->() const - { - return ptr_; - } - - //- Equality - bool operator==(const reverse_iterator_base& iter) const - { - return ptr_ == iter.ptr_; - } - - //- inequality - bool operator!=(const reverse_iterator_base& iter) const - { - return ptr_ != iter.ptr_; - } - }; - - - //- STL reverse_iterator - typedef reverse_iterator_base<false> reverse_iterator; + // Reverse iterator (non-const) //- Return reverse_iterator to begin reverse traversing the FixedList inline reverse_iterator rbegin(); @@ -420,8 +351,7 @@ public: inline reverse_iterator rend(); - //- STL const reverse iterator - typedef reverse_iterator_base<true> const_reverse_iterator; + // Reverse iterator (const) //- Return const_reverse_iterator to begin reverse traversing FixedList inline const_reverse_iterator crbegin() const; @@ -447,31 +377,31 @@ public: //- Always false since zero-sized FixedList is compile-time disabled. inline bool empty() const; - //- Swap content with another FixedList of the same type. - void swap(FixedList<T, Size>& lst); + //- Swap lists by swapping the content of the individual list elements + inline void swap(FixedList<T, Size>& lst); // STL member operators //- Equality operation on FixedLists of the same type. // Returns true when the FixedLists are element-wise equal - // (using FixedList::value_type::operator==). Takes linear time - bool operator==(const FixedList<T, Size>& a) const; + // (using FixedList::value_type::operator==). Takes linear time + bool operator==(const FixedList<T, Size>& list) const; //- The opposite of the equality operation. Takes linear time - bool operator!=(const FixedList<T, Size>& a) const; + bool operator!=(const FixedList<T, Size>& list) const; //- Compare two FixedLists lexicographically. Takes linear time - bool operator<(const FixedList<T, Size>& a) const; + bool operator<(const FixedList<T, Size>& list) const; //- Compare two FixedLists lexicographically. Takes linear time - bool operator>(const FixedList<T, Size>& a) const; + bool operator>(const FixedList<T, Size>& list) const; //- Return true if !(a > b). Takes linear time - bool operator<=(const FixedList<T, Size>& a) const; + bool operator<=(const FixedList<T, Size>& list) const; //- Return true if !(a < b). Takes linear time - bool operator>=(const FixedList<T, Size>& a) const; + bool operator>=(const FixedList<T, Size>& list) const; // Writing @@ -487,27 +417,28 @@ public: // IOstream operators - //- Read List from Istream, discarding contents of existing List + //- Read from Istream, discarding contents of existing List friend Istream& operator>> <T, Size> ( Istream& is, - FixedList<T, Size>& L + FixedList<T, Size>& lst ); - //- Write List to Ostream, as per writeList() with shortListLen=10 + //- Write to Ostream, as per writeList() with shortListLen=10 friend Ostream& operator<< <T, Size> ( Ostream& os, - const FixedList<T, Size>& L + const FixedList<T, Size>& lst ); }; // Global Functions -// Exchange contents of lists - see FixedList::swap(). +//- Swap FixedList contents - see FixedList::swap(). +// Internally this actually swaps the individual list elements template<class T, unsigned Size> -inline void Swap(FixedList<T,Size>& a, FixedList<T,Size>& b); +inline void Swap(FixedList<T,Size>& lhs, FixedList<T,Size>& rhs); // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H b/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H index 258eb38046a8d263af03fb720ed0575a04ad12ab..e24527b27df94e533c3363aa5a578a015b97828c 100644 --- a/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H +++ b/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H @@ -32,11 +32,6 @@ License // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -template<class T, unsigned Size> -inline Foam::FixedList<T, Size>::FixedList() -{} - - template<class T, unsigned Size> inline Foam::FixedList<T, Size>::FixedList(const T& val) { @@ -62,7 +57,7 @@ inline Foam::FixedList<T, Size>::FixedList(const FixedList<T, Size>& lst) { for (unsigned i=0; i<Size; ++i) { - v_[i] = lst[i]; + v_[i] = lst.v_[i]; } } @@ -70,9 +65,6 @@ inline Foam::FixedList<T, Size>::FixedList(const FixedList<T, Size>& lst) template<class T, unsigned Size> inline Foam::FixedList<T, Size>::FixedList(FixedList<T, Size>&& lst) { - // No significant speedup observed for copy assignment on simple types, - // use move assignment for generality with more complex types - for (unsigned i=0; i<Size; ++i) { v_[i] = std::move(lst.v_[i]); @@ -82,7 +74,7 @@ inline Foam::FixedList<T, Size>::FixedList(FixedList<T, Size>&& lst) template<class T, unsigned Size> template<class InputIterator> -Foam::FixedList<T, Size>::FixedList +inline Foam::FixedList<T, Size>::FixedList ( InputIterator begIter, InputIterator endIter @@ -130,7 +122,7 @@ inline Foam::FixedList<T, Size>::FixedList(const SLList<T>& lst) { checkSize(lst.size()); - typename SLList<T>::const_iterator iter = lst.begin(); + auto iter = lst.begin(); for (unsigned i=0; i<Size; ++i) { v_[i] = *iter; @@ -246,28 +238,38 @@ inline bool Foam::FixedList<T, Size>::found template<class T, unsigned Size> -inline void Foam::FixedList<T, Size>::resize(const label s) +inline void Foam::FixedList<T, Size>::resize(const label n) { #ifdef FULLDEBUG - checkSize(s); + checkSize(n); #endif } template<class T, unsigned Size> -inline void Foam::FixedList<T, Size>::setSize(const label s) +inline void Foam::FixedList<T, Size>::setSize(const label n) { #ifdef FULLDEBUG - checkSize(s); + checkSize(n); #endif } template<class T, unsigned Size> -inline void Foam::FixedList<T, Size>::transfer(const FixedList<T, Size>& lst) +inline void Foam::FixedList<T, Size>::swap(FixedList<T, Size>& lst) { for (unsigned i=0; i<Size; ++i) { - v_[i] = lst[i]; + Foam::Swap(v_[i], lst.v_[i]); + } +} + + +template<class T, unsigned Size> +inline void Foam::FixedList<T, Size>::transfer(FixedList<T, Size>& lst) +{ + for (unsigned i=0; i<Size; ++i) + { + v_[i] = std::move(lst[i]); } } @@ -444,7 +446,7 @@ template<class T, unsigned Size> inline typename Foam::FixedList<T, Size>::iterator Foam::FixedList<T, Size>::end() { - return &v_[Size]; + return (v_ + Size); } @@ -452,7 +454,7 @@ template<class T, unsigned Size> inline typename Foam::FixedList<T, Size>::const_iterator Foam::FixedList<T, Size>::end() const { - return &v_[Size]; + return (v_ + Size); } @@ -460,7 +462,7 @@ template<class T, unsigned Size> inline typename Foam::FixedList<T, Size>::const_iterator Foam::FixedList<T, Size>::cend() const { - return &v_[Size]; + return (v_ + Size); } @@ -468,7 +470,7 @@ template<class T, unsigned Size> inline typename Foam::FixedList<T, Size>::reverse_iterator Foam::FixedList<T, Size>::rbegin() { - return reverse_iterator(&v_[Size-1]); + return reverse_iterator(end()); } @@ -476,7 +478,7 @@ template<class T, unsigned Size> inline typename Foam::FixedList<T, Size>::const_reverse_iterator Foam::FixedList<T, Size>::rbegin() const { - return const_reverse_iterator(&v_[Size-1]); + return const_reverse_iterator(end()); } @@ -484,7 +486,7 @@ template<class T, unsigned Size> inline typename Foam::FixedList<T, Size>::const_reverse_iterator Foam::FixedList<T, Size>::crbegin() const { - return const_reverse_iterator(&v_[Size-1]); + return const_reverse_iterator(end()); } @@ -492,7 +494,7 @@ template<class T, unsigned Size> inline typename Foam::FixedList<T, Size>::reverse_iterator Foam::FixedList<T, Size>::rend() { - return reverse_iterator(&v_[-1]); + return reverse_iterator(begin()); } @@ -500,7 +502,7 @@ template<class T, unsigned Size> inline typename Foam::FixedList<T, Size>::const_reverse_iterator Foam::FixedList<T, Size>::rend() const { - return const_reverse_iterator(&v_[-1]); + return const_reverse_iterator(begin()); } @@ -508,7 +510,7 @@ template<class T, unsigned Size> inline typename Foam::FixedList<T, Size>::const_reverse_iterator Foam::FixedList<T, Size>::crend() const { - return const_reverse_iterator(&v_[-1]); + return const_reverse_iterator(begin()); } @@ -562,9 +564,9 @@ inline unsigned Foam::FixedList<T, Size>::Hash<HashT>::operator() // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * // template<class T, unsigned Size> -void Foam::Swap(FixedList<T, Size>& a, FixedList<T, Size>& b) +inline void Foam::Swap(FixedList<T, Size>& lhs, FixedList<T, Size>& rhs) { - a.swap(b); + lhs.swap(rhs); } diff --git a/src/OpenFOAM/containers/Lists/FixedList/FixedListIO.C b/src/OpenFOAM/containers/Lists/FixedList/FixedListIO.C index 87851fcef4a2ee0e9e0c110a8249c2de2a0043f0..cdf8d0b6994f368eb29b7585db7d1a471545eb47 100644 --- a/src/OpenFOAM/containers/Lists/FixedList/FixedListIO.C +++ b/src/OpenFOAM/containers/Lists/FixedList/FixedListIO.C @@ -67,7 +67,7 @@ Foam::Ostream& Foam::FixedList<T, Size>::writeList const label shortListLen ) const { - const FixedList<T, Size>& L = *this; + const FixedList<T, Size>& lst = *this; // Write list contents depending on data format if (os.format() == IOstream::ASCII || !contiguous<T>()) @@ -76,9 +76,9 @@ Foam::Ostream& Foam::FixedList<T, Size>::writeList bool uniform = (Size > 1 && contiguous<T>()); if (uniform) { - forAll(L, i) + for (unsigned i=0; i<Size; ++i) { - if (L[i] != L[0]) + if (lst[i] != lst[0]) { uniform = false; break; @@ -92,7 +92,7 @@ Foam::Ostream& Foam::FixedList<T, Size>::writeList os << Size << token::BEGIN_BLOCK; // Contents - os << L[0]; + os << lst[0]; // End delimiter os << token::END_BLOCK; @@ -107,10 +107,10 @@ Foam::Ostream& Foam::FixedList<T, Size>::writeList os << token::BEGIN_LIST; // Contents - forAll(L, i) + for (unsigned i=0; i<Size; ++i) { if (i) os << token::SPACE; - os << L[i]; + os << lst[i]; } // End delimiter @@ -122,9 +122,9 @@ Foam::Ostream& Foam::FixedList<T, Size>::writeList os << nl << token::BEGIN_LIST << nl; // Contents - forAll(L, i) + for (unsigned i=0; i<Size; ++i) { - os << L[i] << nl; + os << lst[i] << nl; } // End delimiter @@ -133,10 +133,10 @@ Foam::Ostream& Foam::FixedList<T, Size>::writeList } else { - // Contents are binary and contiguous + // Binary, contiguous // write(...) includes surrounding start/end delimiters - os.write(reinterpret_cast<const char*>(L.cdata()), Size*sizeof(T)); + os.write(reinterpret_cast<const char*>(lst.cdata()), Size*sizeof(T)); } os.check(FUNCTION_NAME); @@ -154,7 +154,7 @@ Foam::FixedList<T, Size>::FixedList(Istream& is) template<class T, unsigned Size> -Foam::Istream& Foam::operator>>(Foam::Istream& is, FixedList<T, Size>& L) +Foam::Istream& Foam::operator>>(Foam::Istream& is, FixedList<T, Size>& lst) { is.fatalCheck(FUNCTION_NAME); @@ -169,17 +169,17 @@ Foam::Istream& Foam::operator>>(Foam::Istream& is, FixedList<T, Size>& L) if (firstToken.isCompound()) { - L = dynamicCast<token::Compound<List<T>>> + lst = dynamicCast<token::Compound<List<T>>> ( firstToken.transferCompoundToken(is) ); } else if (firstToken.isLabel()) { - label s = firstToken.labelToken(); + const label len = firstToken.labelToken(); - // Set list length to that read - L.checkSize(s); + // List lengths must match + lst.checkSize(len); } else if (!firstToken.isPunctuation()) { @@ -202,7 +202,7 @@ Foam::Istream& Foam::operator>>(Foam::Istream& is, FixedList<T, Size>& L) { for (unsigned i=0; i<Size; ++i) { - is >> L[i]; + is >> lst[i]; is.fatalCheck ( @@ -224,7 +224,7 @@ Foam::Istream& Foam::operator>>(Foam::Istream& is, FixedList<T, Size>& L) for (unsigned i=0; i<Size; ++i) { - L[i] = element; + lst[i] = element; // Copy the value } } @@ -233,9 +233,9 @@ Foam::Istream& Foam::operator>>(Foam::Istream& is, FixedList<T, Size>& L) } else { - // contents are binary and contiguous + // Binary and contiguous - is.read(reinterpret_cast<char*>(L.data()), Size*sizeof(T)); + is.read(reinterpret_cast<char*>(lst.data()), Size*sizeof(T)); is.fatalCheck ( @@ -249,9 +249,9 @@ Foam::Istream& Foam::operator>>(Foam::Istream& is, FixedList<T, Size>& L) template<class T, unsigned Size> -Foam::Ostream& Foam::operator<<(Ostream& os, const FixedList<T, Size>& L) +Foam::Ostream& Foam::operator<<(Ostream& os, const FixedList<T, Size>& lst) { - return L.writeList(os, 10); + return lst.writeList(os, 10); } diff --git a/src/OpenFOAM/containers/Lists/List/List.C b/src/OpenFOAM/containers/Lists/List/List.C index 1e801804e9172c9e6edc315defae723d3d56c08d..ea2c3005249c71c5828fc5b2ea12a979d04e41b8 100644 --- a/src/OpenFOAM/containers/Lists/List/List.C +++ b/src/OpenFOAM/containers/Lists/List/List.C @@ -373,6 +373,15 @@ Foam::List<T>::List(SortableList<T>&& lst) } +template<class T> +Foam::List<T>::List(SLList<T>&& lst) +: + UList<T>(nullptr, 0) +{ + operator=(std::move(lst)); +} + + // * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * // template<class T> @@ -530,9 +539,11 @@ void Foam::List<T>::operator=(const List<T>& lst) template<class T> void Foam::List<T>::operator=(const SLList<T>& lst) { - reAlloc(lst.size()); + const label len = lst.size(); - if (this->size_) + reAlloc(len); + + if (len) { label i = 0; for (auto iter = lst.cbegin(); iter != lst.cend(); ++iter) @@ -631,6 +642,22 @@ void Foam::List<T>::operator=(SortableList<T>&& lst) } +template<class T> +void Foam::List<T>::operator=(SLList<T>&& lst) +{ + const label len = lst.size(); + + reAlloc(len); + + for (label i = 0; i < len; ++i) + { + this->operator[](i) = std::move(lst.removeHead()); + } + + lst.clear(); +} + + // * * * * * * * * * * * * * * * * IOStream operators * * * * * * * * * * * // #include "ListIO.C" diff --git a/src/OpenFOAM/containers/Lists/List/List.H b/src/OpenFOAM/containers/Lists/List/List.H index e27dffde08aa99286625264c454208035f608de2..73280d2492a4a4b6deb89c4967a614c7f483dc7b 100644 --- a/src/OpenFOAM/containers/Lists/List/List.H +++ b/src/OpenFOAM/containers/Lists/List/List.H @@ -43,6 +43,8 @@ SourceFiles #include "UList.H" #include "autoPtr.H" #include "Xfer.H" +#include "SLListFwd.H" + #include <initializer_list> // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -57,15 +59,9 @@ class Ostream; template<class T> class List; template<class T, unsigned Size> class FixedList; -template<class T> class PtrList; - -class SLListBase; -template<class LListBase, class T> class LList; -template<class T> -using SLList = LList<SLListBase, T>; - template<class T, int SizeMin> class DynamicList; +template<class T> class PtrList; template<class T> class SortableList; template<class T> class IndirectList; template<class T> class UIndirectList; @@ -73,6 +69,7 @@ template<class T> class BiIndirectList; template<class T> Istream& operator>>(Istream& is, List<T>& L); +// Commonly required list types typedef List<char> charList; @@ -217,6 +214,9 @@ public: //- Move construct from SortableList List(SortableList<T>&& lst); + //- Move construct from SLList + List(SLList<T>&& lst); + //- Construct from Istream List(Istream& is); @@ -326,6 +326,9 @@ public: //- Move assignment. Takes constant time. void operator=(SortableList<T>&& lst); + //- Move assignment. Takes constant time + void operator=(SLList<T>&& lst); + // Istream operator diff --git a/src/OpenFOAM/containers/Lists/List/ListIO.C b/src/OpenFOAM/containers/Lists/List/ListIO.C index 86850f91e93e48424481da74c3214b9fc1a54340..55d11dfb52f575ef53816027ffe57b38f76d9dd8 100644 --- a/src/OpenFOAM/containers/Lists/List/ListIO.C +++ b/src/OpenFOAM/containers/Lists/List/ListIO.C @@ -41,10 +41,10 @@ Foam::List<T>::List(Istream& is) template<class T> -Foam::Istream& Foam::operator>>(Istream& is, List<T>& L) +Foam::Istream& Foam::operator>>(Istream& is, List<T>& lst) { // Anull list - L.setSize(0); + lst.setSize(0); is.fatalCheck(FUNCTION_NAME); @@ -52,22 +52,28 @@ Foam::Istream& Foam::operator>>(Istream& is, List<T>& L) is.fatalCheck(FUNCTION_NAME); + // Compound: simply transfer contents if (firstToken.isCompound()) { - L.transfer + lst.transfer ( dynamicCast<token::Compound<List<T>>> ( firstToken.transferCompoundToken(is) ) ); + + return is; } - else if (firstToken.isLabel()) + + + // Label: could be int(..), int{...} or just a plain '0' + if (firstToken.isLabel()) { - const label sz = firstToken.labelToken(); + const label len = firstToken.labelToken(); // Set list length to that read - L.setSize(sz); + lst.setSize(len); // Read list contents depending on data format @@ -76,13 +82,13 @@ Foam::Istream& Foam::operator>>(Istream& is, List<T>& L) // Read beginning of contents const char delimiter = is.readBeginList("List"); - if (sz) + if (len) { if (delimiter == token::BEGIN_LIST) { - for (label i=0; i<sz; ++i) + for (label i=0; i<len; ++i) { - is >> L[i]; + is >> lst[i]; is.fatalCheck ( @@ -103,9 +109,9 @@ Foam::Istream& Foam::operator>>(Istream& is, List<T>& L) "reading the single entry" ); - for (label i=0; i<sz; ++i) + for (label i=0; i<len; ++i) { - L[i] = element; + lst[i] = element; // Copy the value } } } @@ -113,22 +119,24 @@ Foam::Istream& Foam::operator>>(Istream& is, List<T>& L) // Read end of contents is.readEndList("List"); } - else + else if (len) { - // Contents are binary and contiguous + // Non-empty, binary, contiguous - if (sz) - { - is.read(reinterpret_cast<char*>(L.data()), sz*sizeof(T)); + is.read(reinterpret_cast<char*>(lst.data()), len*sizeof(T)); - is.fatalCheck - ( - "operator>>(Istream&, List<T>&) : reading the binary block" - ); - } + is.fatalCheck + ( + "operator>>(Istream&, List<T>&) : reading the binary block" + ); } + + return is; } - else if (firstToken.isPunctuation()) + + + // "(...)" : read as SLList and transfer contents + if (firstToken.isPunctuation()) { if (firstToken.pToken() != token::BEGIN_LIST) { @@ -138,23 +146,22 @@ Foam::Istream& Foam::operator>>(Istream& is, List<T>& L) << exit(FatalIOError); } - // Putback the opening bracket - is.putBack(firstToken); + is.putBack(firstToken); // Putback the opening bracket - // Read as a singly-linked list - SLList<T> sll(is); + SLList<T> sll(is); // Read as singly-linked list - // Convert the singly-linked list to this list - L = sll; - } - else - { - FatalIOErrorInFunction(is) - << "incorrect first token, expected <int> or '(', found " - << firstToken.info() - << exit(FatalIOError); + // Reallocate and move assign list elements + lst = std::move(sll); + + return is; } + + FatalIOErrorInFunction(is) + << "incorrect first token, expected <int> or '(', found " + << firstToken.info() + << exit(FatalIOError); + return is; } diff --git a/src/OpenFOAM/containers/Lists/PackedList/PackedBoolList.C b/src/OpenFOAM/containers/Lists/PackedList/PackedBoolList.C index db6036d715194a70ebbbb89898c83e1cb15cedac..7179796b7e82a1da0925705454e14ee816c9db02 100644 --- a/src/OpenFOAM/containers/Lists/PackedList/PackedBoolList.C +++ b/src/OpenFOAM/containers/Lists/PackedList/PackedBoolList.C @@ -265,26 +265,28 @@ Foam::label Foam::PackedBoolList::subset(const labelUIndList& indices) } -Foam::Xfer<Foam::labelList> Foam::PackedBoolList::used() const +Foam::labelList Foam::PackedBoolList::used() const { - labelList lst(this->count()); + // Number of used (set) entries + const label cnt = this->count(); - if (lst.size()) + labelList lst(cnt); + + if (cnt) { - label nElem = 0; + // The length of the input list + const label len = this->size(); - forAll(*this, elemI) + for (label i=0, usedi=0; (i < len && usedi < cnt); ++i) { - if (get(elemI)) + if (get(i)) { - lst[nElem++] = elemI; + lst[usedi++] = i; } } - - lst.setSize(nElem); } - return lst.xfer(); + return lst; } diff --git a/src/OpenFOAM/containers/Lists/PackedList/PackedBoolList.H b/src/OpenFOAM/containers/Lists/PackedList/PackedBoolList.H index 28209b6ea53d3f3e665d0d39dd22800d98f2de5f..717be74288efeb411da48c90716c7245da9ffaba 100644 --- a/src/OpenFOAM/containers/Lists/PackedList/PackedBoolList.H +++ b/src/OpenFOAM/containers/Lists/PackedList/PackedBoolList.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2017-2018 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -91,7 +91,7 @@ public: // Constructors //- Construct null - inline PackedBoolList(); + PackedBoolList() = default; //- Construct from Istream PackedBoolList(Istream& is); @@ -102,17 +102,17 @@ public: //- Construct with given size and value for all elements inline PackedBoolList(const label size, const bool val); - //- Copy constructor + //- Copy construct inline PackedBoolList(const PackedBoolList& lst); - //- Copy constructor + //- Copy construct explicit inline PackedBoolList(const PackedList<1>& lst); - //- Construct by transferring the parameter contents - inline PackedBoolList(const Xfer<PackedBoolList>& lst); + //- Move construct + inline PackedBoolList(PackedBoolList&& lst); - //- Construct by transferring the parameter contents - inline PackedBoolList(const Xfer<PackedList<1>>& lst); + //- Move construct + inline PackedBoolList(PackedList<1>&& lst); //- Construct with given size and list of labels to set as true. inline PackedBoolList(const label size, const labelUList& indices); @@ -137,7 +137,7 @@ public: // Member Functions - // Access + // Access using PackedList<1>::set; using PackedList<1>::unset; @@ -175,12 +175,11 @@ public: // Return number of elements subsetted. label subset(const labelUIndList& indices); - //- Return indices of the used (true) elements as a list of labels - Xfer<labelList> used() const; + labelList used() const; - // Edit + // Edit //- Transfer the contents of the argument list into this list //- and annul the argument list. @@ -190,21 +189,24 @@ public: //- and annul the argument list. inline void transfer(PackedList<1>& lst); - //- Transfer contents to the Xfer container - inline Xfer<PackedBoolList> xfer(); - // Member Operators //- Assignment of all entries to the given value. inline void operator=(const bool val); - //- Assignment operator. + //- Copy assignment inline void operator=(const PackedBoolList& lst); - //- Assignment operator. + //- Copy assignment inline void operator=(const PackedList<1>& lst); + //- Move assignment + inline void operator=(PackedBoolList&& lst); + + //- Move assignment + inline void operator=(PackedList<1>&& lst); + //- Assignment operator. void operator=(const UList<bool>& lst); diff --git a/src/OpenFOAM/containers/Lists/PackedList/PackedBoolListI.H b/src/OpenFOAM/containers/Lists/PackedList/PackedBoolListI.H index db40fab00b272cbf6501984bd39fb3e6dcf6b8a7..c89acf0bc38b2f14af92d41224c2b6f600e8175c 100644 --- a/src/OpenFOAM/containers/Lists/PackedList/PackedBoolListI.H +++ b/src/OpenFOAM/containers/Lists/PackedList/PackedBoolListI.H @@ -25,12 +25,6 @@ License // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -inline Foam::PackedBoolList::PackedBoolList() -: - PackedList<1>() -{} - - inline Foam::PackedBoolList::PackedBoolList(const label size) : PackedList<1>(size) @@ -59,18 +53,20 @@ inline Foam::PackedBoolList::PackedBoolList(const PackedList<1>& lst) {} -inline Foam::PackedBoolList::PackedBoolList(const Xfer<PackedBoolList>& lst) +inline Foam::PackedBoolList::PackedBoolList(PackedBoolList&& lst) : PackedList<1>() { - transfer(lst()); + transfer(lst); } -inline Foam::PackedBoolList::PackedBoolList(const Xfer<PackedList<1>>& lst) +inline Foam::PackedBoolList::PackedBoolList(PackedList<1>&& lst) : - PackedList<1>(lst) -{} + PackedList<1>() +{ + transfer(lst); +} inline Foam::PackedBoolList::PackedBoolList(const UList<bool>& lst) @@ -147,12 +143,6 @@ inline void Foam::PackedBoolList::transfer(PackedList<1>& lst) } -inline Foam::Xfer<Foam::PackedBoolList> Foam::PackedBoolList::xfer() -{ - return xferMove(*this); -} - - // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // inline void Foam::PackedBoolList::operator=(const bool val) @@ -173,6 +163,18 @@ inline void Foam::PackedBoolList::operator=(const PackedList<1>& lst) } +inline void Foam::PackedBoolList::operator=(PackedBoolList&& lst) +{ + transfer(lst); +} + + +inline void Foam::PackedBoolList::operator=(PackedList<1>&& lst) +{ + transfer(lst); +} + + inline void Foam::PackedBoolList::operator=(const labelUList& indices) { clear(); diff --git a/src/OpenFOAM/containers/Lists/PackedList/PackedList.C b/src/OpenFOAM/containers/Lists/PackedList/PackedList.C index b8069fe3e01ab7bb31d69f1edf3d19f9adaed9be..63c0908961f3c69ebf77e445e851b17e9f56e6cf 100644 --- a/src/OpenFOAM/containers/Lists/PackedList/PackedList.C +++ b/src/OpenFOAM/containers/Lists/PackedList/PackedList.C @@ -141,16 +141,16 @@ void Foam::PackedList<nBits>::flip() template<unsigned nBits> -Foam::Xfer<Foam::labelList> Foam::PackedList<nBits>::values() const +Foam::labelList Foam::PackedList<nBits>::values() const { labelList elems(size_); - forAll(*this, i) + for (label i=0; i < size_; ++i) { elems[i] = get(i); } - return elems.xfer(); + return elems; } @@ -271,34 +271,34 @@ Foam::Istream& Foam::PackedList<nBits>::read(Istream& is) token firstTok(is); is.fatalCheck ( - "PackedList<nBits>::read(Istream&) : " + "PackedList::read(Istream&) : " "reading first token" ); if (firstTok.isLabel()) { - const label sz = firstTok.labelToken(); + const label len = firstTok.labelToken(); // Set list length to that read - lst.resize(sz); + lst.resize(len); // Read list contents depending on data format if (is.format() == IOstream::ASCII) { // Read beginning of contents - const char delimiter = is.readBeginList("PackedList<nBits>"); + const char delimiter = is.readBeginList("PackedList"); - if (sz) + if (len) { if (delimiter == token::BEGIN_LIST) { - for (label i=0; i<sz; ++i) + for (label i=0; i<len; ++i) { lst[i] = lst.readValue(is); is.fatalCheck ( - "PackedList<nBits>::read(Istream&) : " + "PackedList::read(Istream&) : " "reading entry" ); } @@ -310,7 +310,7 @@ Foam::Istream& Foam::PackedList<nBits>::read(Istream& is) is.fatalCheck ( - "PackedList<nBits>::read(Istream&) : " + "PackedList::read(Istream&) : " "reading the single entry" ); } @@ -324,11 +324,11 @@ Foam::Istream& Foam::PackedList<nBits>::read(Istream& is) } // Read end of contents - is.readEndList("PackedList<nBits>"); + is.readEndList("PackedList"); } else { - if (sz) + if (len) { is.read ( @@ -338,7 +338,7 @@ Foam::Istream& Foam::PackedList<nBits>::read(Istream& is) is.fatalCheck ( - "PackedList<nBits>::read(Istream&) : " + "PackedList::read(Istream&) : " "reading the binary block" ); } @@ -413,13 +413,13 @@ Foam::Ostream& Foam::PackedList<nBits>::writeList { const bool indexedOutput = (shortListLen < 0); const PackedList<nBits>& lst = *this; - const label sz = lst.size(); + const label len = lst.size(); // Write list contents depending on data format if (os.format() == IOstream::ASCII) { // Can the contents be considered 'uniform' (ie, identical)? - bool uniform = (sz > 1 && !indexedOutput); + bool uniform = (len > 1 && !indexedOutput); if (uniform) { forAll(lst, i) @@ -435,7 +435,7 @@ Foam::Ostream& Foam::PackedList<nBits>::writeList if (uniform) { // uniform values: - os << sz << token::BEGIN_BLOCK << lst[0] << token::END_BLOCK; + os << len << token::BEGIN_BLOCK << lst[0] << token::END_BLOCK; } else if (indexedOutput) { @@ -457,10 +457,10 @@ Foam::Ostream& Foam::PackedList<nBits>::writeList os << token::END_BLOCK << nl; } - else if (!shortListLen || sz <= shortListLen) + else if (!shortListLen || len <= shortListLen) { // Shorter list, or line-breaks suppressed - os << sz << token::BEGIN_LIST; + os << len << token::BEGIN_LIST; forAll(lst, i) { if (i) os << token::SPACE; @@ -471,7 +471,7 @@ Foam::Ostream& Foam::PackedList<nBits>::writeList else { // Longer list - os << nl << sz << nl << token::BEGIN_LIST << nl; + os << nl << len << nl << token::BEGIN_LIST << nl; forAll(lst, i) { os << lst[i] << nl; @@ -482,9 +482,9 @@ Foam::Ostream& Foam::PackedList<nBits>::writeList else { // Contents are binary and contiguous - os << nl << sz << nl; + os << nl << len << nl; - if (sz) + if (len) { // write(...) includes surrounding start/end delimiters os.write @@ -522,6 +522,13 @@ void Foam::PackedList<nBits>::operator=(const PackedList<nBits>& lst) } +template<unsigned nBits> +void Foam::PackedList<nBits>::operator=(PackedList<nBits>&& lst) +{ + transfer(lst); +} + + template<unsigned nBits> void Foam::PackedList<nBits>::operator=(const labelUList& lst) { diff --git a/src/OpenFOAM/containers/Lists/PackedList/PackedList.H b/src/OpenFOAM/containers/Lists/PackedList/PackedList.H index 2ae542aeaaeac7f00678a11c1a254e10bd8cf0a2..8ffd02e70434dbfaebc90e05d5c80c070b4b8686 100644 --- a/src/OpenFOAM/containers/Lists/PackedList/PackedList.H +++ b/src/OpenFOAM/containers/Lists/PackedList/PackedList.H @@ -235,11 +235,11 @@ public: //- Construct from Istream inline PackedList(Istream& is); - //- Copy constructor + //- Copy construct inline PackedList(const PackedList<nBits>& lst); - //- Construct by transferring the parameter contents - inline PackedList(const Xfer<PackedList<nBits>>& lst); + //- Move construct + inline PackedList(PackedList<nBits>&& lst); //- Construct from a list of labels explicit inline PackedList(const labelUList& lst); @@ -297,7 +297,7 @@ public: unsigned int count() const; //- Return the values as a list of labels - Xfer<labelList> values() const; + labelList values() const; //- Print bit patterns, optionally output unused elements // @@ -356,9 +356,6 @@ public: // and annul the argument list. inline void transfer(PackedList<nBits>& lst); - //- Transfer contents to the Xfer container - inline Xfer<PackedList<nBits>> xfer(); - // IO @@ -407,9 +404,12 @@ public: //- Assignment of all entries to the given value. Takes linear time. inline void operator=(const unsigned int val); - //- Assignment operator. + //- Copy assignment. void operator=(const PackedList<nBits>& lst); + //- Move assignment. + void operator=(PackedList<nBits>&& lst); + //- Assignment operator. void operator=(const labelUList& lst); diff --git a/src/OpenFOAM/containers/Lists/PackedList/PackedListI.H b/src/OpenFOAM/containers/Lists/PackedList/PackedListI.H index 94f91cee4ba1e324bae8f5640d2a33c57bdeeb5d..b4efd00711667359774ddd6403e97601687bd030 100644 --- a/src/OpenFOAM/containers/Lists/PackedList/PackedListI.H +++ b/src/OpenFOAM/containers/Lists/PackedList/PackedListI.H @@ -223,9 +223,13 @@ inline Foam::PackedList<nBits>::PackedList(const PackedList<nBits>& lst) template<unsigned nBits> -inline Foam::PackedList<nBits>::PackedList(const Xfer<PackedList<nBits>>& lst) +inline Foam::PackedList<nBits>::PackedList(PackedList<nBits>&& lst) +: + PackedListCore(), + StorageList(), + size_(0) { - transfer(lst()); + transfer(lst); } @@ -956,13 +960,6 @@ inline void Foam::PackedList<nBits>::transfer(PackedList<nBits>& lst) } -template<unsigned nBits> -inline Foam::Xfer<Foam::PackedList<nBits>> Foam::PackedList<nBits>::xfer() -{ - return xferMove(*this); -} - - template<unsigned nBits> inline unsigned int Foam::PackedList<nBits>::get(const label i) const { diff --git a/src/OpenFOAM/containers/Lists/PtrList/PtrList.C b/src/OpenFOAM/containers/Lists/PtrList/PtrList.C index c0183f501e92164a54e165b6c5da8e0afbb46009..fd0b7263cadfd1e362c756a6d1ee96f15b89be81 100644 --- a/src/OpenFOAM/containers/Lists/PtrList/PtrList.C +++ b/src/OpenFOAM/containers/Lists/PtrList/PtrList.C @@ -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) 2018 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -29,40 +29,30 @@ License // * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * // template<class T> -Foam::PtrList<T>::PtrList() +Foam::PtrList<T>::PtrList(const PtrList<T>& lst) : - UPtrList<T>() -{} - - -template<class T> -Foam::PtrList<T>::PtrList(const label s) -: - UPtrList<T>(s) -{} - - -template<class T> -Foam::PtrList<T>::PtrList(const PtrList<T>& a) -: - UPtrList<T>(a.size()) + UPtrList<T>(lst.size()) { - forAll(*this, i) + const label len = this->size(); + + for (label i=0; i<len; ++i) { - this->ptrs_[i] = (a[i]).clone().ptr(); + this->ptrs_[i] = (lst[i]).clone().ptr(); } } template<class T> template<class CloneArg> -Foam::PtrList<T>::PtrList(const PtrList<T>& a, const CloneArg& cloneArg) +Foam::PtrList<T>::PtrList(const PtrList<T>& lst, const CloneArg& cloneArg) : - UPtrList<T>(a.size()) + UPtrList<T>(lst.size()) { - forAll(*this, i) + const label len = this->size(); + + for (label i=0; i<len; ++i) { - this->ptrs_[i] = (a[i]).clone(cloneArg).ptr(); + this->ptrs_[i] = (lst[i]).clone(cloneArg).ptr(); } } @@ -75,36 +65,33 @@ Foam::PtrList<T>::PtrList(const Xfer<PtrList<T>>& lst) template<class T> -Foam::PtrList<T>::PtrList(PtrList<T>& a, bool reuse) +Foam::PtrList<T>::PtrList(PtrList<T>& lst, bool reuse) : - UPtrList<T>(a, reuse) + UPtrList<T>(lst, reuse) { if (!reuse) { - forAll(*this, i) + const label len = this->size(); + + for (label i=0; i<len; ++i) { - this->ptrs_[i] = (a[i]).clone().ptr(); + this->ptrs_[i] = (lst[i]).clone().ptr(); } } } template<class T> -Foam::PtrList<T>::PtrList(const SLPtrList<T>& sll) +Foam::PtrList<T>::PtrList(const SLPtrList<T>& lst) : - UPtrList<T>(sll.size()) + UPtrList<T>(lst.size()) { - if (sll.size()) + if (lst.size()) { label i = 0; - for - ( - typename SLPtrList<T>::const_iterator iter = sll.begin(); - iter != sll.end(); - ++iter - ) + for (auto iter = lst.cbegin(); iter != lst.cend(); ++iter) { - this->ptrs_[i++] = (iter()).clone().ptr(); + this->ptrs_[i++] = (*iter).clone().ptr(); } } } @@ -115,7 +102,10 @@ Foam::PtrList<T>::PtrList(const SLPtrList<T>& sll) template<class T> Foam::PtrList<T>::~PtrList() { - forAll(*this, i) + const label len = this->size(); + + // Free old pointers + for (label i=0; i<len; ++i) { if (this->ptrs_[i]) { @@ -127,53 +117,12 @@ Foam::PtrList<T>::~PtrList() // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // -template<class T> -void Foam::PtrList<T>::setSize(const label newSize) -{ - if (newSize < 0) - { - FatalErrorInFunction - << "bad set size " << newSize - << " for type " << typeid(T).name() - << abort(FatalError); - } - - label oldSize = this->size(); - - if (newSize == 0) - { - clear(); - } - else if (newSize < oldSize) - { - label i; - for (i=newSize; i<oldSize; i++) - { - if (this->ptrs_[i]) - { - delete this->ptrs_[i]; - } - } - - this->ptrs_.setSize(newSize); - } - else // newSize > oldSize - { - this->ptrs_.setSize(newSize); - - label i; - for (i=oldSize; i<newSize; i++) - { - this->ptrs_[i] = nullptr; - } - } -} - - template<class T> void Foam::PtrList<T>::clear() { - forAll(*this, i) + const label len = this->size(); + + for (label i=0; i<len; ++i) { if (this->ptrs_[i]) { @@ -181,109 +130,82 @@ void Foam::PtrList<T>::clear() } } - this->ptrs_.clear(); -} - - -template<class T> -void Foam::PtrList<T>::transfer(PtrList<T>& a) -{ - clear(); - this->ptrs_.transfer(a.ptrs_); + UPtrList<T>::clear(); } template<class T> -void Foam::PtrList<T>::reorder(const labelUList& oldToNew) +void Foam::PtrList<T>::setSize(const label newLen) { - if (oldToNew.size() != this->size()) + if (newLen <= 0) { - FatalErrorInFunction - << "Size of map (" << oldToNew.size() - << ") not equal to list size (" << this->size() - << ") for type " << typeid(T).name() - << abort(FatalError); + clear(); + return; } - List<T*> newPtrs_(this->ptrs_.size(), reinterpret_cast<T*>(0)); - - forAll(*this, i) + const label oldLen = this->size(); + if (newLen < oldLen) { - label newI = oldToNew[i]; - - if (newI < 0 || newI >= this->size()) + // Truncate - free old pointers + for (label i=newLen; i<oldLen; ++i) { - FatalErrorInFunction - << "Illegal index " << newI << nl - << "Valid indices are 0.." << this->size()-1 - << " for type " << typeid(T).name() - << abort(FatalError); + if (this->ptrs_[i]) + { + delete this->ptrs_[i]; + } } - if (newPtrs_[newI]) - { - FatalErrorInFunction - << "reorder map is not unique; element " << newI - << " already set for type " << typeid(T).name() - << abort(FatalError); - } - newPtrs_[newI] = this->ptrs_[i]; + this->ptrs_.setSize(newLen); } - - forAll(newPtrs_, i) + else if (newLen > oldLen) { - if (!newPtrs_[i]) - { - FatalErrorInFunction - << "Element " << i << " not set after reordering with type " - << typeid(T).name() << nl << abort(FatalError); - } + // Extend - new elements initialized to nullptr + this->ptrs_.setSize(newLen, reinterpret_cast<T*>(0)); } - - this->ptrs_.transfer(newPtrs_); } // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // template<class T> -void Foam::PtrList<T>::operator=(const PtrList<T>& a) +void Foam::PtrList<T>::operator=(const PtrList<T>& lst) { - if (this == &a) + if (this == &lst) { FatalErrorInFunction << "attempted assignment to self for type " << typeid(T).name() << abort(FatalError); } - if (this->size() == 0) - { - setSize(a.size()); + const label oldLen = this->size(); + const label newLen = lst.size(); - forAll(*this, i) - { - this->ptrs_[i] = (a[i]).clone().ptr(); - } - } - else if (a.size() == this->size()) + // Truncate (frees old pointers) or extend the length + setSize(newLen); + + if (newLen < oldLen) { - forAll(*this, i) + // Copy values for existing entries + for (label i=0; i<newLen; ++i) { - (*this)[i] = a[i]; + (*this)[i] = lst[i]; } } else { - FatalErrorInFunction - << "bad size: " << a.size() - << " for type " << typeid(T).name() - << abort(FatalError); + // Copy values for existing entries + for (label i=0; i<oldLen; ++i) + { + (*this)[i] = lst[i]; + } + + // Clone pointers for new entries + for (label i=oldLen; i<newLen; ++i) + { + this->ptrs_[i] = (lst[i]).clone().ptr(); + } } } -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - -#include "PtrListIO.C" - // ************************************************************************* // diff --git a/src/OpenFOAM/containers/Lists/PtrList/PtrList.H b/src/OpenFOAM/containers/Lists/PtrList/PtrList.H index a59a2a4e712525982bdce3e863a46ff4fdad1ca6..c49f70846b9e70442428ee47ed3e2df5066af446 100644 --- a/src/OpenFOAM/containers/Lists/PtrList/PtrList.H +++ b/src/OpenFOAM/containers/Lists/PtrList/PtrList.H @@ -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) 2018 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -25,11 +25,12 @@ Class Foam::PtrList Description - A templated 1D list of pointers to objects of type \<T\>, where the - size of the array is known and used for subscript bounds checking, etc. + A list of pointers to objects of type \<T\>, with allocation/deallocation + management of the pointers. + The operator[] returns a reference to the object, not the pointer. - The element operator [] returns a reference to the object rather than a - pointer. +See Also + Foam::PtrList SourceFiles PtrListI.H @@ -42,27 +43,20 @@ SourceFiles #define PtrList_H #include "UPtrList.H" +#include "SLPtrListFwd.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // namespace Foam { -// Forward declaration of classes +// Forward declarations template<class T> class autoPtr; template<class T> class tmp; - -class SLListBase; -template<class LListBase, class T> class LPtrList; -template<class T> -using SLPtrList = LPtrList<SLListBase, T>; - -// Forward declaration of friend functions and operators template<class T> class PtrList; -template<class T> -Istream& operator>>(Istream&, PtrList<T>&); +template<class T> Istream& operator>>(Istream& is, PtrList<T>& lst); /*---------------------------------------------------------------------------*\ @@ -81,103 +75,106 @@ protected: //- Read from Istream using given Istream constructor class template<class INew> - void read(Istream&, const INew& inewt); + void read(Istream& is, const INew& inew); public: // Constructors - //- Null Constructor - PtrList(); + //- Construct null + PtrList() = default; + + //- Construct with specified size, each element initialized to nullptr + explicit inline PtrList(const label nElem); - //- Construct with size specified - explicit PtrList(const label); + //- Copy construct using 'clone()' method on each element + PtrList(const PtrList<T>& lst); - //- Copy constructor - PtrList(const PtrList<T>&); + //- Move construct + inline PtrList(PtrList<T>&& lst); - //- Copy constructor with additional argument for clone + //- Copy construct with additional argument for 'clone()' template<class CloneArg> - PtrList(const PtrList<T>&, const CloneArg&); + PtrList(const PtrList<T>& lst, const CloneArg& cloneArg); //- Construct by transferring the parameter contents - PtrList(const Xfer<PtrList<T>>&); + PtrList(const Xfer<PtrList<T>>& lst); //- Construct as copy or re-use as specified - PtrList(PtrList<T>&, bool reuse); + PtrList(PtrList<T>& lst, bool reuse); //- Construct as copy of SLPtrList<T> - explicit PtrList(const SLPtrList<T>&); + explicit PtrList(const SLPtrList<T>& lst); //- Construct from Istream using given Istream constructor class template<class INew> - PtrList(Istream&, const INew&); + PtrList(Istream& is, const INew& inew); //- Construct from Istream using default Istream constructor class - PtrList(Istream&); + PtrList(Istream& is); //- Destructor ~PtrList(); - // Member functions + // Member Functions - // Edit + //- Clear the PtrList. Set size to zero and delete allocated entries + void clear(); - //- Reset size of PtrList. If extending the PtrList, new entries are - // set to nullptr. If truncating the PtrList, removed entries are - // deleted - void setSize(const label); + //- Reset size of PtrList. + // New entries are initialized to nullptr, removed entries are deleted + void setSize(const label newLen); - //- Alias for setSize(const label) - inline void resize(const label); + //- Reset size of PtrList. + // New entries are initialized to nullptr, removed entries are deleted + inline void resize(const label newLen); - //- Clear the PtrList, i.e. set size to zero deleting all the - // allocated entries - void clear(); + //- Append an element at the end of the list + using UPtrList<T>::append; - //- Append an element at the end of the list - inline void append(T*); - inline void append(const autoPtr<T>&); - inline void append(const tmp<T>&); + //- Append an element at the end of the list + inline void append(const autoPtr<T>& aptr); - //- Transfer the contents of the argument PtrList into this PtrList - // and annul the argument list - void transfer(PtrList<T>&); + //- Append an element at the end of the list + inline void append(const tmp<T>& tptr); - //- Transfer contents to the Xfer container - inline Xfer<PtrList<T>> xfer(); + //- Transfer into this list and annul the argument list + inline void transfer(PtrList<T>& lst); - //- Is element set - inline bool set(const label) const; + //- Transfer contents to the Xfer container + inline Xfer<PtrList<T>> xfer(); - //- Set element to given T* and return old element (can be nullptr) - inline autoPtr<T> set(const label, T*); + //- Return true if element is set (ie, not a nullptr) + inline bool set(const label i) const; - //- Set element to given autoPtr<T> and return old element - inline autoPtr<T> set(const label, const autoPtr<T>&); + //- Set element to given pointer and return old element (can be null) + inline autoPtr<T> set(const label i, T* ptr); - //- Set element to given tmp<T> and return old element - inline autoPtr<T> set(const label, const tmp<T>&); + //- Set element to given autoPtr and return old element + inline autoPtr<T> set(const label i, const autoPtr<T>& aptr); - //- Reorders elements. Ordering does not have to be done in - // ascending or descending order. Reordering has to be unique. - // (is shuffle) - void reorder(const labelUList&); + //- Set element to given tmp and return old element + inline autoPtr<T> set(const label i, const tmp<T>& tptr); // Member operators - //- Assignment - void operator=(const PtrList<T>&); + //- Copy assignment. + // For existing list entries, values are copied from the list. + // For new list entries, pointers are cloned from the list. + void operator=(const PtrList<T>& lst); + + //- Move assignment + inline void operator=(PtrList<T>&& lst); // IOstream operator //- Read PtrList from Istream, discarding contents of existing PtrList - friend Istream& operator>> <T>(Istream&, PtrList<T>&); + friend Istream& operator>> <T>(Istream& is, PtrList<T>& lst); }; @@ -193,6 +190,7 @@ public: #ifdef NoRepository #include "PtrList.C" + #include "PtrListIO.C" #endif // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/OpenFOAM/containers/Lists/PtrList/PtrListI.H b/src/OpenFOAM/containers/Lists/PtrList/PtrListI.H index 821c00de35b89db6ec33b96f596cb764530c7820..50885d572e051b7a87a1867770ff316d1f1198b7 100644 --- a/src/OpenFOAM/containers/Lists/PtrList/PtrListI.H +++ b/src/OpenFOAM/containers/Lists/PtrList/PtrListI.H @@ -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) 2018 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -26,38 +26,42 @@ License #include "autoPtr.H" #include "tmp.H" -// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // +// * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * // template<class T> -inline void Foam::PtrList<T>::resize(const label newSize) -{ - this->setSize(newSize); -} +inline Foam::PtrList<T>::PtrList(const label nElem) +: + UPtrList<T>(nElem) +{} template<class T> -inline void Foam::PtrList<T>::append(T* ptr) +inline Foam::PtrList<T>::PtrList(PtrList<T>&& lst) +: + UPtrList<T>(std::move(lst)) +{} + + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +template<class T> +inline void Foam::PtrList<T>::resize(const label newLen) { - label sz = this->size(); - this->setSize(sz+1); - this->ptrs_[sz] = ptr; + this->setSize(newLen); } template<class T> inline void Foam::PtrList<T>::append(const autoPtr<T>& aptr) { - return append(const_cast<autoPtr<T>&>(aptr).ptr()); + return UPtrList<T>::append(const_cast<autoPtr<T>&>(aptr).ptr()); } template<class T> -inline void Foam::PtrList<T>::append -( - const tmp<T>& t -) +inline void Foam::PtrList<T>::append(const tmp<T>& tptr) { - return append(const_cast<tmp<T>&>(t).ptr()); + return UPtrList<T>::append(const_cast<tmp<T>&>(tptr).ptr()); } @@ -92,10 +96,18 @@ template<class T> inline Foam::autoPtr<T> Foam::PtrList<T>::set ( const label i, - const tmp<T>& t + const tmp<T>& tptr ) { - return set(i, const_cast<tmp<T>&>(t).ptr()); + return set(i, const_cast<tmp<T>&>(tptr).ptr()); +} + + +template<class T> +inline void Foam::PtrList<T>::transfer(PtrList<T>& lst) +{ + this->clear(); + UPtrList<T>::swap(lst); } @@ -106,4 +118,14 @@ inline Foam::Xfer<Foam::PtrList<T>> Foam::PtrList<T>::xfer() } +// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // + +template<class T> +inline void Foam::PtrList<T>::operator=(PtrList<T>&& lst) +{ + this->clear(); + this->swap(lst); +} + + // ************************************************************************* // diff --git a/src/OpenFOAM/containers/Lists/PtrList/PtrListIO.C b/src/OpenFOAM/containers/Lists/PtrList/PtrListIO.C index 594aa01e4dd0171902b2d53726b71c77f6d08c0f..141225f776bb6289e55bb7b08006b44dfb12de48 100644 --- a/src/OpenFOAM/containers/Lists/PtrList/PtrListIO.C +++ b/src/OpenFOAM/containers/Lists/PtrList/PtrListIO.C @@ -33,7 +33,7 @@ License template<class T> template<class INew> -void Foam::PtrList<T>::read(Istream& is, const INew& inewt) +void Foam::PtrList<T>::read(Istream& is, const INew& inew) { is.fatalCheck(FUNCTION_NAME); @@ -41,58 +41,66 @@ void Foam::PtrList<T>::read(Istream& is, const INew& inewt) is.fatalCheck ( - "PtrList<T>::read(Istream&, const INew&) : " + "PtrList::read(Istream&) : " "reading first token" ); + + // Label: could be int(..), int{...} or just a plain '0' if (firstToken.isLabel()) { // Read size of list - const label sz = firstToken.labelToken(); + const label len = firstToken.labelToken(); // Set list length to that read - setSize(sz); + setSize(len); // Read beginning of contents const char delimiter = is.readBeginList("PtrList"); - if (sz) + if (len) { if (delimiter == token::BEGIN_LIST) { - for (label i=0; i<sz; ++i) + for (label i=0; i<len; ++i) { - set(i, inewt(is)); + T* p = inew(is).ptr(); + set(i, p); is.fatalCheck ( - "PtrList<T>::read(Istream&, const INew&) : " + "PtrList::read(Istream&) : " "reading entry" ); } } else { - T* tPtr = inewt(is).ptr(); - set(0, tPtr); + T* p = inew(is).ptr(); + set(0, p); is.fatalCheck ( - "PtrList<T>::read(Istream&, const INew&) : " + "PtrList::read(Istream&) : " "reading the single entry" ); - for (label i=1; i<sz; ++i) + for (label i=1; i<len; ++i) { - set(i, tPtr->clone()); + set(i, p->clone()); } } } // Read end of contents is.readEndList("PtrList"); + + return; } - else if (firstToken.isPunctuation()) + + + // "(...)" : read as SLList and transfer contents + if (firstToken.isPunctuation()) { if (firstToken.pToken() != token::BEGIN_LIST) { @@ -125,32 +133,28 @@ void Foam::PtrList<T>::read(Istream& is, const INew& inewt) << exit(FatalIOError); } - sllPtrs.append(inewt(is).ptr()); + sllPtrs.append(inew(is).ptr()); is >> lastToken; } setSize(sllPtrs.size()); + // A list of pointers - can simply copy label i = 0; - for - ( - typename SLList<T*>::iterator iter = sllPtrs.begin(); - iter != sllPtrs.end(); - ++iter - ) + for (T* ptr : sllPtrs) { - set(i++, iter()); + set(i++, ptr); } + + return; } - else - { - FatalIOErrorInFunction - ( - is - ) << "incorrect first token, expected <int> or '(', found " - << firstToken.info() - << exit(FatalIOError); - } + + FatalIOErrorInFunction + ( + is + ) << "incorrect first token, expected <int> or '(', found " + << firstToken.info() + << exit(FatalIOError); } @@ -158,9 +162,9 @@ void Foam::PtrList<T>::read(Istream& is, const INew& inewt) template<class T> template<class INew> -Foam::PtrList<T>::PtrList(Istream& is, const INew& inewt) +Foam::PtrList<T>::PtrList(Istream& is, const INew& inew) { - read(is, inewt); + read(is, inew); } @@ -174,10 +178,10 @@ Foam::PtrList<T>::PtrList(Istream& is) // * * * * * * * * * * * * * * * Istream Operator * * * * * * * * * * * * * // template<class T> -Foam::Istream& Foam::operator>>(Istream& is, PtrList<T>& L) +Foam::Istream& Foam::operator>>(Istream& is, PtrList<T>& lst) { - L.clear(); - L.read(is, INew<T>()); + lst.clear(); + lst.read(is, INew<T>()); return is; } diff --git a/src/OpenFOAM/containers/Lists/UIndirectList/UIndirectListIO.C b/src/OpenFOAM/containers/Lists/UIndirectList/UIndirectListIO.C index b990952e2d44c9d9501b0f0e53553fea3a2f3031..df0e3d6d65b2dd816e2321c17e60f6f3df685559 100644 --- a/src/OpenFOAM/containers/Lists/UIndirectList/UIndirectListIO.C +++ b/src/OpenFOAM/containers/Lists/UIndirectList/UIndirectListIO.C @@ -39,16 +39,16 @@ Foam::Ostream& Foam::UIndirectList<T>::writeList { const UIndirectList<T>& L = *this; - const label sz = L.size(); + const label len = L.size(); // Write list contents depending on data format if (os.format() == IOstream::ASCII || !contiguous<T>()) { // Can the contents be considered 'uniform' (ie, identical)? - bool uniform = (sz > 1 && contiguous<T>()); + bool uniform = (len > 1 && contiguous<T>()); if (uniform) { - for (label i=1; i < sz; ++i) + for (label i=1; i < len; ++i) { if (L[i] != L[0]) { @@ -61,7 +61,7 @@ Foam::Ostream& Foam::UIndirectList<T>::writeList if (uniform) { // Size and start delimiter - os << sz << token::BEGIN_BLOCK; + os << len << token::BEGIN_BLOCK; // Contents os << L[0]; @@ -71,15 +71,15 @@ Foam::Ostream& Foam::UIndirectList<T>::writeList } else if ( - sz <= 1 || !shortListLen - || (sz <= shortListLen && contiguous<T>()) + len <= 1 || !shortListLen + || (len <= shortListLen && contiguous<T>()) ) { // Size and start delimiter - os << sz << token::BEGIN_LIST; + os << len << token::BEGIN_LIST; // Contents - for (label i=0; i < sz; ++i) + for (label i=0; i < len; ++i) { if (i) os << token::SPACE; os << L[i]; @@ -91,10 +91,10 @@ Foam::Ostream& Foam::UIndirectList<T>::writeList else { // Size and start delimiter - os << nl << sz << nl << token::BEGIN_LIST << nl; + os << nl << len << nl << token::BEGIN_LIST << nl; // Contents - for (label i=0; i < sz; ++i) + for (label i=0; i < len; ++i) { os << L[i] << nl; } @@ -106,16 +106,16 @@ Foam::Ostream& Foam::UIndirectList<T>::writeList else { // Contents are binary and contiguous - os << nl << sz << nl; + os << nl << len << nl; - if (sz) + if (len) { // The TOTAL number of bytes to be written. // - possibly add start delimiter - os.beginRaw(sz*sizeof(T)); + os.beginRaw(len*sizeof(T)); // Contents - for (label i=0; i < sz; ++i) + for (label i=0; i < len; ++i) { os.writeRaw ( diff --git a/src/OpenFOAM/containers/Lists/UList/UList.C b/src/OpenFOAM/containers/Lists/UList/UList.C index 24cf28a27f95da69f91009f65c3d9b0b49995c04..4df5c630e16838f76c3b5da942a8fccff46a28aa 100644 --- a/src/OpenFOAM/containers/Lists/UList/UList.C +++ b/src/OpenFOAM/containers/Lists/UList/UList.C @@ -63,9 +63,9 @@ Foam::labelRange Foam::UList<T>::validateRange auto iter = start_size.begin(); const label beg = *(iter++); - const label sz = *iter; + const label len = *iter; - return this->validateRange(labelRange(beg, sz)); + return this->validateRange(labelRange(beg, len)); } @@ -122,31 +122,32 @@ void Foam::UList<T>::swapLast(const label i) template<class T> -void Foam::UList<T>::deepCopy(const UList<T>& a) +void Foam::UList<T>::deepCopy(const UList<T>& list) { - if (a.size_ != this->size_) + const label len = this->size_; + + if (len != list.size_) { FatalErrorInFunction << "ULists have different sizes: " - << this->size_ << " " << a.size_ + << len << " " << list.size_ << abort(FatalError); } - - if (this->size_) + else if (len) { #ifdef USEMEMCPY if (contiguous<T>()) { - memcpy(this->v_, a.v_, this->byteSize()); + memcpy(this->v_, list.v_, this->byteSize()); } else #endif { - List_ACCESS(T, (*this), vp); - List_CONST_ACCESS(T, a, ap); - List_FOR_ALL((*this), i) + List_ACCESS(T, (*this), lhs); + List_CONST_ACCESS(T, list, rhs); + for (label i = 0; i < len; ++i) { - vp[i] = ap[i]; + lhs[i] = rhs[i]; } } } @@ -228,8 +229,7 @@ std::streamsize Foam::UList<T>::byteSize() const if (!contiguous<T>()) { FatalErrorInFunction - << "Cannot return the binary size of a list of " - "non-primitive elements" + << "Cannot return binary size of a list with non-primitive elements" << abort(FatalError); } @@ -317,20 +317,22 @@ void Foam::shuffle(UList<T>& a) // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // template<class T> -bool Foam::UList<T>::operator==(const UList<T>& a) const +bool Foam::UList<T>::operator==(const UList<T>& list) const { - bool equal = (this->size_ == a.size_); - if (!equal) + const label len = this->size_; + if (len != list.size_) { return false; } - List_CONST_ACCESS(T, (*this), vp); - List_CONST_ACCESS(T, (a), ap); + bool equal = false; - List_FOR_ALL((*this), i) + List_CONST_ACCESS(T, (*this), lhs); + List_CONST_ACCESS(T, (list), rhs); + + for (label i = 0; i < len; ++i) { - equal = (vp[i] == ap[i]); + equal = (lhs[i] == rhs[i]); if (!equal) break; } @@ -339,55 +341,55 @@ bool Foam::UList<T>::operator==(const UList<T>& a) const template<class T> -bool Foam::UList<T>::operator!=(const UList<T>& a) const +bool Foam::UList<T>::operator!=(const UList<T>& list) const { - return !operator==(a); + return !operator==(list); } template<class T> -bool Foam::UList<T>::operator<(const UList<T>& a) const +bool Foam::UList<T>::operator<(const UList<T>& list) const { for ( - const_iterator vi = begin(), ai = a.begin(); - vi < end() && ai < a.end(); - ++vi, ++ai + const_iterator lhs = begin(), rhs = list.begin(); + lhs < end() && rhs < list.end(); + ++lhs, ++rhs ) { - if (*vi < *ai) + if (*lhs < *rhs) { return true; } - else if (*vi > *ai) + else if (*rhs < *lhs) { return false; } } // Contents look to be identical, or lists have different sizes - return (this->size_ < a.size_); + return (this->size_ < list.size_); } template<class T> -bool Foam::UList<T>::operator>(const UList<T>& a) const +bool Foam::UList<T>::operator>(const UList<T>& list) const { - return a.operator<(*this); + return list.operator<(*this); } template<class T> -bool Foam::UList<T>::operator<=(const UList<T>& a) const +bool Foam::UList<T>::operator<=(const UList<T>& list) const { - return !operator>(a); + return !list.operator<(*this); } template<class T> -bool Foam::UList<T>::operator>=(const UList<T>& a) const +bool Foam::UList<T>::operator>=(const UList<T>& list) const { - return !operator<(a); + return !operator<(list); } diff --git a/src/OpenFOAM/containers/Lists/UList/UList.H b/src/OpenFOAM/containers/Lists/UList/UList.H index 76e5362d3a5894d4940c6910af98308d862ecc49..0902a4e2f646001b4ee2bb914cea715094e87b3d 100644 --- a/src/OpenFOAM/containers/Lists/UList/UList.H +++ b/src/OpenFOAM/containers/Lists/UList/UList.H @@ -51,6 +51,8 @@ SourceFiles #include "Swap.H" #include <initializer_list> +#include <iterator> +#include <type_traits> // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -61,8 +63,6 @@ namespace Foam class labelRange; template<class T> class List; template<class T> class SubList; - -// Forward declaration of friend functions and operators template<class T> class UList; template<class T> Ostream& operator<<(Ostream&, const UList<T>&); template<class T> Istream& operator>>(Istream&, UList<T>&); @@ -97,7 +97,7 @@ class UList // for the default assignment to be either. The solution is to // disallow default assignment and provide separate 'shallowCopy' and // 'deepCopy' member functions - void operator=(const UList<T>&) = delete; + UList<T>& operator=(const UList<T>&) = delete; protected: @@ -124,6 +124,42 @@ protected: public: + // STL type definitions + + //- The value type the list contains + typedef T value_type; + + //- The pointer type for non-const access to value_type items + typedef T* pointer; + + //- The pointer type for const access to value_type items + typedef const T* const_pointer; + + //- The type used for storing into value_type objects + typedef T& reference; + + //- The type used for reading from constant value_type objects. + typedef const T& const_reference; + + //- Random access iterator for traversing a UList + typedef T* iterator; + + //- Random access iterator for traversing a UList + typedef const T* const_iterator; + + //- The type to represent the size of a UList + typedef label size_type; + + //- The difference between iterator objects + typedef label difference_type; + + //- Reverse iterator (non-const access) + typedef std::reverse_iterator<iterator> reverse_iterator; + + //- Reverse iterator (const access) + typedef std::reverse_iterator<const_iterator> const_reverse_iterator; + + // Related types //- Declare friendship with the List class @@ -135,7 +171,7 @@ public: // Static Member Functions - //- Return a null UList + //- Return a UList reference to a nullObject inline static const UList<T>& null(); @@ -169,7 +205,7 @@ public: bool operator()(const label a, const label b) const { - return values[a] > values[b]; + return values[b] < values[a]; } }; @@ -288,10 +324,10 @@ public: // Copy //- Copy the pointer held by the given UList - inline void shallowCopy(const UList<T>& a); + inline void shallowCopy(const UList<T>& list); //- Copy elements of the given UList - void deepCopy(const UList<T>& a); + void deepCopy(const UList<T>& list); // Member operators @@ -337,29 +373,7 @@ public: void operator=(const zero); - // STL type definitions - - //- Type of values the UList contains - typedef T value_type; - - //- The type used for storing into value_type objects - typedef T& reference; - - //- The type used for reading from constant value_type objects. - typedef const T& const_reference; - - //- The type that can represent the difference between any two - //- UList iterator objects - typedef label difference_type; - - //- The type that can represent the size of a UList - typedef label size_type; - - - // STL iterator - - //- Random access iterator for traversing UList - typedef T* iterator; + // Random access iterator (non-const) //- Return an iterator to begin traversing the UList inline iterator begin(); @@ -368,10 +382,7 @@ public: inline iterator end(); - // STL const_iterator - - //- Random access iterator for traversing UList - typedef const T* const_iterator; + // Random access iterator (const) //- Return const_iterator to begin traversing the constant UList inline const_iterator cbegin() const; @@ -385,94 +396,8 @@ public: //- Return const_iterator to end traversing the constant UList inline const_iterator end() const; - // Reverse iterators - - //- Generic const/non-const reverse iterator - template<bool Const> - class reverse_iterator_base - { - public: - //- The const/non-const type for entries - typedef typename std::conditional - <Const, const T, T>::type value_type; - - //- A pointer to a const/non-const entry - typedef value_type* pointer; - - //- A reference to a const/non-const entry - typedef value_type& reference; - - - private: - - //- The element pointer - pointer ptr_; - - - public: - - //- Construct null or from list element pointer - inline reverse_iterator_base(pointer ptr = nullptr) - : - ptr_(ptr) - {} - - - //- Copy construct - inline reverse_iterator_base(const reverse_iterator_base& iter) - : - ptr_(iter.ptr_) - {} - - - //- Reverse increment - inline void operator++() - { - --ptr_; - } - - //- Reverse increment - inline reverse_iterator_base operator++(int) - { - reverse_iterator_base old(*this); - --ptr_; - return old; - } - - //- Reverse increase - inline void operator+=(int n) - { - ptr_ -= n; - } - - //- Dereference iterator - reference operator*() const - { - return *ptr_; - } - - //- Dereference iterator - pointer operator->() const - { - return ptr_; - } - - //- Equality - bool operator==(const reverse_iterator_base& iter) const - { - return ptr_ == iter.ptr_; - } - - //- inequality - bool operator!=(const reverse_iterator_base& iter) const - { - return ptr_ != iter.ptr_; - } - }; - - //- STL reverse_iterator - typedef reverse_iterator_base<false> reverse_iterator; + // Reverse iterators (non-const) //- Return reverse_iterator to begin reverse traversing the UList inline reverse_iterator rbegin(); @@ -481,8 +406,7 @@ public: inline reverse_iterator rend(); - //- STL const reverse iterator - typedef reverse_iterator_base<true> const_reverse_iterator; + // Reverse iterators (const) //- Return const_reverse_iterator to begin reverse traversing the UList inline const_reverse_iterator crbegin() const; @@ -523,7 +447,7 @@ public: bool operator!=(const UList<T>& a) const; //- Compare two ULists lexicographically. Takes linear time - bool operator<(const UList<T>& a) const; + bool operator<(const UList<T>& list) const; //- Compare two ULists lexicographically. Takes linear time bool operator>(const UList<T>& a) const; diff --git a/src/OpenFOAM/containers/Lists/UList/UListI.H b/src/OpenFOAM/containers/Lists/UList/UListI.H index bc40e5bda1a437089acb3c30f542995e5ba496d1..08597036020bfefbdc7970d17c307004faa1379b 100644 --- a/src/OpenFOAM/containers/Lists/UList/UListI.H +++ b/src/OpenFOAM/containers/Lists/UList/UListI.H @@ -188,10 +188,10 @@ inline bool Foam::UList<T>::found(const T& val, const label start) const template<class T> -inline void Foam::UList<T>::shallowCopy(const UList<T>& a) +inline void Foam::UList<T>::shallowCopy(const UList<T>& list) { - size_ = a.size_; - v_ = a.v_; + size_ = list.size_; + v_ = list.v_; } @@ -217,10 +217,8 @@ namespace Foam { return v_[i]; } - else - { - return Foam::pTraits<bool>::zero; - } + + return Foam::pTraits<bool>::zero; } } @@ -268,63 +266,63 @@ template<class T> inline typename Foam::UList<T>::iterator Foam::UList<T>::end() { - return &v_[size_]; + return (v_ + size_); } template<class T> inline typename Foam::UList<T>::const_iterator Foam::UList<T>::end() const { - return &v_[size_]; + return (v_ + size_); } template<class T> inline typename Foam::UList<T>::const_iterator Foam::UList<T>::cend() const { - return &v_[size_]; + return (v_ + size_); } template<class T> inline typename Foam::UList<T>::reverse_iterator Foam::UList<T>::rbegin() { - return reverse_iterator(&v_[size_-1]); + return reverse_iterator(end()); } template<class T> inline typename Foam::UList<T>::const_reverse_iterator Foam::UList<T>::rbegin() const { - return const_reverse_iterator(&v_[size_-1]); + return const_reverse_iterator(end()); } template<class T> inline typename Foam::UList<T>::const_reverse_iterator Foam::UList<T>::crbegin() const { - return const_reverse_iterator(&v_[size_-1]); + return const_reverse_iterator(end()); } template<class T> inline typename Foam::UList<T>::reverse_iterator Foam::UList<T>::rend() { - return reverse_iterator(&v_[-1]); + return reverse_iterator(begin()); } template<class T> inline typename Foam::UList<T>::const_reverse_iterator Foam::UList<T>::rend() const { - return const_reverse_iterator(&v_[-1]); + return const_reverse_iterator(begin()); } template<class T> inline typename Foam::UList<T>::const_reverse_iterator Foam::UList<T>::crend() const { - return const_reverse_iterator(&v_[-1]); + return const_reverse_iterator(begin()); } diff --git a/src/OpenFOAM/containers/Lists/UList/UListIO.C b/src/OpenFOAM/containers/Lists/UList/UListIO.C index e6f9a97ad543ec21e1fbcbae244969177cc08bc9..96bbc6b970afe8656a988b94577bddd23d25bf67 100644 --- a/src/OpenFOAM/containers/Lists/UList/UListIO.C +++ b/src/OpenFOAM/containers/Lists/UList/UListIO.C @@ -76,16 +76,16 @@ Foam::Ostream& Foam::UList<T>::writeList { const UList<T>& L = *this; - const label sz = L.size(); + const label len = L.size(); // Write list contents depending on data format if (os.format() == IOstream::ASCII || !contiguous<T>()) { // Can the contents be considered 'uniform' (ie, identical)? - bool uniform = (sz > 1 && contiguous<T>()); + bool uniform = (len > 1 && contiguous<T>()); if (uniform) { - for (label i=1; i < sz; ++i) + for (label i=1; i < len; ++i) { if (L[i] != L[0]) { @@ -98,7 +98,7 @@ Foam::Ostream& Foam::UList<T>::writeList if (uniform) { // Size and start delimiter - os << sz << token::BEGIN_BLOCK; + os << len << token::BEGIN_BLOCK; // Contents os << L[0]; @@ -108,15 +108,15 @@ Foam::Ostream& Foam::UList<T>::writeList } else if ( - sz <= 1 || !shortListLen - || (sz <= shortListLen && contiguous<T>()) + len <= 1 || !shortListLen + || (len <= shortListLen && contiguous<T>()) ) { // Size and start delimiter - os << sz << token::BEGIN_LIST; + os << len << token::BEGIN_LIST; // Contents - for (label i=0; i < sz; ++i) + for (label i=0; i < len; ++i) { if (i) os << token::SPACE; os << L[i]; @@ -128,10 +128,10 @@ Foam::Ostream& Foam::UList<T>::writeList else { // Size and start delimiter - os << nl << sz << nl << token::BEGIN_LIST << nl; + os << nl << len << nl << token::BEGIN_LIST << nl; // Contents - for (label i=0; i < sz; ++i) + for (label i=0; i < len; ++i) { os << L[i] << nl; } @@ -143,9 +143,9 @@ Foam::Ostream& Foam::UList<T>::writeList else { // Contents are binary and contiguous - os << nl << sz << nl; + os << nl << len << nl; - if (sz) + if (len) { // write(...) includes surrounding start/end delimiters os.write @@ -171,14 +171,18 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const UList<T>& lst) template<class T> -Foam::Istream& Foam::operator>>(Istream& is, UList<T>& L) +Foam::Istream& Foam::operator>>(Istream& is, UList<T>& lst) { + // The target list length - must match with sizes read + const label len = lst.size(); + is.fatalCheck(FUNCTION_NAME); token firstToken(is); is.fatalCheck("operator>>(Istream&, UList<T>&) : reading first token"); + // Compound: simply transfer contents if (firstToken.isCompound()) { List<T> elems; @@ -189,31 +193,38 @@ Foam::Istream& Foam::operator>>(Istream& is, UList<T>& L) firstToken.transferCompoundToken(is) ) ); - // Check list length - const label sz = elems.size(); - if (sz != L.size()) + const label inputLen = elems.size(); + + // List lengths must match + if (inputLen != len) { FatalIOErrorInFunction(is) - << "incorrect length for UList. Read " << sz - << " expected " << L.size() + << "incorrect length for UList. Read " + << inputLen << " expected " << len << exit(FatalIOError); } - for (label i=0; i<sz; ++i) + + for (label i = 0; i < len; ++i) { - L[i] = elems[i]; + lst[i] = std::move(elems[i]); } + + return is; } - else if (firstToken.isLabel()) + + + // Label: could be int(..), int{...} or just a plain '0' + if (firstToken.isLabel()) { - const label sz = firstToken.labelToken(); + const label inputLen = firstToken.labelToken(); - // Set list length to that read - if (sz != L.size()) + // List lengths must match + if (inputLen != len) { FatalIOErrorInFunction(is) - << "incorrect length for UList. Read " << sz - << " expected " << L.size() + << "incorrect length for UList. Read " + << inputLen << " expected " << len << exit(FatalIOError); } @@ -224,13 +235,13 @@ Foam::Istream& Foam::operator>>(Istream& is, UList<T>& L) // Read beginning of contents const char delimiter = is.readBeginList("List"); - if (sz) + if (len) { if (delimiter == token::BEGIN_LIST) { - for (label i=0; i<sz; ++i) + for (label i=0; i<len; ++i) { - is >> L[i]; + is >> lst[i]; is.fatalCheck ( @@ -251,9 +262,9 @@ Foam::Istream& Foam::operator>>(Istream& is, UList<T>& L) "reading the single entry" ); - for (label i=0; i<sz; ++i) + for (label i=0; i<len; ++i) { - L[i] = element; + lst[i] = element; // Copy the value } } } @@ -261,22 +272,24 @@ Foam::Istream& Foam::operator>>(Istream& is, UList<T>& L) // Read end of contents is.readEndList("List"); } - else + else if (len) { - // contents are binary and contiguous + // Non-empty, binary, contiguous - if (sz) - { - is.read(reinterpret_cast<char*>(L.data()), sz*sizeof(T)); + is.read(reinterpret_cast<char*>(lst.data()), len*sizeof(T)); - is.fatalCheck - ( - "operator>>(Istream&, UList<T>&) : reading the binary block" - ); - } + is.fatalCheck + ( + "operator>>(Istream&, UList<T>&) : reading the binary block" + ); } + + return is; } - else if (firstToken.isPunctuation()) + + + // "(...)" : read as SLList and transfer contents + if (firstToken.isPunctuation()) { if (firstToken.pToken() != token::BEGIN_LIST) { @@ -286,40 +299,33 @@ Foam::Istream& Foam::operator>>(Istream& is, UList<T>& L) << exit(FatalIOError); } - // Putback the opening bracket - is.putBack(firstToken); + is.putBack(firstToken); // Putback the opening bracket - // Now read as a singly-linked list - SLList<T> sll(is); + SLList<T> sll(is); // Read as singly-linked list - if (sll.size() != L.size()) + // List lengths must match + if (sll.size() != len) { FatalIOErrorInFunction(is) - << "incorrect length for UList. Read " << sll.size() - << " expected " << L.size() + << "incorrect length for UList. Read " + << sll.size() << " expected " << len << exit(FatalIOError); } - // Convert the singly-linked list to this list - label i = 0; - for - ( - typename SLList<T>::const_iterator iter = sll.begin(); - iter != sll.end(); - ++iter, ++i - ) + // Move assign each list element + for (label i = 0; i < len; ++i) { - L[i] = iter(); + lst[i] = std::move(sll.removeHead()); } + return is; } - else - { - FatalIOErrorInFunction(is) - << "incorrect first token, expected <int> or '(', found " - << firstToken.info() - << exit(FatalIOError); - } + + + FatalIOErrorInFunction(is) + << "incorrect first token, expected <int> or '(', found " + << firstToken.info() + << exit(FatalIOError); return is; } diff --git a/src/OpenFOAM/containers/Lists/UPtrList/UPtrList.C b/src/OpenFOAM/containers/Lists/UPtrList/UPtrList.C index 69737b210ea80312b94cd3b3b1837a80ef2ff01c..317cf817d7a4792c122e32750e717db41b7e1461 100644 --- a/src/OpenFOAM/containers/Lists/UPtrList/UPtrList.C +++ b/src/OpenFOAM/containers/Lists/UPtrList/UPtrList.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2015-2018 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -27,137 +27,66 @@ License // * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * // -template<class T> -Foam::UPtrList<T>::UPtrList() -: - ptrs_() -{} - - -template<class T> -Foam::UPtrList<T>::UPtrList(const label s) -: - ptrs_(s, reinterpret_cast<T*>(0)) -{} - - template<class T> Foam::UPtrList<T>::UPtrList(UList<T>& lst) : ptrs_(lst.size()) { - forAll(lst, i) - { - ptrs_[i] = &lst[i]; - } -} + const label len = lst.size(); - -template<class T> -Foam::UPtrList<T>::UPtrList(PtrList<T>& lst) -: - ptrs_(lst.size()) -{ - forAll(lst, i) + for (label i=0; i<len; ++i) { - ptrs_[i] = &lst[i]; + ptrs_[i] = &(lst[i]); } } -template<class T> -Foam::UPtrList<T>::UPtrList(const Xfer<UPtrList<T>>& lst) -{ - transfer(lst()); -} - - -template<class T> -Foam::UPtrList<T>::UPtrList(UPtrList<T>& a, bool reuse) -: - ptrs_(a.ptrs_, reuse) -{} - - // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // -template<class T> -void Foam::UPtrList<T>::setSize(const label newSize) -{ - label oldSize = size(); - - if (newSize <= 0) - { - clear(); - } - else if (newSize < oldSize) - { - ptrs_.setSize(newSize); - } - else if (newSize > oldSize) - { - ptrs_.setSize(newSize); - - // set new elements to nullptr - for (label i=oldSize; i<newSize; i++) - { - ptrs_[i] = nullptr; - } - } -} - - -template<class T> -void Foam::UPtrList<T>::clear() -{ - ptrs_.clear(); -} - - -template<class T> -void Foam::UPtrList<T>::transfer(UPtrList<T>& a) -{ - ptrs_.transfer(a.ptrs_); -} - - template<class T> void Foam::UPtrList<T>::reorder(const labelUList& oldToNew) { - if (oldToNew.size() != size()) + const label len = this->size(); + + if (oldToNew.size() != len) { FatalErrorInFunction << "Size of map (" << oldToNew.size() - << ") not equal to list size (" << size() - << ")." << abort(FatalError); + << ") not equal to list size (" << len + << ") for type " << typeid(T).name() << nl + << abort(FatalError); } - List<T*> newPtrs_(ptrs_.size(), reinterpret_cast<T*>(0)); + // New list of pointers + List<T*> ptrLst(len, reinterpret_cast<T*>(0)); - forAll(*this, i) + for (label i=0; i<len; ++i) { - label newI = oldToNew[i]; + const label idx = oldToNew[i]; - if (newI < 0 || newI >= size()) + if (idx < 0 || idx >= len) { FatalErrorInFunction - << "Illegal index " << newI << nl - << "Valid indices are 0.." << size()-1 + << "Illegal index " << idx << nl + << "Valid indices are 0.." << len-1 + << " for type " << typeid(T).name() << nl << abort(FatalError); } - if (newPtrs_[newI]) + if (ptrLst[idx]) { FatalErrorInFunction - << "reorder map is not unique; element " << newI - << " already set." << abort(FatalError); + << "reorder map is not unique; element " << idx + << " already set for type " << typeid(T).name() + << abort(FatalError); } - newPtrs_[newI] = ptrs_[i]; + ptrLst[idx] = ptrs_[i]; } - forAll(newPtrs_, i) + // Verify that all pointers were set + for (label i=0; i<len; ++i) { - if (!newPtrs_[i]) + if (!ptrLst[i]) { FatalErrorInFunction << "Element " << i << " not set after reordering." << nl @@ -165,12 +94,8 @@ void Foam::UPtrList<T>::reorder(const labelUList& oldToNew) } } - ptrs_.transfer(newPtrs_); + ptrs_.swap(ptrLst); } -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - -#include "UPtrListIO.C" - // ************************************************************************* // diff --git a/src/OpenFOAM/containers/Lists/UPtrList/UPtrList.H b/src/OpenFOAM/containers/Lists/UPtrList/UPtrList.H index 74e5d316ee562d0f69420532a8ce9f06ab4fc700..b23c6dbe1df25213312b36957ef861e025e891be 100644 --- a/src/OpenFOAM/containers/Lists/UPtrList/UPtrList.H +++ b/src/OpenFOAM/containers/Lists/UPtrList/UPtrList.H @@ -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) 2018 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -25,14 +25,15 @@ Class Foam::UPtrList Description - A templated 1D list of pointers to objects of type \<T\>, where the - size of the array is known and used for subscript bounds checking, etc. + A list of pointers to objects of type \<T\>, without allocation/deallocation + management of the pointers - this is to be done elsewhere. + The operator[] returns a reference to the object, not the pointer. - The element operator [] returns a reference to the object rather than a - pointer. Storage is not allocated during construction or use but is - supplied to the constructor as an argument. +See Also + Foam::PtrList SourceFiles + UPtrListI.H UPtrList.C UPtrListIO.C @@ -43,336 +44,270 @@ SourceFiles #include "List.H" #include "PtrList.H" +#include <iterator> // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // namespace Foam { -// Forward declaration of friend classes -template<class T> class PtrList; +// Forward declarations -// Forward declaration of friend functions and operators +template<class T> class PtrList; template<class T> class UPtrList; -template<class T> -inline typename UPtrList<T>::iterator operator+ -( - const typename UPtrList<T>::iterator&, - label -); - -template<class T> -inline typename UPtrList<T>::iterator operator+ -( - label, - const typename UPtrList<T>::iterator& -); - -template<class T> -inline typename UPtrList<T>::iterator operator- -( - const typename UPtrList<T>::iterator&, - label -); - -template<class T> -inline label operator- -( - const typename UPtrList<T>::iterator&, - const typename UPtrList<T>::iterator& -); - -template<class T> -inline typename UPtrList<T>::const_iterator operator+ -( - const typename UPtrList<T>::const_iterator&, - label -); - -template<class T> -inline typename UPtrList<T>::const_iterator operator+ -( - label, - const typename UPtrList<T>::const_iterator& -); - -template<class T> -inline typename UPtrList<T>::const_iterator operator- -( - const typename UPtrList<T>::const_iterator&, - label -); - -template<class T> -inline label operator- -( - const typename UPtrList<T>::const_iterator&, - const typename UPtrList<T>::const_iterator& -); - -template<class T> -Istream& operator>>(Istream&, UPtrList<T>&); - -template<class T> -Ostream& operator<<(Ostream&, const UPtrList<T>&); +template<class T> Ostream& operator<<(Ostream& os, const UPtrList<T>& lst); /*---------------------------------------------------------------------------*\ - Class UPtrList Declaration + Class UPtrList Declaration \*---------------------------------------------------------------------------*/ template<class T> class UPtrList { - // Private data +protected: + + // Protected data List<T*> ptrs_; public: - // Related types + // STL type definitions + + //- Type of values the list contains + typedef T value_type; + + //- A non-const reference to the value_type + typedef T& reference; + + //- A const reference to the value_type + typedef const T& const_reference; + + //- Random-access iterator with non-const access + class iterator; - //- Declare friendship with the UPtrList class - friend class PtrList<T>; + //- Random-access iterator with const access + class const_iterator; // Constructors - //- Null Constructor - UPtrList(); + //- Construct null + UPtrList() = default; - //- Construct with size specified - explicit UPtrList(const label); + //- Construct with specified size, each element initialized to nullptr + explicit inline UPtrList(const label nElem); - //- Construct from UList - explicit UPtrList(UList<T>&); + //- Construct from PtrList, copying addresses of each list element. + // The argument is non-const access since this UPtrList can be used + // to change its values. + explicit inline UPtrList(PtrList<T>& lst); - //- Construct from PtrList - explicit UPtrList(PtrList<T>&); + //- Construct from UList, taking addresses of each list element + explicit UPtrList(UList<T>& lst); //- Construct by transferring the parameter contents - UPtrList(const Xfer<UPtrList<T>>&); + inline UPtrList(const Xfer<UPtrList<T>>& lst); //- Construct as copy or re-use as specified - UPtrList(UPtrList<T>&, bool reuse); + inline UPtrList(UPtrList<T>& lst, bool reuse); + + //- Copy construct + inline UPtrList(const UPtrList<T>& lst) = default; + + //- Move construct + inline UPtrList(UPtrList<T>&& lst); // Member functions - // Access + // Access + + //- Return the number of elements in the UPtrList + inline label size() const; - //- Return the number of elements in the UPtrList - inline label size() const; + //- Return true if the UPtrList is empty (ie, size() is zero) + inline bool empty() const; - //- Return true if the UPtrList is empty (ie, size() is zero) - inline bool empty() const; + //- Return reference to the first element of the list + inline T& first(); - //- Swap content with another UPtrList - inline void swap(UPtrList<T>& lst); + //- Return reference to first element of the list + inline const T& first() const; - //- Return reference to the first element of the list - inline T& first(); + //- Return reference to the last element of the list + inline T& last(); - //- Return reference to first element of the list - inline const T& first() const; + //- Return reference to the last element of the list + inline const T& last() const; - //- Return reference to the last element of the list - inline T& last(); - //- Return reference to the last element of the list - inline const T& last() const; + // Edit + //- Set list size to zero. + inline void clear(); - // Edit + //- Reset size of UPtrList. + // New entries are initialized to nullptr. + inline void setSize(const label newLen); - //- Reset size of UPtrList. This can only be used to set the size - // of an empty UPtrList, extend a UPtrList, remove entries from - // the end of a UPtrList - void setSize(const label); + //- Reset size of UPtrList. + // New entries are initialized to nullptr. + inline void resize(const label newLen); - //- Reset size of UPtrList. This can only be used to set the size - // of an empty UPtrList, extend a UPtrList, remove entries from - // the end of a UPtrList - inline void resize(const label); + //- Append an element to the end of the list + inline void append(T* ptr); - //- Clear the UPtrList, i.e. set size to zero - void clear(); + //- Swap content + inline void swap(UPtrList<T>& lst); - //- Transfer the contents of the argument UPtrList into this - // UPtrList and annul the argument list - void transfer(UPtrList<T>&); + //- Transfer contents into this list and annul the argument + inline void transfer(UPtrList<T>& lst); - //- Transfer contents to the Xfer container - inline Xfer<UPtrList<T>> xfer(); + //- Transfer contents to the Xfer container + inline Xfer<UPtrList<T>> xfer(); - //- Is element set - inline bool set(const label) const; + //- Return true if element is set (ie, not a nullptr) + inline bool set(const label i) const; - //- Set element. Return old element (can be nullptr). - // No checks on new element - inline T* set(const label, T*); + //- Set element to given pointer and return old element (can be null). + // No checks on new element + inline T* set(const label i, T* ptr); - //- Reorders elements. Ordering does not have to be done in - // ascending or descending order. Reordering has to be unique. - // (is shuffle) - void reorder(const labelUList&); + //- Reorder elements. Reordering must be unique (ie, shuffle). + void reorder(const labelUList& oldToNew); // Member operators //- Return element const reference - inline const T& operator[](const label) const; + inline const T& operator[](const label i) const; //- Return element reference - inline T& operator[](const label); + inline T& operator[](const label i); //- Return element const pointer - inline const T* operator()(const label) const; + inline const T* operator()(const label i) const; + //- Copy assignment + UPtrList<T>& operator=(const UPtrList<T>& lst) = default; - // STL type definitions + //- Move assignment + inline void operator=(UPtrList<T>&& lst); - //- Type of values the UPtrList contains - typedef T value_type; - //- Type that can be used for storing into UPtrList::value_type objects - typedef T& reference; + // Iterators - //- Type that can be used for storing into constant UPtrList::value_type - // objects - typedef const T& const_reference; - - - // STL iterator - // Random access iterator for traversing UPtrList - - class iterator; - friend class iterator; - - //- An STL iterator + //- Random-access iterator with non-const access class iterator { T** ptr_; public: - //- Construct for a given UPtrList entry - inline iterator(T**); - - // Member operators - - inline bool operator==(const iterator&) const; - inline bool operator!=(const iterator&) const; + using iterator_category = std::random_access_iterator_tag; + using value_type = T; + using difference_type = label; + using pointer = T*; + using reference = T&; + friend class const_iterator; - inline T& operator*(); - inline T& operator()(); + //- Construct for a given entry + inline iterator(T** ptr); - inline iterator operator++(); - inline iterator operator++(int); + // Member operators - inline iterator operator--(); - inline iterator operator--(int); + inline bool operator==(const iterator& iter) const; + inline bool operator!=(const iterator& iter) const; - inline iterator operator+=(label); + inline reference operator*() const; + inline reference operator()() const; - friend iterator operator+ <T>(const iterator&, label); - friend iterator operator+ <T>(label, const iterator&); + // Forward iteration + inline iterator& operator++(); + inline iterator operator++(int); - inline iterator operator-=(label); + inline iterator& operator--(); + inline iterator operator--(int); - friend iterator operator- <T>(const iterator&, label); + // Random-access + inline iterator& operator+=(difference_type n); + inline iterator& operator-=(difference_type n); + inline iterator operator+(difference_type n) const; + inline iterator operator-(difference_type n) const; - friend label operator- <T> - ( - const iterator&, - const iterator& - ); + inline difference_type operator-(const iterator& iter) const; - inline T& operator[](label); + inline reference operator[](difference_type n) const; - inline bool operator<(const iterator&) const; - inline bool operator>(const iterator&) const; + inline bool operator<(const iterator& iter) const; + inline bool operator>(const iterator& iter) const; - inline bool operator<=(const iterator&) const; - inline bool operator>=(const iterator&) const; + inline bool operator<=(const iterator& iter) const; + inline bool operator>=(const iterator& iter) const; }; - //- Return an iterator to begin traversing the UPtrList - inline iterator begin(); - - //- Return an iterator to end traversing the UPtrList - inline iterator end(); - - // STL const_iterator - // Random access iterator for traversing UPtrList - - //- An STL-conforming const_iterator + //- Random-access iterator with const access class const_iterator { const T* const* ptr_; public: - //- Construct for a given UPtrList entry - inline const_iterator(const T* const*); + using iterator_category = std::random_access_iterator_tag; + using value_type = const T; + using difference_type = label; + using pointer = const T*; + using reference = const T&; - //- Construct from an iterator - inline const_iterator(const iterator&); + //- Construct for a given entry + inline const_iterator(const T* const* ptr); + //- Copy construct from non-const iterator + inline const_iterator(const iterator& iter); // Member operators - inline bool operator==(const const_iterator&) const; - inline bool operator!=(const const_iterator&) const; + inline bool operator==(const const_iterator& iter) const; + inline bool operator!=(const const_iterator& iter) const; - typedef const T& Tref; - inline Tref operator*(); - inline Tref operator()(); + inline reference operator*() const; + inline reference operator()() const; - inline const_iterator operator++(); - inline const_iterator operator++(int); + // Forward iteration + inline const_iterator& operator++(); + inline const_iterator operator++(int); - inline const_iterator operator--(); - inline const_iterator operator--(int); + inline const_iterator& operator--(); + inline const_iterator operator--(int); - inline const_iterator operator+=(label); + // Random-access + inline const_iterator& operator+=(difference_type n); + inline const_iterator& operator-=(difference_type n); + inline const_iterator operator+(difference_type n) const; + inline const_iterator operator-(difference_type n) const; - friend const_iterator operator+ <T> - ( - const const_iterator&, - label - ); - friend const_iterator operator+ <T> - ( - label, - const const_iterator& - ); + inline difference_type operator-(const const_iterator& iter) const; - inline const_iterator operator-=(label); + inline reference operator[](difference_type n) const; - friend const_iterator operator- <T> - ( - const const_iterator&, - label - ); + inline bool operator<(const const_iterator& iter) const; + inline bool operator>(const const_iterator& iter) const; - friend label operator- <T> - ( - const const_iterator&, - const const_iterator& - ); + inline bool operator<=(const const_iterator& iter) const; + inline bool operator>=(const const_iterator& iter) const; + }; - inline const T& operator[](label); - inline bool operator<(const const_iterator&) const; - inline bool operator>(const const_iterator&) const; + //- Return an iterator to begin traversing the UPtrList + inline iterator begin(); - inline bool operator<=(const const_iterator&) const; - inline bool operator>=(const const_iterator&) const; - }; + //- Return an iterator to end traversing the UPtrList + inline iterator end(); //- Return an const_iterator to begin traversing the UPtrList inline const_iterator cbegin() const; @@ -390,7 +325,7 @@ public: // IOstream operator //- Write UPtrList to Ostream - friend Ostream& operator<< <T>(Ostream&, const UPtrList<T>&); + friend Ostream& operator<< <T>(Ostream& os, const UPtrList<T>& lst); }; @@ -406,6 +341,7 @@ public: #ifdef NoRepository #include "UPtrList.C" + #include "UPtrListIO.C" #endif // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/OpenFOAM/containers/Lists/UPtrList/UPtrListI.H b/src/OpenFOAM/containers/Lists/UPtrList/UPtrListI.H index 29f7592aa0a849baa657358b7f23cd989e6ba846..8ad86a360468965604e351c42c382c5c47e90fd9 100644 --- a/src/OpenFOAM/containers/Lists/UPtrList/UPtrListI.H +++ b/src/OpenFOAM/containers/Lists/UPtrList/UPtrListI.H @@ -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) 2018 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -23,6 +23,43 @@ License \*---------------------------------------------------------------------------*/ +// * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * // + +template<class T> +inline Foam::UPtrList<T>::UPtrList(const label nElem) +: + ptrs_(nElem, reinterpret_cast<T*>(0)) +{} + + +template<class T> +inline Foam::UPtrList<T>::UPtrList(PtrList<T>& lst) +: + ptrs_(lst.ptrs_) +{} + + +template<class T> +inline Foam::UPtrList<T>::UPtrList(UPtrList<T>&& lst) +: + ptrs_(std::move(lst.ptrs_)) +{} + + +template<class T> +inline Foam::UPtrList<T>::UPtrList(const Xfer<UPtrList<T>>& lst) +{ + transfer(lst()); +} + + +template<class T> +inline Foam::UPtrList<T>::UPtrList(UPtrList<T>& lst, bool reuse) +: + ptrs_(lst.ptrs_, reuse) +{} + + // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // template<class T> @@ -39,6 +76,13 @@ inline bool Foam::UPtrList<T>::empty() const } +template<class T> +inline void Foam::UPtrList<T>::clear() +{ + ptrs_.clear(); +} + + template<class T> inline void Foam::UPtrList<T>::swap(UPtrList<T>& lst) { @@ -46,6 +90,13 @@ inline void Foam::UPtrList<T>::swap(UPtrList<T>& lst) } +template<class T> +inline void Foam::UPtrList<T>::transfer(UPtrList<T>& lst) +{ + ptrs_.transfer(lst.ptrs_); +} + + template<class T> inline T& Foam::UPtrList<T>::first() { @@ -75,9 +126,36 @@ inline const T& Foam::UPtrList<T>::last() const template<class T> -inline void Foam::UPtrList<T>::resize(const label newSize) +inline void Foam::UPtrList<T>::setSize(const label newLen) { - this->setSize(newSize); + if (newLen <= 0) + { + clear(); + return; + } + + const label oldLen = this->size(); + if (newLen != oldLen) + { + // Truncate or extend (new elements initialized to nullptr) + ptrs_.setSize(newLen, reinterpret_cast<T*>(0)); + } +} + + +template<class T> +inline void Foam::UPtrList<T>::resize(const label nElem) +{ + this->setSize(nElem); +} + + +template<class T> +inline void Foam::UPtrList<T>::append(T* ptr) +{ + const label idx = this->size(); + ptrs_.setSize(idx + 1); + ptrs_[idx] = ptr; } @@ -153,158 +231,149 @@ inline Foam::UPtrList<T>::iterator::iterator(T** ptr) ptr_(ptr) {} + template<class T> inline bool Foam::UPtrList<T>::iterator::operator==(const iterator& iter) const { return ptr_ == iter.ptr_; } + template<class T> inline bool Foam::UPtrList<T>::iterator::operator!=(const iterator& iter) const { return ptr_ != iter.ptr_; } + template<class T> -inline T& Foam::UPtrList<T>::iterator::operator*() +inline T& Foam::UPtrList<T>::iterator::operator*() const { return **ptr_; } + template<class T> -inline T& Foam::UPtrList<T>::iterator::operator()() +inline T& Foam::UPtrList<T>::iterator::operator()() const { - return operator*(); + return **ptr_; } + template<class T> -inline typename Foam::UPtrList<T>::iterator +inline typename Foam::UPtrList<T>::iterator& Foam::UPtrList<T>::iterator::operator++() { ++ptr_; return *this; } + template<class T> inline typename Foam::UPtrList<T>::iterator Foam::UPtrList<T>::iterator::operator++(int) { - iterator tmp = *this; + iterator iter(*this); ++ptr_; - return tmp; + return iter; } + template<class T> -inline typename Foam::UPtrList<T>::iterator +inline typename Foam::UPtrList<T>::iterator& Foam::UPtrList<T>::iterator::operator--() { --ptr_; return *this; } + template<class T> inline typename Foam::UPtrList<T>::iterator Foam::UPtrList<T>::iterator::operator--(int) { - iterator tmp = *this; + iterator iter(*this); --ptr_; - return tmp; + return iter; } + template<class T> -inline typename Foam::UPtrList<T>::iterator +inline typename Foam::UPtrList<T>::iterator& Foam::UPtrList<T>::iterator::operator+=(label n) { ptr_ += n; return *this; } -template<class T> -inline typename Foam::UPtrList<T>::iterator -Foam::operator+(const typename UPtrList<T>::iterator& iter, label n) -{ - typename UPtrList<T>::iterator tmp = iter; - return tmp += n; -} template<class T> -inline typename Foam::UPtrList<T>::iterator -Foam::operator+(label n, const typename UPtrList<T>::iterator& iter) +inline typename Foam::UPtrList<T>::iterator& +Foam::UPtrList<T>::iterator::operator-=(label n) { - typename UPtrList<T>::iterator tmp = iter; - return tmp += n; + ptr_ -= n; + return *this; } + template<class T> inline typename Foam::UPtrList<T>::iterator -Foam::UPtrList<T>::iterator::operator-=(label n) +Foam::UPtrList<T>::iterator::operator+(label n) const { - ptr_ -= n; - return *this; + return iterator(ptr_ + n); } + template<class T> inline typename Foam::UPtrList<T>::iterator -Foam::operator-(const typename UPtrList<T>::iterator& iter, label n) +Foam::UPtrList<T>::iterator::operator-(label n) const { - typename UPtrList<T>::iterator tmp = iter; - return tmp -= n; + return iterator(ptr_ - n); } + template<class T> -inline Foam::label Foam::operator- -( - const typename UPtrList<T>::iterator& iter1, - const typename UPtrList<T>::iterator& iter2 -) +inline Foam::label +Foam::UPtrList<T>::iterator::operator-(const iterator& iter) const { - return (iter1.ptr_ - iter2.ptr_)/sizeof(T*); + return (ptr_ - iter.ptr_); } + template<class T> -inline T& Foam::UPtrList<T>::iterator::operator[](label n) +inline T& Foam::UPtrList<T>::iterator::operator[](label n) const { return *(*this + n); } + template<class T> inline bool Foam::UPtrList<T>::iterator::operator<(const iterator& iter) const { return ptr_ < iter.ptr_; } + template<class T> inline bool Foam::UPtrList<T>::iterator::operator>(const iterator& iter) const { return ptr_ > iter.ptr_; } + template<class T> inline bool Foam::UPtrList<T>::iterator::operator<=(const iterator& iter) const { return ptr_ <= iter.ptr_; } + template<class T> inline bool Foam::UPtrList<T>::iterator::operator>=(const iterator& iter) const { return ptr_ >= iter.ptr_; } -template<class T> -inline typename Foam::UPtrList<T>::iterator -Foam::UPtrList<T>::begin() -{ - return ptrs_.begin(); -} - -template<class T> -inline typename Foam::UPtrList<T>::iterator -Foam::UPtrList<T>::end() -{ - return ptrs_.end(); -} - // * * * * * * * * * * * * * * * STL const_iterator * * * * * * * * * * * * // @@ -343,21 +412,21 @@ inline bool Foam::UPtrList<T>::const_iterator::operator!= template<class T> -inline const T& Foam::UPtrList<T>::const_iterator::operator*() +inline const T& Foam::UPtrList<T>::const_iterator::operator*() const { return **ptr_; } template<class T> -inline const T& Foam::UPtrList<T>::const_iterator::operator()() +inline const T& Foam::UPtrList<T>::const_iterator::operator()() const { - return operator*(); + return **ptr_; } template<class T> -inline typename Foam::UPtrList<T>::const_iterator +inline typename Foam::UPtrList<T>::const_iterator& Foam::UPtrList<T>::const_iterator::operator++() { ++ptr_; @@ -369,14 +438,14 @@ template<class T> inline typename Foam::UPtrList<T>::const_iterator Foam::UPtrList<T>::const_iterator::operator++(int) { - const_iterator tmp = *this; + const_iterator iter(*this); ++ptr_; - return tmp; + return iter; } template<class T> -inline typename Foam::UPtrList<T>::const_iterator +inline typename Foam::UPtrList<T>::const_iterator& Foam::UPtrList<T>::const_iterator::operator--() { --ptr_; @@ -388,14 +457,14 @@ template<class T> inline typename Foam::UPtrList<T>::const_iterator Foam::UPtrList<T>::const_iterator::operator--(int) { - const_iterator tmp = *this; + const_iterator iter(*this); --ptr_; - return tmp; + return iter; } template<class T> -inline typename Foam::UPtrList<T>::const_iterator +inline typename Foam::UPtrList<T>::const_iterator& Foam::UPtrList<T>::const_iterator::operator+=(label n) { ptr_ += n; @@ -404,54 +473,41 @@ Foam::UPtrList<T>::const_iterator::operator+=(label n) template<class T> -inline typename Foam::UPtrList<T>::const_iterator -Foam::operator+(const typename UPtrList<T>::const_iterator& iter, label n) -{ - typename UPtrList<T>::const_iterator tmp = iter; - return tmp += n; -} - - -template<class T> -inline typename Foam::UPtrList<T>::const_iterator -Foam::operator+(label n, const typename UPtrList<T>::const_iterator& iter) +inline typename Foam::UPtrList<T>::const_iterator& +Foam::UPtrList<T>::const_iterator::operator-=(label n) { - typename UPtrList<T>::const_iterator tmp = iter; - return tmp += n; + ptr_ -= n; + return *this; } template<class T> inline typename Foam::UPtrList<T>::const_iterator -Foam::UPtrList<T>::const_iterator::operator-=(label n) +Foam::UPtrList<T>::const_iterator::operator+(label n) const { - ptr_ -= n; - return *this; + return const_iterator(ptr_ + n); } template<class T> inline typename Foam::UPtrList<T>::const_iterator -Foam::operator-(const typename UPtrList<T>::const_iterator& iter, label n) +Foam::UPtrList<T>::const_iterator::operator-(label n) const { - typename UPtrList<T>::const_iterator tmp = iter; - return tmp -= n; + return const_iterator(ptr_ - n); } template<class T> -inline Foam::label Foam::operator- -( - const typename UPtrList<T>::const_iterator& iter1, - const typename UPtrList<T>::const_iterator& iter2 -) +inline Foam::label +Foam::UPtrList<T>::const_iterator::operator-(const const_iterator& iter) const { - return (iter1.ptr_ - iter2.ptr_)/sizeof(T*); + return (ptr_ - iter.ptr_); } template<class T> -inline const T& Foam::UPtrList<T>::const_iterator::operator[](label n) +inline const T& +Foam::UPtrList<T>::const_iterator::operator[](label n) const { return *(*this + n); } @@ -497,17 +553,19 @@ inline bool Foam::UPtrList<T>::const_iterator::operator>= } +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + template<class T> -inline typename Foam::UPtrList<T>::const_iterator -Foam::UPtrList<T>::begin() const +inline typename Foam::UPtrList<T>::iterator +Foam::UPtrList<T>::begin() { return ptrs_.begin(); } template<class T> -inline typename Foam::UPtrList<T>::const_iterator -Foam::UPtrList<T>::end() const +inline typename Foam::UPtrList<T>::iterator +Foam::UPtrList<T>::end() { return ptrs_.end(); } @@ -517,16 +575,42 @@ template<class T> inline typename Foam::UPtrList<T>::const_iterator Foam::UPtrList<T>::cbegin() const { - return ptrs_.begin(); + return ptrs_.cbegin(); } template<class T> inline typename Foam::UPtrList<T>::const_iterator Foam::UPtrList<T>::cend() const +{ + return ptrs_.cend(); +} + + +template<class T> +inline typename Foam::UPtrList<T>::const_iterator +Foam::UPtrList<T>::begin() const +{ + return ptrs_.begin(); +} + + +template<class T> +inline typename Foam::UPtrList<T>::const_iterator +Foam::UPtrList<T>::end() const { return ptrs_.end(); } +// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // + +template<class T> +inline void Foam::UPtrList<T>::operator=(UPtrList<T>&& lst) +{ + this->clear(); + this->swap(lst); +} + + // ************************************************************************* // diff --git a/src/OpenFOAM/containers/Lists/UPtrList/UPtrListIO.C b/src/OpenFOAM/containers/Lists/UPtrList/UPtrListIO.C index 33ff3d9878ec65fe9bb7ea869b2d0298c8761157..bbc9f60282e74d5f817c200f9c4590ff42e6d71e 100644 --- a/src/OpenFOAM/containers/Lists/UPtrList/UPtrListIO.C +++ b/src/OpenFOAM/containers/Lists/UPtrList/UPtrListIO.C @@ -29,18 +29,18 @@ License // * * * * * * * * * * * * * * * Ostream Operators * * * * * * * * * * * * * // template<class T> -Foam::Ostream& Foam::operator<<(Ostream& os, const UPtrList<T>& L) +Foam::Ostream& Foam::operator<<(Ostream& os, const UPtrList<T>& lst) { - const label sz = L.size(); + const label len = lst.size(); // Size and start delimiter - os << nl << indent << sz << nl + os << nl << indent << len << nl << indent << token::BEGIN_LIST << incrIndent << nl; // Contents - for (label i=0; i < sz; ++i) + for (label i=0; i < len; ++i) { - os << L[i] << nl; + os << lst[i] << nl; } // End delimiter diff --git a/src/OpenFOAM/dimensionedTypes/dimensionedType/dimensionedType.C b/src/OpenFOAM/dimensionedTypes/dimensionedType/dimensionedType.C index d281845b9220af52708310f96d1b1af5dba0690b..014c1e543fa81a9bcdf22910abb84ee508648b07 100644 --- a/src/OpenFOAM/dimensionedTypes/dimensionedType/dimensionedType.C +++ b/src/OpenFOAM/dimensionedTypes/dimensionedType/dimensionedType.C @@ -624,24 +624,24 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const dimensioned<Type>& dt) // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * // template<class Type> -bool Foam::operator> +bool Foam::operator< ( const dimensioned<Type>& dt1, const dimensioned<Type>& dt2 ) { - return dt1.value() > dt2.value(); + return dt1.value() < dt2.value(); } template<class Type> -bool Foam::operator< +bool Foam::operator> ( const dimensioned<Type>& dt1, const dimensioned<Type>& dt2 ) { - return dt1.value() < dt2.value(); + return dt2.value() < dt1.value(); } diff --git a/src/OpenFOAM/dimensionedTypes/dimensionedType/dimensionedType.H b/src/OpenFOAM/dimensionedTypes/dimensionedType/dimensionedType.H index fde67a1fc1eb626f6f66670a662f5ef71699f750..888426003a3ca6113c483f61548de2878c387c8a 100644 --- a/src/OpenFOAM/dimensionedTypes/dimensionedType/dimensionedType.H +++ b/src/OpenFOAM/dimensionedTypes/dimensionedType/dimensionedType.H @@ -274,10 +274,10 @@ template<class Type> dimensioned<Type> min(const dimensioned<Type>&, const dimensioned<Type>&); template<class Type> -bool operator>(const dimensioned<Type>&, const dimensioned<Type>&); +bool operator<(const dimensioned<Type>&, const dimensioned<Type>&); template<class Type> -bool operator<(const dimensioned<Type>&, const dimensioned<Type>&); +bool operator>(const dimensioned<Type>&, const dimensioned<Type>&); template<class Type> dimensioned<Type> operator+(const dimensioned<Type>&, const dimensioned<Type>&); diff --git a/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricBoundaryField.C b/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricBoundaryField.C index c37fcf3fda48345a134cbdf1165368497e58c889..e949298eaa5ac688a9731586007c230ef4e827aa 100644 --- a/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricBoundaryField.C +++ b/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricBoundaryField.C @@ -89,8 +89,8 @@ readField { for ( - IDLList<entry>::const_reverse_iterator iter = dict.rbegin(); - iter != dict.rend(); + IDLList<entry>::const_reverse_iterator iter = dict.crbegin(); + iter != dict.crend(); ++iter ) { diff --git a/src/OpenFOAM/global/fileOperations/masterUncollatedFileOperation/masterUncollatedFileOperation.C b/src/OpenFOAM/global/fileOperations/masterUncollatedFileOperation/masterUncollatedFileOperation.C index 44604a64f5016d359c4f59a35526226bd222f940..298f6ae7ff8a3531f8b0885db3d5da5b8942902c 100644 --- a/src/OpenFOAM/global/fileOperations/masterUncollatedFileOperation/masterUncollatedFileOperation.C +++ b/src/OpenFOAM/global/fileOperations/masterUncollatedFileOperation/masterUncollatedFileOperation.C @@ -1458,7 +1458,7 @@ Foam::instantList Foam::fileOperations::masterUncollatedFileOperation::findTimes } Pstream::scatter(times); - instantList* tPtr = new instantList(times.xfer()); + instantList* tPtr = new instantList(std::move(times)); times_.insert(directory, tPtr); diff --git a/src/OpenFOAM/memory/Xfer/Xfer.H b/src/OpenFOAM/memory/Xfer/Xfer.H index 33dfec74633a3af3d24da0a1781503cc6f69cb23..1a68572f61c5928ef2551384d8c0a66d3027ecd2 100644 --- a/src/OpenFOAM/memory/Xfer/Xfer.H +++ b/src/OpenFOAM/memory/Xfer/Xfer.H @@ -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) 2018 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -38,29 +38,18 @@ Description When transferring between dissimilar types, the xferCopyTo() and xferMoveTo() functions can prove useful. An example is transferring - from a DynamicList to a List. Since the - List\<T\>::transfer(List\<T\>&) method could result in some allocated - memory becoming inaccessible, the xferMoveTo() function should be used to - invoke the correct List\<T\>::transfer(DynamicList\<T\>&) method. + from a DynamicList to a List. \code - DynamicList<label> dynLst; + List<label> list1; + DynamicList<label> list2; ... - labelList plainLst( xferMoveTo<labelList>(dynLst) ); - \endcode - - Of course, since this example is a very common operation, the - DynamicList::xfer() method transfers to a plain List anyhow. - It would thus be simpler (and clearer) just to use the following code: - \code - DynamicList<label> dynLst; - ... - labelList plainLst(dynLst.xfer()); + SomeClass obj(xferCopy(list1), xferMoveTo<labelList>(list1)); \endcode See also - xferCopy, xferCopyTo, xferMove, xferMoveTo, xferTmp + xferCopy, xferCopyTo, xferMove, xferMoveTo SourceFiles XferI.H @@ -75,9 +64,6 @@ SourceFiles namespace Foam { -// Forward declaration of classes -template<class T> class tmp; - /*---------------------------------------------------------------------------*\ Class Xfer Declaration \*---------------------------------------------------------------------------*/ @@ -95,81 +81,80 @@ public: typedef T Type; - // Constructors //- Store object pointer and manage its deletion // Can also be used later to transfer by assignment inline explicit Xfer(T* p = nullptr); - //- Construct by copying or by transferring the parameter contents - inline explicit Xfer(T& t, bool allowTransfer=false); - - //- Construct by copying the parameter contents - inline explicit Xfer(const T& t); - - //- Construct by transferring the contents - inline Xfer(const Xfer<T>& t); + //- Construct, transferring its contents + inline Xfer(const Xfer<T>& xf); //- Destructor inline ~Xfer(); - // Member Functions + // Static Member Functions //- Return a null object reference inline static const Xfer<T>& null(); + //- Construct new Xfer container with forwarding arguments to T + template<class... Args> + inline static Xfer<T> New(Args&&... args); + + + // Member Functions + + //- Pointer to the underlying object + inline T* get() const; + + //- Swaps the managed objects + void swap(Xfer<T>& other) noexcept; + // Member Operators - //- Transfer the contents into the object - inline void operator=(T& t); + //- Transfer contents into the object + inline void operator=(const Xfer<T>& xf); + + //- Reference to the underlying object + inline T& operator*() const; - //- Transfer the contents into the object - inline void operator=(const Xfer<T>& t); + //- Pointer to the underlying object + inline T* operator->() const; - //- Reference to the underlying datatype + //- Reference to the underlying object inline T& operator()() const; - //- Pointer to the underlying datatype - inline T* operator->() const; }; -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * // -//- Construct by copying the contents of the \a arg -// -// \sa xferCopyTo, xferMove, xferMoveTo, xferTmp and Foam::Xfer -template<class T> -inline Xfer<T> xferCopy(const T& t); -//- Construct by transferring the contents of the \a arg +//- Copy construct contents of the \a obj // -// \sa xferCopy, xferCopyTo, xferMoveTo, xferTmp and Foam::Xfer +// \sa xferCopyTo, xferMove, xferMoveTo and Foam::Xfer template<class T> -inline Xfer<T> xferMove(T& t); - +inline Xfer<T> xferCopy(const T& obj); -//- Construct by transferring the contents of the \a arg +//- Transfer construct contents of the \a obj // -// \sa xferCopy, xferCopyTo, xferMove, xferMoveTo and Foam::Xfer +// \sa xferCopy, xferCopyTo, xferMoveTo and Foam::Xfer template<class T> -inline Xfer<T> xferTmp(Foam::tmp<T>& tt); +inline Xfer<T> xferMove(T& obj); -//- Construct by copying the contents of the \a arg -// between dissimilar types +//- Copy construct contents of the \a obj from dissimilar type // -// \sa xferCopy, xferMove, xferMoveTo, xferTmp and Foam::Xfer -template<class To, class From> -inline Xfer<To> xferCopyTo(const From& t); +// \sa xferCopy, xferMove, xferMoveTo and Foam::Xfer +template<class T, class From> +inline Xfer<T> xferCopyTo(const From& obj); -//- Construct by transferring the contents of the \a arg -// between dissimilar types +//- Transfer construct contents of the \a obj from dissimilar type // // \par Example Use // \code @@ -178,9 +163,9 @@ inline Xfer<To> xferCopyTo(const From& t); // labelList plainLst( xferMoveTo<labelList>(dynLst) ); // \endcode // -// \sa xferCopy, xferCopyTo, xferMove, xferTmp and Foam::Xfer -template<class To, class From> -inline Xfer<To> xferMoveTo(From& t); +// \sa xferCopy, xferCopyTo, xferMove and Foam::Xfer +template<class T, class From> +inline Xfer<T> xferMoveTo(From& obj); // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/OpenFOAM/memory/Xfer/XferI.H b/src/OpenFOAM/memory/Xfer/XferI.H index 26e11724db55dab719e0c71ee85ee67293d8a467..bcb21d8adb6f076492d656d9214da5187881ae04 100644 --- a/src/OpenFOAM/memory/Xfer/XferI.H +++ b/src/OpenFOAM/memory/Xfer/XferI.H @@ -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) 2018 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -24,6 +24,7 @@ License \*---------------------------------------------------------------------------*/ #include "nullObject.H" +#include <utility> // * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * // @@ -34,6 +35,15 @@ inline const Foam::Xfer<T>& Foam::Xfer<T>::null() } +template<class T> +template<class... Args> +inline Foam::Xfer<T> Foam::Xfer<T>::New(Args&&... args) +{ + T* ptr = new T(std::forward<Args>(args)...); + return Foam::Xfer<T>(ptr); +} + + // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // template<class T> @@ -44,36 +54,35 @@ inline Foam::Xfer<T>::Xfer(T* p) template<class T> -inline Foam::Xfer<T>::Xfer(T& t, bool allowTransfer) +inline Foam::Xfer<T>::Xfer(const Xfer<T>& xf) : ptr_(new T) { - if (allowTransfer) - { - ptr_->transfer(t); - } - else + T* p = xf.get(); + + if (p && Foam::notNull(p)) { - ptr_->operator=(t); + ptr_->transfer(*p); } } +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + template<class T> -inline Foam::Xfer<T>::Xfer(const T& t) -: - ptr_(new T) +inline T* Foam::Xfer<T>::get() const { - ptr_->operator=(t); + return ptr_; } template<class T> -inline Foam::Xfer<T>::Xfer(const Xfer<T>& t) -: - ptr_(new T) +inline void Foam::Xfer<T>::swap(Xfer<T>& other) noexcept { - ptr_->transfer(*(t.ptr_)); + // Swap pointers + T* tmp = ptr_; + ptr_ = other.ptr; + other.ptr_ = tmp; } @@ -82,36 +91,29 @@ inline Foam::Xfer<T>::Xfer(const Xfer<T>& t) template<class T> inline Foam::Xfer<T>::~Xfer() { - delete ptr_; + if (ptr_ && Foam::notNull(ptr_)) + { + delete ptr_; + } ptr_ = nullptr; } -// * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * * // - - // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // template<class T> -inline void Foam::Xfer<T>::operator=(T& t) -{ - ptr_->transfer(t); -} - - -template<class T> -inline void Foam::Xfer<T>::operator=(const Xfer<T>& t) +inline void Foam::Xfer<T>::operator=(const Xfer<T>& xf) { - // silently ignore attempted copy to self - if (this != &t) + // Silently ignore attempted copy to self + if (this != &xf) { - ptr_->transfer(*(t.ptr_)); + ptr_->transfer(*xf); } } template<class T> -inline T& Foam::Xfer<T>::operator()() const +inline T& Foam::Xfer<T>::operator*() const { return *ptr_; } @@ -124,45 +126,46 @@ inline T* Foam::Xfer<T>::operator->() const } -// * * * * * * * * * * * * * Helper Functions * * * * * * * * * * * * * * * // - - template<class T> -inline Foam::Xfer<T> Foam::xferCopy(const T& t) +inline T& Foam::Xfer<T>::operator()() const { - return Foam::Xfer<T>(t); + return *ptr_; } +// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * // + template<class T> -inline Foam::Xfer<T> Foam::xferMove(T& t) +inline Foam::Xfer<T> Foam::xferCopy(const T& obj) { - return Foam::Xfer<T>(t, true); + T* ptr = new T(obj); + return Foam::Xfer<T>(ptr); } -template<class T> -inline Foam::Xfer<T> Foam::xferTmp(Foam::tmp<T>& tt) +template<class T, class From> +inline Foam::Xfer<T> Foam::xferCopyTo(const From& obj) { - return Foam::Xfer<T>(tt(), tt.isTmp()); + T* ptr = new T(obj); + return Foam::Xfer<T>(ptr); } -template<class To, class From> -inline Foam::Xfer<To> Foam::xferCopyTo(const From& t) +template<class T> +inline Foam::Xfer<T> Foam::xferMove(T& obj) { - Foam::Xfer<To> xf; - xf() = t; - return xf; + T* ptr = new T; + ptr->transfer(obj); + return Foam::Xfer<T>(ptr); } -template<class To, class From> -inline Foam::Xfer<To> Foam::xferMoveTo(From& t) +template<class T, class From> +inline Foam::Xfer<T> Foam::xferMoveTo(From& obj) { - Foam::Xfer<To> xf; - xf().transfer(t); - return xf; + T* ptr = new T; + ptr->transfer(obj); + return Foam::Xfer<T>(ptr); } diff --git a/src/OpenFOAM/memory/tmp/tmp.H b/src/OpenFOAM/memory/tmp/tmp.H index 245b709722095e59330e50fbf24791df06f83537..52dc896a832ada90423d748af8766c2f5939af1c 100644 --- a/src/OpenFOAM/memory/tmp/tmp.H +++ b/src/OpenFOAM/memory/tmp/tmp.H @@ -63,12 +63,12 @@ class tmp CONST_REF }; - //- Type of object - type type_; - //- Pointer to object mutable T* ptr_; + //- Type of object + type type_; + // Private member operators diff --git a/src/OpenFOAM/memory/tmp/tmpI.H b/src/OpenFOAM/memory/tmp/tmpI.H index cd1183d49a565e05812f0384ad447efc055fc1ff..c789bc0dbff257c76b0d4038394dd322156b2f78 100644 --- a/src/OpenFOAM/memory/tmp/tmpI.H +++ b/src/OpenFOAM/memory/tmp/tmpI.H @@ -48,16 +48,16 @@ inline void Foam::tmp<T>::operator++() template<class T> inline Foam::tmp<T>::tmp() : - type_(TMP), - ptr_(nullptr) + ptr_(nullptr), + type_(TMP) {} template<class T> inline Foam::tmp<T>::tmp(T* p) : - type_(TMP), - ptr_(p) + ptr_(p), + type_(TMP) { if (p && !p->unique()) { @@ -72,16 +72,16 @@ inline Foam::tmp<T>::tmp(T* p) template<class T> inline Foam::tmp<T>::tmp(const T& t) : - type_(CONST_REF), - ptr_(const_cast<T*>(&t)) + ptr_(const_cast<T*>(&t)), + type_(CONST_REF) {} template<class T> inline Foam::tmp<T>::tmp(const tmp<T>& t) : - type_(t.type_), - ptr_(t.ptr_) + ptr_(t.ptr_), + type_(t.type_) { if (isTmp()) { @@ -102,8 +102,8 @@ inline Foam::tmp<T>::tmp(const tmp<T>& t) template<class T> inline Foam::tmp<T>::tmp(const tmp<T>&& t) : - type_(t.type_), - ptr_(t.ptr_) + ptr_(t.ptr_), + type_(t.type_) { if (isTmp()) { @@ -115,8 +115,8 @@ inline Foam::tmp<T>::tmp(const tmp<T>&& t) template<class T> inline Foam::tmp<T>::tmp(const tmp<T>& t, bool allowTransfer) : - type_(t.type_), - ptr_(t.ptr_) + ptr_(t.ptr_), + type_(t.type_) { if (isTmp()) { @@ -335,8 +335,8 @@ inline void Foam::tmp<T>::operator=(T* p) << abort(FatalError); } - type_ = TMP; ptr_ = p; + type_ = TMP; } diff --git a/src/OpenFOAM/meshes/meshShapes/cell/cell.H b/src/OpenFOAM/meshes/meshShapes/cell/cell.H index 6db36cdfb12fb547a44cbeaee76119aa478d66d8..b1269443f3b7fae96b0785d5b8f6ba5f2e58871f 100644 --- a/src/OpenFOAM/meshes/meshShapes/cell/cell.H +++ b/src/OpenFOAM/meshes/meshShapes/cell/cell.H @@ -69,11 +69,11 @@ public: //- Construct given size, with invalid point labels (-1) explicit inline cell(const label sz); - //- Construct from list of labels + //- Copy construct from list of labels explicit inline cell(const labelUList& lst); - //- Construct by transferring list of labels - explicit inline cell(const Xfer<labelList>& lst); + //- Move construct from list of labels + explicit inline cell(labelList&& lst); //- Construct from Istream inline cell(Istream& is); diff --git a/src/OpenFOAM/meshes/meshShapes/cell/cellI.H b/src/OpenFOAM/meshes/meshShapes/cell/cellI.H index 0c31f8e99b6501d680cd62d6c98bf2bc599614c1..e438ec80dd4e0531d56141ee92c652603dbdfbb8 100644 --- a/src/OpenFOAM/meshes/meshShapes/cell/cellI.H +++ b/src/OpenFOAM/meshes/meshShapes/cell/cellI.H @@ -41,9 +41,9 @@ inline Foam::cell::cell(const labelUList& lst) {} -inline Foam::cell::cell(const Xfer<labelList>& lst) +inline Foam::cell::cell(labelList&& lst) : - labelList(lst) + labelList(std::move(lst)) {} diff --git a/src/OpenFOAM/meshes/meshShapes/face/face.C b/src/OpenFOAM/meshes/meshShapes/face/face.C index 6f44cb0314a258fa76ac943622f80a31e4195cf8..c1232819b4de38bb4f9b93a307790f30b930fdd4 100644 --- a/src/OpenFOAM/meshes/meshShapes/face/face.C +++ b/src/OpenFOAM/meshes/meshShapes/face/face.C @@ -606,23 +606,26 @@ Foam::face Foam::face::reverseFace() const // Reverse the label list and return // The starting points of the original and reverse face are identical. - const labelList& f = *this; - labelList newList(size()); + const labelUList& origFace = *this; + const label len = origFace.size(); - newList[0] = f[0]; - - for (label pointi = 1; pointi < newList.size(); pointi++) + face newFace(len); + if (len) { - newList[pointi] = f[size() - pointi]; + newFace[0] = origFace[0]; + for (label i=1; i < len; ++i) + { + newFace[i] = origFace[len - i]; + } } - return face(xferMove(newList)); + return newFace; } Foam::label Foam::face::which(const label globalIndex) const { - const labelList& f = *this; + const labelUList& f = *this; forAll(f, localIdx) { diff --git a/src/OpenFOAM/meshes/meshShapes/face/face.H b/src/OpenFOAM/meshes/meshShapes/face/face.H index 0866a7eead9e80ce723a10b370b90b17a9885bca..fc0069f1925fb0aa5b8043d4309329523dd41c60 100644 --- a/src/OpenFOAM/meshes/meshShapes/face/face.H +++ b/src/OpenFOAM/meshes/meshShapes/face/face.H @@ -150,20 +150,17 @@ public: //- Construct given size, with invalid point labels (-1) explicit inline face(const label sz); - //- Construct from list of labels + //- Copy construct from list of labels explicit inline face(const labelUList& lst); - //- Construct from list of labels + //- Copy construct from list of labels template<unsigned Size> explicit inline face(const FixedList<label, Size>& lst); - //- Construct from an initializer list of labels + //- Copy construct from an initializer list of labels explicit inline face(std::initializer_list<label> lst); - //- Transfer (move) construct - explicit inline face(const Xfer<labelList>& lst); - - //- Move construct + //- Move construct from list of labels explicit inline face(labelList&& lst); //- Copy construct from triFace diff --git a/src/OpenFOAM/meshes/meshShapes/face/faceI.H b/src/OpenFOAM/meshes/meshShapes/face/faceI.H index 0ec413433d5916517114ef130aa9cefc878a6a96..d9123f68b099356fa75d6d550b0972e72ef1155e 100644 --- a/src/OpenFOAM/meshes/meshShapes/face/faceI.H +++ b/src/OpenFOAM/meshes/meshShapes/face/faceI.H @@ -70,12 +70,6 @@ inline Foam::face::face(std::initializer_list<label> lst) {} -inline Foam::face::face(const Xfer<labelList>& lst) -: - labelList(lst) -{} - - inline Foam::face::face(labelList&& lst) : labelList(std::move(lst)) diff --git a/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalIndex.C b/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalIndex.C index 5430b5d83dbe1c62f7cc3d8bad476c16eb181786..68254970189c0ad9d691ef096d47f82731249f81 100644 --- a/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalIndex.C +++ b/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalIndex.C @@ -94,12 +94,6 @@ Foam::globalIndex::globalIndex(const label localSize) } -Foam::globalIndex::globalIndex(const labelList& offsets) -: - offsets_(offsets) -{} - - Foam::globalIndex::globalIndex(Istream& is) { is >> offsets_; diff --git a/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalIndex.H b/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalIndex.H index 50492f31662253007eada02248ff4a69c739fc96..c88e69d0db06177d081c3374aec351a300b3beb8 100644 --- a/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalIndex.H +++ b/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalIndex.H @@ -74,7 +74,7 @@ public: // Constructors //- Construct null - inline globalIndex(); + globalIndex() = default; //- Construct from local max size. Does communication with default // communicator and message tag. @@ -90,11 +90,11 @@ public: const bool parallel // use parallel comms ); - //- Construct from components - globalIndex(const labelList& offsets); + //- Copy construct from list of labels + inline globalIndex(const labelUList& offsets); - //- Construct from components - inline globalIndex(const Xfer<labelList>& offsets); + //- Move construct from list of labels + inline globalIndex(labelList&& offsets); //- Construct from Istream globalIndex(Istream& is); diff --git a/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalIndexI.H b/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalIndexI.H index 3f75a6e9d5a9ce44bfac4d0143d700f00e934cdd..748215938e09f30920ae7fbe18c2650786f39092 100644 --- a/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalIndexI.H +++ b/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalIndexI.H @@ -27,13 +27,15 @@ License // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -Foam::globalIndex::globalIndex() +inline Foam::globalIndex::globalIndex(const labelUList& offsets) +: + offsets_(offsets) {} -Foam::globalIndex::globalIndex(const Xfer<labelList>& offsets) +inline Foam::globalIndex::globalIndex(labelList&& offsets) : - offsets_(offsets) + offsets_(std::move(offsets)) {} diff --git a/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalMeshData.C b/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalMeshData.C index 8a693b037ac407a38e159876a41c6c5afa79ddd5..3492a115b702fbed03424e10607313eb7146c228 100644 --- a/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalMeshData.C +++ b/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalMeshData.C @@ -1545,9 +1545,8 @@ void Foam::globalMeshData::calcGlobalPointBoundaryCells() const } - boundaryCellsPtr_.reset(new labelList()); + boundaryCellsPtr_.reset(new labelList(std::move(cellMap))); labelList& boundaryCells = boundaryCellsPtr_(); - boundaryCells.transfer(cellMap.shrink()); // Convert point-cells to global (boundary)cell numbers diff --git a/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalPoints.C b/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalPoints.C index 94fb8a3f4a2f21b49af272a342308b08d0a0f576..f32a551c51490382fe67760cca94b4168f8ec0e3 100644 --- a/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalPoints.C +++ b/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalPoints.C @@ -732,14 +732,14 @@ void Foam::globalPoints::remove // Save old ones. Map<label> oldMeshToProcPoint(std::move(meshToProcPoint_)); meshToProcPoint_.resize(oldMeshToProcPoint.size()); - DynamicList<labelPairList> oldProcPoints(procPoints_.xfer()); + DynamicList<labelPairList> oldProcPoints(std::move(procPoints_)); procPoints_.setCapacity(oldProcPoints.size()); // Go through all equivalences - forAllConstIter(Map<label>, oldMeshToProcPoint, iter) + forAllConstIters(oldMeshToProcPoint, iter) { - label localPointi = iter.key(); - const labelPairList& pointInfo = oldProcPoints[iter()]; + const label localPointi = iter.key(); + const labelPairList& pointInfo = oldProcPoints[iter.object()]; if (pointInfo.size() == 2) { diff --git a/src/OpenFOAM/primitives/strings/fileName/fileName.C b/src/OpenFOAM/primitives/strings/fileName/fileName.C index 5affbb873fe6d1fd6257c4c2965b247698420d36..b3e144b16f40d0a6e6ee08cd260bbc4b73f8692b 100644 --- a/src/OpenFOAM/primitives/strings/fileName/fileName.C +++ b/src/OpenFOAM/primitives/strings/fileName/fileName.C @@ -29,6 +29,7 @@ License #include "OSspecific.H" #include "wordRe.H" #include "fileOperation.H" +#include "stringOps.H" // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // @@ -463,28 +464,20 @@ bool Foam::fileName::hasExt(const wordRe& ending) const Foam::wordList Foam::fileName::components(const char delimiter) const { - DynamicList<word> wrdList(20); + const auto parsed = stringOps::split<string>(*this, delimiter); - size_type beg=0, end=0; + wordList words(parsed.size()); - while ((end = find(delimiter, beg)) != npos) + label i = 0; + for (const auto& sub : parsed) { - // Avoid empty element (caused by doubled slashes) - if (beg < end) - { - wrdList.append(substr(beg, end-beg)); - } - beg = end + 1; - } - - // Avoid empty trailing element - if (beg < size()) - { - wrdList.append(substr(beg)); + // Could easily filter out '.' here too + words[i] = sub.str(); + ++i; } - // Transfer to wordList - return wordList(wrdList.xfer()); + // As a plain wordList + return words; } @@ -494,7 +487,14 @@ Foam::word Foam::fileName::component const char delimiter ) const { - return components(delimiter)[cmpt]; + const auto parsed = stringOps::split<string>(*this, delimiter); + + if (cmpt < parsed.size()) + { + return parsed[cmpt].str(); + } + + return word(); } diff --git a/src/OpenFOAM/primitives/strings/lists/hashedWordList.C b/src/OpenFOAM/primitives/strings/lists/hashedWordList.C index d1a8a8ac9fef802aff3fa38c72f3ee4f317934f4..d47f7e42c968c96865d7fb7b0174bd9fb82972a1 100644 --- a/src/OpenFOAM/primitives/strings/lists/hashedWordList.C +++ b/src/OpenFOAM/primitives/strings/lists/hashedWordList.C @@ -2,8 +2,8 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2016-2018 OpenCFD Ltd. + \\/ M anipulation | ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -24,118 +24,73 @@ License \*---------------------------------------------------------------------------*/ #include "hashedWordList.H" +#include "CStringList.H" // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // Foam::hashedWordList::hashedWordList ( - const label count, - const char** lst, - const bool removeDuplicates + const label len, + const char** array, + bool unique ) : - List<word>(count) + wordList(len) { - forAll(*this, i) + for (label i=0; i < len; ++i) { - List<word>::operator[](i) = lst[i]; + wordList::operator[](i) = array[i]; } - rehash(removeDuplicates); + rehash(unique); } -Foam::hashedWordList::hashedWordList -( - const char** lst, - const bool removeDuplicates -) -{ - // Determine the number of entries - label count = 0; - for (unsigned i = 0; lst[i] && *(lst[i]); ++i) - { - ++count; - } - - List<word>::setSize(count); - forAll(*this, i) - { - List<word>::operator[](i) = lst[i]; - } - - rehash(removeDuplicates); -} - - -Foam::hashedWordList::hashedWordList(Istream& is) -{ - is >> *this; -} - - -// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * // +Foam::hashedWordList::hashedWordList(const char** array, bool unique) +: + hashedWordList(CStringList::count(array), array, unique) +{} -void Foam::hashedWordList::transfer -( - List<word>& lst, - const bool removeDuplicates -) -{ - List<word>::transfer(lst); - rehash(removeDuplicates); -} +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // void Foam::hashedWordList::rehash() const { - indices_.clear(); + lookup_.clear(); - forAll(*this, i) + const wordUList& list = *this; + const label len = list.size(); + + for (label i=0; i < len; ++i) { - indices_.insert(List<word>::operator[](i), i); + lookup_.insert(list[i], i); } } void Foam::hashedWordList::uniq() { - indices_.clear(); + lookup_.clear(); + + wordList& list = *this; + const label len = list.size(); - label nElem = 0; - forAll(*this, i) + label count = 0; + for (label i=0; i < len; ++i) { - const word& item = List<word>::operator[](i); + word& item = list[i]; - if (indices_.insert(item, nElem)) + if (lookup_.insert(item, i)) { - if (nElem != i) + if (count != i) { - List<word>::operator[](nElem) = item; + list[count] = std::move(item); } - ++nElem; + ++count; } } - List<word>::setSize(nElem); -} - - -// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // - -Foam::Istream& Foam::operator>>(Istream& is, hashedWordList& lst) -{ - is >> static_cast<List<word>&>(lst); - lst.rehash(); - - return is; -} - - -Foam::Ostream& Foam::operator<<(Ostream& os, const hashedWordList& lst) -{ - os << static_cast<const UList<word>&>(lst); - return os; + list.resize(count); } diff --git a/src/OpenFOAM/primitives/strings/lists/hashedWordList.H b/src/OpenFOAM/primitives/strings/lists/hashedWordList.H index a2e68df9df32b56fcd542997773ed952adbd9155..c7a451942e43f661f2e9bfb072b2eb11f8cf7c09 100644 --- a/src/OpenFOAM/primitives/strings/lists/hashedWordList.H +++ b/src/OpenFOAM/primitives/strings/lists/hashedWordList.H @@ -25,7 +25,8 @@ Class Foam::hashedWordList Description - A wordList with hashed indices for additional fast lookup by name. + A wordList with hashed named lookup, which can be faster in some + situations than using the normal list find/found methods. SourceFiles hashedWordListI.H @@ -44,11 +45,9 @@ SourceFiles namespace Foam { +// Forward declarations class hashedWordList; - -// Forward declaration of friend functions and operators -Istream& operator>>(Istream& is, hashedWordList& lst); -Ostream& operator<<(Ostream& os, const hashedWordList& lst); +inline Istream& operator>>(Istream& is, hashedWordList& lst); /*---------------------------------------------------------------------------*\ @@ -57,44 +56,34 @@ Ostream& operator<<(Ostream& os, const hashedWordList& lst); class hashedWordList : - public List<word> + public wordList { - // Private data - - //- Hash of words/indices - mutable HashTable<label> indices_; - - - // Private Member Functions + // Private Data - //- Rebuild the lookup hash or make unique entries first. - inline void rehash(const bool unique); + //- Lookup HashTable of words vs list-indices + mutable HashTable<label> lookup_; public: // Constructors - //- Construct null - inline hashedWordList(); + //- Construct an empty list + inline hashedWordList() = default; - //- Copy constructor. + //- Copy construct. inline hashedWordList(const hashedWordList& lst); - //- Construct from list of words, - // optionally eliminating duplicates - inline hashedWordList - ( - const UList<word>& lst, - const bool removeDuplicates=false - ); + //- Move construct. + inline hashedWordList(hashedWordList&& lst); - //- Construct by transferring the parameter contents, - // optionally eliminating duplicates - inline hashedWordList - ( - const Xfer<List<word>>& lst, - const bool removeDuplicates=false - ); + //- Copy construct from list of words + inline explicit hashedWordList(const wordUList& lst); + + //- Copy construct from list of words, eliminating duplicates + inline hashedWordList(const wordUList& lst, bool unique); + + //- Move construct from list of words, optionally eliminating duplicates + inline hashedWordList(wordList&& lst, bool unique=false); //- Construct from an initializer list inline hashedWordList(std::initializer_list<word> lst); @@ -110,19 +99,14 @@ public: //- Construct from number and list of words, // optionally eliminating duplicates - hashedWordList - ( - const label count, - const char** lst, - const bool removeDuplicates=false - ); + hashedWordList(const label len, const char** array, bool unique=false); //- Construct from a nullptr-terminated list of words, // optionally eliminating duplicates - hashedWordList(const char** lst, const bool removeDuplicates=false); + hashedWordList(const char** array, bool unique=false); //- Construct from Istream - hashedWordList(Istream& is); + inline hashedWordList(Istream& is); // Member Functions @@ -132,7 +116,7 @@ public: //- Append an element at the end of the list, // optionally avoid append if it would be a duplicate entry - inline void append(const word& name, const bool avoidDuplicates=false); + inline void append(const word& name, bool unique=false); //- Does the list contain the specified name inline bool found(const word& name) const; @@ -143,51 +127,64 @@ public: //- Return the hash of words/indices for inspection inline const HashTable<label>& lookup() const; + //- Swap contents + inline void swap(hashedWordList& lst); + + //- Transfer contents of the argument into this list + //- and annul the argument list, optionally eliminating duplicates + inline void transfer(hashedWordList& lst); + //- Transfer the contents of the argument List into this list - // and annul the argument list, - // optionally eliminating duplicates - void transfer(List<word>& lst, const bool removeDuplicates=false); + //- and annul the argument list, optionally eliminating duplicates + inline void transfer(wordList& lst, bool unique=false); //- Rebuild the lookup hash indices void rehash() const; - //- Sort the list and rehash the indices - inline void sort(); + //- Rebuild the lookup hash indices, or make unique entries first. + inline void rehash(bool unique); - //- Adjust the list if necessary to eliminate duplicate entries, - // and rehash the indices + //- Adjust the list (if needed) to eliminate duplicate entries, + //- and rehash the indices void uniq(); + //- Sort the list and rehash the indices + inline void sort(); + // Member Operators - //- Return name corresponding to specified index + //- Return name corresponding to specified index. + // Fatal for out of range values. inline const word& operator[](const label index) const; //- Return index corresponding to specified name, or -1 on failure inline label operator[](const word& name) const; - //- Does the list contain the specified name - same as found. - // Makes hashedWordList suitable as a unary predicate. + //- Check hashed values for the specified name - same as found. + // Can be used as a unary predicate. inline bool operator()(const word& name) const; // Assignment - //- Assignment operator from list of words. Rehashes the indices. - inline void operator=(const UList<word>& lst); + //- Copy assignment. Rehashes the indices. + inline void operator=(const hashedWordList& lst); + + //- Copy assignment from list of words. Rehashes the indices. + inline void operator=(const wordUList& lst); - //- Assignment operator from initializer list. Rehashes the indices. + //- Copy assignment from initializer list. Rehashes the indices. inline void operator=(std::initializer_list<word> lst); - //- Assignment operator. Rehashes the indices. - inline void operator=(const hashedWordList& lst); - + //- Move assignment operator. + inline void operator=(hashedWordList&& lst); - // Istream operators + //- Move assignment from list of words. Rehashes the indices. + inline void operator=(wordList&& lst); - friend Istream& operator>>(Istream& is, hashedWordList& lst); - friend Ostream& operator<<(Ostream& os, const hashedWordList& lst); + //- Read from an input stream. Rehashes the indices. + inline friend Istream& operator>>(Istream& is, hashedWordList& lst); }; diff --git a/src/OpenFOAM/primitives/strings/lists/hashedWordListI.H b/src/OpenFOAM/primitives/strings/lists/hashedWordListI.H index f57ef3abcae93d9abffe157ea6d8ef04f5007e56..0f7971c0c2b1efafc2c7d053f610a1848ef6b72d 100644 --- a/src/OpenFOAM/primitives/strings/lists/hashedWordListI.H +++ b/src/OpenFOAM/primitives/strings/lists/hashedWordListI.H @@ -2,8 +2,8 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2016-2018 OpenCFD Ltd. + \\/ M anipulation | ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -23,64 +23,46 @@ License \*---------------------------------------------------------------------------*/ -// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // - -inline void Foam::hashedWordList::rehash(const bool unique) -{ - if (unique) - { - uniq(); - } - else - { - rehash(); - } -} +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // +inline Foam::hashedWordList::hashedWordList(const hashedWordList& lst) +: + hashedWordList(static_cast<const wordUList&>(lst), false) +{} -// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -inline Foam::hashedWordList::hashedWordList() +inline Foam::hashedWordList::hashedWordList(hashedWordList&& lst) : - List<word>() + wordList(std::move(static_cast<wordList&>(lst))), + lookup_(std::move(lst.lookup_)) {} -inline Foam::hashedWordList::hashedWordList(const hashedWordList& lst) +inline Foam::hashedWordList::hashedWordList(const wordUList& lst) : - List<word>(static_cast<const UList<word>&>(lst)) -{ - rehash(); -} + hashedWordList(lst, false) +{} -inline Foam::hashedWordList::hashedWordList -( - const UList<word>& lst, - const bool removeDuplicates -) +inline Foam::hashedWordList::hashedWordList(const wordUList& lst, bool unique) : - List<word>(lst) + wordList(lst) { - rehash(removeDuplicates); + rehash(unique); } -inline Foam::hashedWordList::hashedWordList -( - const Xfer<List<word>>& lst, - const bool removeDuplicates -) +inline Foam::hashedWordList::hashedWordList(wordList&& lst, bool unique) : - List<word>(lst) + wordList(std::move(lst)) { - rehash(removeDuplicates); + rehash(unique); } inline Foam::hashedWordList::hashedWordList(std::initializer_list<word> lst) : - List<word>(lst) + wordList(lst) { rehash(); } @@ -92,37 +74,41 @@ inline Foam::hashedWordList::hashedWordList const HashTable<AnyType, word, AnyHash>& tbl ) : - List<word>(tbl.size()) + wordList(tbl.size()) { + wordList& list = *this; + label count = 0; for (auto iter = tbl.cbegin(); iter != tbl.cend(); ++iter) { - List<word>::operator[](count++) = iter.key(); + list[count++] = iter.key(); } this->sort(); } +inline Foam::hashedWordList::hashedWordList(Istream& is) +{ + is >> *this; +} + + // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // inline void Foam::hashedWordList::clear() { - List<word>::clear(); - indices_.clear(); + wordList::clear(); + lookup_.clear(); } -inline void Foam::hashedWordList::append -( - const word& name, - const bool avoidDuplicates -) +inline void Foam::hashedWordList::append(const word& name, bool unique) { // name is either unique or we don't care about duplicates - if (indices_.insert(name, size()) || !avoidDuplicates) + if (lookup_.insert(name, size()) || !unique) { - List<word>::append(name); + wordList::append(name); } } @@ -130,19 +116,64 @@ inline void Foam::hashedWordList::append inline const Foam::HashTable<Foam::label>& Foam::hashedWordList::lookup() const { - return indices_; + const label lenList = wordList::size(); + const label lenHash = lookup_.size(); + + if ((lenList < lenHash) || (lenList && !lenHash)) + { + rehash(); // Was somehow out of sync + } + + return lookup_; } inline bool Foam::hashedWordList::found(const word& name) const { - return indices_.found(name); + return lookup().found(name); } inline bool Foam::hashedWordList::contains(const word& name) const { - return indices_.found(name); + return lookup().found(name); +} + + +inline void Foam::hashedWordList::swap(hashedWordList& lst) +{ + wordList::swap(static_cast<wordList&>(lst)); + lookup_.swap(lst.lookup_); +} + + +inline void Foam::hashedWordList::transfer +( + hashedWordList& lst +) +{ + wordList::transfer(static_cast<wordList&>(lst)); + lookup_.transfer(lst.lookup_); +} + + +inline void Foam::hashedWordList::transfer(wordList& lst, bool unique) +{ + wordList::transfer(lst); + rehash(unique); +} + + +inline void Foam::hashedWordList::rehash(bool unique) +{ + if (unique) + { + uniq(); + } + else + { + rehash(); + } } @@ -160,46 +191,65 @@ inline const Foam::word& Foam::hashedWordList::operator[] const label index ) const { - return List<word>::operator[](index); + return wordList::operator[](index); } inline Foam::label Foam::hashedWordList::operator[](const word& name) const { - auto iter = indices_.find(name); + return lookup_.lookup(name, -1); // -1 = not found or not hashed +} - if (iter.found()) - { - return iter.object(); - } - return -1; // Not found (or not hashed?) +inline bool Foam::hashedWordList::operator()(const word& name) const +{ + return lookup_.found(name); } -inline bool Foam::hashedWordList::operator()(const word& name) const +inline void Foam::hashedWordList::operator=(const hashedWordList& lst) { - return indices_.found(name); + wordList::operator=(lst); + rehash(); } -inline void Foam::hashedWordList::operator=(const UList<word>& lst) +inline void Foam::hashedWordList::operator=(const wordUList& lst) { - List<word>::operator=(lst); + wordList::operator=(lst); rehash(); } inline void Foam::hashedWordList::operator=(std::initializer_list<word> lst) { - List<word>::operator=(lst); + wordList::operator=(lst); rehash(); } -inline void Foam::hashedWordList::operator=(const hashedWordList& lst) +inline void Foam::hashedWordList::operator=(hashedWordList&& lst) { - operator=(static_cast<const UList<word>&>(lst)); + wordList::operator=(std::move(static_cast<wordList&>(lst))); + lookup_ = std::move(lst.lookup_); +} + + +inline void Foam::hashedWordList::operator=(wordList&& lst) +{ + wordList::operator=(std::move(lst)); + rehash(); +} + + +// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // + +inline Foam::Istream& Foam::operator>>(Istream& is, hashedWordList& lst) +{ + is >> static_cast<wordList&>(lst); + lst.rehash(); + + return is; } diff --git a/src/conversion/polyDualMesh/polyDualMesh.C b/src/conversion/polyDualMesh/polyDualMesh.C index 04b26d29c0c5920ca0e2cc3c97b511f513d04d36..606114c1fc129422b8bd7d66ebe5db815004c07a 100644 --- a/src/conversion/polyDualMesh/polyDualMesh.C +++ b/src/conversion/polyDualMesh/polyDualMesh.C @@ -545,8 +545,6 @@ void Foam::polyDualMesh::splitFace if (subFace.size() > 2) { // Enough vertices to create a face from. - subFace.shrink(); - dualFaces.append(face(subFace)); dualOwner.append(meshPointi); dualNeighbour.append(-1); @@ -559,8 +557,6 @@ void Foam::polyDualMesh::splitFace if (subFace.size() > 2) { // Enough vertices to create a face from. - subFace.shrink(); - dualFaces.append(face(subFace)); dualOwner.append(meshPointi); dualNeighbour.append(-1); @@ -1194,18 +1190,10 @@ void Foam::polyDualMesh::calcDual // Transfer face info to straight lists // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - faceList dualFaces(dynDualFaces.shrink(), true); - dynDualFaces.clear(); - - labelList dualOwner(dynDualOwner.shrink(), true); - dynDualOwner.clear(); - - labelList dualNeighbour(dynDualNeighbour.shrink(), true); - dynDualNeighbour.clear(); - - labelList dualRegion(dynDualRegion.shrink(), true); - dynDualRegion.clear(); - + faceList dualFaces(std::move(dynDualFaces)); + labelList dualOwner(std::move(dynDualOwner)); + labelList dualNeighbour(std::move(dynDualNeighbour)); + labelList dualRegion(std::move(dynDualRegion)); // Dump faces. diff --git a/src/lagrangian/molecularDynamics/potential/potential/potential.C b/src/lagrangian/molecularDynamics/potential/potential/potential.C index 5c120c6e025e21b862b135c0fa092fd4210511c7..e5c87bc2732067c21d3985a11dd9440b62f6135f 100644 --- a/src/lagrangian/molecularDynamics/potential/potential/potential.C +++ b/src/lagrangian/molecularDynamics/potential/potential/potential.C @@ -89,7 +89,7 @@ void Foam::potential::setSiteIdList(const dictionary& moleculePropertiesDict) } } - siteIdList_.transfer(pairPotentialSiteIdList.shrink()); + siteIdList_.transfer(pairPotentialSiteIdList); } diff --git a/src/mesh/snappyHexMesh/meshRefinement/meshRefinementBaffles.C b/src/mesh/snappyHexMesh/meshRefinement/meshRefinementBaffles.C index 0503b996b232517e15e6729dddab6edcd4266213..0d7a72c24f487a251dbbb04807ee315078f160ff 100644 --- a/src/mesh/snappyHexMesh/meshRefinement/meshRefinementBaffles.C +++ b/src/mesh/snappyHexMesh/meshRefinement/meshRefinementBaffles.C @@ -3495,7 +3495,7 @@ void Foam::meshRefinement::allocateInterRegionFaceZone { // Make sure lowest number cellZone is master. Non-cellZone // areas are slave - bool swap = + const bool swap = ( ownZone == -1 || (neiZone != -1 && ownZone > neiZone) @@ -3505,7 +3505,7 @@ void Foam::meshRefinement::allocateInterRegionFaceZone labelPair key(ownZone, neiZone); if (swap) { - Swap(key.first(), key.second()); + key.flip(); } if (!zoneIDsToFaceZone.found(key)) @@ -3528,7 +3528,7 @@ void Foam::meshRefinement::allocateInterRegionFaceZone Pair<word> wordKey(ownZoneName, neiZoneName); if (swap) { - Swap(wordKey.first(), wordKey.second()); + wordKey.flip(); } word fzName = wordKey.first() + "_to_" + wordKey.second(); @@ -4549,7 +4549,7 @@ Foam::autoPtr<Foam::mapPolyMesh> Foam::meshRefinement::zonify label neiZone = cellToZone[mesh_.faceNeighbour()[faceI]]; if (ownZone != neiZone) { - bool swap = + const bool swap = ( ownZone == -1 || (neiZone != -1 && ownZone > neiZone) @@ -4557,7 +4557,7 @@ Foam::autoPtr<Foam::mapPolyMesh> Foam::meshRefinement::zonify labelPair key(ownZone, neiZone); if (swap) { - Swap(key.first(), key.second()); + key.flip(); } faceToZone[faceI] = fZoneLookup[key]; } @@ -4572,7 +4572,7 @@ Foam::autoPtr<Foam::mapPolyMesh> Foam::meshRefinement::zonify label neiZone = neiCellZone[bFaceI]; if (ownZone != neiZone) { - bool swap = + const bool swap = ( ownZone == -1 || (neiZone != -1 && ownZone > neiZone) @@ -4580,7 +4580,7 @@ Foam::autoPtr<Foam::mapPolyMesh> Foam::meshRefinement::zonify labelPair key(ownZone, neiZone); if (swap) { - Swap(key.first(), key.second()); + key.flip(); } faceToZone[faceI] = fZoneLookup[key]; } diff --git a/src/meshTools/regionSplit/regionSplit.C b/src/meshTools/regionSplit/regionSplit.C index 7b86207f5cb19bc10f5a9b06d98345aad3388851..2c169ff2443ee8c3f2aae2b5da08e42491a722cb 100644 --- a/src/meshTools/regionSplit/regionSplit.C +++ b/src/meshTools/regionSplit/regionSplit.C @@ -172,7 +172,7 @@ Foam::autoPtr<Foam::globalIndex> Foam::regionSplit::calcRegionSplit { offsets[i] = mesh().nFaces(); } - const globalIndex globalRegions(offsets.xfer()); + const globalIndex globalRegions(std::move(offsets)); // Minimise regions across connected cells // Note: still uses global decisions so all processors are running @@ -215,7 +215,7 @@ Foam::autoPtr<Foam::globalIndex> Foam::regionSplit::calcRegionSplit compactOffsets[i] = globalToCompact.size(); } - return autoPtr<globalIndex>(new globalIndex(compactOffsets.xfer())); + return autoPtr<globalIndex>(new globalIndex(std::move(compactOffsets))); } diff --git a/src/meshTools/searchableSurfaces/searchableSurfaceCollection/searchableSurfaceCollection.C b/src/meshTools/searchableSurfaces/searchableSurfaceCollection/searchableSurfaceCollection.C index 54cc899e4ff156dd4f73d0c7429dddb6cdb72c34..953d1538603a9f96e6f93ef54c070b9d9cc6e796 100644 --- a/src/meshTools/searchableSurfaces/searchableSurfaceCollection/searchableSurfaceCollection.C +++ b/src/meshTools/searchableSurfaces/searchableSurfaceCollection/searchableSurfaceCollection.C @@ -285,7 +285,7 @@ Foam::searchableSurfaceCollection::~searchableSurfaceCollection() const Foam::wordList& Foam::searchableSurfaceCollection::regions() const { - if (regions_.size() == 0) + if (regions_.empty()) { regionOffset_.setSize(subGeom_.size()); @@ -309,7 +309,7 @@ const Foam::wordList& Foam::searchableSurfaceCollection::regions() const } } } - regions_.transfer(allRegions.shrink()); + regions_.transfer(allRegions); } return regions_; } diff --git a/src/parallel/distributed/distributedTriSurfaceMesh/distributedTriSurfaceMesh.C b/src/parallel/distributed/distributedTriSurfaceMesh/distributedTriSurfaceMesh.C index 60ff416676c827c40421cc9a6bc31ce972b7980f..0e0f5b063b54f066c78a36cc13e782550dfc7b94 100644 --- a/src/parallel/distributed/distributedTriSurfaceMesh/distributedTriSurfaceMesh.C +++ b/src/parallel/distributed/distributedTriSurfaceMesh/distributedTriSurfaceMesh.C @@ -298,8 +298,8 @@ Foam::distributedTriSurfaceMesh::distributeSegments sendMap[proci].transfer(dynSendMap[proci]); } - allSegments.transfer(dynAllSegments.shrink()); - allSegmentMap.transfer(dynAllSegmentMap.shrink()); + allSegments.transfer(dynAllSegments); + allSegmentMap.transfer(dynAllSegmentMap); } @@ -743,9 +743,9 @@ Foam::distributedTriSurfaceMesh::calcLocalQueries sendMap[proci].transfer(dynSendMap[proci]); } - allCentres.transfer(dynAllCentres.shrink()); - allRadiusSqr.transfer(dynAllRadiusSqr.shrink()); - allSegmentMap.transfer(dynAllSegmentMap.shrink()); + allCentres.transfer(dynAllCentres); + allRadiusSqr.transfer(dynAllRadiusSqr); + allSegmentMap.transfer(dynAllSegmentMap); }