Commit 97b64758 authored by Andrew Heather's avatar Andrew Heather
Browse files

Merge branch 'feature-improved-container-classes' into 'develop'

improved container classes

See merge request !108
parents 7edd801c 6747d14d
......@@ -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;
......
......@@ -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);
......
......@@ -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;
}
......
......@@ -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;
......
......@@ -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;
}
......
......@@ -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;
......
......@@ -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
......
......@@ -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;
}
......
......@@ -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;
......
Test-cpluplus1.C
EXE = $(FOAM_USER_APPBIN)/Test-cpluplus1
/* EXE_INC = -I$(LIB_SRC)/cfdTools/include */
/* EXE_LIBS = -lfiniteVolume */
/*---------------------------------------------------------------------------*\
========= |
\\ / 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;
}
// ************************************************************************* //
......@@ -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);
......
......@@ -28,10 +28,6 @@ Description
\*---------------------------------------------------------------------------*/
#include "argList.H"
#include "IOobject.H"
#include "IOstreams.H"
#include "IFstream.H"
#include "IStringStream.H"