Commit 55e5a777 authored by shunbo's avatar shunbo
Browse files

initial commit

parents
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011 OpenFOAM Foundation
Copyright (C) 2017-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Description
\*---------------------------------------------------------------------------*/
#include "argList.H"
#include "Fstream.H"
#include "ListOps.H"
#include "IndirectList.H"
#include "labelIndList.H"
#include "SortList.H"
#include "Random.H"
#include <functional>
using namespace Foam;
template<class ListType>
void printInfo(const ListType& lst)
{
Info<< "full: " << flatOutput(lst.values()) << nl
<< "addr: " << flatOutput(lst.addressing()) << nl
<< "list: " << flatOutput(lst) << nl
<< endl;
Info<<"for-range :";
for (const auto& val : lst)
{
Info<< " " << val;
}
Info<< nl;
}
template<class T, class ListType>
void testFind(const T& val, const ListType& lst)
{
Info<< nl
<< "Search for "<< val << " in " << flatOutput(lst) << nl
<<" found() = " << lst.found(val)
<<" find() = " << lst.find(val)
<<" rfind() = " << lst.rfind(val)
<<" find(2) = " << lst.find(val, 2)
<<" rfind(2) = " << lst.rfind(val, 2) << nl
<< nl;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
argList::noCheckProcessorDirectories();
argList::addOption
(
"binary",
"file",
"write lists in binary to specified file"
);
argList args(argc, argv);
List<label> completeList(20);
forAll(completeList, i)
{
completeList[i] = 10*i;
}
Info<< "raw : " << flatOutput(completeList) << nl << endl;
List<label> addresses{1, 0, 3, 7, 4, 8, 5, 1, 0, 3, 7, 4, 8, 5, };
labelIndList idl1(completeList, addresses);
printInfo(idl1);
for (const label val : { 10, 30, 40, 50, 90, 80, 120 } )
{
testFind(val, idl1);
}
inplaceReverseList(addresses);
idl1.addressing() = std::move(addresses);
printInfo(idl1);
// Test copying
labelUIndList uidl1(idl1);
labelIndList idl2(uidl1);
labelIndList idl3(idl2);
printInfo(uidl1);
idl1.addressing().clear();
// idl2.addressing().clear();
Info<<"after reset addressing:" << nl << endl;
printInfo(uidl1);
printInfo(idl1);
printInfo(idl2);
printInfo(idl3);
fileName binaryOutput;
if (args.readIfPresent("binary", binaryOutput))
{
Info<<"Writing output to " << binaryOutput << endl;
OFstream os(binaryOutput, IOstream::BINARY);
os.writeEntry("idl1", idl1);
os.writeEntry("idl2", idl2);
os.writeEntry("idl3", idl3);
}
if (Pstream::parRun())
{
if (Pstream::master())
{
Pout<< "full: " << flatOutput(idl3.values()) << nl
<< "send: " << flatOutput(idl3) << endl;
for (const int proci : Pstream::subProcs())
{
OPstream toSlave(Pstream::commsTypes::scheduled, proci);
toSlave << idl3;
}
}
else
{
// From master
IPstream fromMaster
(
Pstream::commsTypes::scheduled,
Pstream::masterNo()
);
List<label> recv(fromMaster);
Pout<<"recv: " << flatOutput(recv) << endl;
}
// MPI barrier
bool barrier = true;
Pstream::scatter(barrier);
}
// SortList
{
List<scalar> list1(20);
Random rnd(1234);
for (scalar& val : list1)
{
val = 100 * rnd.sample01<scalar>();
}
// Pick out 1/2 the values and make the negative
for (label i=0; i < list1.size()/2; ++i)
{
label pos = rnd.position(0, list1.size()-1);
list1[pos] = -list1[pos];
}
Info<< nl << "Random list: " << flatOutput(list1) << nl;
SortList<scalar> sorter1(list1);
Info<< nl << "Sort indices: " << flatOutput(sorter1.indices()) << nl;
Info<< nl << "Reverse indices: " << flatOutput(sorter1.indices()) << nl;
sorter1.reverse();
Info<< nl << "Again indices: " << flatOutput(sorter1.indices()) << nl;
sorter1.reverseSort();
Info<< nl << "Reverse indices: " << flatOutput(sorter1.indices()) << nl;
Info<< nl << "Sorted : " << flatOutput(sorter1) << nl;
sorter1.sort(std::greater<scalar>());
SortList<scalar> sorter2(list1, std::greater<scalar>());
sorter2.reverse();
Info<< "sorted: ";
for (const auto& val : sorter2)
{
Info<< ' ' << val;
}
Info<< nl;
sorter2.sort([](scalar a, scalar b) { return mag(a) < mag(b); });
Info<< nl << "Mag sorted: " << flatOutput(sorter2) << nl;
}
Info<< "End\n" << endl;
return 0;
}
// ************************************************************************* //
Test-IntRange.C
EXE = $(FOAM_USER_APPBIN)/Test-IntRange
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
Application
Test-IntRange
Description
Test integer range
\*---------------------------------------------------------------------------*/
#include "argList.H"
#include "labelPair.H"
#include "IntRange.H"
#include "StringStream.H"
using namespace Foam;
template<class T>
void printInfo(const IntRange<T>& range)
{
Info<< " min " << range.min() << nl
<< " max " << range.max() << nl
<< " size " << range.size() << nl
<< "begin end " << *range.cbegin() << ' ' << *range.cend() << nl;
Info<< "rbegin rend " << *range.rbegin() << ' ' << *range.rend() << nl;
}
template<class T>
void printValues(const IntRange<T>& range)
{
Info<< range.size() << "(";
for (const label val : range)
{
Info<< ' ' << val;
}
Info<< " )";
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
argList::noParallel();
argList::noFunctionObjects();
argList args(argc, argv, false, true);
// Fails static_assert
/// Info<< "Default construct float: " << IntRange<float>().size() << nl;
// Does not really make sense, but...
/// Info<< "Default construct bool: " << IntRange<bool>().size() << nl;
typedef IntRange<int> intRange;
Info<< "Default construct int32_t: " << IntRange<int32_t>() << nl
<< "Default construct int64_t: " << IntRange<int64_t>() << nl;
Info<< " one: " << intRange(10) << nl
<< " two: " << intRange(5, 10) << nl;
// Read from stream
{
IStringStream is("(10 100)");
intRange range;
is >> range;
Info<< "From stream int32_t: " << range << nl;
}
for
(
const labelPair& pr
: labelPairList
{
{10, 2},
{2, 3},
{100, 0}
}
)
{
intRange range(pr.first(), pr.second());
Info<< "range: " << range
<< (range ? " non-empty" : " Empty") << nl;
}
{
const intRange range1(3, 16);
auto begIter = range1.begin();
auto endIter = range1.end();
auto midIter = range1.at(range1.size()/2);
Info<< "iterator tests on " << range1 << nl;
Info<< "beg = " << *begIter << nl
<< "end = " << *endIter << nl
<< "mid = " << *midIter << nl
<< "end - beg = " << (endIter - begIter) << nl;
Info<< "distance: " << std::distance(begIter, endIter) << nl;
Info<< "beg + 10 = " << *(begIter + 10) << nl
<< "beg[100] = " << begIter[100] << nl;
// Info<< "10 + beg = " << *(10 + begIter) << nl;
// Will not work:
// Avoid this definition since it participates in too many resolution
// attempts and ruins everything.
std::swap(begIter, endIter);
Info<< "after iterator swap" << nl
<< "beg = " << *begIter << nl
<< "end = " << *endIter << nl;
auto rbegIter = range1.rbegin();
auto rendIter = range1.rend();
Info<< nl
<< "reverse beg = " << *rbegIter << nl
<< "reverse end = " << *rendIter << nl
<< "reverse end - beg = " << (rendIter - rbegIter) << nl;
Info<< "reverse beg + 10 = " << *(rbegIter + 10) << nl
<< "reverse beg[100] = " << rbegIter[100] << nl;
std::swap(rbegIter, rendIter);
Info<< "after iterator swap" << nl
<< "reverse beg = " << *rbegIter << nl
<< "reverse end = " << *rendIter << nl;
}
Info<< "\nEnd\n" << endl;
return 0;
}
// ************************************************************************* //
Test-LabelledItem.C
EXE = $(FOAM_USER_APPBIN)/Test-LabelledItem
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
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 LabelledItem (formerly 'Keyed', but that was never used)
\*---------------------------------------------------------------------------*/
#include "IOstreams.H"
#include "IOobject.H"
#include "IFstream.H"
#include "edge.H"
#include "LabelledItem.H"
#include "List.H"
using namespace Foam;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
typedef LabelledItem<edge> labelledEdge;
List<labelledEdge> edges(10);
forAll(edges, edgei)
{
auto& e = edges[edgei];
e.insert(20-edgei);
e.insert(edgei);
if (!(edgei % 3))
{
e.setIndex(edgei);
}
}
Info<< "edges: " << edges << nl;
Foam::sort(edges);
Info<< "sorted: " << edges << nl;
Info<< "\nEnd\n" << endl;
return 0;
}
// ************************************************************************* //
Test-List.C
EXE = $(FOAM_USER_APPBIN)/Test-List
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2017-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Application
Test-List
Description
Simple tests and examples of use of List
See also
Foam::List
\*---------------------------------------------------------------------------*/
#include "OSspecific.H"
#include "argList.H"
#include "wordRes.H"
#include "IOstreams.H"
#include "StringStream.H"
#include "scalar.H"
#include "vector.H"
#include "labelRange.H"
#include "scalarList.H"
#include "HashOps.H"
#include "ListOps.H"
#include "IndirectList.H"
#include "SubList.H"
#include "SliceList.H"
#include "ListPolicy.H"
#include <list>
#include <numeric>
#include <functional>
// see issue #2083
#undef Foam_constructList_from_iterators
namespace Foam
{
// Verify inheritance
class MyStrings
:
public List<string>
{
public:
using List<string>::List;
};
namespace Detail
{
namespace ListPolicy
{
// Override on a per-type basis
template<> struct short_length<short> : std::integral_constant<short,20> {};
} // End namespace ListPolicy
} // End namespace Detail
} // End namespace Foam
using namespace Foam;
template<class T, class ListType>
void testFind(const T& val, const ListType& lst)
{
Info<< nl
<< "Search for "<< val << " in " << flatOutput(lst) << nl
<<" found() = " << lst.found(val)
<<" find() = " << lst.find(val)
<<" rfind() = " << lst.rfind(val)
<<" find(2) = " << lst.find(val, 2)
<<" rfind(2) = " << lst.rfind(val, 2) << nl
<< nl;
}
void printMyString(const UList<string>& lst)
{
MyStrings slist2(lst);
Info<<slist2 << nl;
}
template<class T>
Ostream& printListOutputType(const char* what)
{
Info<< what
<< " (contiguous="
<< is_contiguous<T>::value << " no_linebreak="
<< Detail::ListPolicy::no_linebreak<T>::value
<< " short_length="
<< Detail::ListPolicy::short_length<T>::value << ')';
return Info;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
argList::noParallel();
argList::noFunctionObjects();
argList::addOption("reList", "reList");
argList::addOption("wordList", "wordList");
argList::addOption("stringList", "stringList");
argList::addOption("float", "xx");
argList::addBoolOption("create", "Test ListOps::create functionality");
argList::addBoolOption("ListList", "Test list of list functionality");
argList::addBoolOption("flag");
#include "setRootCase.H"
{
List<label> ident(15);
std::iota(ident.begin(), ident.end(), 0);
Info<<"Ident:";
forAllConstIters(ident, iter)
{
Info<<" " << *iter;
}
Info<< nl;
Info<<"reverse:";
forAllReverseIters(ident, iter)
{
Info<<" " << *iter;
}
Info<< nl;
Info<<"const reverse:";
forAllConstReverseIters(ident, iter)
{
Info<<" " << *iter;
}
Info<< nl;
}
if (false)
{
labelList intlist(IStringStream("(0 1 2)")());
Info<<"construct from Istream: " << intlist << endl;
IStringStream("(3 4 5)")() >> static_cast<labelUList&>(intlist);
Info<<"is >>: " << intlist << endl;
IStringStream("(6 7 8)")() >> intlist;
Info<<"is >>: " << intlist << endl;
}
List<vector> list1(IStringStream("1 ((0 1 2))")());
Info<< "list1: " << list1 << endl;
List<vector> list2
{
vector(0, 1, 2),
vector(3, 4, 5),
vector(6, 7, 8),
vector(0, 1, 2),
vector(3, 4, 5),
vector(6, 7, 8),
};
Info<< "list2: " << list2 << endl;
Info<< "forAllConstIters(list2): ";
forAllConstIters(list2, iter) { Info<< " " << *iter; }
Info<< endl;
Info<< "forAllConstReverseIters(list2): ";
forAllConstReverseIters(list2, iter) { Info<< " " << *iter; }
Info<< endl;
Info<< "forAllConstIters(list2): ";
forAllIters(list2, iter) { *iter *= 2; Info<< " " << *iter; }
Info<< endl;
Info<< "forAllReverseIters(list2): ";
forAllReverseIters(list2, iter) { *iter *= 0.5; Info<< " " << *iter; }
Info<< endl;
list1.append(list2);
Info<< "list1.append(list2): " << list1 << endl;
for (const vector& val : { vector(3, 4, 5), vector(10,11, 12)} )
{
testFind(val, list2);
}
list2.setSize(10, vector(1, 2, 3));
Info<< "list2: " << list2 << endl;
List<vector> list3(std::move(list2));
Info<< "Move construct" << endl;
Info<< "list2: " << list2 << nl
<< "list3: " << list3 << endl;
List<vector> list4
{
vector(0, 1, 2),
vector(3, 4, 5),
vector(6, 7, 8)
};
Info<< "list4: " << list4 << endl;
List<vector> list5
{
{5, 3, 1},
{10, 2, 2},
{8, 1, 0}
};
Info<< "list5: " << list5 << endl;
list5 =
{
{8, 1, 0},
{5, 3, 1},
{10, 2, 2}
};
Info<< "list5: " << list5 << endl;
list4.swap(list5);
Info<< "Swapped via the swap() method" << endl;
Info<< "list4: " << list4 << nl
<< "list5: " << list5 << endl;
#ifdef Foam_constructList_from_iterators
List<vector> list6(list4.begin(), list4.end());
Info<< "list6: " << list6 << endl;
#else
Info<< "NOTE: no construction from two iterators" << endl;
#endif
// Subset
const labelList map{0, 2};
List<vector> subList3(list3, map);
Info<< "Elements " << map << " out of " << list3
<< " => " << subList3 << endl;
// test flattened output
{
Info<< nl;
labelList longLabelList = identity(15);
// This will not work:
// scalarList slist = identity(15);
//
// More writing, but does work:
#ifdef Foam_constructList_from_iterators
scalarList slist(labelRange().begin(), labelRange(15).end());
Info<<"scalar identity:" << flatOutput(slist) << endl;
#else
Info<<"No iterator means of creating a scalar identity list" << endl;
#endif
printListOutputType<label>("labels") << nl;
Info<< "normal: " << longLabelList << nl;
Info<< "flatOutput: " << flatOutput(longLabelList) << nl;
// Info<< "flatOutput(14): " << flatOutput(longLabelList, 14) << nl;
auto shrtList = ListOps::create<short>
(
longLabelList,
[](const label& val){ return val; }
);
printListOutputType<short>("short") << nl;
Info<< "normal: " << shrtList << nl;
stringList longStringList(12);
forAll(longStringList, i)
{
longStringList[i].resize(3, 'a' + i);
}
printListOutputType<string>("string") << nl;
Info<< "normal: " << longStringList << nl;
Info<< "flatOutput: " << flatOutput(longStringList) << nl;
auto wList = ListOps::create<word>
(
longStringList,
[](const std::string& val){ return val; }
);
printListOutputType<word>("word") << nl;
Info<< "normal: " << wList << nl;
// Shorten
longStringList.resize(8);
wList.resize(8);
Info<< "Test shorter lists" << nl;
printListOutputType<string>("string") << nl;
Info<< "normal: " << longStringList << nl;
printListOutputType<word>("word") << nl;
Info<< "normal: " << wList << nl;
}
// 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;
{
// A valid range
const labelRange subset(4, 5);
// Assign some values
longLabelList.slice(subset) = identity(subset.size());
Info<<"assigned identity in range:" << subset
<< "=> " << flatOutput(longLabelList) << nl;
labelList someList(identity(24));
longLabelList.slice(subset) =
SliceList<label>(someList, sliceRange(8, subset.size(), 2));
Info<<"assigned sliced/stride in range:" << subset
<< "=> " << flatOutput(longLabelList) << nl;
// Does not work - need a reference, not a temporary
// Foam::reverse(longLabelList[subset]);
{
auto sub(longLabelList.slice(subset));
Foam::reverse(sub);
}
Info<<"reversed range:" << subset
<< "=> " << flatOutput(longLabelList) << 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.slice(labelRange(-10, 12)) = 200;
longLabelList.slice({18,3}) = 100;
longLabelList.slice({23,3}) = 400;
// and complete misses
longLabelList.slice({500,50}) = 100;
// -ve size suppressed by internal 'validateRange' = no-op
longLabelList.slice({5,-5}) = 42;
longLabelList.slice({21,100}) = 42;
//Good: does not compile
longLabelList.slice(labelRange(20,50)) = constLabelList;
//Good: does not compile
// longLabelList[labelRange(20,50)] = fixedLabelList;
Info<< "updated: " << constLabelList.slice(labelRange(23,5)) << nl;
Info<< "updated: " << flatOutput(longLabelList) << nl;
//Nope: sort(longLabelList.slice(labelRange(18,5)));
{
// Instead
auto sub = longLabelList.slice(labelRange(8));
sort(sub);
}
Info<< "sub-sorted: " << flatOutput(longLabelList) << nl;
#ifdef Foam_constructList_from_iterators
// Construct from a label-range
labelRange range(25,15);
labelList ident(range.begin(), range.end());
Info<<"range-list (label)=" << ident << nl;
List<scalar> sident(range.begin(), range.end());
Info<<"range-list (scalar)=" << sident << nl;
// // Sub-ranges also work
// List<scalar> sident2(range.at(3), range.at(10));
// Info<<"subrange-list (scalar)=" << sident2 << nl;
// VERY BAD IDEA: List<scalar> sident3(range.at(10), range.at(3));
// This doesn't work, and don't know what it should do anyhow
// List<vector> vident(range.begin(), range.end());
// Info<<"range-list (vector)=" << vident << nl;
// Even weird things like this
List<scalar> sident4(labelRange().begin(), labelRange(8).end());
Info<<"range-list (scalar)=" << sident4 << nl;
#else
Info<< "NOTE: no construction of labelList from range pair" << nl
<< "use identity(...) instead" << endl;
#endif
}
wordReList reLst;
wordList wLst;
stringList sLst;
scalar xxx(-1);
if (args.found("flag"))
{
Info<<"-flag:" << args["flag"] << endl;
}
if (args.found("create"))
{
Info<< nl << "Test ListOps::create functionality" << nl;
const auto labels = identity(15);
Info<< "labels: " << flatOutput(labels) << endl;
{
auto scalars = ListOps::create<scalar>
(
labels,
[](const label& val){ return scalar(1.5*val); }
);
Info<< "scalars: " << flatOutput(scalars) << endl;
}
{
auto vectors = ListOps::create<vector>
(
labels,
[](const label& val){ return vector(1.2*val, -1.2*val, 0); }
);
Info<< "vectors: " << flatOutput(vectors) << endl;
}
{
auto longs = ListOps::create<long>
(
labels,
[](const label& val){ return val; }
);
Info<< "longs: " << flatOutput(longs) << endl;
}
{
auto negs = ListOps::create<label>
(
labels,
std::negate<label>()
);
Info<< "negs: " << flatOutput(negs) << endl;
}
{
auto scalars = ListOps::create<scalar>
(
labelRange().cbegin(),
labelRange(15).cend(),
[](const label& val){ return scalar(-1.125*val); }
);
Info<< "scalars: " << flatOutput(scalars) << endl;
}
#if WM_LABEL_SIZE == 32
{
List<int64_t> input(10);
std::iota(input.begin(), input.end(), 50);
auto output = ListOps::create<label>
(
input,
labelOp<int64_t>()
);
Info<< "label (from int64): " << flatOutput(output) << endl;
}
#elif WM_LABEL_SIZE == 64
{
List<int32_t> input(10);
std::iota(input.begin(), input.end(), 50);
auto output = ListOps::create<label>
(
input,
labelOp<int32_t>()
);
Info<< "label (from int32): " << flatOutput(output) << endl;
}
#endif
labelHashSet locations{ -15, 5, 10, 15, 25, 35 };
Info<< nl << "Test for createWithValue with locations :"
<< flatOutput(locations.sortedToc()) << nl;
{
auto output = ListOps::createWithValue<label>
(
30,
locations.toc(), // Any order
100,
-1 // default value
);
Info<< "with labelUList: " << flatOutput(output)
<< " selector: " << flatOutput(locations.sortedToc()) << nl;
}
{
auto output = ListOps::createWithValue<label>
(
30,
locations,
100,
-1 // default value
);
Info<< "with labelHashSet: " << flatOutput(output)
<< " selector: " << flatOutput(locations) << nl;
}
{
bitSet select = HashSetOps::bitset(locations);
auto output = ListOps::createWithValue<label>
(
30,
select,
100,
-1 // default value
);
Info<< "with bitSet: " << flatOutput(output)
<< " selector: " << flatOutput(select.toc()) << nl;
}
{
List<bool> select = HashSetOps::bools(locations);
auto output = ListOps::createWithValue<label>
(
30,
select,
100,
-1 // default value
);
Info<< "with boolList: " << flatOutput(output)
<< " selector: " << flatOutput(select) << nl;
}
// Repeat with a shorter selector
locations = { -15, 5, 10 };
{
auto output = ListOps::createWithValue<label>
(
30,
locations,
100,
-1 // default value
);
Info<< "with labelHashSet: " << flatOutput(output)
<< " selector: " << flatOutput(locations) << nl;
}
{
bitSet select = HashSetOps::bitset(locations);
auto output = ListOps::createWithValue<label>
(
30,
select,
100,
-1 // default value
);
Info<< "with bitSet: " << flatOutput(output)
<< " selector: " << flatOutput(HashSetOps::used(select))
<< nl;
}
{
List<bool> select = HashSetOps::bools(locations);
auto output = ListOps::createWithValue<label>
(
30,
select,
100,
-1 // default value
);
Info<< "with boolList: " << flatOutput(output)
<< " selector: " << flatOutput(HashSetOps::used(select))
<< nl;
}
}
if (args.found("ListList"))
{
{
labelListList listlist(5, identity(5));
Info<<"list-list with length/val:" << listlist << nl;
}
{
labelListList listlist(one{}, identity(5));
Info<<"list-list 1/val:" << listlist << nl;
}
{
labelList content = identity(5);
labelListList listlist(one{}, content);
Info<<"list-list 1/copy val:" << listlist
<<" - from " << content << nl;
}
{
labelList content = identity(5);
labelListList listlist(one{}, std::move(content));
Info<<"list-list 1/move val:" << listlist
<<" - from " << content << nl;
}
{
labelListList listlist(one{}, Zero);
Info<<"list-list 1/move val:" << listlist
<< nl;
}
}
if (args.readIfPresent<scalar>("float", xxx))
{
Info<<"read float " << xxx << endl;
}
args.readListIfPresent<wordRe>("reList", reLst);
args.readListIfPresent<word>("wordList", wLst);
if (args.readListIfPresent<string>("stringList", sLst))
{
printMyString(sLst);
}
Info<< nl
<< "-reList: " << flatOutput(reLst) << nl
<< "-wordList: " << flatOutput(wLst) << nl
<< "-stringList: " << flatOutput(sLst) << endl;
// Hash values
{
labelList list1(identity(5));
labelList list2(identity(5));
Info<<"hash of " << flatOutput(list1)
<< " = " << Hash<labelList>()(list1) << " or "
<< labelList::hasher()(list1) << nl;
Info<<"hash of " << flatOutput(list2) << " = "
<< Hash<labelList>()(list2) << " or "
<< labelList::hasher()(list2) << nl;
}
return 0;
}
// ************************************************************************* //
Test-List2.C
EXE = $(FOAM_USER_APPBIN)/Test-List2
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Application
Test-List2
Description
Test speeds, usability of some List/FixedList operations
\*---------------------------------------------------------------------------*/
#include "argList.H"
#include "FixedList.H"
#include "labelList.H"
#include "vectorList.H"
#include "ListOps.H"
#include "IFstream.H"
#include "OFstream.H"
#include "cpuTime.H"
#include <initializer_list>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
using namespace Foam;
template<class ListType>
void runResizeTest
(
const label nLoops,
ListType& list,
std::initializer_list<label> sizes
)
{
cpuTime timer;
const label size0 = list.size();
const auto val = list.first();
Info<<"Resize list(" << list.size() << ") to";
for (auto len : sizes)
{
Info<< " " << len;
}
Info<< nl;
Info<< "Perform " << nLoops << " times..." << nl;
for (label iLoop = 0; iLoop < nLoops; ++iLoop)
{
list.setSize(size0, val);
for (auto len : sizes)
{
list.setSize(len, val);
}
}
Info<< "Operation took"
<< " " << timer.cpuTimeIncrement() << " s\n\n";
}
template<class ListType>
void runOrderingTest(const label nLoops, const ListType& list)
{
if (true)
{
cpuTime timer;
float total = 0;
Info<<"forAll - perform " << nLoops << " times..." << nl;
for (label iLoop = 0; iLoop < nLoops; ++iLoop)
{
float sum = 0;
forAll(list, i)
{
sum += list[i];
}
total += sum;
}
Info<< "Operation (sum " << total << ") took"
<< " " << timer.cpuTimeIncrement() << " s\n\n";
}
if (true)
{
cpuTime timer;
float total = 0;
Info<<"reverse pointer loop - perform " << nLoops << " times..." << nl;
for (label iLoop = 0; iLoop < nLoops; ++iLoop)
{
float sum = 0;
const typename ListType::value_type* __restrict__ fp
= (list).end();
label i = (list).size();
while (i--)
{
sum += (*--fp);
}
total += sum;
}
Info<< "Operation (sum " << total << ") took"
<< " " << timer.cpuTimeIncrement() << " s\n\n";
}
if (true)
{
cpuTime timer;
float total = 0;
Info<<"forward pointer loop - perform " << nLoops << " times..." << nl;
for (label iLoop = 0; iLoop < nLoops; ++iLoop)
{
float sum = 0;
const typename ListType::value_type* __restrict__ fp
= (list).begin();
label i = (list).size();
while (i--)
{
sum += (*fp++);
}
total += sum;
}
Info<< "Operation (sum " << total << ") took"
<< " " << timer.cpuTimeIncrement() << " s\n\n";
}
if (true)
{
cpuTime timer;
float total = 0;
Info<<"for loop - perform " << nLoops << " times..." << nl;
for (label iLoop = 0; iLoop < nLoops; ++iLoop)
{
float sum = 0;
const typename ListType::value_type* __restrict__ fp
= (list).begin();
const label sz = (list).size();
for (label i=0; i<sz; ++i)
{
sum += fp[i];
}
total += sum;
}
Info<< "Operation (sum " << total << ") took"
<< " " << timer.cpuTimeIncrement() << " s\n\n";
}
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
int main(int argc, char *argv[])
{
argList::addBoolOption("label");
argList::addBoolOption("float");
argList::addBoolOption("vector");
argList::addBoolOption("order");
argList::addBoolOption("labelList");
argList::addBoolOption("vectorList");
argList args(argc, argv);
if (args.options().empty())
{
Info<< nl << "Specify an option! " << nl << endl;
}
std::initializer_list<label> increments
= {10000, 20000, 40000, 80000, 160000};
if (args.found("label"))
{
List<label> list(10, 1);
runResizeTest(100000, list, increments);
}
if (args.found("float"))
{
List<double> list(10, 1.0);
runResizeTest(10000, list, increments);
}
if (args.found("vector"))
{
List<vector> list(10, vector::one);
runResizeTest(10000, list, increments);
}
if (args.found("labelList"))
{
typedef labelList testType;
testType initVal(500, label(1));
List<testType> list(10, initVal);
runResizeTest(200, list, increments);
}
if (args.found("vectorList"))
{
typedef vectorList testType;
testType initVal(500, vector::one);
List<testType> list(10, initVal);
runResizeTest(100, list, increments);
}
if (args.found("order"))
{
List<label> list(100000000, 1);
runOrderingTest(100, list);
}
Info<< nl << "Done" << nl << endl;
return 0;
}
// ************************************************************************* //
Test-List3.C
EXE = $(FOAM_USER_APPBIN)/Test-List3
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Application
Test-List3
Description
Test list construction
\*---------------------------------------------------------------------------*/
#include "argList.H"
#include "FixedList.H"
#include "labelList.H"
#include "vectorList.H"
#include "ListOps.H"
#include "IFstream.H"
#include "OFstream.H"
#include "cpuTime.H"
#include <initializer_list>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
using namespace Foam;
template<class T>
void printAddress(const UList<T>& list)
{
Info<< "list addr: " << name(&list)
<< " data addr: " << name(list.cdata()) << nl;
}
template<class T>
void printAddress(const SLList<T>& list)
{
Info<< "list addr: " << name(&list)
<< " data addr: ???" << nl;
}
template<class T>
void printAddresses(const List<List<T>>& list)
{
for (const auto& elem : list)
{
printAddress(elem);
}
}
template<class T>
void printAddresses(const SLList<List<T>>& list)
{
for (const auto& elem : list)
{
printAddress(elem);
}
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
int main(int argc, char *argv[])
{
argList::addBoolOption("labelListList");
argList args(argc, argv, false);
if (args.options().empty())
{
Info<< nl << "Specify an option! " << nl << endl;
}
if (args.found("labelListList"))
{
for (label argi=1; argi < args.size(); ++argi)
{
if (true)
{
IFstream is(args.get<fileName>(argi));
Info<< nl << nl
<< "read from " << is.name() << nl << endl;
SLList<List<label>> sll(is);
Info<< "read " << sll.size() << " entries" << nl;
Info<< "sll" << nl;
for (const auto& elem : sll)
{
printAddress(elem);
}
// List<List<label>> list(std::move(sll));
List<List<label>> list;
Info<< "move to List" << nl;
list = std::move(sll);
Info<< "sll" << nl;
for (const auto& elem : sll)
{
printAddress(elem);
}
Info<< "list" << nl;
printAddresses(list);
}
if (true)
{
IFstream is(args.get<fileName>(argi));
Info<< nl << nl
<< "read from " << is.name() << nl << endl;
List<List<label>> list(is);
Info<< "list" << nl;
for (const auto& elem : list)
{
printAddress(elem);
}
}
}
}
Info<< nl << "Done" << nl << endl;
return 0;
}
// ************************************************************************* //
// List of labelList
(
(1 2 3 4)
(5 6 7 8)
(15 16 17 18)
)
Test-ListOps.C
EXE = $(FOAM_USER_APPBIN)/Test-ListOps
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2012-2013 OpenFOAM Foundation
Copyright (C) 2019-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Application
Test-ListOps
Description
\*---------------------------------------------------------------------------*/
#include "argList.H"
#include "List.H"
#include "SubList.H"
#include "ListOps.H"
#include "labelField.H"
#include "MinMax.H"
#include "face.H"
using namespace Foam;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
Info<< "Test Rotations:" << nl << endl;
List<label> forwardRotate(identity(5));
face testFace(identity(4));
for (label i = 0; i < 8; ++i)
{
Info<< "Rotate forward by " << i << " : "
<< rotateList(forwardRotate, i) << endl;
}
for (label i = 0; i < 8; ++i)
{
Info<< "Rotate backward by " << i << " : "
<< rotateList(forwardRotate, -i) << endl;
}
Info<< nl << "Face : " << testFace << endl;
Info<< "Rotate by 2 : " << rotateList(testFace, 2) << endl;
inplaceRotateList<List, label>(testFace, -6);
Info<< "Rotate inplace by -6 : " << testFace << nl << endl;
Info<< "Test inplace rotate : " << forwardRotate << endl;
inplaceRotateList(forwardRotate, 2);
Info<< "Rotate to the right by 2 : " << forwardRotate << endl;
inplaceRotateList(forwardRotate, -2);
Info<< "Rotate to the left by 2 : " << forwardRotate << endl;
List<label> subRotate(identity(10));
SubList<label> subL(subRotate, 5, 3);
Info<< "Test inplace rotate on sublist : " << subRotate << endl;
inplaceRotateList(subL, 3);
Info<< "Rotate to the right by 3 : " << subRotate << endl;
inplaceRotateList(subL, -8);
Info<< "Rotate to the left by 3 : " << subRotate << endl;
Info<< nl << nl << "Test Reversing:" << nl << endl;
Info<< "List : " << identity(5) << endl;
Info<< "Reverse : " << reverseList(identity(5)) << endl;
Info<< "List : " << identity(6) << endl;
Info<< "Reverse : " << reverseList(identity(6)) << nl << endl;
List<label> test1(identity(5));
Info<< "List : " << test1 << endl;
inplaceReverseList(test1);
Info<< "Inplace Reverse : " << test1 << nl << endl;
List<label> test2(identity(6));
Info<< "List : " << test2 << endl;
inplaceReverseList(test2);
Info<< "Inplace Reverse : " << test2 << nl << endl;
face test3(identity(6));
Info<< "Face : " << test3 << endl;
inplaceReverseList(test3);
Info<< "Inplace Reverse : " << test3 << nl << endl;
FixedList<label, 6> test4(identity(6));
Info<< "FixedList : " << test4 << endl;
inplaceReverseList(test4);
Info<< "Inplace Reverse : " << test4 << nl << endl;
List<label> test5(identity(9));
SubList<label> test5SubList(test5, 4, 3);
Info<< "List : " << test5 << endl;
inplaceReverseList(test5SubList);
Info<< "Reverse Sublist between 3 and 6 : " << test5 << nl << endl;
Info<< nl << "Test lambda predicates:" << nl << endl;
List<label> test6(identity(11, -4));
// Add multiplier for mpore interesting testing
std::for_each(test6.begin(), test6.end(), [](label& x){ x *= 3; });
// Randomize the list
Foam::shuffle(test6);
Info<< "randomized input list: " << flatOutput(test6) << nl;
const auto evenNonZero = [](const label& x){ return x && !(x % 2); };
Info<< "location of first even/non-zero: "
<< ListOps::find(test6, evenNonZero) << nl;
Info<< "find > 12 && divisible by 5 : "
<< ListOps::find
(
test6,
[](const label& x) { return x > 12 && !(x % 5); }
) << nl;
Info<< "Found >= 8 : "
<< ListOps::found(test6, labelMinMax(8, labelMax)) << nl;
Info<< "Found >= 25 : "
<< ListOps::found(test6, labelMinMax(25, labelMax)) << nl;
Info<< "Subset of non-zero, even values: "
<< subsetList(test6, evenNonZero) << nl
<< endl;
test6.append(identity(13, 12));
Info<< "Randomized: " << flatOutput(test6) << endl;
inplaceUniqueSort(test6);
Info<< "Unique : " << flatOutput(test6) << endl;
// List reorder
labelList oldToNew(identity(40));
Foam::shuffle(oldToNew);
// Force a few -1:
oldToNew[4] = oldToNew[8] = -1;
Info<<"Test reorder - oldToNew:" << nl
<< flatOutput(oldToNew) << nl << nl;
bitSet bitset
(
ListOps::createWithValue<bool>
(
25,
labelList({8, 12, 15, 22, 4}),
true
)
);
Info<<"bitset input: " << flatOutput(bitset) << nl;
inplaceReorder(oldToNew, bitset);
Info<<" reorder: " << flatOutput(bitset) << nl << nl;
PackedList<2> packed
(
ListOps::createWithValue<label>
(
25,
labelList({8, 12, 15, 22, 4}),
2
)
);
Info<< "packed input: " << packed << nl;
inplaceReorder(oldToNew, packed);
Info<<" reorder: " << packed << nl << nl;
Info<< "\nEnd\n" << endl;
return 0;
}
// ************************************************************************* //
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment