Pair.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2013 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
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.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::Pair
26 
27 Description
28  An ordered pair of two objects of type <T> with first() and second()
29  elements.
30 
31 SeeAlso
32  Foam::Tuple2 for storing two objects of dissimilar types.
33 
34 \*---------------------------------------------------------------------------*/
35 
36 #ifndef Pair_H
37 #define Pair_H
38 
39 #include "FixedList.H"
40 #include "Istream.H"
41 
42 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
43 
44 namespace Foam
45 {
46 
47 /*---------------------------------------------------------------------------*\
48  Class Pair Declaration
49 \*---------------------------------------------------------------------------*/
50 
51 template<class Type>
52 class Pair
53 :
54  public FixedList<Type, 2>
55 {
56 
57 public:
58 
59  // Constructors
60 
61  //- Null constructor
62  inline Pair()
63  {}
64 
65  //- Construct from components
66  inline Pair(const Type& f, const Type& s)
67  {
68  first() = f;
69  second() = s;
70  }
71 
72  //- Construct from FixedList
73  inline Pair(const FixedList<Type, 2>& fl)
74  :
75  FixedList<Type, 2>(fl)
76  {}
77 
78  //- Construct from Istream
79  inline Pair(Istream& is)
80  :
81  FixedList<Type, 2>(is)
82  {}
83 
84 
85  // Member Functions
86 
87  //- Return first
88  inline const Type& first() const
89  {
90  return this->operator[](0);
91  }
92 
93  //- Return first
94  inline Type& first()
95  {
96  return this->operator[](0);
97  }
98 
99  //- Return second
100  inline const Type& second() const
101  {
102  return this->operator[](1);
103  }
104 
105  //- Return second
106  inline Type& second()
107  {
108  return this->operator[](1);
109  }
110 
111  //- Return other
112  inline const Type& other(const Type& a) const
113  {
114  if (first() == second())
115  {
116  FatalErrorIn("Pair<Type>::other(const Type&) const")
117  << "Call to other only valid for Pair with differing"
118  << " elements:" << *this << abort(FatalError);
119  }
120  else if (first() == a)
121  {
122  return second();
123  }
124  else
125  {
126  if (second() != a)
127  {
128  FatalErrorIn("Pair<Type>::other(const Type&) const")
129  << "Pair " << *this
130  << " does not contain " << a << abort(FatalError);
131  }
132  return first();
133  }
134  }
135 
136 
137  //- Compare Pairs
138  // Returning:
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 == b)
145  {
146  return 1;
147  }
148  else if (a == reverse(b))
149  {
150  return -1;
151  }
152  else
153  {
154  return 0;
155  }
156  }
157 };
158 
159 
160 template<class Type>
162 {
163  return Pair<Type>(p.second(), p.first());
164 }
165 
166 
167 template<class Type>
168 bool operator==(const Pair<Type>& a, const Pair<Type>& b)
169 {
170  return (a.first() == b.first() && a.second() == b.second());
171 }
172 
173 
174 template<class Type>
175 bool operator!=(const Pair<Type>& a, const Pair<Type>& b)
176 {
177  return !(a == b);
178 }
179 
180 
181 template<class Type>
182 bool operator<(const Pair<Type>& a, const Pair<Type>& b)
183 {
184  return
185  (
186  a.first() < b.first()
187  ||
188  (
189  !(b.first() < a.first())
190  && a.second() < b.second()
191  )
192  );
193 }
194 
195 
196 template<class Type>
197 bool operator<=(const Pair<Type>& a, const Pair<Type>& b)
198 {
199  return !(b < a);
200 }
201 
202 
203 template<class Type>
204 bool operator>(const Pair<Type>& a, const Pair<Type>& b)
205 {
206  return (b < a);
207 }
208 
209 
210 template<class Type>
211 bool operator>=(const Pair<Type>& a, const Pair<Type>& b)
212 {
213  return !(a < b);
214 }
215 
216 
217 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
218 
219 } // End namespace Foam
220 
221 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
222 
223 #endif
224 
225 // ************************************************************************* //
An ordered pair of two objects of type <T> with first() and second() elements.
Definition: contiguous.H:49
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject( name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE ))
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:28
labelList f(nPoints)
bool operator>=(const FixedList< Type, Size > &) const
Return true if !(a < b). Takes linear time.
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
const Type & second() const
Return second.
Definition: Pair.H:99
Namespace for OpenFOAM.
bool operator!=(const FixedList< Type, Size > &) const
The opposite of the equality operation. Takes linear time.
static int compare(const Pair< Type > &a, const Pair< Type > &b)
Compare Pairs.
Definition: Pair.H:141
volScalarField & p
Definition: createFields.H:51
Type & operator[](const label)
Return element of FixedList.
errorManip< error > abort(error &err)
Definition: errorManip.H:131
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
bool operator>(const FixedList< Type, Size > &) const
Compare two FixedLists lexicographically. Takes linear time.
error FatalError
Pair()
Null constructor.
Definition: Pair.H:61
const Type & first() const
Return first.
Definition: Pair.H:87
void reverse(UList< T > &, const label n)
Definition: UListI.H:322
const Type & other(const Type &a) const
Return other.
Definition: Pair.H:111
bool operator==(const FixedList< Type, Size > &) const
Equality operation on FixedLists of the same type.