ENH: Pair, Tuple2 improvements (#1160)

- move constructors, construct from std::pair
- input/output of std::pair

Makes it easier when using data structures for other codes.
This commit is contained in:
Mark Olesen
2019-01-08 22:16:41 +01:00
parent 08dc8d0e39
commit a0a7da2edd
4 changed files with 240 additions and 133 deletions

View File

@ -25,10 +25,11 @@ Application
Test-Tuple2
Description
Test construction, comparision etc for Tuple2 and Pair.
\*---------------------------------------------------------------------------*/
#include "Pair.H"
#include "labelPair.H"
#include "Tuple2.H"
#include "label.H"
#include "scalar.H"
@ -67,6 +68,26 @@ struct special2
};
// Print info
void printTuple2(const Tuple2<word, word>& t)
{
Info<< "tuple: " << t << nl;
Info<< "first @: " << long(t.first().data()) << nl;
Info<< "second @: " << long(t.second().data()) << nl;
}
// Print info
void printTuple2(const Pair<word>& t)
{
Info<< "tuple: " << t << nl;
Info<< "first @: " << long(t.first().data()) << nl;
Info<< "second @: " << long(t.second().data()) << nl;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
@ -74,11 +95,14 @@ int main()
{
typedef Tuple2<label, scalar> indexedScalar;
Info<< "null constructed Tuple: " << indexedScalar() << nl;
Info<< "null constructed Pair: " << Pair<scalar>() << nl;
indexedScalar t2(1, 3.2);
Info<< "tuple: "
Info<< "Foam::Tuple2: "
<< t2 << " => "
<< t2.first() << " " << t2.second() << nl;
<< t2.first() << ' ' << t2.second() << nl;
// As list. Generated so that we have duplicate indices
List<indexedScalar> list1(3*4);
@ -113,25 +137,83 @@ int main()
<< nl << list1 << nl;
Info<< nl << nl << "Pairs" << nl;
{
Info<< nl << nl << "Foam::Pair" << nl;
typedef Pair<label> indexedLabel;
typedef Pair<label> indexedLabel;
indexedLabel pr(1, 3);
indexedLabel pr(1, 3);
Info<< "pair: "
<< pr << " => "
<< pr.first() << " " << pr.second() << nl;
Info<< "pair: "
<< pr << " => "
<< pr.first() << ' ' << pr.second() << nl;
List<indexedLabel> list2 = ListOps::create<indexedLabel>
(
list1,
[](const indexedScalar& t2) {
return indexedLabel(t2.first(), t2.second());
}
);
List<indexedLabel> list2 = ListOps::create<indexedLabel>
(
list1,
[](const indexedScalar& t2)
{
return indexedLabel(t2.first(), t2.second());
}
);
Info<< "Unsorted pairs:" << nl << list2 << nl;
Info<< "Unsorted pairs:" << nl << list2 << nl;
}
{
Info<< nl << nl << "std::pair" << nl;
typedef std::pair<label, label> indexedLabel;
indexedLabel pr(1, 3);
Info<< "pair: "
<< pr << " => "
<< pr.first << ' ' << pr.second << nl;
List<indexedLabel> list2 = ListOps::create<indexedLabel>
(
list1,
[](const indexedScalar& t2)
{
return indexedLabel(t2.first(), t2.second());
}
);
Info<< "Unsorted pairs:" << nl << list2 << nl;
}
{
word word1("hello");
word word2("word");
Info<< "create with " << word1 << " @ " << long(word1.data())
<< " " << word2 << " @ " << long(word2.data()) << nl;
Tuple2<word, word> tup(std::move(word2), std::move(word1));
printTuple2(tup);
Info<< "input is now " << word1 << " @ " << long(word1.data())
<< " " << word2 << " @ " << long(word2.data()) << nl;
}
{
word word1("hello");
word word2("word");
Info<< "create with " << word1 << " @ " << long(word1.data())
<< " " << word2 << " @ " << long(word2.data()) << nl;
Pair<word> tup(std::move(word2), std::move(word1));
printTuple2(tup);
Info<< "input is now " << word1 << " @ " << long(word1.data())
<< " " << word2 << " @ " << long(word2.data()) << nl;
}
Info<< "\nEnd\n" << endl;