Skip to content
Snippets Groups Projects
Commit 7b427c38 authored by Mark OLESEN's avatar Mark OLESEN
Browse files

ENH: add Tuple2 comparison operators, typedefs

parent 5d6bf3e4
Branches
Tags
No related merge requests found
......@@ -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;
......
......@@ -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)
{
......
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