diff --git a/applications/test/DLList/Test-DLList.C b/applications/test/DLList/Test-DLList.C index d3703489825c0cae6b42521ad06e93d9da2cb10b..8e653c4b20cbee9eb78060e720003139bdee5269 100644 --- a/applications/test/DLList/Test-DLList.C +++ b/applications/test/DLList/Test-DLList.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. @@ -41,6 +41,8 @@ int main(int argc, char *argv[]) { DLList<scalar> myList; + Info<< "DLList<scalar>" << nl; + for (int i = 0; i<10; i++) { myList.append(1.3*i); @@ -49,9 +51,7 @@ int main(int argc, char *argv[]) myList.append(100.3); myList.append(500.3); - Info<< nl << "And again using STL iterator: " << nl << endl; - - forAllIter(DLList<scalar>, myList, iter) + forAllConstIters(myList, iter) { Info<< "element:" << *iter << endl; } @@ -59,7 +59,7 @@ int main(int argc, char *argv[]) Info<< nl << "And again using the same STL iterator: " << nl << endl; - forAllIter(DLList<scalar>, myList, iter) + forAllIters(myList, iter) { Info<< "Removing " << myList.remove(iter) << endl; } @@ -68,13 +68,10 @@ int main(int argc, char *argv[]) myList.append(200.3); myList.append(100.3); - - Info<< nl << "And again using STL const_iterator: " << nl << endl; - - - forAllConstIter(DLList<scalar>, myList, iter) + Info<< nl << "Using range-based for: " << nl << endl; + for (auto val : myList) { - Info<< "element:" << *iter << endl; + Info<< "element:" << val << endl; } Info<< nl << "Testing swapUp and swapDown: " << endl; @@ -84,9 +81,9 @@ int main(int argc, char *argv[]) myList.swapUp(myList.DLListBase::first()); myList.swapUp(myList.DLListBase::last()); - forAllIter(DLList<scalar>, myList, iter) + for (auto val : myList) { - Info<< "element:" << *iter << endl; + Info<< "element:" << val << endl; } Info<< nl << "swapDown" << endl; @@ -94,12 +91,11 @@ int main(int argc, char *argv[]) myList.swapDown(myList.DLListBase::first()); myList.swapDown(myList.DLListBase::last()); - forAllIter(DLList<scalar>, myList, iter) + for (auto val : myList) { - Info<< "element:" << *iter << endl; + Info<< "element:" << val << endl; } - Info<< nl << "Testing transfer: " << nl << nl << "original: " << myList << endl; diff --git a/applications/test/Dictionary/Test-Dictionary.C b/applications/test/Dictionary/Test-Dictionary.C index f48845a36f6254a29bb27869df9eee458694e640..251cb2f8419b7bd9c6df790a0f85c0d9552dda22 100644 --- a/applications/test/Dictionary/Test-Dictionary.C +++ b/applications/test/Dictionary/Test-Dictionary.C @@ -168,9 +168,24 @@ int main(int argc, char *argv[]) Info<< nl << "scalarDict2: " << endl; forAllConstIter(PtrDictionary<Scalar>, scalarDict2, iter) { + std::cout<< "iter: " << typeid(*iter).name() << '\n'; + Info<< "elem = " << *iter << endl; } + // FIXME: the deduction seems to be different here. + // - returns pointer (as perhaps actually expected) not the + // underlying value. + forAllConstIters(scalarDict2, iter) + { + std::cout<< "iter: " << typeid(*iter).name() << '\n'; + + Info<< "elem = " << *(*iter) << endl; + } + + std::cout<< "iter type: " + << typeid(stdFoam::begin(scalarDict2)).name() << '\n'; + scalarDict.transfer(scalarDict2); diff --git a/applications/test/HashSet/Test-hashSet.C b/applications/test/HashSet/Test-hashSet.C index dcfa0aec7a317b20f469e73ca118f1c03854d328..0b219f2e3d1bef47f8d7831043bc6972bb5bbd29 100644 --- a/applications/test/HashSet/Test-hashSet.C +++ b/applications/test/HashSet/Test-hashSet.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,6 +28,8 @@ Description #include "hashedWordList.H" #include "HashSet.H" #include "Map.H" +#include "labelPairHashes.H" +#include "FlatOutput.H" using namespace Foam; @@ -65,6 +67,14 @@ int main(int argc, char *argv[]) tableB.insert("value5", nil()); tableB.insert("value6", nil()); + Info<< "tableA keys: "; tableA.writeKeys(Info) << endl; + + auto keyIterPair = tableA.keys(); + for (const auto& i : keyIterPair) + { + Info<<" keys: " << i << endl; + } + Map<label> mapA { { 1, 1 }, @@ -122,13 +132,22 @@ int main(int argc, char *argv[]) << (wordHashSet(setA) | wordHashSet(tableA) | wordHashSet(tableB)) << nl; - labelHashSet setB { 1, 11, 42 }; + setB = FixedList<label, 4>({1, 2, 3, 4}); + setB = {1, 2, 4}; + setB = List<label>({1, 2, 4}); + Info<< "setB : " << setB << endl; + + labelPair pair(12, 15); + setB.set(pair); + Info<< "setB : " << setB << endl; + setB.unset(pair); + labelHashSet setC(1); setC.insert(2008); @@ -139,7 +158,7 @@ int main(int argc, char *argv[]) labelHashSet setD(1); setD.insert({11, 100, 49, 36, 2008}); - Info<< "setD : " << setD << endl; + Info<< "setD : " << flatOutput(setD) << endl; Info<< "setB == setC: " << (setB == setC) << endl; Info<< "setC != setD: " << (setC != setD) << endl; @@ -178,9 +197,9 @@ int main(int argc, char *argv[]) Info<< "setD has no 11" << endl; } - Info<< "setD : " << setD << endl; + Info<< "setD : " << flatOutput(setD) << endl; - // this doesn't work (yet?) + // This should not work (yet?) // setD[12] = true; List<label> someLst(10); @@ -191,8 +210,14 @@ int main(int argc, char *argv[]) label added = setD.set(someLst); Info<< "added " << added << " from " << someLst.size() << endl; - Info<< "setD : " << setD << endl; + Info<< "setD : " << flatOutput(setD) << endl; + Info<< "setD for-range()" << nl; + + for (auto i : setD) + { + Info << i << endl; + } return 0; } diff --git a/applications/test/HashTable/Test-hashTable.C b/applications/test/HashTable/Test-hashTable.C index dc2f70658eb43ba2054b9131379bcd6b91c94166..31bff05b0f840435784592f04bf60bc9e99b8ebe 100644 --- a/applications/test/HashTable/Test-hashTable.C +++ b/applications/test/HashTable/Test-hashTable.C @@ -24,6 +24,7 @@ License \*---------------------------------------------------------------------------*/ #include "HashTable.H" +#include "List.H" #include "IOstreams.H" #include "IStringStream.H" #include "OStringStream.H" @@ -62,7 +63,7 @@ int main() Info<< "\ntable1 sortedToc: " << table1.sortedToc() << endl; table1.printInfo(Info) << "table1 [" << table1.size() << "] " << endl; - forAllConstIter(HashTable<scalar>, table1, iter) + forAllConstIters(table1, iter) { Info<< iter.key() << " => " << iter() << nl; } @@ -106,7 +107,7 @@ int main() << "\ntable3" << table3 << nl; Info<< "\nerase table2 by iterator" << nl; - forAllIter(HashTable<scalar>, table2, iter) + forAllIters(table2, iter) { Info<< "erasing " << iter.key() << " => " << iter.object() << " ... "; table2.erase(iter); @@ -177,6 +178,23 @@ int main() Info<< "\ntable1" << table1 << nl; + Info<< "\nrange-for(table1) - returns values" << nl; + for (const auto& it : table1) + { + Info<< "val:" << it << nl; + } + + Info<< "\nrange-for(table1.keys()) - returns keys" << nl; + for (const auto& k : table1.keys()) + { + Info<< "key:" << k << nl; + } + + // These do not yet work. Issues resolving the distance. + // + // List<scalar> table1vals(table1.begin(), table1.end()); + // wordList table1keys(table1.begin(), table1.end()); + Info<< "\nDone\n"; return 0; diff --git a/applications/test/ISLList/Test-ISLList.C b/applications/test/ISLList/Test-ISLList.C index ef4fd6614d545dcbb839eeba8eb45697446a2c84..38ba2a1424deb19a04328179c1f14bce4c8f375e 100644 --- a/applications/test/ISLList/Test-ISLList.C +++ b/applications/test/ISLList/Test-ISLList.C @@ -78,7 +78,7 @@ int main(int argc, char *argv[]) Info<< nl << "And again using STL iterator: " << nl << endl; - forAllIter(SLList<scalar>, myList, iter) + forAllIters(myList, iter) { Info<< "element:" << *iter << endl; } @@ -87,7 +87,7 @@ int main(int argc, char *argv[]) const ISLList<Scalar>& const_myList = myList; - forAllConstIter(SLList<scalar>, const_myList, iter) + forAllConstIters(const_myList, iter) { Info<< "element:" << *iter << endl; } diff --git a/applications/test/List/Test-List.C b/applications/test/List/Test-List.C index bbba8219b320a16c88967188cc2f41715e343b4b..9ab2732c2e68ff4e9bb496aa64d27a11720b421f 100644 --- a/applications/test/List/Test-List.C +++ b/applications/test/List/Test-List.C @@ -76,7 +76,6 @@ int main(int argc, char *argv[]) Info<<"is >>: " << intlist << endl; } - List<vector> list1(IStringStream("1 ((0 1 2))")()); Info<< "list1: " << list1 << endl; @@ -150,7 +149,6 @@ int main(int argc, char *argv[]) Info<< "normal: " << longLabelList << nl; Info<< "flatOutput: " << flatOutput(longLabelList) << nl; // Info<< "flatOutput(14): " << flatOutput(longLabelList, 14) << nl; - // Info<< "flatOutput(15): " << flatOutput(longLabelList, 15) << nl; stringList longStringList(12); forAll(longStringList, i) @@ -165,6 +163,66 @@ int main(int argc, char *argv[]) // contiguous longStringList[i].resize(3, 'a' + i); } + // test SubList and labelRange + { + Info<< nl; + labelList longLabelList = identity(25); + reverse(longLabelList); + + FixedList<label, 6> fixedLabelList{0,1,2,3,4,5}; + const labelList constLabelList = identity(25); + + Info<< "full-list: " << flatOutput(longLabelList) << nl; + + labelRange range1(-15, 25); + Info<<"sub range:" << range1 << "="; + Info<< SubList<label>(longLabelList, range1) << nl; + + labelRange range2(7, 8); + Info<<"sub range:" << range2 << "="; + Info<< SubList<label>(longLabelList, range2) << nl; + + // labelRange range2(7, 8); + Info<<"use range " << range2 << " to set value"; + SubList<label>(longLabelList, range2) = -15; + Info<< "=> " << flatOutput(longLabelList) << nl; + + // This syntax looks even nicer: + + // GOOD: does not compile + // > constLabelList[labelRange(23,5)] = 5; + + // Check correct overlaps + longLabelList[labelRange(-10, 12)] = 200; + longLabelList[{18,3}] = 100; + longLabelList[{23,3}] = 400; + // and complete misses + longLabelList[{500,50}] = 100; + + // labelRange automatically suppresses -ve size -> nop + longLabelList[{5,-5}] = 42; + longLabelList[{21,100}] = 42; + + //Good: does not compile + //> longLabelList[labelRange(20,50)] = constLabelList; + + //Good: does not compile + // longLabelList[labelRange(20,50)] = fixedLabelList; + + Info<< "updated: " << constLabelList[labelRange(23,5)] << nl; + Info<< "updated: " << flatOutput(longLabelList) << nl; + + //Nope: sort(longLabelList[labelRange(18,5)]); + { + // Instead + UList<label> sub = longLabelList[labelRange(0, 8)]; + sort(sub); + } + Info<< "sub-sorted: " << flatOutput(longLabelList) << nl; + + // Info<<"Slice=" << longLabelList[labelRange(23,5)] << nl; + } + wordReList reLst; wordList wLst; stringList sLst; diff --git a/applications/test/NamedEnum/Test-NamedEnum.C b/applications/test/NamedEnum/Test-NamedEnum.C index 63386ed79ec7a192526d3703e3f7398bc8c792c9..08d1f74972e66c68471671c0b7d32483ed1762d7 100644 --- a/applications/test/NamedEnum/Test-NamedEnum.C +++ b/applications/test/NamedEnum/Test-NamedEnum.C @@ -77,24 +77,12 @@ int main(int argc, char *argv[]) << "] = '" << namedEnumTest::namedEnum[opt] << "'" << nl; } -#if __cplusplus > 201100L - // C++11 Info<< "loop over enums (C++11 for range):" << nl; - for (auto const& opt : options) + for (const auto& opt : options) { Info<< "option[" << opt << "] = '" << namedEnumTest::namedEnum[opt] << "'" << nl; } -#else - Info<< "loop over enums (via iterator):" << nl; - forAllConstIter(List<namedEnumTest::option>, options, iter) - { - const namedEnumTest::option& opt = *iter; - - Info<< "option[" << opt - << "] = '" << namedEnumTest::namedEnum[opt] << "'" << nl; - } -#endif Info<< nl << namedEnumTest::namedEnum["a"] << nl diff --git a/applications/test/SLList/Test-SLList.C b/applications/test/SLList/Test-SLList.C index 30baaa18e37855007e2081ff2be514816c2137a7..2a6ef7a245bf5c93313b53ab7e7fddfe06cb290e 100644 --- a/applications/test/SLList/Test-SLList.C +++ b/applications/test/SLList/Test-SLList.C @@ -52,29 +52,29 @@ int main(int argc, char *argv[]) Info<< nl << "And again using STL iterator: " << nl << endl; - forAllIter(SLList<scalar>, myList, iter) + for (const auto& val : myList) { - Info<< "element:" << *iter << endl; + Info<< "element:" << val << endl; } Info<< nl << "And again using STL const_iterator: " << nl << endl; const SLList<scalar>& const_myList = myList; - forAllConstIter(SLList<scalar>, const_myList, iter) + forAllConstIters(const_myList, iter) { Info<< "element:" << *iter << endl; } - forAllIter(SLList<scalar>, myList, iter) + forAllIters(myList, iter) { Info<< "Removing element:" << *iter << endl; myList.remove(iter); } - forAllConstIter(SLList<scalar>, const_myList, iter) + for (const auto& val : const_myList) { - Info<< "element:" << *iter << endl; + Info<< "element:" << val << endl; } diff --git a/applications/test/Tuple2/Test-Tuple2.C b/applications/test/Tuple2/Test-Tuple2.C index 64a2d5bd7be8cf2a43e1208774f73399596dc1a0..f505554c79b3b5968cd2b2cdff67156536bc213f 100644 --- a/applications/test/Tuple2/Test-Tuple2.C +++ b/applications/test/Tuple2/Test-Tuple2.C @@ -31,6 +31,7 @@ Description #include "Tuple2.H" #include "label.H" #include "scalar.H" +#include "List.H" using namespace Foam; @@ -39,9 +40,25 @@ using namespace Foam; int main() { - Tuple2<label, scalar> t2(1, 3.2); + typedef Tuple2<label, scalar> indexedScalar; - Info<< t2 << " " << t2.first() << " " << t2.second() << endl; + indexedScalar t2(1, 3.2); + + Info<< "tuple: " + << t2 << " " + << t2.first() << " " << t2.second() << endl; + + List<indexedScalar> list1(10); + forAll(list1, i) + { + list1[i] = indexedScalar(-i, i*i); + } + + sort(list1); + + Info<< "tuples:" << nl + << list1 + << endl; Info<< "End\n" << endl; diff --git a/applications/test/cplusplus1/Make/files b/applications/test/cplusplus1/Make/files new file mode 100644 index 0000000000000000000000000000000000000000..8beb3b204c5efa27da300f91466957e4d1a6e271 --- /dev/null +++ b/applications/test/cplusplus1/Make/files @@ -0,0 +1,3 @@ +Test-cpluplus1.C + +EXE = $(FOAM_USER_APPBIN)/Test-cpluplus1 diff --git a/applications/test/cplusplus1/Make/options b/applications/test/cplusplus1/Make/options new file mode 100644 index 0000000000000000000000000000000000000000..6a9e9810b3d5ce6684bdaf03143933480ff45e42 --- /dev/null +++ b/applications/test/cplusplus1/Make/options @@ -0,0 +1,2 @@ +/* EXE_INC = -I$(LIB_SRC)/cfdTools/include */ +/* EXE_LIBS = -lfiniteVolume */ diff --git a/applications/test/cplusplus1/Test-cpluplus1.C b/applications/test/cplusplus1/Test-cpluplus1.C new file mode 100644 index 0000000000000000000000000000000000000000..b288e9286cc7f9ee3c94325dbd81da008b995526 --- /dev/null +++ b/applications/test/cplusplus1/Test-cpluplus1.C @@ -0,0 +1,88 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / 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/>. + +Description + Test miscellaneous C++ templates/functionality. + +\*---------------------------------------------------------------------------*/ + +#include "string.H" +#include "IOstreams.H" +#include "UList.H" +#include "HashSet.H" + +#include <typeinfo> +#include <type_traits> +#include <utility> + +using namespace Foam; + +// Macros to stringify macro contents. +#define STRINGIFY(content) #content +#define STRING_QUOTE(input) STRINGIFY(input) + +#define PRINT_TYPEID(arg) \ + Info<< typeid(arg).name() << " <= typeid of " << STRING_QUOTE(arg) << nl + + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +// Main program: + +int main(int argc, char *argv[]) +{ + Info<< "various declaration types" << nl << nl; + + PRINT_TYPEID(label); + PRINT_TYPEID(decltype(UList<label>::value_type())); + PRINT_TYPEID(decltype(std::declval<UList<label>>().cbegin())); + PRINT_TYPEID(decltype(*(std::declval<UList<label>>().cbegin()))); + Info<< nl; + + PRINT_TYPEID(decltype(HashTable<label>::key_type())); + PRINT_TYPEID(decltype(HashTable<label>::value_type())); + // Not yet: PRINT_TYPEID(decltype(HashTable<label>::mapped_type())); + PRINT_TYPEID(decltype(std::declval<HashTable<label>>().begin())); + PRINT_TYPEID(decltype(std::declval<const HashTable<label>>().begin())); + PRINT_TYPEID(decltype(*(std::declval<HashTable<label>>().begin()))); + PRINT_TYPEID(decltype(*(std::declval<const HashTable<label>>().begin()))); + + PRINT_TYPEID(decltype(std::declval<const HashTable<label>>().keys())); + Info<< nl; + + PRINT_TYPEID(decltype(HashSet<label>::key_type())); + PRINT_TYPEID(decltype(HashSet<label>::value_type())); + // Not yet: PRINT_TYPEID(decltype(HashSet<label>::mapped_type())); + PRINT_TYPEID(decltype(std::declval<HashSet<label>>().begin())); + PRINT_TYPEID(decltype(std::declval<const HashSet<label>>().begin())); + PRINT_TYPEID(decltype(*(std::declval<HashSet<label>>().begin()))); + PRINT_TYPEID(decltype(*(std::declval<const HashSet<label>>().begin()))); + Info<< nl; + + Info << "\nEnd\n" << endl; + + return 0; +} + + +// ************************************************************************* // diff --git a/applications/test/edges/Test-edges.C b/applications/test/edges/Test-edges.C index abd5f9d8a19e04938ff91801cd105c25e2105351..51da417de3e53e7ce1652ffcbc207ddc4f286ab4 100644 --- a/applications/test/edges/Test-edges.C +++ b/applications/test/edges/Test-edges.C @@ -31,6 +31,7 @@ Description #include "argList.H" #include "edgeList.H" +#include "edgeHashes.H" using namespace Foam; @@ -66,6 +67,17 @@ int main(int argc, char *argv[]) Info<< e3 << " connects " << e2 << " => " << e2.connects(e3) << endl; + labelPair labels(e3); + + Info<< "as labelPair: " << labels << endl; + + edge e5; + // Good: this fails (explicit constructor): printInfo(labels); + // Good: this also fails (no assignment operator): e5 = labels; + + // OK: explicit + edge e6(labels); + Info<< nl << "hash-like functionality" << nl; // doesn't work e4 = -1; @@ -79,6 +91,28 @@ int main(int argc, char *argv[]) printInfo(e4); } + e4.start() = e4.end() = -1; + Info<< "insert from list\n"; + labelHashSet newIndices({2, -1, 2, 1, 4, 1, 2, 3}); + e4.insert(newIndices.toc()); + printInfo(e4); + + e4.start() = e4.end() = -1; + Info<< "insert from list\n"; + e4.insert({0, 5, 2, -1, 2, 1, 4, 1, 2, 3}); + printInfo(e4); + + FixedList<label, 8> otherIndices{12, 2, -1, 1, 4, 1, 2, 3}; + e4.start() = e4.end() = -1; + Info<< "insert from list: " << otherIndices << nl; + e4.insert(otherIndices); + printInfo(e4); + + e4.start() = e4.end(); + Info<< "erase from list: " << otherIndices << nl; + Info<< "removed " << e4.erase(otherIndices) << " values" << nl; + printInfo(e4); + for (label i : {-1, 0, 1, 3}) { bool ok = e4.erase(i); diff --git a/applications/test/labelRanges/Test-labelRanges.C b/applications/test/labelRanges/Test-labelRanges.C index dec03a06f0d6f0ab283ad6a2a936e9bee6360494..ec2d15233900b2cf6abd25678dfcda3cc474f3d0 100644 --- a/applications/test/labelRanges/Test-labelRanges.C +++ b/applications/test/labelRanges/Test-labelRanges.C @@ -28,10 +28,6 @@ Description \*---------------------------------------------------------------------------*/ #include "argList.H" -#include "IOobject.H" -#include "IOstreams.H" -#include "IFstream.H" -#include "IStringStream.H" #include "labelRanges.H" using namespace Foam; @@ -42,6 +38,7 @@ using namespace Foam; int main(int argc, char *argv[]) { argList::noParallel(); + argList::noFunctionObjects(); argList::validArgs.insert("start size .. startN sizeN"); argList::addOption("verbose"); argList::addNote @@ -57,6 +54,34 @@ int main(int argc, char *argv[]) labelRange::debug = 1; } + { + Info<<"test sorting" << endl; + DynamicList<labelRange> list1(10); + list1.append(labelRange(25, 8)); + list1.append(labelRange(0, 10)); + list1.append(labelRange(15, 5)); + list1.append(labelRange(50, -10)); + + sort(list1); + Info<<"sorted" << list1 << endl; + } + + { + Info<<"test intersections" << endl; + labelRange range1(-15, 25); + labelRange range2(7, 8); + labelRange range3(-20, 8); + labelRange range4(50, 8); + + Info<<range1 << " & " << range2 + << " = " << range1.subset(range2) << nl; + + Info<< range1 << " & " << range3 + << " = " << range1.subset(range3) << nl; + + Info<< range2 << " & " << range4 + << " = " << range2.subset(range4) << nl; + } labelRange range; labelRanges ranges; @@ -76,12 +101,9 @@ int main(int argc, char *argv[]) } { - label start = 0; - label size = 0; - - IStringStream(args[argI])() >> start; + label start = args.argRead<label>(argI); + label size = args.argRead<label>(argI+1); ++argI; - IStringStream(args[argI])() >> size; range.reset(start, size); } @@ -90,9 +112,9 @@ int main(int argc, char *argv[]) if (removeMode) { Info<< "del " << range << " :"; - forAllConstIter(labelRange, range, iter) + for (auto i : range) { - Info<< " " << iter(); + Info<< " " << i; } Info<< nl; @@ -101,9 +123,9 @@ int main(int argc, char *argv[]) else { Info<< "add " << range << " :"; - forAllConstIter(labelRange, range, iter) + for (auto i : range) { - Info<< " " << iter(); + Info<< " " << i; } Info<< nl; diff --git a/applications/test/nullObject/Make/files b/applications/test/nullObject/Make/files index 596014a44669c9fdd39d0d30194a23cb23224eba..dffa06d74712a1c7bf3d4eaa480c6ca82768f622 100644 --- a/applications/test/nullObject/Make/files +++ b/applications/test/nullObject/Make/files @@ -1,3 +1,3 @@ Test-nullObject.C -EXE = $(FOAM_USER_APPBIN)/nullObject +EXE = $(FOAM_USER_APPBIN)/Test-nullObject diff --git a/applications/test/nullObject/Test-nullObject.C b/applications/test/nullObject/Test-nullObject.C index 9b43405f45eb2d43ad36dc039b40ce99c9651f93..18524006dc53aca79ba4cd613181b144388d6dcb 100644 --- a/applications/test/nullObject/Test-nullObject.C +++ b/applications/test/nullObject/Test-nullObject.C @@ -21,6 +21,17 @@ int main() SimpleClass* ptrToClass = new SimpleClass; SimpleClass& refToClass(*ptrToClass); + typedef unsigned long ptrval; + + Info<<"nullObject address=" << ptrval(&(nullObjectPtr)) << endl; + Info<<"sizeof(nullObject)" << " == " + << sizeof(NullObject::nullObject) + << " vs. sizeof(void*)" << " == " << sizeof(void*) + << endl; + + Info<<"nullObject pointer:" << ptrval(nullObjectPtr->pointer()) << endl; + Info<<"nullObject value:" << nullObjectPtr->value() << endl; + if (notNull(ptrToClass)) { Info<< "Pass: ptrToClass is not null" << endl; diff --git a/applications/test/pTraits/Test-pTraits.C b/applications/test/pTraits/Test-pTraits.C index 548661573d2a34f885d338cb9154cd13bfb8508a..d300ddc87a5a62752e44bcce557eeb526ec84024 100644 --- a/applications/test/pTraits/Test-pTraits.C +++ b/applications/test/pTraits/Test-pTraits.C @@ -29,6 +29,9 @@ Description #include "pTraits.H" #include "vector.H" #include "tensor.H" +#include "uLabel.H" + +#include <type_traits> using namespace Foam; @@ -40,7 +43,10 @@ void printTraits() { Info<< pTraits<T>::typeName << ": zero=" << pTraits<T>::zero - << " one=" << pTraits<T>::one << endl; + << " one=" << pTraits<T>::one + << " integral=" << std::is_integral<T>::value + << " floating=" << std::is_floating_point<T>::value + << endl; } @@ -51,6 +57,9 @@ void printTraits(const pTraits<T>& p) } +#pragma GCC diagnostic warning "-Wmaybe-uninitialized" +#pragma GCC diagnostic warning "-Wuninitialized" + int main() { printTraits<bool>(); @@ -71,6 +80,27 @@ int main() printTraits(pTraits<scalar>(3.14159)); + label abc; + Info<< "unintialized primitive:"<< abc << endl; + + label def = label(); + Info<< "intialized primitive:"<< def << endl; + + Info<< nl << "some interesting label limits:" << nl; + std::cout<< "sizeof = " << sizeof(label) << nl; + std::cout<< "min = " << pTraits<label>::min << nl; + std::cout<< "max = " << pTraits<label>::max << nl; + std::cout<< "umax = " << pTraits<uLabel>::max << nl; + + std::cout<< "max_2 = " << pTraits<label>::max/2 << " == " + << (1 << (sizeof(label)*8-2)) << nl; + + std::cout<< "max_4 = " << pTraits<label>::max/4 << " == " + << (1 << (sizeof(label)*8-3)) << nl; + + std::cout<< "max_8 = " << pTraits<label>::max/8 << " == " + << (1 << (sizeof(label)*8-4)) << nl; + Info<< "End\n" << endl; return 0; diff --git a/applications/utilities/mesh/generation/foamyMesh/conformalVoronoiMesh/DelaunayMesh/DistributedDelaunayMesh.C b/applications/utilities/mesh/generation/foamyMesh/conformalVoronoiMesh/DelaunayMesh/DistributedDelaunayMesh.C index f13d908eb03d0f86d1c0773ac8613e3595006d59..97311eef3cbdefdb4f6412ac96b75545d673df35 100644 --- a/applications/utilities/mesh/generation/foamyMesh/conformalVoronoiMesh/DelaunayMesh/DistributedDelaunayMesh.C +++ b/applications/utilities/mesh/generation/foamyMesh/conformalVoronoiMesh/DelaunayMesh/DistributedDelaunayMesh.C @@ -536,12 +536,7 @@ Foam::label Foam::DistributedDelaunayMesh<Triangulation>::referVertices if (!pointsNotInserted.empty()) { - for - ( - labelPairHashSet::const_iterator iter = pointsNotInserted.begin(); - iter != pointsNotInserted.end(); - ++iter - ) + forAllConstIters(pointsNotInserted, iter) { if (receivedVertices.found(iter.key())) { diff --git a/src/OSspecific/POSIX/regExp.C b/src/OSspecific/POSIX/regExp.C index 15d0ea2ba205595ac91aa308cbe5a885311370cb..a22ba01a56330a202b409bf38ca4cb40f8fa8659 100644 --- a/src/OSspecific/POSIX/regExp.C +++ b/src/OSspecific/POSIX/regExp.C @@ -24,16 +24,14 @@ License \*---------------------------------------------------------------------------*/ #include "regExp.H" -#include "string.H" #include "List.H" // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // -template<class StringType> bool Foam::regExp::matchGrouping ( const std::string& text, - List<StringType>& groups + List<std::string>& groups ) const { if (preg_ && !text.empty()) @@ -194,7 +192,7 @@ std::string::size_type Foam::regExp::find(const std::string& text) const } } - return string::npos; + return std::string::npos; } @@ -231,28 +229,4 @@ bool Foam::regExp::match } -bool Foam::regExp::match -( - const std::string& text, - List<Foam::string>& groups -) const -{ - return matchGrouping(text, groups); -} - - -// * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * // - -void Foam::regExp::operator=(const char* pattern) -{ - set(pattern); -} - - -void Foam::regExp::operator=(const std::string& pattern) -{ - set(pattern); -} - - // ************************************************************************* // diff --git a/src/OSspecific/POSIX/regExp.H b/src/OSspecific/POSIX/regExp.H index 03ce52b39ec6ca7eacf66e4ebcdd94eed4a20c00..76d2deba93c8128ac6652a23167af074860b6c22 100644 --- a/src/OSspecific/POSIX/regExp.H +++ b/src/OSspecific/POSIX/regExp.H @@ -51,7 +51,6 @@ namespace Foam { // Forward declaration of classes -class string; template<class T> class List; @@ -76,12 +75,10 @@ class regExp void operator=(const regExp&) = delete; //- Return true if it matches and sets the sub-groups matched. - // Templated to support both std::string and Foam::string - template<class StringType> bool matchGrouping ( const std::string&, - List<StringType>& groups + List<std::string>& groups ) const; @@ -96,16 +93,7 @@ public: // range: '[', ']' \n // // Don't bother checking for '{digit}' bounds - inline static bool meta(const char c) - { - return - ( - (c == '.') // any character - || (c == '*' || c == '+' || c == '?') // quantifiers - || (c == '(' || c == ')' || c == '|') // grouping/branching - || (c == '[' || c == ']') // range - ); - } + inline static bool meta(const char c); // Constructors @@ -129,22 +117,13 @@ public: // Access //- Return true if a precompiled expression does not exist - inline bool empty() const - { - return !preg_; - } + inline bool empty() const; //- Does a precompiled expression exist? - inline bool exists() const - { - return preg_ ? true : false; - } + inline bool exists() const; //- The number of capture groups for a non-empty expression - inline unsigned ngroups() const - { - return preg_ ? preg_->re_nsub : 0; - } + inline unsigned ngroups() const; // Editing @@ -173,26 +152,19 @@ public: // The begin-of-line (^) and end-of-line ($) anchors are implicit bool match(const std::string& text, List<std::string>& groups) const; - //- Return true if it matches and sets the sub-groups matched - // The begin-of-line (^) and end-of-line ($) anchors are implicit - bool match(const std::string& text, List<string>& groups) const; - //- Return true if the regex was found within string - bool search(const std::string& text) const - { - return std::string::npos != find(text); - } + bool search(const std::string& text) const; // Member Operators //- Assign and compile pattern from a character array // Always case sensitive - void operator=(const char* pattern); + inline void operator=(const char* pattern); //- Assign and compile pattern from string // Always case sensitive - void operator=(const std::string& pattern); + inline void operator=(const std::string& pattern); }; @@ -202,6 +174,8 @@ public: // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +#include "regExpI.H" + #endif // ************************************************************************* // diff --git a/src/OSspecific/POSIX/regExpI.H b/src/OSspecific/POSIX/regExpI.H new file mode 100644 index 0000000000000000000000000000000000000000..64865c95fa0169272806eb986090db6ecc24b253 --- /dev/null +++ b/src/OSspecific/POSIX/regExpI.H @@ -0,0 +1,81 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / 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/>. + +\*---------------------------------------------------------------------------*/ + + +// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // + +inline bool Foam::regExp::meta(const char c) +{ + return + ( + (c == '.') // any character + || (c == '*' || c == '+' || c == '?') // quantifiers + || (c == '(' || c == ')' || c == '|') // grouping/branching + || (c == '[' || c == ']') // range + ); +} + + +// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * // + +inline bool Foam::regExp::empty() const +{ + return !preg_; +} + + +inline bool Foam::regExp::exists() const +{ + return preg_ ? true : false; +} + + +inline unsigned Foam::regExp::ngroups() const +{ + return preg_ ? preg_->re_nsub : 0; +} + + +inline bool Foam::regExp::search(const std::string& text) const +{ + return std::string::npos != find(text); +} + + +// * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * // + +inline void Foam::regExp::operator=(const char* pattern) +{ + set(pattern); +} + + +inline void Foam::regExp::operator=(const std::string& pattern) +{ + set(pattern); +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.C b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.C index 288d16dbc63bcbe81d9e0790b61aa9356cdf0d84..4143d25d85299c2e3ea2510bf1f80d16796a3a50 100644 --- a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.C +++ b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.C @@ -31,7 +31,7 @@ License template<class T, class Key, class Hash> Foam::HashPtrTable<T, Key, Hash>::HashPtrTable(const label size) : - HashTable<T*, Key, Hash>(size) + parent_type(size) {} @@ -41,11 +41,11 @@ Foam::HashPtrTable<T, Key, Hash>::HashPtrTable const HashPtrTable<T, Key, Hash>& ht ) : - HashTable<T*, Key, Hash>() + parent_type(ht.capacity()) { for (const_iterator iter = ht.begin(); iter != ht.end(); ++iter) { - const T* ptr = *iter; + const T* ptr = iter.object(); if (ptr) { this->insert(iter.key(), new T(*ptr)); @@ -72,8 +72,8 @@ Foam::HashPtrTable<T, Key, Hash>::~HashPtrTable() template<class T, class Key, class Hash> T* Foam::HashPtrTable<T, Key, Hash>::remove(iterator& iter) { - T* ptr = *iter; - HashTable<T*, Key, Hash>::erase(iter); + T* ptr = iter.object(); + this->parent_type::erase(iter); return ptr; } @@ -81,9 +81,9 @@ T* Foam::HashPtrTable<T, Key, Hash>::remove(iterator& iter) template<class T, class Key, class Hash> bool Foam::HashPtrTable<T, Key, Hash>::erase(iterator& iter) { - T* ptr = *iter; + T* ptr = iter.object(); - if (HashTable<T*, Key, Hash>::erase(iter)) + if (this->parent_type::erase(iter)) { if (ptr) { @@ -102,17 +102,12 @@ bool Foam::HashPtrTable<T, Key, Hash>::erase(iterator& iter) template<class T, class Key, class Hash> void Foam::HashPtrTable<T, Key, Hash>::clear() { - for - ( - iterator iter = this->begin(); - iter != this->end(); - ++iter - ) + for (iterator iter = this->begin(); iter != this->end(); ++iter) { - delete *iter; + delete iter.object(); } - HashTable<T*, Key, Hash>::clear(); + this->parent_type::clear(); } @@ -136,7 +131,7 @@ void Foam::HashPtrTable<T, Key, Hash>::operator= for (const_iterator iter = rhs.begin(); iter != rhs.end(); ++iter) { - const T* ptr = *iter; + const T* ptr = iter.object(); if (ptr) { this->insert(iter.key(), new T(*ptr)); diff --git a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.H b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.H index 611a7d147652c4bba759ec87d5620743dc818670..7cdf3743e311b3acd4e63f42b8fa3e753a7368a3 100644 --- a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.H +++ b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.H @@ -25,7 +25,7 @@ Class Foam::HashPtrTable Description - A HashTable specialization for hashing pointers. + A HashTable of pointers to objects of type \<T\>. SourceFiles HashPtrTable.C @@ -51,10 +51,10 @@ class Ostream; template<class T, class Key, class Hash> class HashPtrTable; template<class T, class Key, class Hash> -Istream& operator>>(Istream& is, HashPtrTable<T, Key, Hash>& L); +Istream& operator>>(Istream& is, HashPtrTable<T, Key, Hash>& tbl); template<class T, class Key, class Hash> -Ostream& operator<<(Ostream& os, const HashPtrTable<T, Key, Hash>& L); +Ostream& operator<<(Ostream& os, const HashPtrTable<T, Key, Hash>& tbl); /*---------------------------------------------------------------------------*\ @@ -77,11 +77,16 @@ class HashPtrTable void read(const dictionary& dict, const INew& inewt); - public: - typedef typename HashTable<T*, Key, Hash>::iterator iterator; - typedef typename HashTable<T*, Key, Hash>::const_iterator const_iterator; + //- The template instance used for this table + typedef HashPtrTable<T, Key, Hash> this_type; + + //- The template instance used for the parent HashTable + typedef HashTable<T*, Key, Hash> parent_type; + + using iterator = typename parent_type::iterator; + using const_iterator = typename parent_type::const_iterator; // Constructors @@ -100,7 +105,7 @@ public: HashPtrTable(const dictionary& dict); //- Construct as copy - HashPtrTable(const HashPtrTable<T, Key, Hash>& ht); + HashPtrTable(const this_type& ht); //- Destructor @@ -109,25 +114,25 @@ public: // Member Functions - // Edit + // Edit - //- Remove and return the pointer specified by given iterator - T* remove(iterator& iter); + //- Remove and return the pointer specified by given iterator + T* remove(iterator& iter); - //- Erase an hashedEntry specified by given iterator - bool erase(iterator& iter); + //- Erase an hashedEntry specified by given iterator + bool erase(iterator& iter); - //- Clear all entries from table - void clear(); + //- Clear all entries from table + void clear(); - //- Write - void write(Ostream& os) const; + //- Write + void write(Ostream& os) const; // Member Operators //- Copy assignment - void operator=(const HashPtrTable<T, Key, Hash>& rhs); + void operator=(const this_type& rhs); // IOstream Operators @@ -135,13 +140,13 @@ public: friend Istream& operator>> <T, Key, Hash> ( Istream& is, - HashPtrTable<T, Key, Hash>& L + HashPtrTable<T, Key, Hash>& tbl ); friend Ostream& operator<< <T, Key, Hash> ( Ostream& os, - const HashPtrTable<T, Key, Hash>& L + const HashPtrTable<T, Key, Hash>& tbl ); }; diff --git a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTableIO.C b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTableIO.C index a19dd42b0350dc0922b127b5626b1451675ee0c4..7a679ef149b6841e49e6be9a7d3d35382db74112 100644 --- a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTableIO.C +++ b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTableIO.C @@ -35,7 +35,7 @@ template<class T, class Key, class Hash> template<class INew> void Foam::HashPtrTable<T, Key, Hash>::read(Istream& is, const INew& inewt) { - is.fatalCheck("HashPtrTable<T, Key, Hash>::read(Istream&, const INew&)"); + is.fatalCheck(FUNCTION_NAME); token firstToken(is); @@ -131,7 +131,7 @@ void Foam::HashPtrTable<T, Key, Hash>::read(Istream& is, const INew& inewt) << exit(FatalIOError); } - is.fatalCheck("HashPtrTable<T, Key, Hash>::read(Istream&, const INew&)"); + is.fatalCheck(FUNCTION_NAME); } @@ -145,11 +145,9 @@ void Foam::HashPtrTable<T, Key, Hash>::read { forAllConstIter(dictionary, dict, iter) { - this->insert - ( - iter().keyword(), - inewt(dict.subDict(iter().keyword())).ptr() - ); + const word& k = iter().keyword(); + + this->insert(k, inewt(dict.subDict(k)).ptr()); } } @@ -157,16 +155,9 @@ void Foam::HashPtrTable<T, Key, Hash>::read template<class T, class Key, class Hash> void Foam::HashPtrTable<T, Key, Hash>::write(Ostream& os) const { - - for - ( - typename HashPtrTable<T, Key, Hash>::const_iterator - iter = this->begin(); - iter != this->end(); - ++iter - ) + for (const_iterator iter = this->begin(); iter != this->end(); ++iter) { - const T* ptr = *iter; + const T* ptr = iter.object(); if (ptr) { ptr->write(os); @@ -202,10 +193,10 @@ Foam::HashPtrTable<T, Key, Hash>::HashPtrTable(const dictionary& dict) // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // template<class T, class Key, class Hash> -Foam::Istream& Foam::operator>>(Istream& is, HashPtrTable<T, Key, Hash>& L) +Foam::Istream& Foam::operator>>(Istream& is, HashPtrTable<T, Key, Hash>& tbl) { - L.clear(); - L.read(is, INew<T>()); + tbl.clear(); + tbl.read(is, INew<T>()); return is; } @@ -215,21 +206,18 @@ template<class T, class Key, class Hash> Foam::Ostream& Foam::operator<< ( Ostream& os, - const HashPtrTable<T, Key, Hash>& L + const HashPtrTable<T, Key, Hash>& tbl ) { + using const_iterator = typename HashPtrTable<T, Key, Hash>::const_iterator; + // Write size and start delimiter - os << nl << L.size() << nl << token::BEGIN_LIST << nl; + os << nl << tbl.size() << nl << token::BEGIN_LIST << nl; // Write contents - for - ( - typename HashPtrTable<T, Key, Hash>::const_iterator iter = L.begin(); - iter != L.end(); - ++iter - ) + for (const_iterator iter = tbl.cbegin(); iter != tbl.cend(); ++iter) { - const T* ptr = *iter; + const T* ptr = iter.object(); os << iter.key(); if (ptr) @@ -242,8 +230,7 @@ Foam::Ostream& Foam::operator<< // Write end delimiter os << token::END_LIST; - // Check state of IOstream - os.check("Ostream& operator<<(Ostream&, const HashPtrTable&)"); + os.check(FUNCTION_NAME); return os; } diff --git a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.C b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.C index 0b1456f81d8d934b112fdc25d618193d7c8b380f..321b8dd5dfe6bf9092ee0fe09dffbf8aae23e004 100644 --- a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.C +++ b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.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) 2016 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -27,6 +27,52 @@ License #define HashSet_C #include "HashSet.H" +#include "FixedList.H" + +// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // + +template<class Key, class Hash> +template<class InputIter> +inline Foam::label Foam::HashSet<Key, Hash>::insertMultiple +( + const InputIter begIter, + const InputIter endIter +) +{ + label changed = 0; + for (InputIter iter = begIter; iter != endIter; ++iter) + { + if (insert(*iter)) + { + ++changed; + } + } + return changed; +} + + +template<class Key, class Hash> +template<class InputIter> +inline Foam::label Foam::HashSet<Key, Hash>::assignMultiple +( + const InputIter begIter, + const InputIter endIter, + const label sz +) +{ + if (!this->capacity()) + { + // Could be zero-sized from a previous transfer()? + this->resize(sz); + } + else + { + this->clear(); + } + + return insertMultiple(begIter, endIter); +} + // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // @@ -35,9 +81,22 @@ Foam::HashSet<Key, Hash>::HashSet(const UList<Key>& lst) : HashTable<nil, Key, Hash>(2*lst.size()) { - forAll(lst, elemI) + for (const auto& k : lst) { - this->insert(lst[elemI]); + this->insert(k); + } +} + + +template<class Key, class Hash> +template<unsigned Size> +Foam::HashSet<Key, Hash>::HashSet(const FixedList<Key, Size>& lst) +: + HashTable<nil, Key, Hash>(2*lst.size()) +{ + for (const auto& k : lst) + { + this->insert(k); } } @@ -47,7 +106,7 @@ Foam::HashSet<Key, Hash>::HashSet(std::initializer_list<Key> lst) : HashTable<nil, Key, Hash>(2*lst.size()) { - for (const Key& k : lst) + for (const auto& k : lst) { this->insert(k); } @@ -58,20 +117,17 @@ template<class Key, class Hash> template<class AnyType, class AnyHash> Foam::HashSet<Key, Hash>::HashSet ( - const HashTable<AnyType, Key, AnyHash>& h + const HashTable<AnyType, Key, AnyHash>& tbl ) : - HashTable<nil, Key, Hash>(h.size()) + HashTable<nil, Key, Hash>(tbl.capacity()) { - for - ( - typename HashTable<AnyType, Key, AnyHash>::const_iterator - cit = h.cbegin(); - cit != h.cend(); - ++cit - ) + using other_iter = + typename HashTable<AnyType, Key, AnyHash>::const_iterator; + + for (other_iter iter = tbl.cbegin(); iter != tbl.cend(); ++iter) { - this->insert(cit.key()); + this->insert(iter.key()); } } @@ -81,36 +137,49 @@ Foam::HashSet<Key, Hash>::HashSet template<class Key, class Hash> Foam::label Foam::HashSet<Key, Hash>::insert(const UList<Key>& lst) { - label count = 0; - forAll(lst, elemI) - { - if (this->insert(lst[elemI])) - { - ++count; - } - } + return insertMultiple(lst.begin(), lst.end()); +} + - return count; +template<class Key, class Hash> +template<unsigned Size> +Foam::label Foam::HashSet<Key, Hash>::insert(const FixedList<Key, Size>& lst) +{ + return insertMultiple(lst.begin(), lst.end()); } + template<class Key, class Hash> Foam::label Foam::HashSet<Key, Hash>::insert(std::initializer_list<Key> lst) { - label count = 0; - for (const Key& k : lst) - { - if (this->insert(k)) - { - ++count; - } - } - - return count; + return insertMultiple(lst.begin(), lst.end()); } // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // +template<class Key, class Hash> +void Foam::HashSet<Key, Hash>::operator=(const UList<Key>& lst) +{ + assignMultiple(lst.begin(), lst.end(), 2*lst.size()); +} + + +template<class Key, class Hash> +template<unsigned Size> +void Foam::HashSet<Key, Hash>::operator=(const FixedList<Key, Size>& lst) +{ + assignMultiple(lst.begin(), lst.end(), 2*lst.size()); +} + + +template<class Key, class Hash> +void Foam::HashSet<Key, Hash>::operator=(std::initializer_list<Key> lst) +{ + assignMultiple(lst.begin(), lst.end(), 2*lst.size()); +} + + template<class Key, class Hash> inline bool Foam::HashSet<Key, Hash>::operator[](const Key& key) const { @@ -142,7 +211,7 @@ bool Foam::HashSet<Key, Hash>::operator==(const HashSet<Key, Hash>& rhs) const template<class Key, class Hash> bool Foam::HashSet<Key, Hash>::operator!=(const HashSet<Key, Hash>& rhs) const { - return !(operator==(rhs)); + return !operator==(rhs); } @@ -200,6 +269,15 @@ void Foam::HashSet<Key, Hash>::operator-=(const HashSet<Key, Hash>& rhs) } +// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // + +template<class Key, class Hash> +Foam::Ostream& Foam::operator<<(Ostream& os, const HashSet<Key, Hash>& tbl) +{ + return tbl.writeList(os, 10); // 10=consistent with UList +} + + /* * * * * * * * * * * * * * * * Global operators * * * * * * * * * * * * * */ template<class Key, class Hash> @@ -243,6 +321,66 @@ Foam::operator^ return out; } + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +template<class Key, class Hash> +inline typename Foam::HashSet<Key, Hash>::iterator +Foam::HashSet<Key, Hash>::begin() +{ + return HashTableCore::iterator_begin<iterator> + ( + static_cast<parent_type&>(*this) + ); +} + + +template<class Key, class Hash> +inline typename Foam::HashSet<Key, Hash>::const_iterator +Foam::HashSet<Key, Hash>::begin() const +{ + return HashTableCore::iterator_begin<const_iterator> + ( + static_cast<const parent_type&>(*this) + ); +} + + +template<class Key, class Hash> +inline typename Foam::HashSet<Key, Hash>::const_iterator +Foam::HashSet<Key, Hash>::cbegin() const +{ + return HashTableCore::iterator_begin<const_iterator> + ( + static_cast<const parent_type&>(*this) + ); +} + + +template<class Key, class Hash> +inline const typename Foam::HashSet<Key, Hash>::iterator& +Foam::HashSet<Key, Hash>::end() +{ + return HashTableCore::iterator_end<iterator>(); +} + + +template<class Key, class Hash> +inline const typename Foam::HashSet<Key, Hash>::const_iterator& +Foam::HashSet<Key, Hash>::end() const +{ + return HashTableCore::iterator_end<const_iterator>(); +} + + +template<class Key, class Hash> +inline const typename Foam::HashSet<Key, Hash>::const_iterator& +Foam::HashSet<Key, Hash>::cend() const +{ + return HashTableCore::iterator_cend<const_iterator>(); +} + + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // #endif diff --git a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H index 437ca74eb684a0bac713b87caf62b31bae1a742c..300fc111325b22d1cd863f9b94dacb5ae1641419 100644 --- a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H +++ b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.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) 2016 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -52,6 +52,13 @@ Description namespace Foam { +// Forward declaration of friend functions and operators +template<class Key, class Hash> class HashSet; + +template<class Key, class Hash> +Ostream& operator<<(Ostream& os, const HashSet<Key, Hash>& tbl); + + /*---------------------------------------------------------------------------*\ Class HashSet Declaration \*---------------------------------------------------------------------------*/ @@ -61,11 +68,39 @@ class HashSet : public HashTable<nil, Key, Hash> { + // Private Member Functions + + //- Insert values, using begin/end iterators. + template<class InputIter> + inline label insertMultiple + ( + const InputIter begIter, + const InputIter endIter + ); + + //- Assign using begin/end iterators. + template<class InputIter> + inline label assignMultiple + ( + const InputIter begIter, + const InputIter endIter, + const label sz + ); + public: - typedef typename HashTable<nil, Key, Hash>::iterator iterator; - typedef typename HashTable<nil, Key, Hash>::const_iterator const_iterator; + //- The template instance used for this HashSet + typedef HashSet<Key, Hash> this_type; + + //- The template instance used for the parent HashTable + typedef HashTable<nil, Key, Hash> parent_type; + + //- An iterator, returning reference to the key + using iterator = typename parent_type::key_iterator; + + //- A const_iterator, returning reference to the key + using const_iterator = typename parent_type::const_key_iterator; // Constructors @@ -73,59 +108,68 @@ public: //- Construct given initial size HashSet(const label size = 128) : - HashTable<nil, Key, Hash>(size) + parent_type(size) {} //- Construct from Istream HashSet(Istream& is) : - HashTable<nil, Key, Hash>(is) + parent_type(is) {} //- Construct from UList of Key - HashSet(const UList<Key>& lst); + explicit HashSet(const UList<Key>& lst); + + //- Construct from FixedList of Key + template<unsigned Size> + explicit HashSet(const FixedList<Key, Size>& lst); //- Construct from an initializer list of Key - HashSet(std::initializer_list<Key>); + HashSet(std::initializer_list<Key> lst); //- Construct as copy HashSet(const HashSet<Key, Hash>& hs) : - HashTable<nil, Key, Hash>(hs) + parent_type(hs) {} //- Construct by transferring the parameter contents HashSet(const Xfer<HashSet<Key, Hash>>& hs) : - HashTable<nil, Key, Hash>(hs) + parent_type(hs) {} //- Construct by transferring the parameter contents HashSet(const Xfer<HashTable<nil, Key, Hash>>& hs) : - HashTable<nil, Key, Hash>(hs) + parent_type(hs) {} //- Construct from the keys of another HashTable, // the type of values held is arbitrary. template<class AnyType, class AnyHash> - HashSet(const HashTable<AnyType, Key, AnyHash>& h); + explicit HashSet(const HashTable<AnyType, Key, AnyHash>& tbl); // Member Functions - // Edit + // Edit //- Insert a new entry bool insert(const Key& key) { - return HashTable<nil, Key, Hash>::insert(key, nil()); + return this->parent_type::insert(key, nil()); } - //- Insert keys from a UList of Key + //- Insert keys from the list of Key // Return the number of new elements inserted label insert(const UList<Key>& lst); + //- Insert keys from the list of Key + // Return the number of new elements inserted + template<unsigned Size> + label insert(const FixedList<Key, Size>& lst); + //- Insert keys from a initializer list of Key // Return the number of new elements inserted label insert(std::initializer_list<Key> lst); @@ -142,6 +186,13 @@ public: return insert(lst); } + //- Same as insert (cannot overwrite nil content) + template<unsigned Size> + label set(const FixedList<Key, Size>& lst) + { + return insert(lst); + } + //- Same as insert (cannot overwrite nil content) label set(std::initializer_list<Key> lst) { @@ -151,21 +202,75 @@ public: //- Unset the specified key - same as erase bool unset(const Key& key) { - return HashTable<nil, Key, Hash>::erase(key); + return this->parent_type::erase(key); + } + + //- Unset the listed keys - same as erase + label unset(const UList<Key>& lst) + { + return this->parent_type::erase(lst); + } + + //- Unset the listed keys - same as erase + template<unsigned Size> + label unset(const FixedList<Key, Size>& lst) + { + return this->parent_type::erase(lst); + } + + //- Unset the listed keys - same as erase + label unset(std::initializer_list<Key> lst) + { + return this->parent_type::erase(lst); + } + + + // STL iterators + + iterator begin(); + const_iterator begin() const; + const_iterator cbegin() const; + + const iterator& end(); + const const_iterator& end() const; + const const_iterator& cend() const; + + + // Writing + + //- Write the unordered keys as a list, with line-breaks if list length + // exceeds shortListLen. Using '0' suppresses line-breaks entirely. + Ostream& writeList(Ostream& os, const label shortListLen=0) const + { + return this->writeKeys(os, shortListLen); } // Member Operators + //- This operation doesn't make much sense for a hash-set + void operator()(const Key& key) = delete; + //- Return true if the entry exists, same as found() inline bool operator[](const Key& key) const; //- Equality. Two hashset are equal when they have the same keys. // Independent of table size or order. - bool operator==(const HashSet<Key, Hash>& rhs) const; + bool operator==(const this_type& rhs) const; //- The opposite of the equality operation. - bool operator!=(const HashSet<Key, Hash>& rhs) const; + bool operator!=(const this_type& rhs) const; + + + //- Assignment from a UList of keys + void operator=(const UList<Key>& lst); + + //- Assignment from a FixedList of keys + template<unsigned Size> + void operator=(const FixedList<Key, Size>& lst); + + //- Assignment from an initializer list of keys + void operator=(std::initializer_list<Key> lst); //- Combine entries from HashSets @@ -185,6 +290,16 @@ public: //- Remove entries listed in the given HashSet from this HashSet void operator-=(const HashSet<Key, Hash>& rhs); + + + // IOstream Operator + + friend Ostream& operator<< <Key, Hash> + ( + Ostream& os, + const HashSet<Key, Hash>& tbl + ); + }; diff --git a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C index eebdde251919a9b85b05cff406f1aca02c3ac0d7..b0b96c5b19aec6d0e56f0678df055a31e59c8556 100644 --- a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C +++ b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.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,8 +28,38 @@ License #include "HashTable.H" #include "List.H" +#include "FixedList.H" #include "Tuple2.H" +// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * // + +template<class T, class Key, class Hash> +template<class InputIter> +Foam::label Foam::HashTable<T, Key, Hash>::eraseMultiple +( + const InputIter begIter, + const InputIter endIter +) +{ + const label nTotal = this->size(); + label changed = 0; + + for + ( + InputIter iter = begIter; + changed < nTotal && iter != endIter; // terminate early + ++iter + ) + { + if (this->erase(*iter)) + { + ++changed; + } + } + return changed; +} + + // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // template<class T, class Key, class Hash> @@ -46,7 +76,7 @@ Foam::HashTable<T, Key, Hash>::HashTable(const label size) for (label hashIdx = 0; hashIdx < tableSize_; hashIdx++) { - table_[hashIdx] = 0; + table_[hashIdx] = nullptr; } } } @@ -59,7 +89,7 @@ Foam::HashTable<T, Key, Hash>::HashTable(const HashTable<T, Key, Hash>& ht) { for (const_iterator iter = ht.cbegin(); iter != ht.cend(); ++iter) { - insert(iter.key(), *iter); + insert(iter.key(), iter.object()); } } @@ -85,7 +115,7 @@ Foam::HashTable<T, Key, Hash>::HashTable std::initializer_list<Tuple2<Key, T>> lst ) : - HashTable<T, Key, Hash>(lst.size()) + HashTable<T, Key, Hash>(2*lst.size()) { for (const Tuple2<Key, T>& pair : lst) { @@ -238,8 +268,8 @@ bool Foam::HashTable<T, Key, Hash>::set const label hashIdx = hashKeyIndex(key); - hashedEntry* existing = 0; - hashedEntry* prev = 0; + hashedEntry* existing = nullptr; + hashedEntry* prev = nullptr; for (hashedEntry* ep = table_[hashIdx]; ep; ep = ep->next_) { @@ -251,10 +281,10 @@ bool Foam::HashTable<T, Key, Hash>::set prev = ep; } - // Not found, insert it at the head if (!existing) { - table_[hashIdx] = new hashedEntry(key, table_[hashIdx], newEntry); + // Not found, insert it at the head + table_[hashIdx] = new hashedEntry(key, newEntry, table_[hashIdx]); nElmts_++; if (double(nElmts_)/tableSize_ > 0.8 && tableSize_ < maxTableSize) @@ -286,7 +316,7 @@ bool Foam::HashTable<T, Key, Hash>::set { // Found - overwrite existing entry // this corresponds to the Perl convention - hashedEntry* ep = new hashedEntry(key, existing->next_, newEntry); + hashedEntry* ep = new hashedEntry(key, newEntry, existing->next_); // Replace existing element - within list or insert at the head if (prev) @@ -306,17 +336,17 @@ bool Foam::HashTable<T, Key, Hash>::set template<class T, class Key, class Hash> -bool Foam::HashTable<T, Key, Hash>::iteratorBase::erase() +bool Foam::HashTable<T, Key, Hash>::iterator_base::erase() { // Note: entryPtr_ is nullptr for end(), so this catches that too if (entryPtr_) { // Search element before entryPtr_ - hashedEntry* prev = 0; + entry_type* prev = nullptr; for ( - hashedEntry* ep = hashTable_->table_[hashIndex_]; + entry_type* ep = hashTable_->table_[hashIndex_]; ep; ep = ep->next_ ) @@ -341,8 +371,7 @@ bool Foam::HashTable<T, Key, Hash>::iteratorBase::erase() hashTable_->table_[hashIndex_] = entryPtr_->next_; delete entryPtr_; - // Assign any non-nullptr value so it doesn't look - // like end()/cend() + // Assign any non-nullptr value so it doesn't look like end() entryPtr_ = reinterpret_cast<hashedEntry*>(this); // Mark with special hashIndex value to signal it has been rewound. @@ -389,19 +418,28 @@ bool Foam::HashTable<T, Key, Hash>::erase(const Key& key) template<class T, class Key, class Hash> Foam::label Foam::HashTable<T, Key, Hash>::erase(const UList<Key>& keys) { - const label nTotal = nElmts_; - label count = 0; + return eraseMultiple(keys.begin(), keys.end()); +} + + +template<class T, class Key, class Hash> +template<unsigned Size> +Foam::label Foam::HashTable<T, Key, Hash>::erase +( + const FixedList<Key, Size>& keys +) +{ + return eraseMultiple(keys.begin(), keys.end()); +} - // Remove listed keys from this table - terminates early if possible - for (label keyI = 0; count < nTotal && keyI < keys.size(); ++keyI) - { - if (erase(keys[keyI])) - { - ++count; - } - } - return count; +template<class T, class Key, class Hash> +Foam::label Foam::HashTable<T, Key, Hash>::erase +( + std::initializer_list<Key> keys +) +{ + return eraseMultiple(keys.begin(), keys.end()); } @@ -409,29 +447,57 @@ template<class T, class Key, class Hash> template<class AnyType, class AnyHash> Foam::label Foam::HashTable<T, Key, Hash>::erase ( - const HashTable<AnyType, Key, AnyHash>& rhs + const HashTable<AnyType, Key, AnyHash>& other ) { - label count = 0; + // Remove other keys from this table + const label nTotal = this->size(); + label changed = 0; - // Remove rhs keys from this table - terminates early if possible - // Could optimize depending on which hash is smaller ... - for (iterator iter = begin(); iter != end(); ++iter) + if (other.size() < nTotal) + { + // other is smaller, use its keys for removal + using other_iter = + typename HashTable<AnyType, Key, AnyHash>::const_iterator; + + for + ( + other_iter iter = other.begin(); + changed < nTotal && iter != other.end(); // terminate early + ++iter + ) + { + if (erase(iter.key())) + { + ++changed; + } + } + } + else { - if (rhs.found(iter.key()) && erase(iter)) + // other is same/larger: iterate ourselves and check for key in other + for + ( + iterator iter = begin(); + changed < nTotal && iter != end(); // terminate early + ++iter + ) { - ++count; + if (other.found(iter.key()) && erase(iter)) + { + ++changed; + } } } - return count; + return changed; } template<class T, class Key, class Hash> void Foam::HashTable<T, Key, Hash>::resize(const label sz) { - label newSize = HashTableCore::canonicalSize(sz); + const label newSize = HashTableCore::canonicalSize(sz); if (newSize == tableSize_) { @@ -449,10 +515,10 @@ void Foam::HashTable<T, Key, Hash>::resize(const label sz) for (const_iterator iter = cbegin(); iter != cend(); ++iter) { - tmpTable->insert(iter.key(), *iter); + tmpTable->insert(iter.key(), iter.object()); } - label oldSize = tableSize_; + const label oldSize = tableSize_; tableSize_ = tmpTable->tableSize_; tmpTable->tableSize_ = oldSize; @@ -480,7 +546,7 @@ void Foam::HashTable<T, Key, Hash>::clear() ep = next; } delete ep; - table_[hashIdx] = 0; + table_[hashIdx] = nullptr; } } nElmts_ = 0; @@ -496,19 +562,6 @@ void Foam::HashTable<T, Key, Hash>::clearStorage() } -template<class T, class Key, class Hash> -void Foam::HashTable<T, Key, Hash>::shrink() -{ - const label newSize = HashTableCore::canonicalSize(nElmts_); - - if (newSize < tableSize_) - { - // Avoid having the table disappear on us - resize(newSize ? newSize : 2); - } -} - - template<class T, class Key, class Hash> void Foam::HashTable<T, Key, Hash>::transfer(HashTable<T, Key, Hash>& ht) { @@ -558,7 +611,7 @@ void Foam::HashTable<T, Key, Hash>::operator= for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter) { - insert(iter.key(), *iter); + insert(iter.key(), iter.object()); } } @@ -572,14 +625,14 @@ void Foam::HashTable<T, Key, Hash>::operator= // Could be zero-sized from a previous transfer() if (!tableSize_) { - resize(lst.size()); + resize(2*lst.size()); } else { clear(); } - for (const Tuple2<Key, T>& pair : lst) + for (const auto& pair : lst) { insert(pair.first(), pair.second()); } @@ -600,9 +653,9 @@ bool Foam::HashTable<T, Key, Hash>::operator== for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter) { - const_iterator fnd = find(iter.key()); + const_iterator other = find(iter.key()); - if (fnd == cend() || fnd() != iter()) + if (!other.found() || other.object() != iter.object()) { return false; } @@ -618,7 +671,7 @@ bool Foam::HashTable<T, Key, Hash>::operator!= const HashTable<T, Key, Hash>& rhs ) const { - return !(operator==(rhs)); + return !operator==(rhs); } diff --git a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H index b2c048879bfbf85c942abce8bb7580198b777bb8..bd60050ee443b517b9faaa4cb29caf923076c863 100644 --- a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H +++ b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H @@ -34,9 +34,17 @@ Note often result in a different hash order. Use a sorted table-of-contents when the hash order is important. + The end iterator of all hash-tables has a nullptr to the hash entry. + Thus avoid separate allocation for each table and use a single one with + a nullptr. The hash-table iterators always have an entry-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 HashTableI.H HashTable.C + HashTableCore.C HashTableIO.C \*---------------------------------------------------------------------------*/ @@ -49,6 +57,8 @@ SourceFiles #include "word.H" #include "Xfer.H" #include "className.H" +#include "nullObject.H" + #include <initializer_list> // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -60,32 +70,30 @@ namespace Foam template<class T> class List; template<class T> class UList; +template<class T, unsigned Size> class FixedList; +template<class T1, class T2> class Tuple2; template<class T, class Key, class Hash> class HashTable; -template<class T, class Key, class Hash> class HashPtrTable; - -template<class Type1, class Type2> -class Tuple2; template<class T, class Key, class Hash> Istream& operator>>(Istream& is, HashTable<T, Key, Hash>& L); template<class T, class Key, class Hash> -Ostream& operator<<(Ostream& os, const HashTable<T, Key, Hash>& L); +Ostream& operator<<(Ostream& os, const HashTable<T, Key, Hash>& tbl); /*---------------------------------------------------------------------------*\ - Class HashTableCore Declaration + Class HashTableCore Declaration \*---------------------------------------------------------------------------*/ -//- Template-invariant bits for HashTable +//- Bits that are independent of the HashTable template parameters. struct HashTableCore { - //- Return a canonical (power-of-two) size - static label canonicalSize(const label size); - - //- Maximum allowable table size + //- Maximum allowable internal table size. Approximately labelMax/4 static const label maxTableSize; + //- Return a canonical (power-of-two) of the requested size. + static label canonicalSize(const label requested_size); + //- Construct null HashTableCore() {} @@ -93,25 +101,57 @@ struct HashTableCore //- Define template name and debug ClassName("HashTable"); - //- A zero-sized end iterator - struct iteratorEnd - { - //- Construct null - iteratorEnd() - {} - }; + static_assert + ( + sizeof(NullObject) >= sizeof(void*), + "NullObject is too small to reinterpret_cast as HashTable::iterator" + ); - //- iteratorEnd set to beyond the end of any HashTable - inline static iteratorEnd cend() - { - return iteratorEnd(); - } - //- iteratorEnd set to beyond the end of any HashTable - inline static iteratorEnd end() + //- Factory method to create a non-const iterator begin + template<class IteratorType, class TableType> + inline static IteratorType iterator_begin(TableType& table); + + //- Factory method to create a const iterator begin + template<class IteratorType, class TableType> + inline static IteratorType iterator_begin(const TableType& table); + + //- Factory method to create a const iterator begin + template<class IteratorType, class TableType> + inline static IteratorType iterator_cbegin(const TableType& table); + + //- Factory method to create a non-const iterator end + // Simply reinterprets a NullObject as a hash-table iterator. + template<class IteratorType> + inline static const IteratorType& iterator_end(); + + //- Factory method to create a const iterator cend + // Simply reinterprets a NullObject as a hash-table iterator. + template<class IteratorType> + inline static const IteratorType& iterator_cend(); + + + //- Factory class for creating a begin/end pair for any const iterator. + template<class IteratorType, class TableType> + class const_iterator_pair { - return iteratorEnd(); - } + label size_; + IteratorType iter_; + + public: + + inline const_iterator_pair(const TableType& tbl); + + inline label size() const; + inline bool empty() const; + + inline IteratorType begin() const; + inline IteratorType cbegin() const; + + inline const IteratorType& end() const; + inline const IteratorType& cend() const; + }; + }; @@ -124,23 +164,59 @@ class HashTable : public HashTableCore { +public: + + //- The template instance used for this HashTable + typedef HashTable<T, Key, Hash> this_type; + + + // STL type definitions + + //- Type of keys that the HashTable uses. + typedef Key key_type; + + //- Type of values that the HashTable contains. + typedef T value_type; + + //- The type used for storing into value_type objects. + // This type is usually value_type&. + typedef T& reference; + + //- The type used for reading from constant value_type objects. + typedef const T& const_reference; + + //- The type to represent the difference between two iterators + typedef label difference_type; + + //- The type that can represent the size of a HashTable. + typedef label size_type; + + + //- Forward iterator with non-const access + class iterator; + + //- Forward iterator with const access + class const_iterator; + + +private: + // Private data type for table entries - //- Structure to hold a hashed entry with SLList for collisions + //- Structure to hold a hashed entry, with a SLList for collisions struct hashedEntry { //- The lookup key Key key_; - //- Pointer to next hashedEntry in sub-list - hashedEntry* next_; - //- The data object T obj_; - //- Construct from key, next pointer and object - inline hashedEntry(const Key&, hashedEntry* next, const T&); + //- Pointer to next hashedEntry in sub-list + hashedEntry* next_; + //- Construct from key, next pointer and object + inline hashedEntry(const Key& key, const T& obj, hashedEntry* next); private: //- Disallow default bitwise copy construct @@ -169,31 +245,32 @@ class HashTable // No checks for zero-sized tables. inline label hashKeyIndex(const Key& key) const; - //- Assign a new hashedEntry to a possibly already existing key + //- Assign a new hash-entry to a possibly already existing key. + // Return true if the new entry was set. bool set(const Key& key, const T& newEntry, const bool protect); -public: +protected: - // Forward declaration of iterators + //- Internally used base for iterator and const_iterator + class iterator_base; - class iteratorBase; - class iterator; - class const_iterator; + //- Friendship with the iterator_base is required. + friend class iterator_base; - //- Declare friendship with the HashPtrTable class - template<class T2, class Key2, class Hash2> - friend class HashPtrTable; - //- Declare friendship with the iteratorBase - friend class iteratorBase; + // Protected Member Functions - //- Declare friendship with the iterator - friend class iterator; + //- Remove using begin/end iterators of listed keys + template<class InputIter> + inline label eraseMultiple + ( + const InputIter begIter, + const InputIter endIter + ); - //- Declare friendship with the const_iterator - friend class const_iterator; +public: // Constructors @@ -201,7 +278,7 @@ public: HashTable(const label size = 128); //- Construct from Istream - HashTable(Istream&, const label size = 128); + HashTable(Istream& is, const label size = 128); //- Construct as copy HashTable(const HashTable<T, Key, Hash>& ht); @@ -219,83 +296,89 @@ public: // Member Functions - // Access + // Access - //- The size of the underlying table - inline label capacity() const; + //- The size of the underlying table + inline label capacity() const; - //- Return number of elements in table - inline label size() const; + //- Return number of elements in table + inline label size() const; - //- Return true if the hash table is empty - inline bool empty() const; + //- Return true if the hash table is empty + inline bool empty() const; - //- Return true if hashedEntry is found in table - bool found(const Key& key) const; + //- Return true if hashedEntry is found in table + bool found(const Key& key) const; - //- Find and return an iterator set at the hashedEntry - // If not found iterator = end() - iterator find(const Key& key); + //- Find and return an iterator set at the hashedEntry + // If not found iterator = end() + iterator find(const Key& key); - //- Find and return an const_iterator set at the hashedEntry - // If not found iterator = end() - const_iterator find(const Key& key) const; + //- Find and return an const_iterator set at the hashedEntry + // If not found iterator = end() + const_iterator find(const Key& key) const; - //- Return the table of contents - List<Key> toc() const; + //- Return the table of contents + List<Key> toc() const; - //- Return the table of contents as a sorted list - List<Key> sortedToc() const; + //- Return the table of contents as a sorted list + List<Key> sortedToc() const; - //- Print information - Ostream& printInfo(Ostream& os) const; + // Edit - // Edit + //- Insert a new hashedEntry + // Return true if the entry inserted, which means that it did + // not previously exist in the table. + inline bool insert(const Key& key, const T& newEntry); - //- Insert a new hashedEntry - inline bool insert(const Key& key, const T& newEntry); + //- Assign a new hashedEntry, overwriting existing entries. + // Returns true. + inline bool set(const Key& key, const T& newEntry); - //- Assign a new hashedEntry, overwriting existing entries - inline bool set(const Key& key, const T& newEntry); + //- Erase a hashedEntry specified by given iterator + // This invalidates the iterator until the next ++ operation + bool erase(const iterator& iter); - //- Erase a hashedEntry specified by given iterator - // This invalidates the iterator until the next operator++ - bool erase(const iterator& iter); + //- Erase a hashedEntry specified by the given key + bool erase(const Key& key); - //- Erase a hashedEntry specified by the given key - bool erase(const Key& key); + //- Remove entries given by the listed keys from this HashTable + // Return the number of elements removed + label erase(const UList<Key>& keys); - //- Remove entries given by the listed keys from this HashTable - // Return the number of elements removed - label erase(const UList<Key>& keys); + //- Remove entries given by the listed keys from this HashTable + // Return the number of elements removed + template<unsigned Size> + label erase(const FixedList<Key, Size>& keys); - //- Remove entries given by the given keys from this HashTable - // Return the number of elements removed. - // The parameter HashTable needs the same type of key, but the - // type of values held and the hashing function are arbitrary. - template<class AnyType, class AnyHash> - label erase(const HashTable<AnyType, Key, AnyHash>& rhs); + //- Remove entries given by the listed keys from this HashTable + // Return the number of elements removed + label erase(std::initializer_list<Key> keys); - //- Resize the hash table for efficiency - void resize(const label sz); + //- Remove entries given by the given keys from this HashTable + // Return the number of elements removed. + // The parameter HashTable needs the same type of key, but the + // type of values held and the hashing function are arbitrary. + template<class AnyType, class AnyHash> + label erase(const HashTable<AnyType, Key, AnyHash>& other); - //- Clear all entries from table - void clear(); + //- Resize the hash table for efficiency + void resize(const label sz); - //- Clear the table entries and the table itself. - // Equivalent to clear() followed by resize(0) - void clearStorage(); + //- Clear all entries from table + void clear(); - //- Shrink the allocated table to approx. twice number of elements - void shrink(); + //- Clear the table entries and the table itself. + // Equivalent to clear() followed by resize(0) + void clearStorage(); - //- Transfer the contents of the argument table into this table - // and annul the argument table. - void transfer(HashTable<T, Key, Hash>& ht); + //- Transfer the contents of the argument table into this table + // and annul the argument table. + void transfer(HashTable<T, Key, Hash>& ht); - //- Transfer contents to the Xfer container - inline Xfer<HashTable<T, Key, Hash>> xfer(); + //- Transfer contents to the Xfer container + inline Xfer<HashTable<T, Key, Hash>> xfer(); // Member Operators @@ -306,13 +389,15 @@ public: //- Find and return a hashedEntry inline const T& operator[](const Key& key) const; - //- Find and return a hashedEntry, create it null if not present + //- Return existing entry or create a new entry. + // A newly created entry is created as a nameless T() and is thus + // value-initialized. For primitives, this will be zero. inline T& operator()(const Key& key); //- Assignment void operator=(const HashTable<T, Key, Hash>& rhs); - //- Assignment to an initializer list + //- Assignment from an initializer list void operator=(std::initializer_list<Tuple2<Key, T>> lst); //- Equality. Hash tables are equal if the keys and values are equal. @@ -323,219 +408,256 @@ public: bool operator!=(const HashTable<T, Key, Hash>& rhs) const; - - // STL type definitions - - //- Type of values the HashTable contains. - typedef T value_type; - - //- Type that can be used for storing into HashTable::value_type - // objects. This type is usually List::value_type&. - typedef T& reference; - - //- Type that can be used for storing into constant - // HashTable::value_type objects. This type is usually const - // HashTable::value_type&. - typedef const T& const_reference; - - //- The type that can represent the size of a HashTable. - typedef label size_type; - +protected: // Iterators and helpers - //- The iterator base for HashTable + //- The iterator base for HashTable (internal use only). // Note: data and functions are protected, to allow reuse by iterator // and prevent most external usage. // iterator and const_iterator have the same size, allowing // us to reinterpret_cast between them (if desired) - class iteratorBase + class iterator_base { - // Private Data - - //- Pointer to the HashTable for which this is an iterator - // This allows use of the default bitwise copy/assignment - HashTable<T, Key, Hash>* hashTable_; - - //- Current element - hashedEntry* entryPtr_; - - //- Current hash index - label hashIndex_; + public: + // Public typedefs + using table_type = this_type; + using key_type = this_type::key_type; + using difference_type = this_type::difference_type; + private: + using entry_type = hashedEntry; - protected: + // Private Data - // Constructors + //- Currently selected entry. + // MUST be the first member for easy comparison between iterators + // and for reinterpret_cast from nullObject + entry_type* entryPtr_; - //- Construct null - equivalent to an 'end' position - inline iteratorBase(); + //- Pointer to the hash-table for which this is an iterator + // This allows use of the default bitwise copy/assignment + table_type* hashTable_; - //- Construct from hash table, moving to its 'begin' position - inline explicit iteratorBase - ( - const HashTable<T, Key, Hash>* hashTbl - ); + //- Current hash index within the hash-table data. + // A signed value, since erase() uses a negative value to signal + // the erasure state. + label hashIndex_; - //- Construct from hash table, element and hash index - inline iteratorBase - ( - const HashTable<T, Key, Hash>* hashTbl, - const hashedEntry* elmt, - const label hashIndex - ); + protected: + // Protected Member Functions - // Protected Member Functions + //- Increment to the next position + inline void increment(); - //- Increment to the next position - inline void increment(); + //- The referenced object/value element + inline T& element() const; - //- Erase the HashTable element at the current position - bool erase(); + //- Erase the entry at the current position + bool erase(); - //- Return non-const access to referenced object - inline T& object(); public: - // Member operators + // Constructors + + //- Construct null (end iterator) + inline iterator_base(); - // Access + //- Construct from begin of hash-table + inline explicit iterator_base(const table_type* hashTbl); - //- True if iterator points to a hashedEntry. - // This can be used instead of a comparison to end() - inline bool found() const; + //- Construct from hash table, element and hash index + inline iterator_base + ( + const table_type* hashTbl, + const entry_type* elmt, + const label hashIndex + ); - //- Return the Key corresponding to the iterator - inline const Key& key() const; + // Member functions/operators - //- Return const access to referenced object - inline const T& cobject() const; + //- True if iterator points to an entry + // This can be used directly instead of comparing to end() + inline bool found() const; - //- Compare hashedEntry element pointers - inline bool operator==(const iteratorBase& iter) const; - inline bool operator!=(const iteratorBase& iter) const; + //- Return the Key corresponding to the iterator + inline const Key& key() const; - //- Compare hashedEntry to iteratorEnd pointers - inline bool operator==(const iteratorEnd& unused) const; - inline bool operator!=(const iteratorEnd& unused) const; + //- Compare hash-entry element pointers + inline bool operator==(const iterator_base& iter) const; + inline bool operator!=(const iterator_base& iter) const; }; - //- An STL-conforming iterator - class iterator +public: + + //- An iterator wrapper for returning a reference to the key + template<class WrappedIterator> + class key_iterator_base : - public iteratorBase + public WrappedIterator { - friend class HashTable; + public: + using reference = const Key&; + using difference_type = typename WrappedIterator::difference_type; - // Private Member Functions + //- Implicit conversion + inline key_iterator_base(const WrappedIterator& iter); - //- Construct from hash table, moving to its 'begin' position - inline explicit iterator - ( - HashTable<T, Key, Hash>* hashTbl - ); + //- Return the key + inline reference operator*() const; + }; - //- Construct from hash table, element and hash index - inline iterator - ( - HashTable<T, Key, Hash>* hashTbl, - hashedEntry* elmt, - const label hashIndex - ); + // STL iterator + + //- Forward iterator with non-const access + class iterator + : + public iterator_base + { + friend class HashTable; // Uses iterator::erase() method + using entry_type = hashedEntry; + public: - // Constructors + // Public typedefs + using table_type = this_type; + using key_type = this_type::key_type; + using reference = this_type::reference; + using difference_type = typename iterator_base::difference_type; - //- Construct null (end iterator) - inline iterator(); + // Constructors - //- Construct end iterator - inline iterator(const iteratorEnd& unused); + //- Construct null (end iterator) + inline iterator(); + //- Construct from begin of hash-table + inline explicit iterator(table_type* hashTbl); - // Member operators + //- Construct from hash table, element and hash index + // Used by the hash-table find() method. + inline iterator + ( + table_type* hashTbl, + entry_type* elmt, + const label hashIndex + ); - //- Return non-const access to referenced object - using iteratorBase::object; + // Member functions/operators - //- Return non-const access to referenced object - inline T& operator*(); - inline T& operator()(); + //- Return non-const access to referenced object + inline reference object() const; - //- Return const access to referenced object - inline const T& operator*() const; - inline const T& operator()() const; + //- Return non-const access to referenced object + inline reference operator*() const; + inline reference operator()() const; - inline iterator& operator++(); - inline iterator operator++(int); + inline iterator& operator++(); + inline iterator operator++(int); }; - //- Iterator set to the beginning of the HashTable - inline iterator begin(); - // STL const_iterator - //- An STL-conforming const_iterator + //- Forward iterator with const access class const_iterator : - public iteratorBase + public iterator_base { - friend class HashTable; + using entry_type = const hashedEntry; - // Private Member Functions + public: - //- Construct from hash table, moving to its 'begin' position - inline explicit const_iterator - ( - const HashTable<T, Key, Hash>* hashTbl - ); + // Public typedefs + using table_type = const this_type; + using key_type = this_type::key_type; + using reference = this_type::const_reference; + using difference_type = typename iterator_base::difference_type; - //- Construct from hash table, element and hash index - inline const_iterator - ( - const HashTable<T, Key, Hash>* hashTbl, - const hashedEntry* elmt, - const label hashIndex - ); + // Constructors + //- Construct null (end iterator) + inline const_iterator(); - public: + //- Construct from begin of hash-table + inline explicit const_iterator(table_type* hashTbl); - // Constructors + //- Construct from hash table, element and hash index. + // Used by the hash-table find() method. + inline const_iterator + ( + table_type* hashTbl, + entry_type* elmt, + const label hashIndex + ); - //- Construct null (end iterator) - inline const_iterator(); + //- Copy construct from iterator + inline const_iterator(const iterator& iter); - //- Construct from iterator - inline const_iterator(const iterator& iter); + // Member functions/operators - //- Construct end iterator - inline const_iterator(const iteratorEnd& unused); + //- Return const access to referenced object + inline reference object() const; + //- Return const access to referenced object + inline reference operator*() const; + inline reference operator()() const; - // Member operators + inline const_iterator& operator++(); + inline const_iterator operator++(int); + }; - //- Return const access to referenced object - inline const T& operator*() const; - inline const T& operator()() const; - inline const_iterator& operator++(); - inline const_iterator operator++(int); - }; + //- Iterating over keys only + //- Forward iterator returning the key + using key_iterator = key_iterator_base<iterator>; + + //- Forward const iterator returning the key + using const_key_iterator = key_iterator_base<const_iterator>; + + //- A const iterator begin/end pair for iterating over keys + const_iterator_pair<const_key_iterator, this_type> keys() const + { + return + const_iterator_pair<const_key_iterator,this_type>(*this); + } - //- const_iterator set to the beginning of the HashTable - inline const_iterator cbegin() const; + + // Iterator access + + //- Iterator set to the beginning of the HashTable + inline iterator begin(); //- const_iterator set to the beginning of the HashTable inline const_iterator begin() const; + //- const_iterator set to the beginning of the HashTable + inline const_iterator cbegin() const; + + //- iterator to signal the end for any HashTable + inline const iterator& end(); + + //- const_iterator to signal the end for any HashTable + inline const const_iterator& end() const; + + //- const_iterator to signal the end for any HashTable + inline const const_iterator& cend() const; + + + // Writing + + //- Print information + Ostream& printInfo(Ostream& os) const; + + //- Write the unordered keys as a list, with line-breaks if list length + // exceeds shortListLen. Using '0' suppresses line-breaks entirely. + Ostream& writeKeys(Ostream& os, const label shortListLen=0) const; + // IOstream Operator @@ -548,7 +670,7 @@ public: friend Ostream& operator<< <T, Key, Hash> ( Ostream& os, - const HashTable<T, Key, Hash>& L + const HashTable<T, Key, Hash>& tbl ); }; @@ -559,7 +681,8 @@ public: // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - #include "HashTableI.H" +#include "HashTableCoreI.H" +#include "HashTableI.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/OpenFOAM/containers/HashTables/HashTable/HashTableCore.C b/src/OpenFOAM/containers/HashTables/HashTable/HashTableCore.C index 58fcdf27b1c9d0146891edf4a16b9f1503897ad5..e57192f1e36a2dbf26979f738d6290e6cd732666 100644 --- a/src/OpenFOAM/containers/HashTables/HashTable/HashTableCore.C +++ b/src/OpenFOAM/containers/HashTables/HashTable/HashTableCore.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2012 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -33,38 +33,53 @@ namespace Foam defineTypeNameAndDebug(HashTableCore, 0); } -const Foam::label Foam::HashTableCore::maxTableSize -( - Foam::HashTableCore::canonicalSize - ( - Foam::labelMax/2 - ) -); + +// Approximately labelMax/4 +const Foam::label Foam::HashTableCore::maxTableSize(1 << (sizeof(label)*8-3)); // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * // -Foam::label Foam::HashTableCore::canonicalSize(const label size) +Foam::label Foam::HashTableCore::canonicalSize(const label requested_size) { - if (size < 1) + if (requested_size < 1) { return 0; } - // enforce power of two - uLabel goodSize = size; + // Enforce power of two - makes for a vey fast modulus etc. + // Use unsigned for these calculations. + // + // - The lower limit (8) is somewhat arbitrary, but if the hash table + // is too small, there will be many direct table collisions. + // - The uper limit (approx. labelMax/4) must be a power of two, + // need not be extremely large for hashing. + + uLabel powerOfTwo = 8; // lower-limit - if (goodSize & (goodSize - 1)) + const uLabel size = requested_size; + if (size < powerOfTwo) + { + return powerOfTwo; + } + else if (requested_size >= maxTableSize) + { + return maxTableSize; + } + else if (size & (size-1)) // <- Modulus of i^2 { - // brute-force is fast enough - goodSize = 1; - while (goodSize < unsigned(size)) + // Determine power-of-two. Brute-force is fast enough. + while (powerOfTwo < size) { - goodSize <<= 1; + powerOfTwo <<= 1; } - } - return goodSize; + return powerOfTwo; + } + else + { + return size; + } } diff --git a/src/OpenFOAM/containers/HashTables/HashTable/HashTableCoreI.H b/src/OpenFOAM/containers/HashTables/HashTable/HashTableCoreI.H new file mode 100644 index 0000000000000000000000000000000000000000..8198e2e4c33d4bcbfd99c820c9cc0dd8f2b853a5 --- /dev/null +++ b/src/OpenFOAM/containers/HashTables/HashTable/HashTableCoreI.H @@ -0,0 +1,146 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / 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/>. + +\*---------------------------------------------------------------------------*/ + +// * * * * * * * * * * * * * * * helper methods * * * * * * * * * * * * * * // + +template<class IteratorType, class TableType> +inline IteratorType Foam::HashTableCore::iterator_begin +( + TableType& table +) +{ + return IteratorType(table.begin()); +} + + +template<class IteratorType, class TableType> +inline IteratorType Foam::HashTableCore::iterator_begin +( + const TableType& table +) +{ + return IteratorType(table.begin()); +} + + +template<class IteratorType, class TableType> +inline IteratorType Foam::HashTableCore::iterator_cbegin +( + const TableType& table +) +{ + return IteratorType(table.cbegin()); +} + + +template<class IteratorType> +inline const IteratorType& Foam::HashTableCore::iterator_end() +{ + return *reinterpret_cast<const IteratorType*>(nullObjectPtr); +} + + +template<class IteratorType> +inline const IteratorType& Foam::HashTableCore::iterator_cend() +{ + return *reinterpret_cast<const IteratorType*>(nullObjectPtr); +} + + +// * * * * * * * * * * * * * const iterator pair * * * * * * * * * * * * * * // + +template<class IteratorType, class TableType> +inline Foam::HashTableCore::const_iterator_pair<IteratorType, TableType> +::const_iterator_pair +( + const TableType& tbl +) +: + size_(tbl.size()), + iter_(tbl.begin()) +{} + + +template<class IteratorType, class TableType> +inline Foam::label +Foam::HashTableCore::const_iterator_pair<IteratorType, TableType>::size() const +{ + return size_; +} + + +template<class IteratorType, class TableType> +inline bool +Foam::HashTableCore::const_iterator_pair<IteratorType, TableType>::empty() const +{ + return !size_; +} + + +template<class IteratorType, class TableType> +inline IteratorType Foam::HashTableCore::const_iterator_pair +< + IteratorType, + TableType +>::begin() const +{ + return iter_; +} + + +template<class IteratorType, class TableType> +inline IteratorType Foam::HashTableCore::const_iterator_pair +< + IteratorType, + TableType +>::cbegin() const +{ + return iter_; +} + + +template<class IteratorType, class TableType> +inline const IteratorType& Foam::HashTableCore::const_iterator_pair +< + IteratorType, + TableType +>::end() const +{ + return HashTableCore::iterator_cend<IteratorType>(); +} + + +template<class IteratorType, class TableType> +inline const IteratorType& Foam::HashTableCore::const_iterator_pair +< + IteratorType, + TableType +>::cend() const +{ + return HashTableCore::iterator_cend<IteratorType>(); +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/containers/HashTables/HashTable/HashTableI.H b/src/OpenFOAM/containers/HashTables/HashTable/HashTableI.H index 6246312021bf975fb698ed16628c9a9b702e2f50..f8df2437591dfafd274ff86cb9867d29d113767e 100644 --- a/src/OpenFOAM/containers/HashTables/HashTable/HashTableI.H +++ b/src/OpenFOAM/containers/HashTables/HashTable/HashTableI.H @@ -31,13 +31,13 @@ template<class T, class Key, class Hash> inline Foam::HashTable<T, Key, Hash>::hashedEntry::hashedEntry ( const Key& key, - hashedEntry* next, - const T& obj + const T& obj, + hashedEntry* next ) : key_(key), - next_(next), - obj_(obj) + obj_(obj), + next_(next) {} @@ -112,7 +112,7 @@ inline T& Foam::HashTable<T, Key, Hash>::operator[](const Key& key) { iterator iter = this->find(key); - if (iter == this->end()) + if (!iter.found()) { FatalErrorInFunction << key << " not found in table. Valid entries: " @@ -120,7 +120,7 @@ inline T& Foam::HashTable<T, Key, Hash>::operator[](const Key& key) << exit(FatalError); } - return *iter; + return iter.object(); } @@ -129,7 +129,7 @@ inline const T& Foam::HashTable<T, Key, Hash>::operator[](const Key& key) const { const_iterator iter = this->find(key); - if (iter == this->cend()) + if (!iter.found()) { FatalErrorInFunction << key << " not found in table. Valid entries: " @@ -137,7 +137,7 @@ inline const T& Foam::HashTable<T, Key, Hash>::operator[](const Key& key) const << exit(FatalError); } - return *iter; + return iter.object(); } @@ -146,40 +146,52 @@ inline T& Foam::HashTable<T, Key, Hash>::operator()(const Key& key) { iterator iter = this->find(key); - if (iter == this->end()) + if (iter.found()) { - this->insert(key, T()); - return *find(key); - } - else - { - return *iter; + return iter.object(); } + + this->insert(key, T()); + return find(key).object(); } // * * * * * * * * * * * * * * * iterator base * * * * * * * * * * * * * * * // template<class T, class Key, class Hash> -inline Foam::HashTable<T, Key, Hash>::iteratorBase::iteratorBase() +inline Foam::HashTable<T, Key, Hash>::iterator_base::iterator_base() : - hashTable_(0), - entryPtr_(0), + entryPtr_(nullptr), + hashTable_(nullptr), hashIndex_(0) {} template<class T, class Key, class Hash> -inline Foam::HashTable<T, Key, Hash>::iteratorBase::iteratorBase +inline Foam::HashTable<T, Key, Hash>::iterator_base::iterator_base +( + const table_type* hashTbl, + const entry_type* elmt, + const label hashIndex +) +: + entryPtr_(const_cast<entry_type*>(elmt)), + hashTable_(const_cast<table_type*>(hashTbl)), + hashIndex_(hashIndex) +{} + + +template<class T, class Key, class Hash> +inline Foam::HashTable<T, Key, Hash>::iterator_base::iterator_base ( - const HashTable<T, Key, Hash>* hashTbl + const table_type* hashTbl ) : - hashTable_(const_cast<HashTable<T, Key, Hash>*>(hashTbl)), - entryPtr_(0), + entryPtr_(nullptr), + hashTable_(const_cast<table_type*>(hashTbl)), hashIndex_(0) { - if (hashTable_->nElmts_) + if (hashTable_ && hashTable_->nElmts_) { // find first non-nullptr table entry while @@ -192,30 +204,16 @@ inline Foam::HashTable<T, Key, Hash>::iteratorBase::iteratorBase if (hashIndex_ >= hashTable_->tableSize_) { // make into an end iterator - entryPtr_ = 0; + entryPtr_ = nullptr; hashIndex_ = 0; } } } -template<class T, class Key, class Hash> -inline Foam::HashTable<T, Key, Hash>::iteratorBase::iteratorBase -( - const HashTable<T, Key, Hash>* hashTbl, - const hashedEntry* elmt, - const label hashIndex -) -: - hashTable_(const_cast<HashTable<T, Key, Hash>*>(hashTbl)), - entryPtr_(const_cast<hashedEntry*>(elmt)), - hashIndex_(hashIndex) -{} - - template<class T, class Key, class Hash> inline void -Foam::HashTable<T, Key, Hash>::iteratorBase::increment() +Foam::HashTable<T, Key, Hash>::iterator_base::increment() { // A negative index is a special value from erase if (hashIndex_ < 0) @@ -251,7 +249,7 @@ Foam::HashTable<T, Key, Hash>::iteratorBase::increment() if (hashIndex_ >= hashTable_->tableSize_) { // make into an end iterator - entryPtr_ = 0; + entryPtr_ = nullptr; hashIndex_ = 0; } } @@ -259,40 +257,30 @@ Foam::HashTable<T, Key, Hash>::iteratorBase::increment() template<class T, class Key, class Hash> inline bool -Foam::HashTable<T, Key, Hash>::iteratorBase::found() const +Foam::HashTable<T, Key, Hash>::iterator_base::found() const { return entryPtr_; } template<class T, class Key, class Hash> -inline -const Key& Foam::HashTable<T, Key, Hash>::iteratorBase::key() const +inline const Key& Foam::HashTable<T, Key, Hash>::iterator_base::key() const { return entryPtr_->key_; } template<class T, class Key, class Hash> -inline T& -Foam::HashTable<T, Key, Hash>::iteratorBase::object() -{ - return entryPtr_->obj_; -} - - -template<class T, class Key, class Hash> -inline const T& -Foam::HashTable<T, Key, Hash>::iteratorBase::cobject() const +inline T& Foam::HashTable<T, Key, Hash>::iterator_base::element() const { return entryPtr_->obj_; } template<class T, class Key, class Hash> -inline bool Foam::HashTable<T, Key, Hash>::iteratorBase::operator== +inline bool Foam::HashTable<T, Key, Hash>::iterator_base::operator== ( - const iteratorBase& iter + const iterator_base& iter ) const { return entryPtr_ == iter.entryPtr_; @@ -300,32 +288,36 @@ inline bool Foam::HashTable<T, Key, Hash>::iteratorBase::operator== template<class T, class Key, class Hash> -inline bool Foam::HashTable<T, Key, Hash>::iteratorBase::operator!= +inline bool Foam::HashTable<T, Key, Hash>::iterator_base::operator!= ( - const iteratorBase& iter + const iterator_base& iter ) const { return entryPtr_ != iter.entryPtr_; } +// * * * * * * * * * * * * * * key iterator base * * * * * * * * * * * * * * // + template<class T, class Key, class Hash> -inline bool Foam::HashTable<T, Key, Hash>::iteratorBase::operator== +template<class WrappedIterator> +inline Foam::HashTable<T, Key, Hash>::key_iterator_base<WrappedIterator> +::key_iterator_base ( - const iteratorEnd& -) const -{ - return !entryPtr_; -} + const WrappedIterator& iter +) +: + WrappedIterator(iter) +{} template<class T, class Key, class Hash> -inline bool Foam::HashTable<T, Key, Hash>::iteratorBase::operator!= -( - const iteratorEnd& -) const +template<class WrappedIterator> +inline const Key& +Foam::HashTable<T, Key, Hash>::key_iterator_base<WrappedIterator> +::operator*() const { - return entryPtr_; + return this->key(); } @@ -334,77 +326,58 @@ inline bool Foam::HashTable<T, Key, Hash>::iteratorBase::operator!= template<class T, class Key, class Hash> inline Foam::HashTable<T, Key, Hash>::iterator::iterator() : - iteratorBase() + iterator_base() {} template<class T, class Key, class Hash> inline Foam::HashTable<T, Key, Hash>::iterator::iterator ( - const iteratorEnd& + table_type* hashTbl ) : - iteratorBase() + iterator_base(hashTbl) {} template<class T, class Key, class Hash> inline Foam::HashTable<T, Key, Hash>::iterator::iterator ( - HashTable<T, Key, Hash>* hashTbl -) -: - iteratorBase(hashTbl) -{} - - -template<class T, class Key, class Hash> -inline Foam::HashTable<T, Key, Hash>::iterator::iterator -( - HashTable<T, Key, Hash>* hashTbl, - hashedEntry* elmt, + table_type* hashTbl, + entry_type* elmt, const label hashIndex ) : - iteratorBase(hashTbl, elmt, hashIndex) + iterator_base(hashTbl, elmt, hashIndex) {} template<class T, class Key, class Hash> inline T& -Foam::HashTable<T, Key, Hash>::iterator::operator*() +Foam::HashTable<T, Key, Hash>::iterator::object() const { - return this->object(); + return this->element(); } template<class T, class Key, class Hash> inline T& -Foam::HashTable<T, Key, Hash>::iterator::operator()() -{ - return this->object(); -} - - -template<class T, class Key, class Hash> -inline const T& Foam::HashTable<T, Key, Hash>::iterator::operator*() const { - return this->cobject(); + return this->object(); } template<class T, class Key, class Hash> -inline const T& +inline T& Foam::HashTable<T, Key, Hash>::iterator::operator()() const { - return this->cobject(); + return this->object(); } template<class T, class Key, class Hash> -inline -typename Foam::HashTable<T, Key, Hash>::iterator& +inline typename Foam::HashTable<T, Key, Hash>::iterator& Foam::HashTable<T, Key, Hash>::iterator::operator++() { this->increment(); @@ -422,20 +395,12 @@ Foam::HashTable<T, Key, Hash>::iterator::operator++(int) } -template<class T, class Key, class Hash> -inline typename Foam::HashTable<T, Key, Hash>::iterator -Foam::HashTable<T, Key, Hash>::begin() -{ - return iterator(this); -} - - -// * * * * * * * * * * * * * * * STL const_iterator * * * * * * * * * * * * * // +// * * * * * * * * * * * * * * * STL const_iterator * * * * * * * * * * * * // template<class T, class Key, class Hash> inline Foam::HashTable<T, Key, Hash>::const_iterator::const_iterator() : - iteratorBase() + iterator_base() {} @@ -445,47 +410,45 @@ inline Foam::HashTable<T, Key, Hash>::const_iterator::const_iterator const HashTable<T, Key, Hash>::iterator& iter ) : - iteratorBase(iter) + iterator_base(iter) {} template<class T, class Key, class Hash> inline Foam::HashTable<T, Key, Hash>::const_iterator::const_iterator ( - const iteratorEnd& + table_type* hashTbl ) : - iteratorBase() + iterator_base(hashTbl) {} template<class T, class Key, class Hash> inline Foam::HashTable<T, Key, Hash>::const_iterator::const_iterator ( - const HashTable<T, Key, Hash>* hashTbl + table_type* hashTbl, + entry_type* elmt, + const label hashIndex ) : - iteratorBase(hashTbl) + iterator_base(hashTbl, elmt, hashIndex) {} template<class T, class Key, class Hash> -inline Foam::HashTable<T, Key, Hash>::const_iterator::const_iterator -( - const HashTable<T, Key, Hash>* hashTbl, - const hashedEntry* elmt, - const label hashIndex -) -: - iteratorBase(hashTbl, elmt, hashIndex) -{} +inline const T& +Foam::HashTable<T, Key, Hash>::const_iterator::object() const +{ + return this->element(); +} template<class T, class Key, class Hash> inline const T& Foam::HashTable<T, Key, Hash>::const_iterator::operator*() const { - return this->cobject(); + return this->object(); } @@ -493,13 +456,12 @@ template<class T, class Key, class Hash> inline const T& Foam::HashTable<T, Key, Hash>::const_iterator::operator()() const { - return this->cobject(); + return this->object(); } template<class T, class Key, class Hash> -inline -typename Foam::HashTable<T, Key, Hash>::const_iterator& +inline typename Foam::HashTable<T, Key, Hash>::const_iterator& Foam::HashTable<T, Key, Hash>::const_iterator::operator++() { this->increment(); @@ -517,9 +479,19 @@ Foam::HashTable<T, Key, Hash>::const_iterator::operator++(int) } +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +template<class T, class Key, class Hash> +inline typename Foam::HashTable<T, Key, Hash>::iterator +Foam::HashTable<T, Key, Hash>::begin() +{ + return iterator(this); +} + + template<class T, class Key, class Hash> inline typename Foam::HashTable<T, Key, Hash>::const_iterator -Foam::HashTable<T, Key, Hash>::cbegin() const +Foam::HashTable<T, Key, Hash>::begin() const { return const_iterator(this); } @@ -527,9 +499,33 @@ Foam::HashTable<T, Key, Hash>::cbegin() const template<class T, class Key, class Hash> inline typename Foam::HashTable<T, Key, Hash>::const_iterator -Foam::HashTable<T, Key, Hash>::begin() const +Foam::HashTable<T, Key, Hash>::cbegin() const +{ + return const_iterator(this); +} + + +template<class T, class Key, class Hash> +inline const typename Foam::HashTable<T, Key, Hash>::iterator& +Foam::HashTable<T, Key, Hash>::end() +{ + return iterator_end<iterator>(); +} + + +template<class T, class Key, class Hash> +inline const typename Foam::HashTable<T, Key, Hash>::const_iterator& +Foam::HashTable<T, Key, Hash>::end() const +{ + return iterator_end<const_iterator>(); +} + + +template<class T, class Key, class Hash> +inline const typename Foam::HashTable<T, Key, Hash>::const_iterator& +Foam::HashTable<T, Key, Hash>::cend() const { - return this->cbegin(); + return iterator_cend<const_iterator>(); } diff --git a/src/OpenFOAM/containers/HashTables/HashTable/HashTableIO.C b/src/OpenFOAM/containers/HashTables/HashTable/HashTableIO.C index 4d9c37384518d0cd8ce9d86c140d19ad8b500f6e..0eb3fadb459ed6fb047ce7a14e1f09248afc1527 100644 --- a/src/OpenFOAM/containers/HashTables/HashTable/HashTableIO.C +++ b/src/OpenFOAM/containers/HashTables/HashTable/HashTableIO.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. @@ -43,7 +43,7 @@ Foam::HashTable<T, Key, Hash>::HashTable(Istream& is, const label size) for (label hashIdx = 0; hashIdx < tableSize_; ++hashIdx) { - table_[hashIdx] = 0; + table_[hashIdx] = nullptr; } } @@ -54,8 +54,7 @@ Foam::HashTable<T, Key, Hash>::HashTable(Istream& is, const label size) // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * // template<class T, class Key, class Hash> -Foam::Ostream& -Foam::HashTable<T, Key, Hash>::printInfo(Ostream& os) const +Foam::Ostream& Foam::HashTable<T, Key, Hash>::printInfo(Ostream& os) const { label used = 0; label maxChain = 0; @@ -90,6 +89,53 @@ Foam::HashTable<T, Key, Hash>::printInfo(Ostream& os) const } +template<class T, class Key, class Hash> +Foam::Ostream& Foam::HashTable<T, Key, Hash>::writeKeys +( + Ostream& os, + const label shortListLen +) const +{ + // Similar to UList::writeList version except the following: + // - the keys can never be uniform + // - never write in binary + + label i = this->size(); + + if (i <= 1 || !shortListLen || (i <= shortListLen)) + { + // Write size and start delimiter + os << i << token::BEGIN_LIST; + + i = 0; + for (const_iterator iter = this->cbegin(); iter != this->cend(); ++iter) + { + if (i++) os << token::SPACE; + os << iter.key(); + } + + os << token::END_LIST; // End delimiter + } + else + { + // Write size and start delimiter + os << nl << i << nl << token::BEGIN_LIST << nl; + + for (const_iterator iter = this->cbegin(); iter != this->cend(); ++iter) + { + os << iter.key() << nl; + } + + os << token::END_LIST << nl; // End delimiter + } + + // Check state of IOstream + os.check(FUNCTION_NAME); + + return os; +} + + // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // template<class T, class Key, class Hash> @@ -215,28 +261,25 @@ template<class T, class Key, class Hash> Foam::Ostream& Foam::operator<< ( Ostream& os, - const HashTable<T, Key, Hash>& L + const HashTable<T, Key, Hash>& tbl ) { + using const_iterator = typename HashTable<T, Key, Hash>::const_iterator; + // Write size and start delimiter - os << nl << L.size() << nl << token::BEGIN_LIST << nl; + os << nl << tbl.size() << nl << token::BEGIN_LIST << nl; // Write contents - for - ( - typename HashTable<T, Key, Hash>::const_iterator iter = L.cbegin(); - iter != L.cend(); - ++iter - ) + for (const_iterator iter = tbl.cbegin(); iter != tbl.cend(); ++iter) { - os << iter.key() << token::SPACE << iter() << nl; + os << iter.key() << token::SPACE << iter.object() << nl; } // Write end delimiter os << token::END_LIST; // Check state of IOstream - os.check("Ostream& operator<<(Ostream&, const HashTable&)"); + os.check(FUNCTION_NAME); return os; } diff --git a/src/OpenFOAM/containers/HashTables/Map/Map.H b/src/OpenFOAM/containers/HashTables/Map/Map.H index 8ef2f560bcbef9597422ae8700441e067ab79f9a..4ac6e90e60de910576029cbd93aa98413c31ee01 100644 --- a/src/OpenFOAM/containers/HashTables/Map/Map.H +++ b/src/OpenFOAM/containers/HashTables/Map/Map.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. @@ -51,50 +51,54 @@ class Map : public HashTable<T, label, Hash<label>> { - public: - typedef typename HashTable<T, label, Hash<label>>::iterator iterator; + //- The template instance used for this Map + typedef Map<T> this_type; + + //- The template instance used for the parent HashTable + typedef HashTable<T, label, Hash<label>> parent_type; + + using iterator = typename parent_type::iterator; + using const_iterator = typename parent_type::const_iterator; - typedef typename HashTable<T, label, Hash<label>>::const_iterator - const_iterator; // Constructors //- Construct given initial size Map(const label size = 128) : - HashTable<T, label, Hash<label>>(size) + parent_type(size) {} //- Construct from Istream Map(Istream& is) : - HashTable<T, label, Hash<label>>(is) + parent_type(is) {} //- Construct as copy Map(const Map<T>& map) : - HashTable<T, label, Hash<label>>(map) + parent_type(map) {} //- Construct by transferring the parameter contents Map(const Xfer<Map<T>>& map) : - HashTable<T, label, Hash<label>>(map) + parent_type(map) {} //- Construct by transferring the parameter contents Map(const Xfer<HashTable<T, label, Hash<label>>>& map) : - HashTable<T, label, Hash<label>>(map) + parent_type(map) {} //- Construct from an initializer list Map(std::initializer_list<Tuple2<label, T>> map) : - HashTable<T, label, Hash<label>>(map) + parent_type(map) {} }; diff --git a/src/OpenFOAM/containers/HashTables/PtrMap/PtrMap.H b/src/OpenFOAM/containers/HashTables/PtrMap/PtrMap.H index 134a7fa9b08cc9d4bac3d6bbed362273062a93d6..178bc670989b69fc12276b080d3a89ac62d52377 100644 --- a/src/OpenFOAM/containers/HashTables/PtrMap/PtrMap.H +++ b/src/OpenFOAM/containers/HashTables/PtrMap/PtrMap.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. @@ -51,27 +51,33 @@ class PtrMap : public HashPtrTable<T, label, Hash<label>> { - public: + //- The template instance used for this PtrMap + typedef PtrMap<T> this_type; + + //- The template instance used for the parent HashTable + typedef HashPtrTable<T, label, Hash<label>> parent_type; + + // Constructors //- Construct given initial map size PtrMap(const label size = 128) : - HashPtrTable<T, label, Hash<label>>(size) + parent_type(size) {} //- Construct from Istream PtrMap(Istream& is) : - HashPtrTable<T, label, Hash<label>>(is) + parent_type(is) {} //- Construct as copy - PtrMap(const PtrMap<T>& map) + PtrMap(const this_type& map) : - HashPtrTable<T, label, Hash<label>>(map) + parent_type(map) {} }; diff --git a/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.C b/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.C index 05ad88f33e959c31fbc4a969884cc33c7629453e..948a451d47f6695cf49afa0ce7633cd623e2de39 100644 --- a/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.C +++ b/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.C @@ -30,32 +30,6 @@ License #include "List.H" #include "IOstreams.H" -// * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * // - -Foam::label Foam::StaticHashTableCore::canonicalSize(const label size) -{ - if (size < 1) - { - return 0; - } - - // Enforce power of two - unsigned int goodSize = size; - - if (goodSize & (goodSize - 1)) - { - // Brute-force is fast enough - goodSize = 1; - while (goodSize < unsigned(size)) - { - goodSize <<= 1; - } - } - - return goodSize; -} - - // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // template<class T, class Key, class Hash> diff --git a/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.H b/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.H index 67dfdfa5a92e9de2fe185f14b528bcd03402f209..639a434d654dcf3f073e861714e0428e6ba0629c 100644 --- a/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.H +++ b/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.H @@ -78,8 +78,8 @@ template<class T, class Key, class Hash> Ostream& operator<< //- Template-invariant bits for StaticHashTable struct StaticHashTableCore { - //- Return a canonical (power-of-two) size - static label canonicalSize(const label); + //- Return a canonical (power-of-two) of the requested size. + static label canonicalSize(const label requested_size); //- Construct null StaticHashTableCore() @@ -119,9 +119,6 @@ class StaticHashTable //- The current number of elements in table label nElmts_; - //- Return a canonical (power-of-two) size - static label canonicalSize(const label); - //- Return the hash index of the Key within the current table size. // No checks for zero-sized tables. inline label hashKeyIndex(const Key&) const; @@ -397,7 +394,7 @@ private: // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - #include "StaticHashTableI.H" +#include "StaticHashTableI.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTableCore.C b/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTableCore.C index 760839b0c683207050f2abd5b4a88782f01d25e4..31c02ebf5ecb3a2848f701f46319a517b2f55021 100644 --- a/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTableCore.C +++ b/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTableCore.C @@ -24,6 +24,7 @@ License \*---------------------------------------------------------------------------*/ #include "StaticHashTable.H" +#include "uLabel.H" // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // @@ -33,4 +34,53 @@ defineTypeNameAndDebug(StaticHashTableCore, 0); } +// Approximately labelMax/4 +static const Foam::label maxTableSize(1 << (sizeof(Foam::label)*8-3)); + + +// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * // + +Foam::label Foam::StaticHashTableCore::canonicalSize(const label requested_size) +{ + if (requested_size < 1) + { + return 0; + } + + // Enforce power of two - makes for a vey fast modulus etc. + // Use unsigned for these calculations. + // + // - The lower limit (8) is somewhat arbitrary, but if the hash table + // is too small, there will be many direct table collisions. + // - The uper limit (approx. labelMax/4) must be a power of two, + // need not be extremely large for hashing. + + uLabel powerOfTwo = 8; // lower-limit + + const uLabel size = requested_size; + if (size < powerOfTwo) + { + return powerOfTwo; + } + else if (requested_size >= maxTableSize) + { + return maxTableSize; + } + else if (size & (size-1)) // <- Modulus of i^2 + { + // Determine power-of-two. Brute-force is fast enough. + while (powerOfTwo < size) + { + powerOfTwo <<= 1; + } + + return powerOfTwo; + } + else + { + return size; + } +} + + // ************************************************************************* // diff --git a/src/OpenFOAM/containers/Lists/FixedList/FixedList.H b/src/OpenFOAM/containers/Lists/FixedList/FixedList.H index c969f4f4ad870afd92a23c1260200ec396f4f368..bdf75aa40101e8f61511b421a3318a09c0d88179 100644 --- a/src/OpenFOAM/containers/Lists/FixedList/FixedList.H +++ b/src/OpenFOAM/containers/Lists/FixedList/FixedList.H @@ -329,7 +329,7 @@ public: //- Return size of the largest possible FixedList inline label max_size() const; - //- Return true if the FixedList is empty (ie, size() is zero) + //- Always false since zero-sized FixedList is compile-time disabled. inline bool empty() const; //- Swap two FixedLists of the same type in constant time diff --git a/src/OpenFOAM/containers/Lists/SubList/SubList.H b/src/OpenFOAM/containers/Lists/SubList/SubList.H index b55fced6bb53d9272febef281fa1eacc5e82c901..1a02f11d79dd1c6907cd561cb92c281f9551bc54 100644 --- a/src/OpenFOAM/containers/Lists/SubList/SubList.H +++ b/src/OpenFOAM/containers/Lists/SubList/SubList.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. @@ -40,6 +40,7 @@ SourceFiles #define SubList_H #include "List.H" +#include "labelRange.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -55,7 +56,6 @@ class SubList : public UList<T> { - public: // Static Member Functions @@ -81,6 +81,23 @@ public: const label startIndex ); + //- Construct from UList and a (start,size) range. + // The range is subsetted with the list size itself to ensure that the + // result always addresses a valid section of the list. + inline SubList + ( + const UList<T>& list, + const labelRange& range + ); + + //- Construct from UList and a (start,size) range, but bypassing + // run-time range checking. + inline SubList + ( + const labelRange& range, + const UList<T>& list + ); + // Member operators @@ -88,13 +105,13 @@ public: inline operator const Foam::List<T>&() const; //- Assignment of all entries to the given sub-list - inline void operator=(const SubList<T>&); + inline void operator=(const SubList<T>& list); //- Assignment of all entries to the given list - inline void operator=(const UList<T>&); + inline void operator=(const UList<T>& list); //- Assignment of all entries to the given value - inline void operator=(const T&); + inline void operator=(const T& t); }; diff --git a/src/OpenFOAM/containers/Lists/SubList/SubListI.H b/src/OpenFOAM/containers/Lists/SubList/SubListI.H index 733b813b51c39a39723e3f2d6f66ac429a5c9d57..42accb1871ca9d7c1f9b336398ec163a698f2a20 100644 --- a/src/OpenFOAM/containers/Lists/SubList/SubListI.H +++ b/src/OpenFOAM/containers/Lists/SubList/SubListI.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. @@ -51,7 +51,6 @@ inline Foam::SubList<T>::SubList UList<T>(&(list.v_[startIndex]), subSize) { #ifdef FULLDEBUG - // Artificially allow the start of a zero-sized subList to be // one past the end of the original list. if (subSize) @@ -69,6 +68,28 @@ inline Foam::SubList<T>::SubList } +template<class T> +inline Foam::SubList<T>::SubList +( + const UList<T>& list, + const labelRange& range +) +: + SubList<T>(list.validateRange(range), list) +{} + + +template<class T> +inline Foam::SubList<T>::SubList +( + const labelRange& range, + const UList<T>& list +) +: + SubList<T>(list, range.size(), range.start()) +{} + + // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // template<class T> @@ -88,16 +109,16 @@ inline Foam::SubList<T>::operator const Foam::List<T>&() const template<class T> -inline void Foam::SubList<T>::operator=(const SubList<T>& sl) +inline void Foam::SubList<T>::operator=(const SubList<T>& list) { - UList<T>::deepCopy(sl); + UList<T>::deepCopy(list); } template<class T> -inline void Foam::SubList<T>::operator=(const UList<T>& l) +inline void Foam::SubList<T>::operator=(const UList<T>& list) { - UList<T>::deepCopy(l); + UList<T>::deepCopy(list); } diff --git a/src/OpenFOAM/containers/Lists/UList/UList.C b/src/OpenFOAM/containers/Lists/UList/UList.C index 7bc6b9d20c52ab2a0e7651cccd8b6d4081a7a467..f3feb5b83526355c5a80105435866d10fad2c02e 100644 --- a/src/OpenFOAM/containers/Lists/UList/UList.C +++ b/src/OpenFOAM/containers/Lists/UList/UList.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. @@ -26,9 +26,49 @@ License #include "UList.H" #include "ListLoopM.H" #include "contiguous.H" +#include "labelRange.H" #include <algorithm> +// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * // + +template<class T> +Foam::labelRange Foam::UList<T>::validateRange(const labelRange& range) const +{ + const labelRange slice = range.subset0(this->size()); + + #ifdef FULLDEBUG + this->checkStart(slice.start()); + this->checkSize(slice.start() + slice.size()); + #endif + + return slice; +} + + +template<class T> +Foam::labelRange Foam::UList<T>::validateRange +( + std::initializer_list<label> start_size_pair +) const +{ + if (start_size_pair.size() != 2) + { + FatalErrorInFunction + << "range specified with " << start_size_pair.size() + << " elements instead of 2" + << abort(FatalError); + } + + auto iter = start_size_pair.begin(); + + const label beg = *(iter++); + const label sz = *iter; + + return this->validateRange(labelRange(beg, sz)); +} + + // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // template<class T> @@ -64,6 +104,47 @@ void Foam::UList<T>::deepCopy(const UList<T>& a) // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // +template<class T> +Foam::UList<T> Foam::UList<T>::operator[](const labelRange& range) +{ + const labelRange slice = validateRange(range); + + return UList<T>(&(this->v_[slice.start()]), slice.size()); // SubList +} + +template<class T> +const Foam::UList<T> Foam::UList<T>::operator[](const labelRange& range) const +{ + const labelRange slice = validateRange(range); + + return UList<T>(&(this->v_[slice.start()]), slice.size()); // SubList +} + + +template<class T> +Foam::UList<T> Foam::UList<T>::operator[] +( + std::initializer_list<label> start_size_pair +) +{ + const labelRange slice = validateRange(start_size_pair); + + return UList<T>(&(this->v_[slice.start()]), slice.size()); // SubList +} + +template<class T> +const Foam::UList<T> Foam::UList<T>::operator[] +( + std::initializer_list<label> start_size_range +) const +{ + // Restricted range + const labelRange slice = validateRange(start_size_range); + + return UList<T>(&(this->v_[slice.start()]), slice.size()); // SubList +} + + template<class T> void Foam::UList<T>::operator=(const T& t) { diff --git a/src/OpenFOAM/containers/Lists/UList/UList.H b/src/OpenFOAM/containers/Lists/UList/UList.H index 362a15ccbc251b460af01a039af43adcffaf5060..6b58437b36fa2bd312669c0a265384bf10784d56 100644 --- a/src/OpenFOAM/containers/Lists/UList/UList.H +++ b/src/OpenFOAM/containers/Lists/UList/UList.H @@ -47,13 +47,16 @@ SourceFiles #include "uLabel.H" #include "nullObject.H" #include "zero.H" +#include "stdFoam.H" +#include <initializer_list> // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // namespace Foam { -// Forward declaration of friend classes +// Forward declarations +class labelRange; template<class T> class List; template<class T> class SubList; @@ -100,6 +103,16 @@ protected: //- Write the UList with its compound type void writeEntry(Ostream& os) const; + //- Return a validated (start,size) subset range, which means that it + // always addresses a valid section of the list. + labelRange validateRange(const labelRange& range) const; + + //- Return a validated (start,size) subset range, which means that it + // always addresses a valid section of the list. + labelRange validateRange + ( + std::initializer_list<label> start_size_pair + ) const; public: @@ -238,6 +251,29 @@ public: // an out-of-range element returns false without any ill-effects inline const T& operator[](const label i) const; + //- Return (start,size) subset from UList with non-const access. + // The range is subsetted with the list size itself to ensure that the + // result always addresses a valid section of the list. + UList<T> operator[](const labelRange& range); + + //- Return (start,size) subset from UList with const access. + // The range is subsetted with the list size itself to ensure that the + // result always addresses a valid section of the list. + const UList<T> operator[](const labelRange& range) const; + + //- Return (start,size) subset from UList with non-const access. + // The range is subsetted with the list size itself to ensure that the + // result always addresses a valid section of the list. + UList<T> operator[](std::initializer_list<label> start_size_range); + + //- Return (start,size) subset from UList with const access. + // The range is subsetted with the list size itself to ensure that the + // result always addresses a valid section of the list. + const UList<T> operator[] + ( + std::initializer_list<label> start_size_range + ) const; + //- Allow cast to a const List<T>& inline operator const Foam::List<T>&() const; @@ -253,12 +289,10 @@ public: //- Type of values the UList contains typedef T value_type; - //- Type that can be used for storing into - // UList::value_type objects + //- The type used for storing into value_type objects typedef T& reference; - //- Type that can be used for storing into - // constant UList::value_type objects + //- 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 @@ -427,69 +461,6 @@ inline void reverse(UList<T>& ul); #include "UListI.H" -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - -//- Loop across all elements in \a list -// \par Usage -// \code -// forAll(anyList, i) -// { -// statements; -// } -// \endcode -// \sa forAllReverse -#define forAll(list, i) \ - for (Foam::label i=0; i<(list).size(); ++i) - -//- Reverse loop across all elements in \a list -// \par Usage -// \code -// forAllReverse(anyList, i) -// { -// statements; -// } -// \endcode -// \sa forAll -#define forAllReverse(list, i) \ - for (Foam::label i=(list).size()-1; i>=0; --i) - -//- Iterate across all elements in the \a container object of type -// \a Container. -// \par Usage -// \code -// forAll(ContainerType, container, iter) -// { -// statements; -// } -// \endcode -// \sa forAllConstIter -#define forAllIter(Container,container,iter) \ - for \ - ( \ - Container::iterator iter = (container).begin(); \ - iter != (container).end(); \ - ++iter \ - ) - -//- Iterate across all elements in the \a container object of type -// \a Container with const access. -// \par Usage -// \code -// forAllConstIter(ContainerType, container, iter) -// { -// statements; -// } -// \endcode -// \sa forAllIter -#define forAllConstIter(Container,container,iter) \ - for \ - ( \ - Container::const_iterator iter = (container).cbegin(); \ - iter != (container).cend(); \ - ++iter \ - ) - - // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // #ifdef NoRepository diff --git a/src/OpenFOAM/containers/Lists/UList/UListI.H b/src/OpenFOAM/containers/Lists/UList/UListI.H index bd9fe339ebd7697b2924fbeee98390439615958b..971fa7faa0530ceadb3964ada7746733a85efa37 100644 --- a/src/OpenFOAM/containers/Lists/UList/UListI.H +++ b/src/OpenFOAM/containers/Lists/UList/UListI.H @@ -71,7 +71,7 @@ inline Foam::label Foam::UList<T>::rcIndex(const label i) const template<class T> inline void Foam::UList<T>::checkStart(const label start) const { - if (start<0 || (start && start>=size_)) + if (start < 0 || (start && start >= size_)) { FatalErrorInFunction << "start " << start << " out of range 0 ... " << max(size_-1, 0) @@ -83,7 +83,7 @@ inline void Foam::UList<T>::checkStart(const label start) const template<class T> inline void Foam::UList<T>::checkSize(const label size) const { - if (size<0 || size>size_) + if (size < 0 || size > size_) { FatalErrorInFunction << "size " << size << " out of range 0 ... " << size_ @@ -162,7 +162,6 @@ inline void Foam::UList<T>::shallowCopy(const UList<T>& a) // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // - template<class T> inline T& Foam::UList<T>::operator[](const label i) { @@ -172,7 +171,6 @@ inline T& Foam::UList<T>::operator[](const label i) return v_[i]; } - namespace Foam { // Template specialization for bool @@ -191,7 +189,6 @@ namespace Foam } } - template<class T> inline const T& Foam::UList<T>::operator[](const label i) const { diff --git a/src/OpenFOAM/containers/NamedEnum/NamedEnum.C b/src/OpenFOAM/containers/NamedEnum/NamedEnum.C index 99f723d987f70096c3962c70589d1938bf06dfb5..89784a1400b731514148fcb57d313b9e3bd92c00 100644 --- a/src/OpenFOAM/containers/NamedEnum/NamedEnum.C +++ b/src/OpenFOAM/containers/NamedEnum/NamedEnum.C @@ -64,14 +64,14 @@ Enum Foam::NamedEnum<Enum, nEnum>::read(Istream& is) const HashTable<int>::const_iterator iter = find(name); - if (iter == HashTable<int>::end()) + if (!iter.found()) { FatalIOErrorInFunction(is) << name << " is not in enumeration: " << sortedToc() << exit(FatalIOError); } - return Enum(iter()); + return Enum(iter.object()); } diff --git a/src/OpenFOAM/include/stdFoam.H b/src/OpenFOAM/include/stdFoam.H new file mode 100644 index 0000000000000000000000000000000000000000..59b0d9f704a8463605ead378ed254d83868f04dd --- /dev/null +++ b/src/OpenFOAM/include/stdFoam.H @@ -0,0 +1,212 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / 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/>. + +Namespace + stdFoam + +Description + Includes some global templates and macros used by OpenFOAM. + + Some of the templates are defined here correspond to useful + std templates that are part of future C++ standards, or that + are in a state of change. Defining them here provides some additional + control over which definition are used within the OpenFOAM code-base. + +SeeAlso + - http://en.cppreference.com/w/cpp/iterator/end + - http://en.cppreference.com/w/cpp/iterator/begin + +\*---------------------------------------------------------------------------*/ + +#ifndef StdFoam_H +#define StdFoam_H + +#include <initializer_list> + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace stdFoam +{ + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +//- Return iterator to the beginning of the container \a c or array. +// Definition as per std::begin C++17 +template<class C> +constexpr auto begin(C& c) -> decltype(c.begin()) +{ + return c.begin(); +} + +//- Return const_iterator to the beginning of the container \a c or array. +// Definition as per std::begin C++17 +template<class C> +constexpr auto begin(const C& c) -> decltype(c.begin()) +{ + return c.begin(); +} + +//- Return const_iterator to the beginning of the container \a c or array. +// Definition as per std::cbegin C++17 +template<class C> +constexpr auto cbegin(const C& c) -> decltype(c.begin()) +{ + return c.begin(); +} + +//- Return iterator to the end of the container \a c or array. +// Definition as per std::end C++17 +template<class C> +constexpr auto end(C& c) -> decltype(c.end()) +{ + return c.end(); +} + +//- Return const_iterator to the end of the container \a c or array. +// Definition as per std::end C++17 +template<class C> +constexpr auto end(const C& c) -> decltype(c.end()) +{ + return c.end(); +} + +//- Return const_iterator to the end of the container \a c or array. +// Definition as per std::cend C++17 +template<class C> +constexpr auto cend(const C& c) -> decltype(c.end()) +{ + return c.end(); +} + + +} // End namespace stdFoam + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +//- Iterate across all elements in the \a container object of type +// \a Container. +// \par Usage +// \code +// forAllIters(container, iter) +// { +// statements; +// } +// \endcode +// \sa forAllConstIters, forAllIter, forAllConstIters +#define forAllIters(container,it) \ + for \ + ( \ + auto it = stdFoam::begin(container); \ + it != stdFoam::end(container); \ + ++it \ + ) + + +//- Iterate across all elements of the \a container object with const access. +// \par Usage +// \code +// forAllConstIters(container, iter) +// { +// statements; +// } +// \endcode +// \sa forAllIters, forAllIter, forAllConstIter +#define forAllConstIters(container,cit) \ + for \ + ( \ + auto cit = stdFoam::cbegin(container); \ + cit != stdFoam::cend(container); \ + ++cit \ + ) + + +//- Loop across all elements in \a list +// \par Usage +// \code +// forAll(anyList, i) +// { +// statements; +// } +// \endcode +// \sa forAllReverse +#define forAll(list, i) \ + for (Foam::label i=0; i<(list).size(); ++i) + + +//- Reverse loop across all elements in \a list +// \par Usage +// \code +// forAllReverse(anyList, i) +// { +// statements; +// } +// \endcode +// \sa forAll +#define forAllReverse(list, i) \ + for (Foam::label i=(list).size()-1; i>=0; --i) + + +//- Iterate across all elements in the \a container object +// of type \a Container. +// \par Usage +// \code +// forAllIter(ContainerType, container, iter) +// { +// statements; +// } +// \endcode +// \sa forAllConstIter +#define forAllIter(Container,container,iter) \ + for \ + ( \ + Container::iterator iter = (container).begin(); \ + iter != (container).end(); \ + ++iter \ + ) + + +//- Iterate across all elements in the \a container object +// of type \a Container with const access. +// \par Usage +// \code +// forAllConstIter(ContainerType, container, iter) +// { +// statements; +// } +// \endcode +// \sa forAllIter +#define forAllConstIter(Container,container,iter) \ + for \ + ( \ + Container::const_iterator iter = (container).cbegin(); \ + iter != (container).cend(); \ + ++iter \ + ) + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/OpenFOAM/meshes/boundBox/boundBox.H b/src/OpenFOAM/meshes/boundBox/boundBox.H index 4a8351ad84684478e6347fac0f55583335964f49..dc85f279f07a53d1ae33313a83360f5ad97a8f1b 100644 --- a/src/OpenFOAM/meshes/boundBox/boundBox.H +++ b/src/OpenFOAM/meshes/boundBox/boundBox.H @@ -50,11 +50,8 @@ namespace Foam class boundBox; template<class T> class tmp; -bool operator==(const boundBox&, const boundBox&); -bool operator!=(const boundBox&, const boundBox&); - -Istream& operator>>(Istream&, boundBox&); -Ostream& operator<<(Ostream&, const boundBox&); +Istream& operator>>(Istream& is, boundBox& bb); +Ostream& operator<<(Ostream& os, const boundBox& bb); /*---------------------------------------------------------------------------*\ @@ -289,12 +286,6 @@ public: inline void operator+=(const boundBox& bb); - // Friend Operators - - inline friend bool operator==(const boundBox& a, const boundBox& b); - inline friend bool operator!=(const boundBox& a, const boundBox& b); - - // IOstream operator friend Istream& operator>>(Istream& is, boundBox& bb); @@ -306,6 +297,11 @@ public: template<> inline bool contiguous<boundBox>() {return contiguous<point>();} +// Global Operators + +inline bool operator==(const boundBox& a, const boundBox& b); +inline bool operator!=(const boundBox& a, const boundBox& b); + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/OpenFOAM/meshes/boundBox/boundBoxI.H b/src/OpenFOAM/meshes/boundBox/boundBoxI.H index c6e668e41dafb18a62a25a7d08b62c4794c2baf7..03be3b7305e218ced42354fbefaff9a4dd0bbe85 100644 --- a/src/OpenFOAM/meshes/boundBox/boundBoxI.H +++ b/src/OpenFOAM/meshes/boundBox/boundBoxI.H @@ -268,11 +268,11 @@ inline void Foam::boundBox::operator+=(const boundBox& bb) } -// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * // +// * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * * // inline bool Foam::operator==(const boundBox& a, const boundBox& b) { - return (a.min_ == b.min_) && (a.max_ == b.max_); + return (a.min() == b.min()) && (a.max() == b.max()); } diff --git a/src/OpenFOAM/meshes/meshShapes/cell/cellI.H b/src/OpenFOAM/meshes/meshShapes/cell/cellI.H index 48d14b9e3f345d2f0d0b276b40dd4a5d21ca0c22..82878d2278a7054ee1e7d801978d5899347d46f0 100644 --- a/src/OpenFOAM/meshes/meshShapes/cell/cellI.H +++ b/src/OpenFOAM/meshes/meshShapes/cell/cellI.H @@ -63,7 +63,7 @@ inline Foam::label Foam::cell::nFaces() const inline bool Foam::operator!=(const cell& a, const cell& b) { - return (!(a == b)); + return !(a == b); } diff --git a/src/OpenFOAM/meshes/meshShapes/cellModel/cellModel.H b/src/OpenFOAM/meshes/meshShapes/cellModel/cellModel.H index 0ff615f1c276cca2099d9acc491004e093dbb0e0..4a450b5a6a756d255c7303d7171b90833ac85c6f 100644 --- a/src/OpenFOAM/meshes/meshShapes/cellModel/cellModel.H +++ b/src/OpenFOAM/meshes/meshShapes/cellModel/cellModel.H @@ -59,7 +59,7 @@ Ostream& operator<<(Ostream& os, const cellModel& c); /*---------------------------------------------------------------------------*\ - Class cellModel Declaration + Class cellModel Declaration \*---------------------------------------------------------------------------*/ class cellModel @@ -122,13 +122,13 @@ public: inline label nFaces() const; //- Return list of edges - inline edgeList edges(const labelList& pointLabels) const; + inline edgeList edges(const UList<label>& pointLabels) const; //- Return a raw list of model faces inline const faceList& modelFaces() const; //- Return list of faces - inline faceList faces(const labelList& pointLabels) const; + inline faceList faces(const UList<label>& pointLabels) const; //- Vector centroid diff --git a/src/OpenFOAM/meshes/meshShapes/cellModel/cellModelI.H b/src/OpenFOAM/meshes/meshShapes/cellModel/cellModelI.H index c910527123e69a2ec916bffe23d996d31eae6723..0e7509c684ceba7c117aec9d5c4df5a32871f856 100644 --- a/src/OpenFOAM/meshes/meshShapes/cellModel/cellModelI.H +++ b/src/OpenFOAM/meshes/meshShapes/cellModel/cellModelI.H @@ -21,45 +21,38 @@ License 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 "error.H" #include "cellModel.H" -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - -namespace Foam -{ - // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // -inline const word& cellModel::name() const +inline const Foam::word& Foam::cellModel::name() const { return name_; } -inline label cellModel::index() const +inline Foam::label Foam::cellModel::index() const { return index_; } -inline label cellModel::nPoints() const +inline Foam::label Foam::cellModel::nPoints() const { return nPoints_; } -inline label cellModel::nEdges() const +inline Foam::label Foam::cellModel::nEdges() const { return edges_.size(); } -inline label cellModel::nFaces() const +inline Foam::label Foam::cellModel::nFaces() const { return faces_.size(); } @@ -67,7 +60,10 @@ inline label cellModel::nFaces() const // Return the faces of a cellModel by untangling the geometry // supplied in terms of the face labels -inline edgeList cellModel::edges(const labelList& pointLabels) const +inline Foam::edgeList Foam::cellModel::edges +( + const UList<label>& pointLabels +) const { edgeList e(edges_.size()); @@ -86,7 +82,7 @@ inline edgeList cellModel::edges(const labelList& pointLabels) const } -inline const faceList& cellModel::modelFaces() const +inline const Foam::faceList& Foam::cellModel::modelFaces() const { return faces_; } @@ -94,7 +90,10 @@ inline const faceList& cellModel::modelFaces() const // Return the faces of a cellModel by untangling the geometry // supplied in terms of the face labels -inline faceList cellModel::faces(const labelList& pointLabels) const +inline Foam::faceList Foam::cellModel::faces +( + const UList<label>& pointLabels +) const { faceList f(faces_.size()); @@ -120,20 +119,16 @@ inline faceList cellModel::faces(const labelList& pointLabels) const // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * // // Equality operator: true => ptr to models are equal ! -inline bool operator==(const cellModel& m1, const cellModel& m2) +inline bool Foam::operator==(const cellModel& m1, const cellModel& m2) { return (&m1 == &m2); } // Inequality operator: true => ptr to models are not equal ! -inline bool operator!=(const cellModel& m1, const cellModel& m2) +inline bool Foam::operator!=(const cellModel& m1, const cellModel& m2) { return (&m1 != &m2); } -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - -} // End namespace Foam - // ************************************************************************* // diff --git a/src/OpenFOAM/meshes/meshShapes/edge/edge.H b/src/OpenFOAM/meshes/meshShapes/edge/edge.H index f804fda8141f96111a69881af9fcbe3c8705d7b8..52bf97cb7cf65cfebbe2e30ca0a26881b808d3d7 100644 --- a/src/OpenFOAM/meshes/meshShapes/edge/edge.H +++ b/src/OpenFOAM/meshes/meshShapes/edge/edge.H @@ -28,6 +28,14 @@ Description An edge is a list of two point labels. The functionality it provides supports the discretisation on a 2-D flat mesh. + The edge is implemented as a FixedList of labels. + As well as geometrically relevant methods, it also provides methods + similar to HashSet for additional convenience. + Valid point labels are always non-negative (since they correspond to + addressing within the mesh). The value '-1' is used to tag invalid + point labels that correspond conceptually to open 'slots', which + can be filled with a HashSet-like functionality. + SourceFiles edgeI.H @@ -37,6 +45,7 @@ SourceFiles #define edge_H #include "FixedList.H" +#include "labelPair.H" #include "pointField.H" #include "linePointRef.H" @@ -45,21 +54,32 @@ SourceFiles namespace Foam { -// Forward declaration of friend functions and operators - -class edge; -inline bool operator==(const edge& a, const edge& b); -inline bool operator!=(const edge& a, const edge& b); - - /*---------------------------------------------------------------------------*\ - Class edge Declaration + Class edge Declaration \*---------------------------------------------------------------------------*/ class edge : public FixedList<label, 2> { + // Private Member Functions + + //- Insert values, using begin/end iterators. + template<class InputIter> + inline label insertMultiple + ( + const InputIter begIter, + const InputIter endIter + ); + + //- Remove values, using begin/end iterators. + template<class InputIter> + inline label eraseMultiple + ( + const InputIter begIter, + const InputIter endIter + ); + public: @@ -79,6 +99,9 @@ public: //- Construct, optionally sorted with start less-than end inline edge(const label from, const label to, const bool doSort); + //- Construct from two labels + inline edge(const labelPair& pair); + //- Construct from FixedList inline edge(const FixedList<label, 2>& lst); @@ -91,6 +114,8 @@ public: // Member Functions + // Access + //- Return start vertex label inline label start() const; @@ -103,80 +128,155 @@ public: //- Return end vertex label inline label& end(); + //- Return reverse edge as copy. + // No special handling of negative point labels. + inline edge reverseEdge() const; + + + // Queries + + //- Return the smallest point label used by the edge + // No special handling of negative point labels. + inline label minVertex() const; + + //- Return the largest point label used by the edge + // No special handling of negative point labels. + inline label maxVertex() const; + + //- True if start() is less-than end() + // No special handling of negative point labels. + inline bool sorted() const; + + //- Return true if point label is found in edge. + // Always false for a negative label. + inline bool found(const label index) const; + //- Do the edges share a common vertex index? + // Negative point labels never connect. inline bool connects(const edge& other) const; //- Return vertex common with otherEdge or -1 on failure + // Negative point labels are never considered common between edges. inline label commonVertex(const edge& other) const; //- Given one vertex index, return the other one. + // No special treatment for negative point labels. inline label otherVertex(const label index) const; - //- 'Collapse' edge by marking duplicate point labels. - // Duplicates point labels are marked with '-1'. - // (the lower vertex is retained). - // Return the collapsed size. + + // Editing + + //- 'Collapse' edge by marking duplicate point labels as '-1', + // the lower vertex is retained. + // Return the effective size after collapsing. inline label collapse(); - //- Return true if point label is found in edge - // No special treatment for '-1'. - inline bool found(const label index) const; + //- Flip the edge in-place. + // No special handling of negative point labels. + inline void flip(); + + //- Sort so that start() is less-than end() + // No special handling of negative point labels. + inline void sort(); + + + // Hash-like functions //- Return the number of unique, valid (non -1) point labels. // Similar to a HashTable::size(). inline label count() const; - //- Insert the index if it did not previously exist on the edge. + //- Return true if edge has no valid point labels. + inline bool empty() const; + + //- 'Clears' edge by setting both ends to invalid point labels. + inline void clear(); + + //- Fill any open slot with the index if it did not previously exist. // Returns true on success. A negative label never inserts. // Similar to a HashTable::insert(). inline bool insert(const label index); + //- Fill open slots with the indices if they did not previously exist. + // Returns true on success. Negative labels never insert. + // Return the number of slots filled. + // Similar to a HashTable::insert(). + inline label insert(const UList<label>& lst); + + //- Fill open slots with the indices if they did not previously exist. + // Returns true on success. Negative labels never insert. + // Return the number of slots filled. + // Similar to a HashTable::insert(). + template<unsigned AnySize> + inline label insert(const FixedList<label, AnySize>& lst); + + //- Fill open slots with the indices if they did not previously exist. + // Returns true on success. Negative labels never insert. + // Return the number of slots filled. + // Similar to a HashTable::insert(). + inline label insert(std::initializer_list<label> lst); + //- Remove an existing index from the edge and set its location to '-1'. - // Returns true on success. A negative label never removes. + // Returns the number of changes. A negative label never removes. // Similar to a HashTable::erase(). - inline bool erase(const label index); + inline label erase(const label index); + //- Remove existing indices from the edge and set locations to '-1'. + // Returns the number of changes. + inline label erase(const UList<label>& lst); - //- True if the edge is sorted such that start is less-than end - inline bool sorted() const; + //- Remove existing indices from the edge and set locations to '-1'. + // Returns the number of changes. + template<unsigned AnySize> + inline label erase(const FixedList<label, AnySize>& lst); - //- Sort start/end that start is less-than end - inline void sort(); + //- Remove existing indices from the edge and set locations to '-1'. + // Returns the number of changes. + inline label erase(std::initializer_list<label> lst); - //- Flip the edge in-place. - inline void flip(); - //- Return reverse edge - inline edge reverseEdge() const; + // Geometric functions - //- Return centre (centroid) + //- Return centre point (centroid) of the edge. + // No special handling of negative point labels. inline point centre(const UList<point>& pts) const; //- Return the vector (end - start) + // No special handling of negative point labels. inline vector vec(const UList<point>& pts) const; //- Return the unit vector (end - start) + // No special handling of negative point labels. inline vector unitVec(const UList<point>& pts) const; - //- Return scalar magnitude + //- Return scalar magnitude of the edge. + // No special handling of negative point labels. inline scalar mag(const UList<point>& pts) const; //- Return edge line + // No special handling of negative point labels. inline linePointRef line(const UList<point>& pts) const; + + // Comparison + //- Compare edges // Returns: // - 0: different - // - +1: identical - // - -1: same edge, but different orientation + // - +1: identical values and order used + // - -1: identical values, but in different order static inline int compare(const edge& a, const edge& b); +}; - // Friend Operators - friend bool operator==(const edge& a, const edge& b); - friend bool operator!=(const edge& a, const edge& b); -}; +// Global Operators + +//- Compare edges for equal content, ignoring orientation +inline bool operator==(const edge& a, const edge& b); + +//- Compare edges for non-equal content, ignoring orientation +inline bool operator!=(const edge& a, const edge& b); //- Hash specialization for hashing edges - a commutative hash value. diff --git a/src/OpenFOAM/meshes/meshShapes/edge/edgeI.H b/src/OpenFOAM/meshes/meshShapes/edge/edgeI.H index b1b4f493682179a70bdd6b242626cf9ab935c449..75a5ba23655c5dd5fbe1d82cbf5a0ddb06a98bf9 100644 --- a/src/OpenFOAM/meshes/meshShapes/edge/edgeI.H +++ b/src/OpenFOAM/meshes/meshShapes/edge/edgeI.H @@ -45,6 +45,66 @@ inline int Foam::edge::compare(const edge& a, const edge& b) } +// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // + +template<class InputIter> +inline Foam::label Foam::edge::insertMultiple +( + const InputIter begIter, + const InputIter endIter +) +{ + // Available slots. + // Don't use count() since it has special treatment for duplicates + const int maxChange = (start() < 0 ? 1 : 0) + (end() < 0 ? 1 : 0); + + int changed = 0; + if (maxChange) + { + for (InputIter iter = begIter; iter != endIter; ++iter) + { + if (insert(*iter)) + { + if (++changed >= maxChange) + { + break; + } + } + } + } + + return changed; +} + + +template<class InputIter> +inline Foam::label Foam::edge::eraseMultiple +( + const InputIter begIter, + const InputIter endIter +) +{ + // Occupied slots. + // Don't use count() since it has special treatment for duplicates + const int maxChange = (start() >= 0 ? 1 : 0) + (end() >= 0 ? 1 : 0); + + int changed = 0; + if (maxChange) + { + for (InputIter iter = begIter; iter != endIter; ++iter) + { + changed += erase(*iter); + if (changed >= maxChange) + { + break; + } + } + } + + return changed; +} + + // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // inline Foam::edge::edge() @@ -75,6 +135,13 @@ inline Foam::edge::edge(const label from, const label to, const bool doSort) } +inline Foam::edge::edge(const labelPair& pair) +{ + start() = pair.first(); + end() = pair.second(); +} + + inline Foam::edge::edge(const FixedList<label, 2>& lst) { start() = lst[0]; @@ -127,27 +194,40 @@ inline Foam::label& Foam::edge::end() } +inline Foam::label Foam::edge::minVertex() const +{ + return (start() < end() ? start() : end()); +} + + +inline Foam::label Foam::edge::maxVertex() const +{ + return (start() > end() ? start() : end()); +} + + inline bool Foam::edge::found(const label index) const { - return (index == start() || index == end()); + // -1: always false + return (index >= 0 && (index == start() || index == end())); } inline bool Foam::edge::connects(const edge& other) const { - return (other.found(this->start()) || other.found(this->end())); + return (other.found(start()) || other.found(end())); } inline Foam::label Foam::edge::commonVertex(const edge& other) const { - if (other.found(this->start())) + if (other.found(start())) { - return this->start(); + return start(); } - else if (other.found(this->end())) + else if (other.found(end())) { - return this->end(); + return end(); } else { @@ -182,12 +262,12 @@ inline Foam::label Foam::edge::collapse() // catch any '-1' (eg, if called multiple times) label n = 2; - if (start() == end() || end() == -1) + if (start() == end() || end() < 0) { end() = -1; --n; } - if (start() == -1) + if (start() < 0) { --n; } @@ -196,15 +276,48 @@ inline Foam::label Foam::edge::collapse() } +inline bool Foam::edge::sorted() const +{ + return (start() < end()); +} + + +inline void Foam::edge::sort() +{ + if (start() > end()) + { + flip(); + } +} + + +inline void Foam::edge::flip() +{ + Swap(operator[](0), operator[](1)); +} + + +inline Foam::edge Foam::edge::reverseEdge() const +{ + return edge(end(), start()); +} + + +inline void Foam::edge::clear() +{ + start() = -1; + end() = -1; +} + + inline Foam::label Foam::edge::count() const { label n = 2; - if (start() == end() || end() == -1) + if (start() == end() || end() < 0) { --n; } - - if (start() == -1) + if (start() < 0) { --n; } @@ -213,15 +326,21 @@ inline Foam::label Foam::edge::count() const } +inline bool Foam::edge::empty() const +{ + return (start() < 0 && end() < 0); +} + + inline bool Foam::edge::insert(const label index) { if (index < 0) { - // Can never insert invalid point labels. - // Use direct assignment for that. + // Cannot insert invalid point labels (use direct assignment for that) return false; } - else if (start() == -1) + + if (start() < 0) { // Store at [0], if not duplicate of [1] if (index != end()) @@ -230,7 +349,7 @@ inline bool Foam::edge::insert(const label index) return true; } } - else if (end() == -1) + else if (end() < 0) { // Store at [1], if not duplicate of [0] if (index != start()) @@ -244,22 +363,41 @@ inline bool Foam::edge::insert(const label index) } -inline bool Foam::edge::erase(const label index) +inline Foam::label Foam::edge::insert(const UList<label>& lst) +{ + return insertMultiple(lst.begin(), lst.end()); +} + + +template<unsigned AnySize> +inline Foam::label Foam::edge::insert(const FixedList<label, AnySize>& lst) +{ + return insertMultiple(lst.begin(), lst.end()); +} + + +inline Foam::label Foam::edge::insert(std::initializer_list<label> lst) +{ + return insertMultiple(lst.begin(), lst.end()); +} + + +inline Foam::label Foam::edge::erase(const label index) { if (index < 0) { // Can never remove invalid point labels! - return false; + return 0; } - int n = 0; + label n = 0; if (index == start()) { start() = -1; ++n; } - // Automatically handle duplicates, should not have been there anyhow + // Automatically handle duplicates, which should not have been there anyhow if (index == end()) { end() = -1; @@ -270,47 +408,68 @@ inline bool Foam::edge::erase(const label index) } -inline bool Foam::edge::sorted() const +inline Foam::label Foam::edge::erase(const UList<label>& lst) { - return (start() < end()); + return eraseMultiple(lst.begin(), lst.end()); } -inline void Foam::edge::sort() +template<unsigned AnySize> +inline Foam::label Foam::edge::erase(const FixedList<label, AnySize>& lst) { - if (start() > end()) - { - flip(); - } + return eraseMultiple(lst.begin(), lst.end()); } -inline void Foam::edge::flip() +inline Foam::label Foam::edge::erase(std::initializer_list<label> lst) { - Swap(operator[](0), operator[](1)); + return eraseMultiple(lst.begin(), lst.end()); } -inline Foam::edge Foam::edge::reverseEdge() const -{ - return edge(end(), start()); -} - +// Geometric inline Foam::point Foam::edge::centre(const UList<point>& pts) const { + #ifdef FULLDEBUG + if (start() < 0 || end() < 0) + { + FatalErrorInFunction + << "negative point index on edge " << *this + << abort(FatalError); + } + #endif + return 0.5*(pts[start()] + pts[end()]); } inline Foam::vector Foam::edge::vec(const UList<point>& pts) const { + #ifdef FULLDEBUG + if (start() < 0 || end() < 0) + { + FatalErrorInFunction + << "negative point index on edge " << *this + << abort(FatalError); + } + #endif + return pts[end()] - pts[start()]; } inline Foam::vector Foam::edge::unitVec(const UList<point>& pts) const { + #ifdef FULLDEBUG + if (start() < 0 || end() < 0) + { + FatalErrorInFunction + << "negative point index on edge " << *this + << abort(FatalError); + } + #endif + Foam::vector v = pts[end()] - pts[start()]; v /= ::Foam::mag(v) + VSMALL; @@ -326,11 +485,20 @@ inline Foam::scalar Foam::edge::mag(const UList<point>& pts) const inline Foam::linePointRef Foam::edge::line(const UList<point>& pts) const { + #ifdef FULLDEBUG + if (start() < 0 || end() < 0) + { + FatalErrorInFunction + << "negative point index on edge " << *this + << abort(FatalError); + } + #endif + return linePointRef(pts[start()], pts[end()]); } -// * * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * // +// * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * * // inline bool Foam::operator==(const edge& a, const edge& b) { diff --git a/src/OpenFOAM/meshes/meshShapes/face/face.H b/src/OpenFOAM/meshes/meshShapes/face/face.H index 762ef89ee1764e624eeffcb7122403aea6298ad6..2d83def92090a598a8b38213dc82f79a0c2b301e 100644 --- a/src/OpenFOAM/meshes/meshShapes/face/face.H +++ b/src/OpenFOAM/meshes/meshShapes/face/face.H @@ -65,8 +65,6 @@ class triFace; template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv> class DynamicList; -inline bool operator==(const face& a, const face& b); -inline bool operator!=(const face& a, const face& b); inline Istream& operator>>(Istream& is, face& f); /*---------------------------------------------------------------------------*\ @@ -382,12 +380,6 @@ public: static bool sameVertices(const face& a, const face& b); - // Friend Operators - - friend bool operator==(const face& a, const face& b); - friend bool operator!=(const face& a, const face& b); - - // Istream Operator friend Istream& operator>>(Istream& is, face& f); @@ -418,6 +410,11 @@ public: }; +// Global operators +inline bool operator==(const face& a, const face& b); +inline bool operator!=(const face& a, const face& b); + + // Global functions //- Find the longest edge on a face. Face point labels index into pts. diff --git a/src/OpenFOAM/meshes/meshShapes/face/faceI.H b/src/OpenFOAM/meshes/meshShapes/face/faceI.H index cdb5f0e59ade9c5989c6dd9d046eb4a9fb4bcc3f..b45193f0e730b3d59ede32f0f443bf62b252046a 100644 --- a/src/OpenFOAM/meshes/meshShapes/face/faceI.H +++ b/src/OpenFOAM/meshes/meshShapes/face/faceI.H @@ -135,14 +135,14 @@ inline Foam::label Foam::face::nTriangles() const return size() - 2; } -// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * // + +// * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * // inline bool Foam::operator==(const face& a, const face& b) { return face::compare(a,b) != 0; } - inline bool Foam::operator!=(const face& a, const face& b) { return face::compare(a,b) == 0; diff --git a/src/OpenFOAM/meshes/meshShapes/labelledTri/labelledTriI.H b/src/OpenFOAM/meshes/meshShapes/labelledTri/labelledTriI.H index 0e0c72af77c9836d9c175c22a297dc3063be0b4e..861be7665ae56edbff050e0dd13b687adfac06cf 100644 --- a/src/OpenFOAM/meshes/meshShapes/labelledTri/labelledTriI.H +++ b/src/OpenFOAM/meshes/meshShapes/labelledTri/labelledTriI.H @@ -162,8 +162,7 @@ inline Foam::Ostream& Foam::operator<<(Ostream& os, const labelledTri& t) } // Check state of Ostream - os.check("Ostream& operator<<(Ostream&, const labelledTri&)"); - + os.check(FUNCTION_NAME); return os; } diff --git a/src/OpenFOAM/meshes/meshShapes/triFace/triFace.H b/src/OpenFOAM/meshes/meshShapes/triFace/triFace.H index 840e28b7f697743c3dcf5120a469297d56226931..e0e586673acd317ba968050ae2d270137d6f2dfd 100644 --- a/src/OpenFOAM/meshes/meshShapes/triFace/triFace.H +++ b/src/OpenFOAM/meshes/meshShapes/triFace/triFace.H @@ -229,13 +229,16 @@ public: // - -1: same face, but different orientation static inline int compare(const triFace& a, const triFace& b); - // Friend Operators - - inline friend bool operator==(const triFace& a, const triFace& b); - inline friend bool operator!=(const triFace& a, const triFace& b); }; +template<> +inline bool contiguous<triFace>() {return true;} + +inline bool operator==(const triFace& a, const triFace& b); +inline bool operator!=(const triFace& a, const triFace& b); + + //- Hash specialization for hashing triFace - a commutative hash value. // Hash incrementally. template<> @@ -260,10 +263,6 @@ inline unsigned Hash<triFace>::operator()(const triFace& t) const } -template<> -inline bool contiguous<triFace>() {return true;} - - //- Hash specialization to offset faces in ListListOps::combineOffset template<> class offsetOp<triFace> diff --git a/src/OpenFOAM/meshes/meshShapes/triFace/triFaceI.H b/src/OpenFOAM/meshes/meshShapes/triFace/triFaceI.H index e6e2fe8c34d49f8688929d985f3354a7ebdd9f0a..69d2a4033b8607c32d13a876b3670a9effb0763c 100644 --- a/src/OpenFOAM/meshes/meshShapes/triFace/triFaceI.H +++ b/src/OpenFOAM/meshes/meshShapes/triFace/triFaceI.H @@ -377,7 +377,7 @@ inline int Foam::triFace::edgeDirection(const edge& e) const } -// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * // +// * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * // inline bool Foam::operator==(const triFace& a, const triFace& b) { diff --git a/src/OpenFOAM/meshes/primitiveShapes/objectHit/objectHit.H b/src/OpenFOAM/meshes/primitiveShapes/objectHit/objectHit.H index a7a716eb7f72d3e2288b3d7c59d6f86b1d9b6419..dd8a081b6dfa49907d0691be857d30affe042919 100644 --- a/src/OpenFOAM/meshes/primitiveShapes/objectHit/objectHit.H +++ b/src/OpenFOAM/meshes/primitiveShapes/objectHit/objectHit.H @@ -84,31 +84,18 @@ public: // Member Functions //- Is there a hit - bool hit() const + inline bool hit() const { return hit_; } //- Return hit object - label hitObject() const + inline label hitObject() const { return hitObject_; } - // Friend Operators - - inline friend bool operator==(const objectHit& a, const objectHit& b) - { - return (a.hit_ == b.hit_) && (a.hitObject_ == b.hitObject_); - } - - inline friend bool operator!=(const objectHit& a, const objectHit& b) - { - return !(a == b); - } - - // Ostream operator inline friend Ostream& operator<<(Ostream& os, const objectHit& obj) @@ -118,6 +105,20 @@ public: }; +// Global Operators + +inline bool operator==(const objectHit& a, const objectHit& b) +{ + return a.hit() == b.hit() && a.hitObject() == b.hitObject(); +} + + +inline bool operator!=(const objectHit& a, const objectHit& b) +{ + return !(a == b); +} + + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // } // End namespace Foam diff --git a/src/OpenFOAM/meshes/primitiveShapes/plane/plane.C b/src/OpenFOAM/meshes/primitiveShapes/plane/plane.C index a5ad75c2134b2d29e321f80db267b2ca0ef21261..05ef84af2c5d02259360169c0528deaf47ffdd42 100644 --- a/src/OpenFOAM/meshes/primitiveShapes/plane/plane.C +++ b/src/OpenFOAM/meshes/primitiveShapes/plane/plane.C @@ -487,14 +487,14 @@ void Foam::plane::writeDict(Ostream& os) const os.endBlock() << flush; } - -// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * // +// * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * // bool Foam::operator==(const plane& a, const plane& b) { - return (a.point_ == b.point_ && a.normal_ == b.normal_); + return (a.refPoint() == b.refPoint() && a.normal() == b.normal()); } + bool Foam::operator!=(const plane& a, const plane& b) { return !(a == b); diff --git a/src/OpenFOAM/meshes/primitiveShapes/plane/plane.H b/src/OpenFOAM/meshes/primitiveShapes/plane/plane.H index a6e1d59d0272ea3ce8890460cc102bd3283a9dff..91973a5d6b6d5b6723869459cd77d3bc9680e3c6 100644 --- a/src/OpenFOAM/meshes/primitiveShapes/plane/plane.H +++ b/src/OpenFOAM/meshes/primitiveShapes/plane/plane.H @@ -49,8 +49,6 @@ namespace Foam // Forward declaration of friend functions and operators class plane; -bool operator==(const plane& a, const plane& b); -bool operator!=(const plane& a, const plane& b); Ostream& operator<<(Ostream& os, const plane& pln); @@ -193,7 +191,7 @@ public: //- Return the cutting line between this plane and another. // Returned as direction vector and point line goes through. - ray planeIntersect(const plane&) const; + ray planeIntersect(const plane& plane2) const; //- Return the cutting point between this plane and two other planes point planePlaneIntersect @@ -217,12 +215,6 @@ public: void writeDict(Ostream& os) const; - // friend Operators - - friend bool operator==(const plane& a, const plane& b); - friend bool operator!=(const plane& a, const plane& b); - - // IOstream Operators //- Write plane properties @@ -231,6 +223,12 @@ public: }; +// Global Operators + +bool operator==(const plane& a, const plane& b); +bool operator!=(const plane& a, const plane& b); + + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // } // End namespace Foam diff --git a/src/OpenFOAM/meshes/treeBoundBox/treeBoundBox.C b/src/OpenFOAM/meshes/treeBoundBox/treeBoundBox.C index 462c318755e603b05282db2e6829a5e63d60ac6b..072e76511090a5656e6c197d927b6f7530e2d34b 100644 --- a/src/OpenFOAM/meshes/treeBoundBox/treeBoundBox.C +++ b/src/OpenFOAM/meshes/treeBoundBox/treeBoundBox.C @@ -555,24 +555,6 @@ Foam::label Foam::treeBoundBox::distanceCmp } -// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * // - -bool Foam::operator==(const treeBoundBox& a, const treeBoundBox& b) -{ - return operator== - ( - static_cast<const boundBox&>(a), - static_cast<const boundBox&>(b) - ); -} - - -bool Foam::operator!=(const treeBoundBox& a, const treeBoundBox& b) -{ - return !(a == b); -} - - // * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * // Foam::Ostream& Foam::operator<<(Ostream& os, const treeBoundBox& bb) diff --git a/src/OpenFOAM/meshes/treeBoundBox/treeBoundBox.H b/src/OpenFOAM/meshes/treeBoundBox/treeBoundBox.H index 7c27f9fc264a82df9221ea870e62d74552cf80b8..adcac28e03554c28a30612ad62e1e745156d7e88 100644 --- a/src/OpenFOAM/meshes/treeBoundBox/treeBoundBox.H +++ b/src/OpenFOAM/meshes/treeBoundBox/treeBoundBox.H @@ -77,10 +77,6 @@ class Random; // Forward declaration of friend functions and operators class treeBoundBox; - -bool operator==(const treeBoundBox& a, const treeBoundBox& b); -bool operator!=(const treeBoundBox& a, const treeBoundBox& b); - Istream& operator>>(Istream& is, treeBoundBox& bb); Ostream& operator<<(Ostream& os, const treeBoundBox& bb); @@ -340,12 +336,6 @@ public: inline treeBoundBox extend(Random& rndGen, const scalar s) const; - // Friend Operators - - friend bool operator==(const treeBoundBox& a, const treeBoundBox& b); - friend bool operator!=(const treeBoundBox& a, const treeBoundBox& b); - - // IOstream operator friend Istream& operator>>(Istream& is, treeBoundBox& bb); @@ -358,6 +348,12 @@ template<> inline bool contiguous<treeBoundBox>() {return contiguous<boundBox>();} +// Global Operators + +inline bool operator==(const treeBoundBox& a, const treeBoundBox& b); +inline bool operator!=(const treeBoundBox& a, const treeBoundBox& b); + + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // } // End namespace Foam diff --git a/src/OpenFOAM/meshes/treeBoundBox/treeBoundBoxI.H b/src/OpenFOAM/meshes/treeBoundBox/treeBoundBoxI.H index 750a6737ff6b08bc888fa333326b0d08c63d23c4..ea6cc3328a31f652ab14c1b2e31ee361860b5bbc 100644 --- a/src/OpenFOAM/meshes/treeBoundBox/treeBoundBoxI.H +++ b/src/OpenFOAM/meshes/treeBoundBox/treeBoundBoxI.H @@ -343,4 +343,17 @@ inline Foam::treeBoundBox Foam::treeBoundBox::extend } +// * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * * // + +inline bool Foam::operator==(const treeBoundBox& a, const treeBoundBox& b) +{ + return static_cast<const boundBox&>(a) == static_cast<const boundBox&>(b); +} + + +inline bool Foam::operator!=(const treeBoundBox& a, const treeBoundBox& b) +{ + return !(a == b); +} + // ************************************************************************* // diff --git a/src/OpenFOAM/primitives/Pair/Pair.H b/src/OpenFOAM/primitives/Pair/Pair.H index 1a9ab62fc816f587701c473e9812732b6407e951..a78397a35849230aea591f0e14f657dff2d8cadd 100644 --- a/src/OpenFOAM/primitives/Pair/Pair.H +++ b/src/OpenFOAM/primitives/Pair/Pair.H @@ -70,9 +70,9 @@ public: } //- Construct from FixedList - inline Pair(const FixedList<Type, 2>& fl) + inline Pair(const FixedList<Type, 2>& lst) : - FixedList<Type, 2>(fl) + FixedList<Type, 2>(lst) {} //- Construct from Istream @@ -134,18 +134,20 @@ public: } + // Comparison + //- Compare Pairs - // Returning: + // Returns: // - 0: different - // - +1: identical - // - -1: same pair, but reversed order + // - +1: identical values and order used + // - -1: identical values, but in reversed order static inline int compare(const Pair<Type>& a, const Pair<Type>& b) { - if (a == b) + if (a[0] == b[0] && a[1] == b[1]) { return 1; } - else if (a == reverse(b)) + else if (a[0] == b[1] && a[1] == b[0]) { return -1; } @@ -157,6 +159,8 @@ public: }; +// * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * * // + template<class Type> Pair<Type> reverse(const Pair<Type>& p) { @@ -167,14 +171,14 @@ Pair<Type> reverse(const Pair<Type>& p) template<class Type> bool operator==(const Pair<Type>& a, const Pair<Type>& b) { - return (a.first() == b.first() && a.second() == b.second()); + return Pair<Type>::compare(a,b) != 0; } template<class Type> bool operator!=(const Pair<Type>& a, const Pair<Type>& b) { - return !(a == b); + return Pair<Type>::compare(a,b) == 0; } diff --git a/src/OpenFOAM/primitives/Tuple2/Tuple2.H b/src/OpenFOAM/primitives/Tuple2/Tuple2.H index 6b556af0fa1f4dbfa39680a538c4fb4c36634f32..c19b645ddf384b5730a7c222c494046799a8904a 100644 --- a/src/OpenFOAM/primitives/Tuple2/Tuple2.H +++ b/src/OpenFOAM/primitives/Tuple2/Tuple2.H @@ -25,7 +25,9 @@ Class Foam::Tuple2 Description - A 2-tuple for storing two objects of different types. + A 2-tuple for storing two objects of dissimilar types. + The container is similar in purpose to std::pair, but does not expose + its members directly. See also Foam::Pair for storing two objects of identical types. @@ -48,10 +50,10 @@ template<class Type1, class Type2> class Tuple2; template<class Type1, class Type2> -inline Istream& operator>>(Istream&, Tuple2<Type1, Type2>&); +inline Istream& operator>>(Istream& is, Tuple2<Type1, Type2>& t2); template<class Type1, class Type2> -inline Ostream& operator<<(Ostream&, const Tuple2<Type1, Type2>&); +inline Ostream& operator<<(Ostream& os, const Tuple2<Type1, Type2>& t2); /*---------------------------------------------------------------------------*\ @@ -66,12 +68,20 @@ class Tuple2 Type1 f_; Type2 s_; - public: + // Typedefs (cf. std::pair) + + //- Type of member first, the first template parameter (Type1) + typedef Type1 first_type; + + //- Type of member second, the second template parameter (Type2) + typedef Type2 second_type; + + // Constructors - //- Null constructor for lists + //- Null constructor for lists and hashes inline Tuple2() {} @@ -164,6 +174,59 @@ inline bool operator!= } +template<class Type1, class Type2> +inline bool operator< +( + const Tuple2<Type1, Type2>& a, + const Tuple2<Type1, Type2>& b +) +{ + return + ( + a.first() < b.first() + || + ( + !(b.first() < a.first()) + && a.second() < b.second() + ) + ); +} + + + +template<class Type1, class Type2> +inline bool operator<= +( + const Tuple2<Type1, Type2>& a, + const Tuple2<Type1, Type2>& b +) +{ + return !(b < a); +} + + +template<class Type1, class Type2> +inline bool operator> +( + const Tuple2<Type1, Type2>& a, + const Tuple2<Type1, Type2>& b +) +{ + return (b < a); +} + + +template<class Type1, class Type2> +inline bool operator>= +( + const Tuple2<Type1, Type2>& a, + const Tuple2<Type1, Type2>& b +) +{ + return !(a < b); +} + + template<class Type1, class Type2> inline Istream& operator>>(Istream& is, Tuple2<Type1, Type2>& t2) { diff --git a/src/OpenFOAM/primitives/nullObject/nullObject.H b/src/OpenFOAM/primitives/nullObject/nullObject.H index 71217836c15561f8ed76f5e25e72ea68f92ee1b6..e0ab12029ef88fa979be5a3b955362ff93e6443c 100644 --- a/src/OpenFOAM/primitives/nullObject/nullObject.H +++ b/src/OpenFOAM/primitives/nullObject/nullObject.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2014-2016 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -25,7 +25,9 @@ Class Foam::nullObject Description - Singleton null-object class and instance + Singleton null-object class and instance. + It occupies enough space to reinterpret its content as a class with + a null pointer for its content. SourceFiles nullObjectI.H @@ -47,20 +49,41 @@ namespace Foam class NullObject { + //- Ensure it occupies enough space to reinterpret_cast to a class + // having some member data + const union + { + void* ptr; + unsigned long val; + } null; + //- Private constructor NullObject() + : + null{nullptr} {} //- Disallow default bitwise copy construct - NullObject(const NullObject&); + NullObject(const NullObject&) = delete; //- Disallow default bitwise assignment - void operator=(const NullObject&); + void operator=(const NullObject&) = delete; public: - //- The unique null object static const NullObject nullObject; + + //- A nullptr pointer content + inline const void* pointer() const + { + return null.ptr; + } + + //- A zero value content + inline unsigned long value() const + { + return null.val; + } }; diff --git a/src/OpenFOAM/primitives/ranges/labelRange/labelRange.C b/src/OpenFOAM/primitives/ranges/labelRange/labelRange.C index 2bf9810f6c6178bef8f2972c1a99e9a2d80aa9bf..b1033ac17279b4bf00810ac80b6ede0fe51e63aa 100644 --- a/src/OpenFOAM/primitives/ranges/labelRange/labelRange.C +++ b/src/OpenFOAM/primitives/ranges/labelRange/labelRange.C @@ -29,7 +29,10 @@ License // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // -int Foam::labelRange::debug(::Foam::debug::debugSwitch("labelRange", 0)); +namespace Foam +{ +int labelRange::debug(debug::debugSwitch("labelRange", 0)); +} // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // @@ -49,11 +52,17 @@ void Foam::labelRange::adjust() { if (start_ < 0) { - size_ += start_; + if (size_ <= 0) + { + size_ = 0; + } + else + { + size_ += start_; + } start_ = 0; } - - if (size_ < 0) + else if (size_ < 0) { size_ = 0; } @@ -62,22 +71,21 @@ void Foam::labelRange::adjust() bool Foam::labelRange::overlaps(const labelRange& range, bool touches) const { - const label final = touches ? 1 : 0; + const label extra = touches ? 1 : 0; return ( - this->size() - && range.size() + this->size() && range.size() && ( ( range.first() >= this->first() - && range.first() <= this->last() + final + && range.first() <= this->last() + extra ) || ( this->first() >= range.first() - && this->first() <= range.last() + final + && this->first() <= range.last() + extra ) ) ); @@ -91,35 +99,78 @@ Foam::labelRange Foam::labelRange::join(const labelRange& range) const { return *this; } - else if (!range.size_) + else if (!range.size()) { return range; } const label lower = Foam::min(this->first(), range.first()); const label upper = Foam::max(this->last(), range.last()); - const label sz = upper - lower + 1; + const label total = upper+1 - lower; + // last = start+size-1 + // size = last+1-start - return labelRange(lower, sz); + return labelRange(lower, total); } -// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // +Foam::labelRange Foam::labelRange::subset(const labelRange& range) const +{ + const label lower = Foam::max(this->first(), range.first()); + const label upper = Foam::min(this->last(), range.last()); + const label total = upper+1 - lower; + // last = start+size-1 + // size = last+1-start + + if (total > 0) + { + return labelRange(lower, total); + } + else + { + return labelRange(); + } +} + -void Foam::labelRange::operator+=(const labelRange& rhs) +Foam::labelRange Foam::labelRange::subset +( + const label start, + const label size +) const { - if (!size_) + const label lower = Foam::max(this->start(), start); + const label upper = Foam::min(this->last(), start+Foam::max(0,size-1)); + const label total = upper+1 - lower; + // last = start+size-1 + // size = last+1-start + + if (total > 0) { - // trivial case - operator=(rhs); + return labelRange(lower, total); } - else if (rhs.size_) + else { - const label lower = Foam::min(this->first(), rhs.first()); - const label upper = Foam::max(this->last(), rhs.last()); + return labelRange(); + } +} + - start_ = lower; - size_ = upper - lower + 1; +Foam::labelRange Foam::labelRange::subset0(const label size) const +{ + const label lower = Foam::max(this->start(), 0); + const label upper = Foam::min(this->last(), Foam::max(0,size-1)); + const label total = upper+1 - lower; + // last = start+size-1 + // size = last+1-start + + if (total > 0) + { + return labelRange(lower, total); + } + else + { + return labelRange(); } } @@ -148,7 +199,7 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const labelRange& range) { // Write ASCII only for now os << token::BEGIN_LIST - << range.start_ << token::SPACE << range.size_ + << range.start() << token::SPACE << range.size() << token::END_LIST; os.check("operator<<(Ostream&, const labelRange&)"); diff --git a/src/OpenFOAM/primitives/ranges/labelRange/labelRange.H b/src/OpenFOAM/primitives/ranges/labelRange/labelRange.H index 3fa69c4ff379f367cedb3848ee395fb642069b9b..c4eb8488a62b143cea7ba469ec637804892f35be 100644 --- a/src/OpenFOAM/primitives/ranges/labelRange/labelRange.H +++ b/src/OpenFOAM/primitives/ranges/labelRange/labelRange.H @@ -25,7 +25,7 @@ Class Foam::labelRange Description - A range of labels. + A range of labels defined by a start and a size. SourceFiles labelRange.C @@ -65,29 +65,25 @@ public: static int debug; + // STL type definitions similar to what UList has - // Public classes + //- Type of values the range contains + typedef label value_type; - //- Less function class for sorting labelRange - class less - { - public: + //- The type that can represent the difference between two iterators + typedef label difference_type; - bool operator()(const labelRange& a, const labelRange& b) - { - return a.operator<(b); - } - }; + //- The type that can represent the size of the range + typedef label size_type; // Constructors - //- Construct an empty range with zero as start and size. + //- An empty range with zero for start/size. inline labelRange(); - //- Construct a range from start and size. + //- Construct a range from start and size, enforcing non-negative size. // Optionally adjust the start to avoid any negative indices. - // Always reduce a negative size to zero. inline labelRange ( const label start, @@ -101,12 +97,21 @@ public: // Member Functions - //- Alias for setSize(const label) - inline void resize(const label n); + //- Adjust start position + inline void setStart(const label i); //- Adjust size inline void setSize(const label n); + //- Alias for setSize() + inline void resize(const label n); + + //- Decrease the size by 1, but never below 0. + inline void decrement(); + + //- Increase the size by 1. + inline void increment(); + //- Reset to zero start and zero size inline void clear(); @@ -116,21 +121,29 @@ public: //- Adjust the start to avoid any negative indices void adjust(); - //- Is the range valid (non-empty)? + //- Is the range non-empty? inline bool valid() const; - //- Return the effective size of the range + //- The (inclusive) lower value of the range + inline label start() const; + + //- The effective size of the range inline label size() const; - //- The (inclusive) lower value of the range + //- The (inclusive) lower value of the range - same as start inline label first() const; //- The (inclusive) upper value of the range inline label last() const; - //- Reset start and size. + //- The value before the start of the range + inline label before() const; + + //- The value after the last element in the range + inline label after() const; + + //- Reset start and size, enforcing non-negative size. // Optionally adjust the start to avoid any negative indices. - // Always reduce a negative size to zero. // Return true if the updated range valid (non-empty). inline bool reset ( @@ -139,8 +152,8 @@ public: const bool adjustStart = false ); - //- Return true if the value is within the range - inline bool contains(const label value) const; + //- Return true if the value is located the range + inline bool found(const label value) const; //- Return true if the ranges overlap. // Optional test for ranges that also just touch each other @@ -150,73 +163,76 @@ public: // A prior overlaps() check can be used to avoid squashing gaps. labelRange join(const labelRange& range) const; + //- Calculate the intersection of the range with another. + // If there is no intersection, it returns an empty range with zero + // for start/size. + labelRange subset(const labelRange& range) const; - // Member Operators + //- Calculate the intersection with the given start/size range. + // If there is no intersection, it returns an empty range with zero + // for start/size. + labelRange subset(const label start, const label size) const; - //- Return element in range, no bounds checking - inline label operator[](const label i) const; + //- Calculate the intersection with the given 0/size range. + // If there is no intersection, it returns an empty range with zero + // for start/size. + labelRange subset0(const label size) const; - //- Comparison function for sorting, compares the start. - // If the start values are equal, also compares the size. - inline bool operator<(const labelRange& rhs) const; - //- Join ranges, squashing any gaps in between - // A prior overlaps() check can be used to avoid squashing gaps. - void operator+=(const labelRange& rhs); + // Member Operators + + //- Return element in the range, no bounds checking + inline label operator[](const label localIndex) const; + + //- Increase the size by 1. + inline label operator++(); + inline label operator++(int); - inline bool operator==(const labelRange& rhs) const; - inline bool operator!=(const labelRange& rhs) const; + //- Decrease the size by 1, but never below 0. + inline label operator--(); + inline label operator--(int); // STL iterator - //- An STL const_iterator + //- Forward iterator with const access class const_iterator { - friend class labelRange; + //- The current label (not the local index) + label index_; - // Private data - - //- Reference to the range for which this is an iterator - const labelRange& range_; - - //- Current index - label index_; - - - // Constructors + public: - //- Construct from range at 'begin' or 'end' position - inline const_iterator - ( - const labelRange& range, - const bool endIter = false - ); + // Constructors - public: + //- Construct from range at given local index. + // A negative index signals the 'end' position + inline const_iterator(const labelRange* range, const label i = 0); - // Member operators + // Member operators - inline bool operator==(const const_iterator& iter) const; - inline bool operator!=(const const_iterator& iter) const; + //- Return the current label + inline label operator*() const; - inline label operator*() const; - inline label operator()() const; + inline const_iterator& operator++(); + inline const_iterator operator++(int); - inline const_iterator& operator++(); - inline const_iterator operator++(int); + inline bool operator==(const const_iterator& iter) const; + inline bool operator!=(const const_iterator& iter) const; }; //- A const_iterator set to the beginning of the range + // The value returned is guaranteed to be the same as start() + inline const_iterator begin() const; + + //- A const_iterator set to the beginning of the range + // The value returned is guaranteed to be the same as start() inline const_iterator cbegin() const; //- A const_iterator set to beyond the end of the range inline const const_iterator cend() const; - //- A const_iterator set to the beginning of the range - inline const_iterator begin() const; - //- A const_iterator set to beyond the end of the range inline const const_iterator end() const; @@ -229,6 +245,51 @@ public: }; +// Global Operators + +inline bool operator==(const labelRange& a, const labelRange& b) +{ + return (a.first() == b.first() && a.size() == b.size()); +} + +inline bool operator!=(const labelRange& a, const labelRange& b) +{ + return !(a == b); +} + + +//- Comparison function for sorting, compares the start. +// If the start values are equal, also compares the size. +inline bool operator<(const labelRange& a, const labelRange& b) +{ + return + ( + a.first() < b.first() + || + ( + !(b.first() < a.first()) + && a.size() < b.size() + ) + ); +} + +inline bool operator<=(const labelRange& a, const labelRange& b) +{ + return !(b < a); +} + + +inline bool operator>(const labelRange& a, const labelRange& b) +{ + return (b < a); +} + +inline bool operator>=(const labelRange& a, const labelRange& b) +{ + return !(a < b); +} + + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // } // End namespace Foam diff --git a/src/OpenFOAM/primitives/ranges/labelRange/labelRangeI.H b/src/OpenFOAM/primitives/ranges/labelRange/labelRangeI.H index 927a6f57a083bdd1d1b8ac2b44d61a05d003c9d7..0563c1cd22f100df318029dbadff834d11e3d406 100644 --- a/src/OpenFOAM/primitives/ranges/labelRange/labelRangeI.H +++ b/src/OpenFOAM/primitives/ranges/labelRange/labelRangeI.H @@ -60,15 +60,41 @@ inline Foam::labelRange::labelRange inline Foam::labelRange::const_iterator::const_iterator ( - const labelRange& range, - const bool endIter + const labelRange* range, + const label i ) : - range_(range), - index_(endIter ? range_.size() : 0) + index_ + ( + range->start() + + ((i < 0 || i > range->size()) ? range->size() : i) + ) {} +inline Foam::label Foam::labelRange::const_iterator::operator*() const +{ + return index_; +} + + +inline Foam::labelRange::const_iterator& +Foam::labelRange::const_iterator::operator++() +{ + ++index_; + return *this; +} + + +inline Foam::labelRange::const_iterator +Foam::labelRange::const_iterator::operator++(int) +{ + const_iterator old = *this; + ++index_; + return old; +} + + inline bool Foam::labelRange::const_iterator::operator== ( const const_iterator& iter @@ -87,75 +113,61 @@ inline bool Foam::labelRange::const_iterator::operator!= } -inline Foam::label Foam::labelRange::const_iterator::operator*() const +inline Foam::labelRange::const_iterator Foam::labelRange::begin() const { - return range_[index_]; + return const_iterator(this, 0); } -inline Foam::label Foam::labelRange::const_iterator::operator()() const +inline Foam::labelRange::const_iterator Foam::labelRange::cbegin() const { - return range_[index_]; + return const_iterator(this, 0); } -inline Foam::labelRange::const_iterator& -Foam::labelRange::const_iterator::operator++() +inline const Foam::labelRange::const_iterator Foam::labelRange::end() const { - ++index_; - return *this; + return const_iterator(this, -1); } -inline Foam::labelRange::const_iterator -Foam::labelRange::const_iterator::operator++(int) +inline const Foam::labelRange::const_iterator Foam::labelRange::cend() const { - const_iterator old = *this; - ++index_; - return old; + return const_iterator(this, -1); } -inline Foam::labelRange::const_iterator Foam::labelRange::cbegin() const -{ - return const_iterator(*this); -} - +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // -inline const Foam::labelRange::const_iterator Foam::labelRange::cend() const +inline void Foam::labelRange::setStart(const label i) { - return const_iterator(*this, true); + start_ = i; } -inline Foam::labelRange::const_iterator Foam::labelRange::begin() const +inline void Foam::labelRange::setSize(const label n) { - return const_iterator(*this); + size_ = n; + if (size_ < 0) size_ = 0; } -inline const Foam::labelRange::const_iterator Foam::labelRange::end() const +inline void Foam::labelRange::resize(const label n) { - return const_iterator(*this, true); + setSize(n); } -// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // - -inline void Foam::labelRange::resize(const label n) +inline void Foam::labelRange::decrement() { - setSize(n); + --size_; + if (size_ < 0) size_ = 0; } -inline void Foam::labelRange::setSize(const label n) +inline void Foam::labelRange::increment() { - size_ = n; - - if (size_ < 0) - { - size_ = 0; - } + ++size_; } @@ -183,6 +195,12 @@ inline Foam::label Foam::labelRange::size() const } +inline Foam::label Foam::labelRange::start() const +{ + return start_; +} + + inline Foam::label Foam::labelRange::first() const { return start_; @@ -195,6 +213,18 @@ inline Foam::label Foam::labelRange::last() const } +inline Foam::label Foam::labelRange::before() const +{ + return start_ - 1; +} + + +inline Foam::label Foam::labelRange::after() const +{ + return start_ + size_; +} + + inline bool Foam::labelRange::reset ( const label start, @@ -220,39 +250,44 @@ inline bool Foam::labelRange::reset } -inline bool Foam::labelRange::contains(const label value) const +inline bool Foam::labelRange::found(const label value) const { - return value >= this->first() && value <= this->last(); + return (value >= this->first() && value <= this->last()); } // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // -inline Foam::label Foam::labelRange::operator[](const label i) const +inline Foam::label Foam::labelRange::operator[](const label localIndex) const { - return start_ + i; + return start_ + localIndex; } -inline bool Foam::labelRange::operator<(const labelRange& rhs) const +inline Foam::label Foam::labelRange::operator++() { - return - ( - this->first() < rhs.first() - || (this->first() == rhs.first() && this->size() < rhs.size()) - ); + return ++size_; } -inline bool Foam::labelRange::operator==(const labelRange& rhs) const +inline Foam::label Foam::labelRange::operator++(int) { - return start_ == rhs.start_ && size_ == rhs.size_; + return size_++; } -inline bool Foam::labelRange::operator!=(const labelRange& rhs) const +inline Foam::label Foam::labelRange::operator--() { - return !(operator==(rhs)); + decrement(); + return size_; +} + + +inline Foam::label Foam::labelRange::operator--(int) +{ + const label old = size_; + decrement(); + return old; } diff --git a/src/OpenFOAM/primitives/ranges/labelRange/labelRanges.C b/src/OpenFOAM/primitives/ranges/labelRange/labelRanges.C index d9edff0e47abf2f07c347bbd74666ccb63beed19..e1a2258f45073d21fac98dbbc78da4b5c00b518a 100644 --- a/src/OpenFOAM/primitives/ranges/labelRange/labelRanges.C +++ b/src/OpenFOAM/primitives/ranges/labelRange/labelRanges.C @@ -44,7 +44,7 @@ void Foam::labelRanges::insertBefore << *this << endl; } - ParentType::setSize(nElem+1); + StorageContainer::setSize(nElem+1); if (labelRange::debug) { @@ -58,7 +58,7 @@ void Foam::labelRanges::insertBefore Info<<"copy from " << (i) << " to " << (i+1) << nl; } - ParentType::operator[](i+1) = ParentType::operator[](i); + StorageContainer::operator[](i+1) = StorageContainer::operator[](i); } // finally insert the range @@ -66,7 +66,7 @@ void Foam::labelRanges::insertBefore { Info<< "finally insert the range at " << insert << nl; } - ParentType::operator[](insert) = range; + StorageContainer::operator[](insert) = range; } @@ -76,18 +76,19 @@ void Foam::labelRanges::purgeEmpty() label nElem = 0; forAll(*this, elemI) { - if (!ParentType::operator[](elemI).empty()) + if (!StorageContainer::operator[](elemI).empty()) { if (nElem != elemI) { - ParentType::operator[](nElem) = ParentType::operator[](elemI); + StorageContainer::operator[](nElem) = + StorageContainer::operator[](elemI); } ++nElem; } } // truncate - this->ParentType::setSize(nElem); + this->StorageContainer::setSize(nElem); } @@ -134,20 +135,20 @@ bool Foam::labelRanges::add(const labelRange& range) // find the correct place for insertion forAll(*this, elemI) { - labelRange& currRange = ParentType::operator[](elemI); + labelRange& currRange = StorageContainer::operator[](elemI); if (currRange.overlaps(range, true)) { // absorb into the existing (adjacent/overlapping) range - currRange += range; + currRange.join(range); // might connect with the next following range(s) for (; elemI < this->size()-1; ++elemI) { - labelRange& nextRange = ParentType::operator[](elemI+1); + labelRange& nextRange = StorageContainer::operator[](elemI+1); if (currRange.overlaps(nextRange, true)) { - currRange += nextRange; + currRange.join(nextRange); nextRange.clear(); } else @@ -187,7 +188,7 @@ bool Foam::labelRanges::remove(const labelRange& range) forAll(*this, elemI) { - labelRange& currRange = ParentType::operator[](elemI); + labelRange& currRange = StorageContainer::operator[](elemI); if (range.first() > currRange.first()) { @@ -290,14 +291,14 @@ bool Foam::labelRanges::remove(const labelRange& range) Foam::Istream& Foam::operator>>(Istream& is, labelRanges& ranges) { - is >> static_cast<labelRanges::ParentType&>(ranges); + is >> static_cast<labelRanges::StorageContainer&>(ranges); return is; } Foam::Ostream& Foam::operator<<(Ostream& os, const labelRanges& ranges) { - os << static_cast<const labelRanges::ParentType&>(ranges); + os << static_cast<const labelRanges::StorageContainer&>(ranges); return os; } diff --git a/src/OpenFOAM/primitives/ranges/labelRange/labelRanges.H b/src/OpenFOAM/primitives/ranges/labelRange/labelRanges.H index 134acb048202aeff4d10db35b7ee02ab792c3447..4a7019cbb937e3540b10f6f513a753e7dc0b6302 100644 --- a/src/OpenFOAM/primitives/ranges/labelRange/labelRanges.H +++ b/src/OpenFOAM/primitives/ranges/labelRange/labelRanges.H @@ -62,7 +62,7 @@ class labelRanges { // Private typedefs for convenience - typedef DynamicList<labelRange> ParentType; + typedef DynamicList<labelRange> StorageContainer; // Private Member Functions @@ -99,8 +99,8 @@ public: //- Return true if the list is empty using DynamicList<labelRange>::empty; - //- Return true if the value is within any of the ranges - inline bool contains(const label value) const; + //- Return true if the value is found any of the sub-ranges + inline bool found(const label value) const; //- Add the range to the list bool add(const labelRange& range); @@ -116,38 +116,35 @@ public: { friend class labelRanges; - // Private data + // Private data - //- Reference to the list for which this is an iterator - const labelRanges& list_; + //- Reference to the list for which this is an iterator + const labelRanges* list_; - //- Current list index - label index_; + //- Current list-index + label index_; - //- Index of current element at listIndex - label subIndex_; + //- Index of current element at list-index + label subIndex_; - // Constructors + // Constructors - //- Construct from ranges at 'begin' or 'end' position - inline const_iterator - ( - const labelRanges& lst, - const bool endIter = false - ); + //- Construct from ranges at given index. + // A negative index signals the 'end' position + inline const_iterator(const labelRanges* lst, const label i); public: - // Member operators + // Member operators - inline bool operator==(const const_iterator& iter) const; - inline bool operator!=(const const_iterator& iter) const; + inline bool operator==(const const_iterator& iter) const; + inline bool operator!=(const const_iterator& iter) const; - inline label operator*(); - inline label operator()(); + //- Return the current label + inline label operator*(); - inline const_iterator& operator++(); - inline const_iterator operator++(int); + inline const_iterator& operator++(); + inline const_iterator operator++(int); }; diff --git a/src/OpenFOAM/primitives/ranges/labelRange/labelRangesI.H b/src/OpenFOAM/primitives/ranges/labelRange/labelRangesI.H index 9ccad0e300c20cf75dabaf922bdced742ada7374..55a1f00df73ee77bd406f09d9ff3cd63eadda9a8 100644 --- a/src/OpenFOAM/primitives/ranges/labelRange/labelRangesI.H +++ b/src/OpenFOAM/primitives/ranges/labelRange/labelRangesI.H @@ -28,13 +28,13 @@ License inline Foam::labelRanges::labelRanges() : - ParentType() + StorageContainer() {} inline Foam::labelRanges::labelRanges(const label nElem) : - ParentType(nElem) + StorageContainer(nElem) {} @@ -42,12 +42,12 @@ inline Foam::labelRanges::labelRanges(const label nElem) inline Foam::labelRanges::const_iterator::const_iterator ( - const labelRanges& lst, - const bool endIter + const labelRanges* lst, + const label i ) : list_(lst), - index_(endIter ? lst.size() : 0), + index_(i < 0 ? lst->size() : i), subIndex_(0) {} @@ -76,20 +76,14 @@ inline bool Foam::labelRanges::const_iterator::operator!= inline Foam::label Foam::labelRanges::const_iterator::operator*() { - return list_[index_][subIndex_]; -} - - -inline Foam::label Foam::labelRanges::const_iterator::operator()() -{ - return list_[index_][subIndex_]; + return list_->operator[](index_)[subIndex_]; } inline Foam::labelRanges::const_iterator& Foam::labelRanges::const_iterator::operator++() { - if (++subIndex_ >= list_[index_].size()) + if (++subIndex_ >= list_->operator[](index_).size()) { // Next sub-list ++index_; @@ -111,35 +105,35 @@ Foam::labelRanges::const_iterator::operator++(int) inline Foam::labelRanges::const_iterator Foam::labelRanges::cbegin() const { - return const_iterator(*this); + return const_iterator(this, 0); } inline const Foam::labelRanges::const_iterator Foam::labelRanges::cend() const { - return const_iterator(*this, true); + return const_iterator(this, -1); } inline Foam::labelRanges::const_iterator Foam::labelRanges::begin() const { - return const_iterator(*this); + return const_iterator(this, 0); } inline const Foam::labelRanges::const_iterator Foam::labelRanges::end() const { - return const_iterator(*this, true); + return const_iterator(this, -1); } // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // -inline bool Foam::labelRanges::contains(const label value) const +inline bool Foam::labelRanges::found(const label value) const { forAll(*this, i) { - if (this->ParentType::operator[](i).contains(value)) + if (StorageContainer::operator[](i).found(value)) { return true; } diff --git a/src/OpenFOAM/primitives/ranges/scalarRange/scalarRange.C b/src/OpenFOAM/primitives/ranges/scalarRange/scalarRange.C index 2022071101387ec5101420289679dc21be3881fd..83a946bf77b43dd4ff66780585029eb938018ac6 100644 --- a/src/OpenFOAM/primitives/ranges/scalarRange/scalarRange.C +++ b/src/OpenFOAM/primitives/ranges/scalarRange/scalarRange.C @@ -28,7 +28,10 @@ License // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // -int Foam::scalarRange::debug(::Foam::debug::debugSwitch("scalarRange", 0)); +namespace Foam +{ +int scalarRange::debug(debug::debugSwitch("scalarRange", 0)); +} // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // @@ -47,11 +50,9 @@ Foam::scalarRange::scalarRange(const scalar lower, const scalar upper) value_(lower), value2_(upper) { - // mark invalid range as empty if (lower > upper) { - type_ = EMPTY; - value_ = value2_ = 0; + clear(); // Mark invalid range as empty } } @@ -73,6 +74,14 @@ Foam::scalarRange::scalarRange(Istream& is) // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // +void Foam::scalarRange::clear() +{ + type_ = scalarRange::EMPTY; + value_ = 0; + value2_ = 0; +} + + bool Foam::scalarRange::empty() const { return type_ == EMPTY; @@ -210,15 +219,14 @@ Foam::Istream& Foam::operator>>(Istream& is, scalarRange& range) // a number is now required if (!toks[nTok-1].isNumber()) { - is.setBad(); - range.type_ = scalarRange::EMPTY; - range.value_ = range.value2_ = 0; + range.clear(); // Mark invalid range as empty Info<< "rejected ill-formed or empty range:"; for (label i=0; i<nTok; ++i) { Info<< " " << toks[i]; } Info<< endl; + is.setBad(); return is; } @@ -245,15 +253,14 @@ Foam::Istream& Foam::operator>>(Istream& is, scalarRange& range) { if (range.type_ == scalarRange::UPPER) { - is.setBad(); - range.type_ = scalarRange::EMPTY; - range.value_ = range.value2_ = 0; + range.clear(); // Mark invalid range as empty Info<< "rejected ill-formed range:"; for (label i=0; i<nTok; ++i) { Info<< " " << toks[i]; } Info<< endl; + is.setBad(); return is; } @@ -308,16 +315,15 @@ Foam::Istream& Foam::operator>>(Istream& is, scalarRange& range) ) ) { - is.setBad(); - range.type_ = scalarRange::EMPTY; - range.value_ = range.value2_ = 0; - + range.clear(); // Mark invalid range as empty Info<< "rejected ill-formed range:"; for (label i=0; i<nTok; ++i) { Info<< " " << toks[i]; } Info<< endl; + is.setBad(); + return is; } return is; diff --git a/src/OpenFOAM/primitives/ranges/scalarRange/scalarRange.H b/src/OpenFOAM/primitives/ranges/scalarRange/scalarRange.H index 7b5a62a806251d462df4e7e4846c749b0bbfd286..71d855e76619dc8fb31d84f56bfe99c253ecddc7 100644 --- a/src/OpenFOAM/primitives/ranges/scalarRange/scalarRange.H +++ b/src/OpenFOAM/primitives/ranges/scalarRange/scalarRange.H @@ -52,8 +52,8 @@ class Ostream; // Forward declaration of friend functions and operators class scalarRange; -Istream& operator>>(Istream&, scalarRange&); -Ostream& operator<<(Ostream&, const scalarRange&); +Istream& operator>>(Istream& is, scalarRange& range); +Ostream& operator<<(Ostream& os, const scalarRange& range); /*---------------------------------------------------------------------------*\ @@ -73,7 +73,7 @@ class scalarRange }; - // Private data + // Private Member Data enum rangeType type_; scalar value_; @@ -96,11 +96,14 @@ public: //- Construct from Istream. // Since commas can be used as list delimiters, // leading and trailing commas are ignored. - scalarRange(Istream&); + scalarRange(Istream& is); // Member Functions + //- Reset to an empty range. + void clear(); + //- Is the range empty? bool empty() const; @@ -121,19 +124,19 @@ public: scalar upper() const; //- Return true if the value is within the range - bool selected(const scalar) const; + bool selected(const scalar value) const; // Member Operators - bool operator==(const scalarRange&) const; - bool operator!=(const scalarRange&) const; + bool operator==(const scalarRange& range) const; + bool operator!=(const scalarRange& range) const; // IOstream Operators - friend Istream& operator>>(Istream&, scalarRange&); - friend Ostream& operator<<(Ostream&, const scalarRange&); + friend Istream& operator>>(Istream& is, scalarRange& range); + friend Ostream& operator<<(Ostream& os, const scalarRange& range); }; diff --git a/src/OpenFOAM/primitives/ranges/scalarRange/scalarRanges.C b/src/OpenFOAM/primitives/ranges/scalarRange/scalarRanges.C index 819e615934f15e45cb4a493e81588f7823490b40..429efdca0ae6b690dbb2b7d227fd85d8c520d984 100644 --- a/src/OpenFOAM/primitives/ranges/scalarRange/scalarRanges.C +++ b/src/OpenFOAM/primitives/ranges/scalarRange/scalarRanges.C @@ -72,7 +72,7 @@ bool Foam::scalarRanges::selected(const scalar value) const Foam::List<bool> Foam::scalarRanges::selected ( - const List<scalar>& values + const UList<scalar>& values ) const { List<bool> lst(values.size(), false); diff --git a/src/OpenFOAM/primitives/ranges/scalarRange/scalarRanges.H b/src/OpenFOAM/primitives/ranges/scalarRange/scalarRanges.H index 99f9e5e01b3fc4cce925c259fb025ac6eac22d40..f82312b6125872493b759ae2c0b841d67ee2716a 100644 --- a/src/OpenFOAM/primitives/ranges/scalarRange/scalarRanges.H +++ b/src/OpenFOAM/primitives/ranges/scalarRange/scalarRanges.H @@ -55,27 +55,28 @@ public: // Constructors - //- Construct Null + //- Construct null scalarRanges(); //- Construct from Istream. // The list items are comma-delimited. - scalarRanges(Istream&); + scalarRanges(Istream& is); + // Member Functions //- Return true if the given value is within the ranges - bool selected(const scalar) const; + bool selected(const scalar value) const; //- Return the set of selected entries in the given list // that are within the ranges - List<bool> selected(const List<scalar>&) const; + List<bool> selected(const UList<scalar>& values) const; //- Select a list of values that are within the ranges - List<scalar> select(const List<scalar>&) const; + List<scalar> select(const List<scalar>& values) const; //- Select a list of values that are within the ranges - void inplaceSelect(List<scalar>&) const; + void inplaceSelect(List<scalar>& values) const; }; diff --git a/src/OpenFOAM/primitives/strings/lists/wordReListMatcher.H b/src/OpenFOAM/primitives/strings/lists/wordReListMatcher.H index 2bba686964cd2c74adbc30beb81a7ff8bf990445..1f5785d3e493bd1effa133076d9804c171974107 100644 --- a/src/OpenFOAM/primitives/strings/lists/wordReListMatcher.H +++ b/src/OpenFOAM/primitives/strings/lists/wordReListMatcher.H @@ -68,28 +68,28 @@ public: // Member Functions - // Access + // Access - inline label size() const; - inline bool empty() const; + inline label size() const; + inline bool empty() const; - //- Return underlying list of wordRe - inline const UList<wordRe>& operator()() const; + //- Return underlying list of wordRe + inline const UList<wordRe>& operator()() const; - // Searching + // Searching - //- Return true if string matches any of the regular expressions - // Smart match as regular expression or as a string. - // Optionally specify a literal match only. - inline bool match(const string&, bool literalMatch=false) const; + //- Return true if string matches any of the regular expressions + // Smart match as regular expression or as a string. + // Optionally specify a literal match only. + inline bool match(const std::string&, bool literalMatch=false) const; - // Helpers + // Helpers - //- Return a wordReList with duplicate words filtered out. - // No filtering is done on regular expressions. - static wordReList uniq(const UList<wordRe>& input); + //- Return a wordReList with duplicate words filtered out. + // No filtering is done on regular expressions. + static wordReList uniq(const UList<wordRe>& input); }; diff --git a/src/OpenFOAM/primitives/strings/lists/wordReListMatcherI.H b/src/OpenFOAM/primitives/strings/lists/wordReListMatcherI.H index 9c484297df7d939992d8761af6aa00845ac2b36b..d4b3c7dce23f9747d68055548187e2b10c8910f3 100644 --- a/src/OpenFOAM/primitives/strings/lists/wordReListMatcherI.H +++ b/src/OpenFOAM/primitives/strings/lists/wordReListMatcherI.H @@ -58,14 +58,14 @@ Foam::wordReListMatcher::operator()() const inline bool Foam::wordReListMatcher::match ( - const string& str, + const std::string& text, bool literalMatch ) const { - const label nElem = reList_.size(); - for (label elemI = 0; elemI < nElem; ++elemI) + const label n = reList_.size(); + for (label i = 0; i < n; ++i) { - if (reList_[elemI].match(str, literalMatch)) + if (reList_[i].match(text, literalMatch)) { return true; } diff --git a/src/OpenFOAM/primitives/strings/string/string.H b/src/OpenFOAM/primitives/strings/string/string.H index 7010427059effe434f7dd4f30494ef5c286c4915..b1129c7c3a6519e7ab3dd37bdab7279afc84b01f 100644 --- a/src/OpenFOAM/primitives/strings/string/string.H +++ b/src/OpenFOAM/primitives/strings/string/string.H @@ -63,9 +63,9 @@ class Ostream; // Forward declaration of friend functions and operators class string; -Istream& operator>>(Istream&, string&); -Ostream& operator<<(Ostream&, const string&); -Ostream& operator<<(Ostream&, const std::string&); +Istream& operator>>(Istream& is, string& s); +Ostream& operator<<(Ostream& os, const string& s); +Ostream& operator<<(Ostream& os, const std::string& s); /*---------------------------------------------------------------------------*\ @@ -124,33 +124,33 @@ public: //- Count and return the number of a given character in the string size_type count(const char c) const; - //- Is this string type valid? + //- Does the string contain valid characters only? template<class String> - static inline bool valid(const string& str); + static inline bool valid(const std::string& str); - //- Does this string have particular meta-characters? + //- Does this string contain meta-characters? // The meta characters can be optionally quoted. template<class String> - static inline bool meta(const string& str, const char quote = '\\'); + static inline bool meta(const std::string& str, const char quote='\\'); //- Strip invalid characters from the given string template<class String> - static inline bool stripInvalid(string& str); + static inline bool stripInvalid(std::string& str); //- Return a valid String from the given string template<class String> - static inline String validate(const string& str); + static inline String validate(const std::string& str); //- Return a String with quoted meta-characters from the given string template<class String> static inline string quotemeta ( - const string& str, + const std::string& str, const char quote = '\\' ); //- True when strings match literally - inline bool match(const std::string& str) const; + inline bool match(const std::string& text) const; //- Avoid masking the normal std::string replace using std::string::replace; diff --git a/src/OpenFOAM/primitives/strings/string/stringI.H b/src/OpenFOAM/primitives/strings/string/stringI.H index 3dc2fe043fa87f792b31f1bb9f6533cbde1e6b1b..ccd5d29616e1baee69ef287a8b9b0785f0925377 100644 --- a/src/OpenFOAM/primitives/strings/string/stringI.H +++ b/src/OpenFOAM/primitives/strings/string/stringI.H @@ -61,7 +61,7 @@ inline Foam::string::string(const char c) // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // template<class String> -inline bool Foam::string::valid(const string& str) +inline bool Foam::string::valid(const std::string& str) { for (const_iterator iter = str.begin(); iter != str.end(); ++iter) { @@ -75,7 +75,7 @@ inline bool Foam::string::valid(const string& str) template<class String> -inline bool Foam::string::stripInvalid(string& str) +inline bool Foam::string::stripInvalid(std::string& str) { if (!valid<String>(str)) { @@ -85,11 +85,11 @@ inline bool Foam::string::stripInvalid(string& str) for ( const_iterator iter1 = iter2; - iter1 != const_cast<const string&>(str).end(); + iter1 != const_cast<const std::string&>(str).end(); iter1++ ) { - char c = *iter1; + const char c = *iter1; if (String::valid(c)) { @@ -109,20 +109,21 @@ inline bool Foam::string::stripInvalid(string& str) template<class String> -inline bool Foam::string::meta(const string& str, const char quote) +inline bool Foam::string::meta(const std::string& str, const char quote) { int escaped = 0; for (const_iterator iter = str.begin(); iter != str.end(); ++iter) { - if (quote && *iter == quote) + const char c = *iter; + if (quote && c == quote) { escaped ^= 1; // toggle state } else if (escaped) { - escaped = false; + escaped = 0; } - else if (String::meta(*iter)) + else if (String::meta(c)) { return true; } @@ -133,7 +134,7 @@ inline bool Foam::string::meta(const string& str, const char quote) template<class String> inline Foam::string -Foam::string::quotemeta(const string& str, const char quote) +Foam::string::quotemeta(const std::string& str, const char quote) { if (!quote) { @@ -146,7 +147,8 @@ Foam::string::quotemeta(const string& str, const char quote) int escaped = 0; for (const_iterator iter = str.begin(); iter != str.end(); ++iter) { - if (*iter == quote) + const char c = *iter; + if (c == quote) { escaped ^= 1; // toggle state } @@ -154,12 +156,12 @@ Foam::string::quotemeta(const string& str, const char quote) { escaped = 0; } - else if (String::meta(*iter)) + else if (String::meta(c)) { sQuoted += quote; } - sQuoted += *iter; + sQuoted += c; } sQuoted.resize(sQuoted.length()); @@ -169,17 +171,17 @@ Foam::string::quotemeta(const string& str, const char quote) template<class String> -inline String Foam::string::validate(const string& str) +inline String Foam::string::validate(const std::string& str) { string ss = str; stripInvalid<String>(ss); return ss; } -inline bool Foam::string::match(const std::string& str) const +inline bool Foam::string::match(const std::string& text) const { // check as string - return (str == *this); + return (text == *this); } diff --git a/src/OpenFOAM/primitives/strings/stringOps/stringOps.C b/src/OpenFOAM/primitives/strings/stringOps/stringOps.C index 9b7f73a4f2d4c0c68ed42b417ddcc41633cf0226..6df273e13842a0c66895b9e7ea1d3682609e2f3d 100644 --- a/src/OpenFOAM/primitives/strings/stringOps/stringOps.C +++ b/src/OpenFOAM/primitives/strings/stringOps/stringOps.C @@ -183,7 +183,7 @@ Foam::string& Foam::stringOps::inplaceExpand HashTable<string, word, string::hash>::const_iterator fnd = mapping.find(varName); - if (fnd != HashTable<string, word, string::hash>::end()) + if (fnd.found()) { if (altPos != string::npos && altType == '+') { diff --git a/src/dynamicMesh/fvMeshSubset/fvMeshSubset.C b/src/dynamicMesh/fvMeshSubset/fvMeshSubset.C index ad848b7328ae5c93a14d56eceaae4822b93a7ce7..422891cae64b5ac6aefe3720dd48d3575a85789c 100644 --- a/src/dynamicMesh/fvMeshSubset/fvMeshSubset.C +++ b/src/dynamicMesh/fvMeshSubset/fvMeshSubset.C @@ -1384,6 +1384,23 @@ void Foam::fvMeshSubset::setLargeCellSubset } +void Foam::fvMeshSubset::setLargeCellSubset +( + const UList<label>& globalCellMap, + const label patchID, + const bool syncPar +) +{ + labelList region(baseMesh().nCells(), 0); + + for (auto cellId : globalCellMap) + { + region[cellId] = 1; + } + setLargeCellSubset(region, 1, patchID, syncPar); +} + + void Foam::fvMeshSubset::setLargeCellSubset ( const labelHashSet& globalCellMap, diff --git a/src/dynamicMesh/fvMeshSubset/fvMeshSubset.H b/src/dynamicMesh/fvMeshSubset/fvMeshSubset.H index 54c4141107368f3655d1ed35152042eb6151e55e..7d9c453b5e3a58f75bec2f02a069158855d32318 100644 --- a/src/dynamicMesh/fvMeshSubset/fvMeshSubset.H +++ b/src/dynamicMesh/fvMeshSubset/fvMeshSubset.H @@ -177,6 +177,14 @@ public: const bool syncCouples = true ); + //- setLargeCellSubset but only marking certain cells + void setLargeCellSubset + ( + const UList<label>& globalCellMap, + const label patchID = -1, + const bool syncPar = true + ); + //- setLargeCellSubset but with labelHashSet. void setLargeCellSubset ( diff --git a/src/dynamicMesh/motionSmoother/motionSmootherAlgo.C b/src/dynamicMesh/motionSmoother/motionSmootherAlgo.C index 2374086254e9a68ef4c9286e3eb9b28872def6f2..959e13a0bb714bcce698e7c6922067b173755d94 100644 --- a/src/dynamicMesh/motionSmoother/motionSmootherAlgo.C +++ b/src/dynamicMesh/motionSmoother/motionSmootherAlgo.C @@ -90,6 +90,22 @@ void Foam::motionSmootherAlgo::checkFld(const pointScalarField& fld) } +Foam::labelHashSet Foam::motionSmootherAlgo::getPoints +( + const UList<label>& faceLabels +) const +{ + labelHashSet usedPoints(mesh_.nPoints()/100); + + for (auto faceId : faceLabels) + { + usedPoints.insert(mesh_.faces()[faceId]); + } + + return usedPoints; +} + + Foam::labelHashSet Foam::motionSmootherAlgo::getPoints ( const labelHashSet& faceLabels @@ -99,12 +115,7 @@ Foam::labelHashSet Foam::motionSmootherAlgo::getPoints forAllConstIter(labelHashSet, faceLabels, iter) { - const face& f = mesh_.faces()[iter.key()]; - - forAll(f, fp) - { - usedPoints.insert(f[fp]); - } + usedPoints.insert(mesh_.faces()[iter.key()]); } return usedPoints; diff --git a/src/dynamicMesh/motionSmoother/motionSmootherAlgo.H b/src/dynamicMesh/motionSmoother/motionSmootherAlgo.H index fe223d73dbf265f14f15b9fc335aa529d20a0fcd..d1b53c32677c7dc7b8f5683c6da2ad7947bcb7f4 100644 --- a/src/dynamicMesh/motionSmoother/motionSmootherAlgo.H +++ b/src/dynamicMesh/motionSmoother/motionSmootherAlgo.H @@ -209,7 +209,10 @@ class motionSmootherAlgo static void checkFld(const pointScalarField&); //- Get points used by given faces - labelHashSet getPoints(const labelHashSet&) const; + labelHashSet getPoints(const UList<label>& faceLabels) const; + + //- Get points used by given faces + labelHashSet getPoints(const labelHashSet& faceLabels) const; //- Calculate per-edge weight tmp<scalarField> calcEdgeWeights(const pointField&) const; diff --git a/src/dynamicMesh/polyTopoChange/polyTopoChange/edgeCollapser.C b/src/dynamicMesh/polyTopoChange/polyTopoChange/edgeCollapser.C index 0d258a914b9276bb547ad47c2615f15c6ecc04d1..f804091922d686db01782f080e762cccc8ba0700 100644 --- a/src/dynamicMesh/polyTopoChange/polyTopoChange/edgeCollapser.C +++ b/src/dynamicMesh/polyTopoChange/polyTopoChange/edgeCollapser.C @@ -42,7 +42,7 @@ defineTypeNameAndDebug(edgeCollapser, 0); } -Foam::HashSet<Foam::label> Foam::edgeCollapser::checkBadFaces +Foam::labelHashSet Foam::edgeCollapser::checkBadFaces ( const polyMesh& mesh, const dictionary& meshQualityDict diff --git a/src/dynamicMesh/polyTopoChange/polyTopoChange/edgeCollapser.H b/src/dynamicMesh/polyTopoChange/polyTopoChange/edgeCollapser.H index 2d80b2fa35f84cd96123d0682c3a82df6f683f44..4f5f6e7feb465ed4756b209de0577c1b01ab909c 100644 --- a/src/dynamicMesh/polyTopoChange/polyTopoChange/edgeCollapser.H +++ b/src/dynamicMesh/polyTopoChange/polyTopoChange/edgeCollapser.H @@ -263,7 +263,7 @@ public: // Check //- Calls motionSmoother::checkMesh and returns a set of bad faces - static HashSet<label> checkBadFaces + static labelHashSet checkBadFaces ( const polyMesh& mesh, const dictionary& meshQualityDict diff --git a/src/finiteVolume/fvMesh/wallDist/patchDistMethods/patchDistMethod/patchDistMethod.C b/src/finiteVolume/fvMesh/wallDist/patchDistMethods/patchDistMethod/patchDistMethod.C index 617671bef48506fdd488806c357bc5caf9909971..c4c6432e116d8efcbbbfab05497efe378d9ccc40 100644 --- a/src/finiteVolume/fvMesh/wallDist/patchDistMethods/patchDistMethod/patchDistMethod.C +++ b/src/finiteVolume/fvMesh/wallDist/patchDistMethods/patchDistMethod/patchDistMethod.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2015 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -77,6 +77,36 @@ Foam::autoPtr<Foam::patchDistMethod> Foam::patchDistMethod::New } + +Foam::autoPtr<Foam::patchDistMethod> Foam::patchDistMethod::New +( + const dictionary& dict, + const fvMesh& mesh, + const labelHashSet& patchIDs, + const word& defaultPatchDistMethod +) +{ + word patchDistMethodType = defaultPatchDistMethod; + dict.readIfPresent("method", patchDistMethodType); + + Info<< "Selecting patchDistMethod " << patchDistMethodType << endl; + dictionaryConstructorTable::iterator cstrIter = + dictionaryConstructorTablePtr_->find(patchDistMethodType); + + if (cstrIter == dictionaryConstructorTablePtr_->end()) + { + FatalErrorInFunction + << "Unknown patchDistMethodType type " + << patchDistMethodType << endl << endl + << "Valid patchDistMethod types are : " << endl + << dictionaryConstructorTablePtr_->sortedToc() + << exit(FatalError); + } + + return cstrIter()(dict, mesh, patchIDs); +} + + // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // Foam::patchDistMethod::~patchDistMethod() diff --git a/src/finiteVolume/fvMesh/wallDist/patchDistMethods/patchDistMethod/patchDistMethod.H b/src/finiteVolume/fvMesh/wallDist/patchDistMethods/patchDistMethod/patchDistMethod.H index 6f23e3ae7efb815f9844c30be349105147e800e5..573a1a45399cd0271919e2ac479854a88bb209a6 100644 --- a/src/finiteVolume/fvMesh/wallDist/patchDistMethods/patchDistMethod/patchDistMethod.H +++ b/src/finiteVolume/fvMesh/wallDist/patchDistMethods/patchDistMethod/patchDistMethod.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2015-2016 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -118,6 +118,14 @@ public: const labelHashSet& patchIDs ); + static autoPtr<patchDistMethod> New + ( + const dictionary& dict, + const fvMesh& mesh, + const labelHashSet& patchIDs, + const word& defaultPatchDistMethod + ); + //- Destructor virtual ~patchDistMethod(); diff --git a/src/finiteVolume/fvMesh/wallDist/wallDist/wallDist.C b/src/finiteVolume/fvMesh/wallDist/wallDist/wallDist.C index cab438a468f5326372e47c6532d8c15824df9cc6..498daf163758f5705b633ce54c1f0a48bbd93be8 100644 --- a/src/finiteVolume/fvMesh/wallDist/wallDist/wallDist.C +++ b/src/finiteVolume/fvMesh/wallDist/wallDist/wallDist.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2015-2016 OpenFOAM Foundation - \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -68,17 +68,28 @@ void Foam::wallDist::constructn() const // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -Foam::wallDist::wallDist(const fvMesh& mesh, const word& patchTypeName) +Foam::wallDist::wallDist +( + const fvMesh& mesh, + const labelHashSet& patchIDs, + const word& patchTypeName +) : MeshObject<fvMesh, Foam::UpdateableMeshObject, wallDist>(mesh), - patchIDs_(mesh.boundaryMesh().findPatchIDs<wallPolyPatch>()), + patchIDs_(patchIDs), patchTypeName_(patchTypeName), + dict_ + ( + static_cast<const fvSchemes&>(mesh).subOrEmptyDict + ( + patchTypeName_ & "Dist" + ) + ), pdm_ ( patchDistMethod::New ( - static_cast<const fvSchemes&>(mesh) - .subDict(patchTypeName_ & "Dist"), + dict_, mesh, patchIDs_ ) @@ -95,17 +106,9 @@ Foam::wallDist::wallDist(const fvMesh& mesh, const word& patchTypeName) dimensionedScalar("y" & patchTypeName_, dimLength, SMALL), patchDistMethod::patchTypes<scalar>(mesh, patchIDs_) ), - nRequired_ - ( - static_cast<const fvSchemes&>(mesh).subDict(patchTypeName_ & "Dist") - .lookupOrDefault<Switch>("nRequired", false) - ), + nRequired_(dict_.lookupOrDefault<Switch>("nRequired", false)), n_(volVectorField::null()), - updateInterval_ - ( - static_cast<const fvSchemes&>(mesh).subDict(patchTypeName_ & "Dist") - .lookupOrDefault<label>("updateInterval", 1) - ), + updateInterval_(dict_.lookupOrDefault<label>("updateInterval", 1)), requireUpdate_(true) { if (nRequired_) @@ -120,6 +123,7 @@ Foam::wallDist::wallDist(const fvMesh& mesh, const word& patchTypeName) Foam::wallDist::wallDist ( const fvMesh& mesh, + const word& defaultPatchDistMethod, const labelHashSet& patchIDs, const word& patchTypeName ) @@ -127,14 +131,21 @@ Foam::wallDist::wallDist MeshObject<fvMesh, Foam::UpdateableMeshObject, wallDist>(mesh), patchIDs_(patchIDs), patchTypeName_(patchTypeName), + dict_ + ( + static_cast<const fvSchemes&>(mesh).subOrEmptyDict + ( + patchTypeName_ & "Dist" + ) + ), pdm_ ( patchDistMethod::New ( - static_cast<const fvSchemes&>(mesh) - .subDict(patchTypeName_ & "Dist"), + dict_, mesh, - patchIDs_ + patchIDs_, + defaultPatchDistMethod ) ), y_ @@ -149,17 +160,9 @@ Foam::wallDist::wallDist dimensionedScalar("y" & patchTypeName_, dimLength, SMALL), patchDistMethod::patchTypes<scalar>(mesh, patchIDs_) ), - nRequired_ - ( - static_cast<const fvSchemes&>(mesh).subDict(patchTypeName_ & "Dist") - .lookupOrDefault<Switch>("nRequired", false) - ), + nRequired_(dict_.lookupOrDefault<Switch>("nRequired", false)), n_(volVectorField::null()), - updateInterval_ - ( - static_cast<const fvSchemes&>(mesh).subDict(patchTypeName_ & "Dist") - .lookupOrDefault<label>("updateInterval", 1) - ), + updateInterval_(dict_.lookupOrDefault<label>("updateInterval", 1)), requireUpdate_(true) { if (nRequired_) @@ -171,6 +174,17 @@ Foam::wallDist::wallDist } +Foam::wallDist::wallDist(const fvMesh& mesh, const word& patchTypeName) +: + wallDist + ( + mesh, + mesh.boundaryMesh().findPatchIDs<wallPolyPatch>(), + patchTypeName + ) +{} + + // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // Foam::wallDist::~wallDist() diff --git a/src/finiteVolume/fvMesh/wallDist/wallDist/wallDist.H b/src/finiteVolume/fvMesh/wallDist/wallDist/wallDist.H index 5d0aa325fd88993a948e385d0f6f6ea4e78cc771..11a17c3d3328c3bc36afe1a44c4b2f18c5ba2c13 100644 --- a/src/finiteVolume/fvMesh/wallDist/wallDist/wallDist.H +++ b/src/finiteVolume/fvMesh/wallDist/wallDist/wallDist.H @@ -82,6 +82,9 @@ class wallDist //- Name for the patch set, e.g. "wall" const word patchTypeName_; + //- Construction dictionary + const dictionary dict_; + //- Run-time selected method to generate the distance-to-wall field mutable autoPtr<patchDistMethod> pdm_; @@ -121,21 +124,31 @@ public: // Constructors - //- Construct from mesh and optional patch type name + //- Construct from mesh, patch IDs and optional patch type name wallDist ( const fvMesh& mesh, - const word& patchTypeName = "wall" + const labelHashSet& patchIDs, + const word& patchTypeName = "patch" ); - //- Construct from mesh, patch IDs and optional patch type name + //- Construct from mesh, patch IDs, default patch distance method + // and optional patch type name wallDist ( const fvMesh& mesh, + const word& defaultPatchDistMethod, const labelHashSet& patchIDs, const word& patchTypeName = "patch" ); + //- Construct from mesh and optional patch type name + wallDist + ( + const fvMesh& mesh, + const word& patchTypeName = "wall" + ); + //- Destructor virtual ~wallDist(); diff --git a/src/functionObjects/field/ddt2/ddt2.C b/src/functionObjects/field/ddt2/ddt2.C index a3876716585aa57bcea8897ca5cf0e57e0ee2029..08c23fd42f000a65da79d733ebb40902263bc4e9 100644 --- a/src/functionObjects/field/ddt2/ddt2.C +++ b/src/functionObjects/field/ddt2/ddt2.C @@ -178,7 +178,7 @@ bool Foam::functionObjects::ddt2::execute() { results_.clear(); - wordHashSet candidates = subsetStrings(selectFields_, mesh_.names()); + wordHashSet candidates(subsetStrings(selectFields_, mesh_.names())); DynamicList<word> missing(selectFields_.size()); DynamicList<word> ignored(selectFields_.size()); diff --git a/src/functionObjects/field/zeroGradient/zeroGradient.C b/src/functionObjects/field/zeroGradient/zeroGradient.C index ff07607652bf19fa5f3dab52c6dc2d5841998d8b..dedde1fe994816cacd52731038107f08ddc0ac98 100644 --- a/src/functionObjects/field/zeroGradient/zeroGradient.C +++ b/src/functionObjects/field/zeroGradient/zeroGradient.C @@ -135,7 +135,7 @@ bool Foam::functionObjects::zeroGradient::execute() { results_.clear(); - wordHashSet candidates = subsetStrings(selectFields_, mesh_.names()); + wordHashSet candidates(subsetStrings(selectFields_, mesh_.names())); DynamicList<word> missing(selectFields_.size()); DynamicList<word> ignored(selectFields_.size()); diff --git a/src/functionObjects/utilities/ensightWrite/ensightWrite.C b/src/functionObjects/utilities/ensightWrite/ensightWrite.C index 02a17215a71cb8ccc98d2b50c45e30b0dbbd0dbe..30b11ffc9e373ea8f9f08bec40f13a8f6af3d8f4 100644 --- a/src/functionObjects/utilities/ensightWrite/ensightWrite.C +++ b/src/functionObjects/utilities/ensightWrite/ensightWrite.C @@ -246,7 +246,7 @@ bool Foam::functionObjects::ensightWrite::write() ensCase().setTime(t.value(), t.timeIndex()); } - wordHashSet candidates = subsetStrings(selectFields_, mesh_.names()); + wordHashSet candidates(subsetStrings(selectFields_, mesh_.names())); DynamicList<word> missing(selectFields_.size()); DynamicList<word> ignored(selectFields_.size()); diff --git a/src/fvMotionSolver/motionDiffusivity/inverseDistance/inverseDistanceDiffusivity.C b/src/fvMotionSolver/motionDiffusivity/inverseDistance/inverseDistanceDiffusivity.C index d628d09563df40f00612cf0d83562d0a97990713..387bdbdbdaf60f068aafccc8c8e2893ec0d6e34a 100644 --- a/src/fvMotionSolver/motionDiffusivity/inverseDistance/inverseDistanceDiffusivity.C +++ b/src/fvMotionSolver/motionDiffusivity/inverseDistance/inverseDistanceDiffusivity.C @@ -30,6 +30,7 @@ License #include "surfaceInterpolate.H" #include "zeroGradientFvPatchFields.H" #include "wallDist.H" +#include "meshWavePatchDistMethod.H" // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // @@ -72,12 +73,13 @@ Foam::inverseDistanceDiffusivity::~inverseDistanceDiffusivity() void Foam::inverseDistanceDiffusivity::correct() { faceDiffusivity_ = - 1.0 + dimensionedScalar("one", dimLength, 1) /fvc::interpolate ( wallDist::New ( mesh(), + patchDistMethods::meshWave::typeName, mesh().boundaryMesh().patchSet(patchNames_) ).y() ); diff --git a/src/mesh/snappyHexMesh/meshRefinement/meshRefinement.H b/src/mesh/snappyHexMesh/meshRefinement/meshRefinement.H index 0fd25e3fde5911bf02efa4d5e47bbac80e53f519..8b04374266f1fb48309617e6a497e876dacb073f 100644 --- a/src/mesh/snappyHexMesh/meshRefinement/meshRefinement.H +++ b/src/mesh/snappyHexMesh/meshRefinement/meshRefinement.H @@ -1352,6 +1352,12 @@ public: const labelHashSet& set ) const; + // Pick up faces of cells of faces in set. + labelList growFaceCellFace + ( + const UList<label>& set + ) const; + // Pick up faces of cells of faces in set. labelList growFaceCellFace ( diff --git a/src/mesh/snappyHexMesh/meshRefinement/meshRefinementMerge.C b/src/mesh/snappyHexMesh/meshRefinement/meshRefinementMerge.C index 3b27edf918309797dc21cd143bb23bd1e45cf69a..ba70983f591c2c957e9eaa5b15965f0164b064b9 100644 --- a/src/mesh/snappyHexMesh/meshRefinement/meshRefinementMerge.C +++ b/src/mesh/snappyHexMesh/meshRefinement/meshRefinementMerge.C @@ -790,37 +790,76 @@ Foam::labelList Foam::meshRefinement::collectFaces } -// Pick up faces of cells of faces in set. -Foam::labelList Foam::meshRefinement::growFaceCellFace -( - const labelHashSet& set -) const +namespace Foam { - boolList selected(mesh_.nFaces(), false); - - forAllConstIter(faceSet, set, iter) + // Pick up faces of cells of faces in set. + // file-scope + static inline void markGrowFaceCellFace + ( + const polyMesh& pMesh, + const label faceI, + boolList& selected + ) { - label faceI = iter.key(); - - label own = mesh_.faceOwner()[faceI]; + const label own = pMesh.faceOwner()[faceI]; - const cell& ownFaces = mesh_.cells()[own]; + const cell& ownFaces = pMesh.cells()[own]; forAll(ownFaces, ownFaceI) { selected[ownFaces[ownFaceI]] = true; } - if (mesh_.isInternalFace(faceI)) + if (pMesh.isInternalFace(faceI)) { - label nbr = mesh_.faceNeighbour()[faceI]; + const label nbr = pMesh.faceNeighbour()[faceI]; - const cell& nbrFaces = mesh_.cells()[nbr]; + const cell& nbrFaces = pMesh.cells()[nbr]; forAll(nbrFaces, nbrFaceI) { selected[nbrFaces[nbrFaceI]] = true; } } } +} + + +// Pick up faces of cells of faces in set. +Foam::labelList Foam::meshRefinement::growFaceCellFace +( + const UList<label>& set +) const +{ + boolList selected(mesh_.nFaces(), false); + + for (auto faceI : set) + { + markGrowFaceCellFace(mesh_, faceI, selected); + } + + syncTools::syncFaceList + ( + mesh_, + selected, + orEqOp<bool>() // combine operator + ); + return findIndices(selected, true); +} + + +// Pick up faces of cells of faces in set. +Foam::labelList Foam::meshRefinement::growFaceCellFace +( + const labelHashSet& set +) const +{ + boolList selected(mesh_.nFaces(), false); + + forAllConstIter(labelHashSet, set, iter) + { + const label faceI = iter.key(); + markGrowFaceCellFace(mesh_, faceI, selected); + } + syncTools::syncFaceList ( mesh_, diff --git a/src/meshTools/coordinateSystems/coordinateRotation/coordinateRotationNew.C b/src/meshTools/coordinateSystems/coordinateRotation/coordinateRotationNew.C index 02b111038be5f2d6322479681fb1a3c2356e801c..ef1a0a0a278a938244d3d8e8f9c9df20b365e703 100644 --- a/src/meshTools/coordinateSystems/coordinateRotation/coordinateRotationNew.C +++ b/src/meshTools/coordinateSystems/coordinateRotation/coordinateRotationNew.C @@ -30,22 +30,24 @@ License Foam::autoPtr<Foam::coordinateRotation> Foam::coordinateRotation::New ( - const dictionary& dict, const objectRegistry& obr + const dictionary& dict, + const objectRegistry& obr ) { if (debug) { - Pout<< "coordinateRotation::New(const dictionary&) : " + Pout<< "coordinateRotation::New" + "(const dictionary&, const objectRegistry&) : " << "constructing coordinateRotation" << endl; } - word rotType = dict.lookup("type"); + const word rotType = dict.lookup("type"); objectRegistryConstructorTable::iterator cstrIter = objectRegistryConstructorTablePtr_->find(rotType); - if (cstrIter == dictionaryConstructorTablePtr_->end()) + if (!cstrIter.found()) { FatalIOErrorInFunction ( @@ -54,7 +56,7 @@ Foam::autoPtr<Foam::coordinateRotation> Foam::coordinateRotation::New << rotType << nl << nl << "Valid coordinateRotation types are :" << nl << "[default: axes ]" - << dictionaryConstructorTablePtr_->sortedToc() + << objectRegistryConstructorTablePtr_->sortedToc() << exit(FatalIOError); } @@ -74,12 +76,12 @@ Foam::autoPtr<Foam::coordinateRotation> Foam::coordinateRotation::New << endl; } - word rotType = dict.lookup("type"); + const word rotType = dict.lookup("type"); dictionaryConstructorTable::iterator cstrIter = dictionaryConstructorTablePtr_->find(rotType); - if (cstrIter == dictionaryConstructorTablePtr_->end()) + if (!cstrIter.found()) { FatalIOErrorInFunction ( diff --git a/src/meshTools/edgeFaceCirculator/edgeFaceCirculator.C b/src/meshTools/edgeFaceCirculator/edgeFaceCirculator.C index 9149eaf92004d857a4689402ac26136cfffd0119..40d52a2fba2bf708c222f4f21ddef7e94131051d 100644 --- a/src/meshTools/edgeFaceCirculator/edgeFaceCirculator.C +++ b/src/meshTools/edgeFaceCirculator/edgeFaceCirculator.C @@ -25,12 +25,16 @@ License #include "edgeFaceCirculator.H" #include "primitiveMesh.H" +#include "nullObject.H" // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // const Foam::edgeFaceCirculator Foam::edgeFaceCirculator::endConstIter ( - *reinterpret_cast<primitiveMesh*>(0), // primitiveMesh + *reinterpret_cast<const primitiveMesh*> + ( + NullObject::nullObject.pointer() // nullptr dummy + ), -1, // faceLabel false, // ownerSide -1, // index diff --git a/src/meshTools/sets/topoSets/cellSet.C b/src/meshTools/sets/topoSets/cellSet.C index c38bb209c60b65cfdd6c32df0f201e68f4882acf..32a9506f3925db3c58382deef656ad9143ff4390 100644 --- a/src/meshTools/sets/topoSets/cellSet.C +++ b/src/meshTools/sets/topoSets/cellSet.C @@ -30,29 +30,26 @@ License #include "addToRunTimeSelectionTable.H" #include "mapDistributePolyMesh.H" -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // namespace Foam { - -// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // - defineTypeNameAndDebug(cellSet, 0); addToRunTimeSelectionTable(topoSet, cellSet, word); addToRunTimeSelectionTable(topoSet, cellSet, size); addToRunTimeSelectionTable(topoSet, cellSet, set); - +} // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -cellSet::cellSet(const IOobject& obj) +Foam::cellSet::cellSet(const IOobject& obj) : topoSet(obj, typeName) {} -cellSet::cellSet +Foam::cellSet::cellSet ( const polyMesh& mesh, const word& name, @@ -67,7 +64,7 @@ cellSet::cellSet } -cellSet::cellSet +Foam::cellSet::cellSet ( const polyMesh& mesh, const word& name, @@ -79,7 +76,7 @@ cellSet::cellSet {} -cellSet::cellSet +Foam::cellSet::cellSet ( const polyMesh& mesh, const word& name, @@ -91,7 +88,7 @@ cellSet::cellSet {} -cellSet::cellSet +Foam::cellSet::cellSet ( const polyMesh& mesh, const word& name, @@ -103,8 +100,20 @@ cellSet::cellSet {} +Foam::cellSet::cellSet +( + const polyMesh& mesh, + const word& name, + const UList<label>& set, + writeOption w +) +: + topoSet(mesh, name, set, w) +{} + + // Database constructors (for when no mesh available) -cellSet::cellSet +Foam::cellSet::cellSet ( const Time& runTime, const word& name, @@ -114,32 +123,13 @@ cellSet::cellSet : topoSet ( - IOobject - ( - name, - runTime.findInstance - ( - polyMesh::meshSubDir/"sets", //polyMesh::meshSubDir, - word::null, //"faces" - IOobject::MUST_READ, - runTime.findInstance - ( - polyMesh::meshSubDir, - "faces", - IOobject::READ_IF_PRESENT - ) - ), - polyMesh::meshSubDir/"sets", - runTime, - r, - w - ), + findIOobject(runTime, name, r, w), typeName ) {} -cellSet::cellSet +Foam::cellSet::cellSet ( const Time& runTime, const word& name, @@ -149,32 +139,13 @@ cellSet::cellSet : topoSet ( - IOobject - ( - name, - runTime.findInstance - ( - polyMesh::meshSubDir/"sets", //polyMesh::meshSubDir, - word::null, //"faces" - IOobject::NO_READ, - runTime.findInstance - ( - polyMesh::meshSubDir, - "faces", - IOobject::READ_IF_PRESENT - ) - ), - polyMesh::meshSubDir/"sets", - runTime, - IOobject::NO_READ, - w - ), + findIOobject(runTime, name, IOobject::NO_READ, w), size ) {} -cellSet::cellSet +Foam::cellSet::cellSet ( const Time& runTime, const word& name, @@ -184,26 +155,7 @@ cellSet::cellSet : topoSet ( - IOobject - ( - name, - runTime.findInstance - ( - polyMesh::meshSubDir/"sets", //polyMesh::meshSubDir, - word::null, //"faces" - IOobject::NO_READ, - runTime.findInstance - ( - polyMesh::meshSubDir, - "faces", - IOobject::READ_IF_PRESENT - ) - ), - polyMesh::meshSubDir/"sets", - runTime, - IOobject::NO_READ, - w - ), + findIOobject(runTime, name, IOobject::NO_READ, w), set ) {} @@ -211,25 +163,25 @@ cellSet::cellSet // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // -cellSet::~cellSet() +Foam::cellSet::~cellSet() {} // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // -label cellSet::maxSize(const polyMesh& mesh) const +Foam::label Foam::cellSet::maxSize(const polyMesh& mesh) const { return mesh.nCells(); } -void cellSet::updateMesh(const mapPolyMesh& morphMap) +void Foam::cellSet::updateMesh(const mapPolyMesh& morphMap) { updateLabels(morphMap.reverseCellMap()); } -void cellSet::distribute(const mapDistributePolyMesh& map) +void Foam::cellSet::distribute(const mapDistributePolyMesh& map) { boolList inSet(map.nOldCells()); forAllConstIter(cellSet, *this, iter) @@ -271,8 +223,4 @@ void Foam::cellSet::writeDebug } -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - -} // End namespace Foam - // ************************************************************************* // diff --git a/src/meshTools/sets/topoSets/cellSet.H b/src/meshTools/sets/topoSets/cellSet.H index 4f02b2cdb6e84cd11c4acae34abab22c75d58da7..260695f6ad1d9df71dee0bdc76bade63c3b5440b 100644 --- a/src/meshTools/sets/topoSets/cellSet.H +++ b/src/meshTools/sets/topoSets/cellSet.H @@ -53,7 +53,7 @@ class cellSet // Private Member Functions //- Disallow default bitwise copy construct - cellSet(const cellSet&); + cellSet(const cellSet&) = delete; public: @@ -81,7 +81,7 @@ public: ( const polyMesh& mesh, const word& name, - const label sizes, + const label size, writeOption w=NO_WRITE ); @@ -90,7 +90,7 @@ public: ( const polyMesh& mesh, const word& name, - const topoSet&, + const topoSet& set, writeOption w=NO_WRITE ); @@ -99,7 +99,16 @@ public: ( const polyMesh& mesh, const word& name, - const labelHashSet&, + const labelHashSet& set, + writeOption w=NO_WRITE + ); + + //- Construct from additional list of labels for the labelHashSet + cellSet + ( + const polyMesh& mesh, + const word& name, + const UList<label>& set, writeOption w=NO_WRITE ); diff --git a/src/meshTools/sets/topoSets/faceSet.C b/src/meshTools/sets/topoSets/faceSet.C index 7049f36a66f4429f592597cc19d53c41d50f5a83..4d9c25640be866fb3addf784505c9010b77141a6 100644 --- a/src/meshTools/sets/topoSets/faceSet.C +++ b/src/meshTools/sets/topoSets/faceSet.C @@ -31,29 +31,26 @@ License #include "addToRunTimeSelectionTable.H" -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // namespace Foam { - -// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // - defineTypeNameAndDebug(faceSet, 0); addToRunTimeSelectionTable(topoSet, faceSet, word); addToRunTimeSelectionTable(topoSet, faceSet, size); addToRunTimeSelectionTable(topoSet, faceSet, set); +} +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - -faceSet::faceSet(const IOobject& obj) +Foam::faceSet::faceSet(const IOobject& obj) : topoSet(obj, typeName) {} -faceSet::faceSet +Foam::faceSet::faceSet ( const polyMesh& mesh, const word& name, @@ -67,7 +64,7 @@ faceSet::faceSet } -faceSet::faceSet +Foam::faceSet::faceSet ( const polyMesh& mesh, const word& name, @@ -79,7 +76,7 @@ faceSet::faceSet {} -faceSet::faceSet +Foam::faceSet::faceSet ( const polyMesh& mesh, const word& name, @@ -91,7 +88,7 @@ faceSet::faceSet {} -faceSet::faceSet +Foam::faceSet::faceSet ( const polyMesh& mesh, const word& name, @@ -103,15 +100,27 @@ faceSet::faceSet {} +Foam::faceSet::faceSet +( + const polyMesh& mesh, + const word& name, + const UList<label>& set, + writeOption w +) +: + topoSet(mesh, name, set, w) +{} + + // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // -faceSet::~faceSet() +Foam::faceSet::~faceSet() {} // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // -void faceSet::sync(const polyMesh& mesh) +void Foam::faceSet::sync(const polyMesh& mesh) { boolList set(mesh.nFaces(), false); @@ -150,19 +159,19 @@ void faceSet::sync(const polyMesh& mesh) } -label faceSet::maxSize(const polyMesh& mesh) const +Foam::label Foam::faceSet::maxSize(const polyMesh& mesh) const { return mesh.nFaces(); } -void faceSet::updateMesh(const mapPolyMesh& morphMap) +void Foam::faceSet::updateMesh(const mapPolyMesh& morphMap) { updateLabels(morphMap.reverseFaceMap()); } -void faceSet::distribute(const mapDistributePolyMesh& map) +void Foam::faceSet::distribute(const mapDistributePolyMesh& map) { boolList inSet(map.nOldFaces()); forAllConstIter(faceSet, *this, iter) @@ -193,7 +202,7 @@ void faceSet::distribute(const mapDistributePolyMesh& map) } -void faceSet::writeDebug +void Foam::faceSet::writeDebug ( Ostream& os, const primitiveMesh& mesh, @@ -204,8 +213,4 @@ void faceSet::writeDebug } -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - -} // End namespace Foam - // ************************************************************************* // diff --git a/src/meshTools/sets/topoSets/faceSet.H b/src/meshTools/sets/topoSets/faceSet.H index 75b5f817d1e7006cba77b7379adc28da9699b740..18787ca5debf8e0aa1f3047b2c272750b3c2ee5b 100644 --- a/src/meshTools/sets/topoSets/faceSet.H +++ b/src/meshTools/sets/topoSets/faceSet.H @@ -51,7 +51,6 @@ class faceSet public topoSet { - public: //- Runtime type information @@ -78,7 +77,7 @@ public: ( const polyMesh& mesh, const word& name, - const label, + const label size, writeOption w=NO_WRITE ); @@ -87,7 +86,7 @@ public: ( const polyMesh& mesh, const word& name, - const topoSet&, + const topoSet& set, writeOption w=NO_WRITE ); @@ -96,7 +95,16 @@ public: ( const polyMesh& mesh, const word& name, - const labelHashSet&, + const labelHashSet& set, + writeOption w=NO_WRITE + ); + + //- Construct from additional list of labels for the labelHashSet + faceSet + ( + const polyMesh& mesh, + const word& name, + const UList<label>& set, writeOption w=NO_WRITE ); diff --git a/src/meshTools/sets/topoSets/pointSet.C b/src/meshTools/sets/topoSets/pointSet.C index e9d3542cf7eb85064d14224101004c9342782bf3..32d1d90f9b85edde5793b8332fd9f17529af2660 100644 --- a/src/meshTools/sets/topoSets/pointSet.C +++ b/src/meshTools/sets/topoSets/pointSet.C @@ -28,32 +28,28 @@ License #include "polyMesh.H" #include "syncTools.H" #include "mapDistributePolyMesh.H" - #include "addToRunTimeSelectionTable.H" -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // namespace Foam { - -// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // - defineTypeNameAndDebug(pointSet, 0); addToRunTimeSelectionTable(topoSet, pointSet, word); addToRunTimeSelectionTable(topoSet, pointSet, size); addToRunTimeSelectionTable(topoSet, pointSet, set); - +} // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -pointSet::pointSet(const IOobject& obj) +Foam::pointSet::pointSet(const IOobject& obj) : topoSet(obj, typeName) {} -pointSet::pointSet +Foam::pointSet::pointSet ( const polyMesh& mesh, const word& name, @@ -67,7 +63,7 @@ pointSet::pointSet } -pointSet::pointSet +Foam::pointSet::pointSet ( const polyMesh& mesh, const word& name, @@ -79,7 +75,7 @@ pointSet::pointSet {} -pointSet::pointSet +Foam::pointSet::pointSet ( const polyMesh& mesh, const word& name, @@ -91,7 +87,7 @@ pointSet::pointSet {} -pointSet::pointSet +Foam::pointSet::pointSet ( const polyMesh& mesh, const word& name, @@ -103,15 +99,27 @@ pointSet::pointSet {} +Foam::pointSet::pointSet +( + const polyMesh& mesh, + const word& name, + const UList<label>& set, + writeOption w +) +: + topoSet(mesh, name, set, w) +{} + + // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // -pointSet::~pointSet() +Foam::pointSet::~pointSet() {} // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // -void pointSet::sync(const polyMesh& mesh) +void Foam::pointSet::sync(const polyMesh& mesh) { // Convert to boolList @@ -145,19 +153,19 @@ void pointSet::sync(const polyMesh& mesh) } -label pointSet::maxSize(const polyMesh& mesh) const +Foam::label Foam::pointSet::maxSize(const polyMesh& mesh) const { return mesh.nPoints(); } -void pointSet::updateMesh(const mapPolyMesh& morphMap) +void Foam::pointSet::updateMesh(const mapPolyMesh& morphMap) { updateLabels(morphMap.reversePointMap()); } -void pointSet::distribute(const mapDistributePolyMesh& map) +void Foam::pointSet::distribute(const mapDistributePolyMesh& map) { boolList inSet(map.nOldPoints()); forAllConstIter(pointSet, *this, iter) @@ -188,7 +196,7 @@ void pointSet::distribute(const mapDistributePolyMesh& map) } -void pointSet::writeDebug +void Foam::pointSet::writeDebug ( Ostream& os, const primitiveMesh& mesh, @@ -198,8 +206,4 @@ void pointSet::writeDebug topoSet::writeDebug(os, mesh.points(), maxLen); } -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - -} // End namespace Foam - // ************************************************************************* // diff --git a/src/meshTools/sets/topoSets/pointSet.H b/src/meshTools/sets/topoSets/pointSet.H index e5f92b12052af3d78a70dcbc8b6be1b78dd46d6a..c31f86e688aaebdb730c064a298520b94d1b94b2 100644 --- a/src/meshTools/sets/topoSets/pointSet.H +++ b/src/meshTools/sets/topoSets/pointSet.H @@ -78,16 +78,16 @@ public: ( const polyMesh& mesh, const word& name, - const label, + const label size, writeOption w=NO_WRITE ); - //- Construct from additional labelHashSet + //- Construct from existing set pointSet ( const polyMesh& mesh, const word& name, - const topoSet&, + const topoSet& set, writeOption w=NO_WRITE ); @@ -96,7 +96,16 @@ public: ( const polyMesh& mesh, const word& name, - const labelHashSet&, + const labelHashSet& set, + writeOption w=NO_WRITE + ); + + //- Construct from additional list of labels for the labelHashSet + pointSet + ( + const polyMesh& mesh, + const word& name, + const UList<label>& set, writeOption w=NO_WRITE ); diff --git a/src/meshTools/sets/topoSets/topoSet.C b/src/meshTools/sets/topoSets/topoSet.C index 59f0ea2ff5ad6fa8847c3979da84ab3374e4d9b3..cc65eb8097e075fd63845f991507a64e76a4bcc8 100644 --- a/src/meshTools/sets/topoSets/topoSet.C +++ b/src/meshTools/sets/topoSets/topoSet.C @@ -289,6 +289,63 @@ void Foam::topoSet::writeDebug } +Foam::IOobject Foam::topoSet::findIOobject +( + const polyMesh& mesh, + const word& name, + readOption r, + writeOption w +) +{ + return IOobject + ( + name, + mesh.time().findInstance + ( + mesh.dbDir()/polyMesh::meshSubDir/"sets", + word::null, + r, + mesh.facesInstance() + ), + polyMesh::meshSubDir/"sets", + mesh, + r, + w + ); +} + + +Foam::IOobject Foam::topoSet::findIOobject +( + const Time& runTime, + const word& name, + readOption r, + writeOption w +) +{ + return IOobject + ( + name, + runTime.findInstance + ( + polyMesh::meshSubDir/"sets", + word::null, + IOobject::MUST_READ, + runTime.findInstance + ( + polyMesh::meshSubDir, + "faces", + IOobject::READ_IF_PRESENT + ) + ), + polyMesh::meshSubDir/"sets", + runTime, + r, + w + ); +} + + // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // Foam::topoSet::topoSet(const IOobject& obj, const word& wantedType) @@ -324,24 +381,7 @@ Foam::topoSet::topoSet writeOption w ) : - regIOobject - ( - IOobject - ( - name, - mesh.time().findInstance - ( - mesh.dbDir()/polyMesh::meshSubDir/"sets", - word::null, - r, //IOobject::MUST_READ, - mesh.facesInstance() - ), - polyMesh::meshSubDir/"sets", - mesh, - r, - w - ) - ) + regIOobject(findIOobject(mesh, name, r, w)) { if ( @@ -371,24 +411,7 @@ Foam::topoSet::topoSet writeOption w ) : - regIOobject - ( - IOobject - ( - name, - mesh.time().findInstance - ( - mesh.dbDir()/polyMesh::meshSubDir/"sets", - word::null, - IOobject::NO_READ, - mesh.facesInstance() - ), - polyMesh::meshSubDir/"sets", - mesh, - IOobject::NO_READ, - w - ) - ), + regIOobject(findIOobject(mesh, name, IOobject::NO_READ, w)), labelHashSet(size) {} @@ -401,24 +424,20 @@ Foam::topoSet::topoSet writeOption w ) : - regIOobject - ( - IOobject - ( - name, - mesh.time().findInstance - ( - mesh.dbDir()/polyMesh::meshSubDir/"sets", - word::null, - IOobject::NO_READ, - mesh.facesInstance() - ), - polyMesh::meshSubDir/"sets", - mesh, - IOobject::NO_READ, - w - ) - ), + regIOobject(findIOobject(mesh, name, IOobject::NO_READ, w)), + labelHashSet(set) +{} + + +Foam::topoSet::topoSet +( + const polyMesh& mesh, + const word& name, + const UList<label>& set, + writeOption w +) +: + regIOobject(findIOobject(mesh, name, IOobject::NO_READ, w)), labelHashSet(set) {} diff --git a/src/meshTools/sets/topoSets/topoSet.H b/src/meshTools/sets/topoSets/topoSet.H index 3d0ec883b9998b1ca229ff235aa82ae8ae11b0cc..7a8ecbae92c98caad0db035226484ff1bc625287 100644 --- a/src/meshTools/sets/topoSets/topoSet.H +++ b/src/meshTools/sets/topoSets/topoSet.H @@ -108,7 +108,29 @@ protected: //- Disallow default bitwise copy construct - topoSet(const topoSet&); + topoSet(const topoSet&) = delete; + + +protected: + + //- Helper for constructor - return IOobject in the polyMesh/sets + static IOobject findIOobject + ( + const polyMesh& mesh, + const word& name, + readOption r, + writeOption w + ); + + //- Helper for constructor - return IOobject in the polyMesh/sets + static IOobject findIOobject + ( + const Time& runTime, + const word& name, + readOption r, + writeOption w + ); + public: @@ -175,7 +197,7 @@ public: //- Construct from IOobject as explicitly passed type. // Can't use typeName info here since subclasses not yet instantiated - topoSet(const IOobject&, const word& wantedType); + topoSet(const IOobject& obj, const word& wantedType); //- Construct from polyMesh and name. Searches for a polyMesh/sets // directory but not beyond mesh.facesInstance. @@ -195,7 +217,18 @@ public: ( const polyMesh& mesh, const word& name, - const label, + const label size, + writeOption w=NO_WRITE + ); + + //- Construct empty from additional labelHashSet + // Searches for a polyMesh/sets + // directory but not beyond mesh.facesInstance. + topoSet + ( + const polyMesh& mesh, + const word& name, + const labelHashSet& set, writeOption w=NO_WRITE ); @@ -206,7 +239,7 @@ public: ( const polyMesh& mesh, const word& name, - const labelHashSet&, + const UList<label>&, writeOption w=NO_WRITE ); diff --git a/src/sampling/probes/patchProbesTemplates.C b/src/sampling/probes/patchProbesTemplates.C index d43444cb919c8fea2c0aa6c1fe05e2011d3dfadf..ab2b3208ff8a5ff434ebaaa1a85bbf523e84f8c5 100644 --- a/src/sampling/probes/patchProbesTemplates.C +++ b/src/sampling/probes/patchProbesTemplates.C @@ -115,7 +115,7 @@ void Foam::patchProbes::sampleAndWrite if ( - iter != objectRegistry::end() + iter.found() && iter()->type() == GeometricField<Type, fvPatchField, volMesh>::typeName ) @@ -167,7 +167,7 @@ void Foam::patchProbes::sampleAndWriteSurfaceFields if ( - iter != objectRegistry::end() + iter.found() && iter()->type() == GeometricField<Type, fvsPatchField, surfaceMesh>::typeName ) diff --git a/src/sampling/probes/probesTemplates.C b/src/sampling/probes/probesTemplates.C index 3ac7b08379f0502ce0906a1908f7a4aa1a97200e..b5c469365774132a4c1c495e98d56e452622831b 100644 --- a/src/sampling/probes/probesTemplates.C +++ b/src/sampling/probes/probesTemplates.C @@ -141,7 +141,7 @@ void Foam::probes::sampleAndWrite(const fieldGroup<Type>& fields) if ( - iter != objectRegistry::end() + iter.found() && iter()->type() == GeometricField<Type, fvPatchField, volMesh>::typeName ) @@ -190,7 +190,7 @@ void Foam::probes::sampleAndWriteSurfaceFields(const fieldGroup<Type>& fields) if ( - iter != objectRegistry::end() + iter.found() && iter()->type() == GeometricField<Type, fvsPatchField, surfaceMesh>::typeName ) diff --git a/src/surfMesh/surfaceFormats/starcd/STARCDsurfaceFormatCore.C b/src/surfMesh/surfaceFormats/starcd/STARCDsurfaceFormatCore.C index 1894f293af4f2e887425125f7df3d5b4568ec9f2..b926f96802d0510054cc5287c773dda39a0b87c4 100644 --- a/src/surfMesh/surfaceFormats/starcd/STARCDsurfaceFormatCore.C +++ b/src/surfMesh/surfaceFormats/starcd/STARCDsurfaceFormatCore.C @@ -58,7 +58,7 @@ Foam::fileFormats::STARCDsurfaceFormatCore::readInpCellTable ); string line; - List<string> groups; + List<std::string> groups; while (is.good() && is.getLine(line).good()) { if (ctnameRE.match(line, groups)) diff --git a/src/thermophysicalModels/thermophysicalFunctions/thermophysicalFunction/thermophysicalFunction.C b/src/thermophysicalModels/thermophysicalFunctions/thermophysicalFunction/thermophysicalFunction.C index 16e8f0971a8f029d3034d1f8ce166559535efc4c..f26d2426812c47ecb2ab3aec4c02a3301e91cefe 100644 --- a/src/thermophysicalModels/thermophysicalFunctions/thermophysicalFunction/thermophysicalFunction.C +++ b/src/thermophysicalModels/thermophysicalFunctions/thermophysicalFunction/thermophysicalFunction.C @@ -55,7 +55,7 @@ Foam::autoPtr<Foam::thermophysicalFunction> Foam::thermophysicalFunction::New IstreamConstructorTable::iterator cstrIter = IstreamConstructorTablePtr_->find(thermophysicalFunctionType); - if (cstrIter == IstreamConstructorTablePtr_->end()) + if (!cstrIter.found()) { FatalErrorInFunction << "Unknown thermophysicalFunction type " @@ -87,7 +87,7 @@ Foam::autoPtr<Foam::thermophysicalFunction> Foam::thermophysicalFunction::New dictionaryConstructorTable::iterator cstrIter = dictionaryConstructorTablePtr_->find(thermophysicalFunctionType); - if (cstrIter == IstreamConstructorTablePtr_->end()) + if (!cstrIter.found()) { FatalErrorInFunction << "Unknown thermophysicalFunction type "