Pair.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2020 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 See also
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 
41 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
42 
43 namespace Foam
44 {
45 
46 // Forward declaration of friend functions and operators
47 
48 template<class Type>
49 class Pair;
50 
51 template<class Type>
52 inline Istream& operator>>(Istream&, Pair<Type>&);
53 
54 template<class Type>
55 inline Ostream& operator<<(Ostream&, const Pair<Type>&);
56 
57 
58 /*---------------------------------------------------------------------------*\
59  Class Pair Declaration
60 \*---------------------------------------------------------------------------*/
61 
62 template<class Type>
63 class Pair
64 :
65  public FixedList<Type, 2>
66 {
67 
68 public:
69 
70  // Constructors
71 
72  //- Null constructor
73  inline Pair()
74  {}
75 
76  //- Construct from components
77  inline Pair(const Type& f, const Type& s)
78  {
79  first() = f;
80  second() = s;
81  }
82 
83  //- Construct from FixedList
84  inline Pair(const FixedList<Type, 2>& fl)
85  :
86  FixedList<Type, 2>(fl)
87  {}
88 
89  //- Construct from Istream
90  inline Pair(Istream& is)
91  {
92  is >> *this;
93  }
94 
95 
96  // Member Functions
97 
98  //- Return first
99  inline const Type& first() const
100  {
101  return this->operator[](0);
102  }
103 
104  //- Return first
105  inline Type& first()
106  {
107  return this->operator[](0);
108  }
109 
110  //- Return second
111  inline const Type& second() const
112  {
113  return this->operator[](1);
114  }
115 
116  //- Return second
117  inline Type& second()
118  {
119  return this->operator[](1);
120  }
121 
122  //- Return other
123  inline const Type& other(const Type& a) const
124  {
125  if (first() == second())
126  {
128  << "Call to other only valid for Pair with differing"
129  << " elements:" << *this << abort(FatalError);
130  return first();
131  }
132  else if (first() == a)
133  {
134  return second();
135  }
136  else
137  {
138  if (second() != a)
139  {
141  << "Pair " << *this
142  << " does not contain " << a << abort(FatalError);
143  }
144  return first();
145  }
146  }
147 
148 
149  //- Compare Pairs
150  // Returning:
151  // - 0: different
152  // - +1: identical
153  // - -1: same pair, but reversed order
154  static inline int compare(const Pair<Type>& a, const Pair<Type>& b)
155  {
156  if (a == b)
157  {
158  return 1;
159  }
160  else if (a == reverse(b))
161  {
162  return -1;
163  }
164  else
165  {
166  return 0;
167  }
168  }
169 };
170 
171 
172 template<class Type>
174 {
175  return Pair<Type>(p.second(), p.first());
176 }
177 
178 
179 template<class Type>
180 bool operator==(const Pair<Type>& a, const Pair<Type>& b)
181 {
182  return (a.first() == b.first() && a.second() == b.second());
183 }
184 
185 
186 template<class Type>
187 bool operator!=(const Pair<Type>& a, const Pair<Type>& b)
188 {
189  return !(a == b);
190 }
191 
192 
193 template<class Type>
194 bool operator<(const Pair<Type>& a, const Pair<Type>& b)
195 {
196  return
197  (
198  a.first() < b.first()
199  ||
200  (
201  !(b.first() < a.first())
202  && a.second() < b.second()
203  )
204  );
205 }
206 
207 
208 template<class Type>
209 bool operator<=(const Pair<Type>& a, const Pair<Type>& b)
210 {
211  return !(b < a);
212 }
213 
214 
215 template<class Type>
216 bool operator>(const Pair<Type>& a, const Pair<Type>& b)
217 {
218  return (b < a);
219 }
220 
221 
222 template<class Type>
223 bool operator>=(const Pair<Type>& a, const Pair<Type>& b)
224 {
225  return !(a < b);
226 }
227 
228 
229 template<class Type>
230 inline Istream& operator>>(Istream& is, Pair<Type>& p)
231 {
232  is.readBegin("Pair");
233  is >> p.first() >> p.second();
234  is.readEnd("Pair");
235 
236  // Check state of Istream
237  is.check("operator>>(Istream&, Pair<Type>&)");
238 
239  return is;
240 }
241 
242 
243 template<class Type>
244 inline Ostream& operator<<(Ostream& os, const Pair<Type>& p)
245 {
246  os << token::BEGIN_LIST
247  << p.first() << token::SPACE << p.second()
248  << token::END_LIST;
249 
250  return os;
251 }
252 
253 
254 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
255 
256 } // End namespace Foam
257 
258 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
259 
260 #endif
261 
262 // ************************************************************************* //
A 1D vector of objects of type <T> with a fixed size <Size>.
Definition: FixedList.H:78
Type & operator[](const label)
Return element of FixedList.
Definition: FixedListI.H:247
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
Istream & readEnd(const char *funcName)
Definition: Istream.C:103
Istream & readBegin(const char *funcName)
Definition: Istream.C:86
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
An ordered pair of two objects of type <T> with first() and second() elements.
Definition: Pair.H:65
const Type & second() const
Return second.
Definition: Pair.H:110
const Type & other(const Type &a) const
Return other.
Definition: Pair.H:122
static int compare(const Pair< Type > &a, const Pair< Type > &b)
Compare Pairs.
Definition: Pair.H:153
Pair()
Null constructor.
Definition: Pair.H:72
const Type & first() const
Return first.
Definition: Pair.H:98
T & first()
Return the first element of the list.
Definition: UListI.H:114
@ BEGIN_LIST
Definition: token.H:106
@ END_LIST
Definition: token.H:107
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
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.name(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
volScalarField & b
Definition: createFields.H:27
Namespace for OpenFOAM.
bool operator!=(const particle &, const particle &)
Definition: particle.C:1257
tmp< fvMatrix< Type > > operator==(const fvMatrix< Type > &, const fvMatrix< Type > &)
bool operator<(const instant &, const instant &)
Definition: instant.C:79
errorManip< error > abort(error &err)
Definition: errorManip.H:131
bool operator<=(const Pair< Type > &a, const Pair< Type > &b)
Definition: Pair.H:208
void reverse(UList< T > &, const label n)
Definition: UListI.H:334
bool operator>=(const Pair< Type > &a, const Pair< Type > &b)
Definition: Pair.H:222
Istream & operator>>(Istream &, directionInfo &)
bool operator>(const instant &, const instant &)
Definition: instant.C:85
error FatalError
Ostream & operator<<(Ostream &, const ensightPart &)
labelList f(nPoints)
volScalarField & p