initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / primitives / Pair / Pair.H
blobe0514b63b8e446632d0a7225dc401caf355f77b3
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software; you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Class
26     Foam::Pair
28 Description
29     An ordered pair of two objects of type \<T\> with first() and second()
30     elements.
32 SeeAlso
33     Foam::Tuple2 for storing two objects of dissimilar types.
35 \*---------------------------------------------------------------------------*/
37 #ifndef Pair_H
38 #define Pair_H
40 #include "FixedList.H"
41 #include "Istream.H"
43 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45 namespace Foam
48 /*---------------------------------------------------------------------------*\
49                             Class Pair Declaration
50 \*---------------------------------------------------------------------------*/
52 template<class Type>
53 class Pair
55     public FixedList<Type, 2>
58 public:
60     // Constructors
62         //- Null constructor
63         inline Pair()
64         {}
66         //- Construct from components
67         inline Pair(const Type& f, const Type& s)
68         {
69             first() = f;
70             second() = s;
71         }
73         //- Construct from Istream
74         inline Pair(Istream& is)
75         :
76             FixedList<Type, 2>(is)
77         {}
80     // Member Functions
82         //- Return first
83         inline const Type& first() const
84         {
85             return this->operator[](0);
86         }
88         //- Return first
89         inline Type& first()
90         {
91             return this->operator[](0);
92         }
94         //- Return second
95         inline const Type& second() const
96         {
97             return this->operator[](1);
98         }
100         //- Return second
101         inline Type& second()
102         {
103             return this->operator[](1);
104         }
106         //- Return reverse pair
107         inline Pair<Type> reversePair() const
108         {
109             return Pair<Type>(second(), first());
110         }
112         //- Return other
113         inline const Type& other(const Type& a) const
114         {
115             if (first() == second())
116             {
117                 FatalErrorIn("Pair<Type>::other(const Type&) const")
118                     << "Call to other only valid for Pair with differing"
119                     << " elements:" << *this << abort(FatalError);
120             }
121             else if (first() == a)
122             {
123                 return second();
124             }
125             else
126             {
127                 if (second() != a)
128                 {
129                     FatalErrorIn("Pair<Type>::other(const Type&) const")
130                         << "Pair " << *this
131                         << " does not contain " << a << abort(FatalError);
132                 }
133                 return first();
134             }
135         }
138         //- compare Pairs
139         //  -  0: different
140         //  - +1: identical
141         //  - -1: same pair, but reversed order
142         static inline int compare(const Pair<Type>& a, const Pair<Type>& b)
143         {
144             if (a[0] == b[0] && a[1] == b[1])
145             {
146                 return 1;
147             }
148             else if (a[0] == b[1] && a[1] == b[0])
149             {
150                 return -1;
151             }
152             else
153             {
154                 return 0;
155             }
156         }
159     // Friend Operators
161         friend bool operator==(const Pair<Type>& a, const Pair<Type>& b)
162         {
163             return (a.first() == b.first() && a.second() == b.second());
164         }
166         friend bool operator!=(const Pair<Type>& a, const Pair<Type>& b)
167         {
168             return !(a == b);
169         }
173 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
175 } // End namespace Foam
177 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
179 #endif
181 // ************************************************************************* //