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/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) {