PairI.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-2025 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 \*---------------------------------------------------------------------------*/
25 
26 #include "Pair.H"
27 #include "tmp.H"
28 #include "token.H"
29 
30 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
31 
32 template<class Type>
34 {
35  return NullObjectRef<Pair<Type>>();
36 }
37 
38 
39 template<class Type>
41 {
42  return 2;
43 }
44 
45 
46 template<class Type>
47 inline void Foam::Pair<Type>::checkIndex(const label i)
48 {
49  if (i < 0 || unsigned(i) > 1)
50  {
52  << "index " << i << " out of range 0 ... 1"
53  << abort(FatalError);
54  }
55 }
56 
57 
58 template<class Type>
59 inline int Foam::Pair<Type>::compare(const Pair<Type>& a, const Pair<Type>& b)
60 {
61  if (a == b)
62  {
63  return 1;
64  }
65  else if (a == reverse(b))
66  {
67  return -1;
68  }
69  else
70  {
71  return 0;
72  }
73 }
74 
75 
76 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
77 
78 template<class Type>
80 {}
81 
82 
83 template<class Type>
84 inline Foam::Pair<Type>::Pair(const Type& f, const Type& s)
85 :
86  f_(f),
87  s_(s)
88 {}
89 
90 
91 template<class Type>
93 {
94  is >> *this;
95 }
96 
97 
98 template<class Type>
99 template<class HashT>
101 {}
102 
103 
104 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
105 
106 template<class Type>
107 inline const Type& Foam::Pair<Type>::first() const
108 {
109  return f_;
110 }
111 
112 
113 template<class Type>
115 {
116  return f_;
117 }
118 
119 
120 template<class Type>
121 inline const Type& Foam::Pair<Type>::second() const
122 {
123  return s_;
124 }
125 
126 
127 template<class Type>
129 {
130  return s_;
131 }
132 
133 
134 template<class Type>
135 inline const Type& Foam::Pair<Type>::other(const Type& a) const
136 {
137  if (first() == second())
138  {
140  << "Call to other only valid for Pair with differing"
141  << " elements:" << *this << abort(FatalError);
142  return first();
143  }
144  else if (first() == a)
145  {
146  return second();
147  }
148  else
149  {
150  if (second() != a)
151  {
153  << "Pair " << *this
154  << " does not contain " << a << abort(FatalError);
155  }
156  return first();
157  }
158 }
159 
160 
161 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
162 
163 template<class Type>
164 inline Type& Foam::Pair<Type>::operator[](const label i)
165 {
166  #ifdef FULLDEBUG
167  checkIndex(i);
168  #endif
169  return i ? s_ : f_;
170 }
171 
172 
173 template<class Type>
174 inline const Type& Foam::Pair<Type>::operator[](const label i) const
175 {
176  #ifdef FULLDEBUG
177  checkIndex(i);
178  #endif
179  return i ? s_ : f_;
180 }
181 
182 
183 template<class Type>
184 template<class HashT>
186 (
187  const Pair<Type>& p,
188  unsigned seed
189 ) const
190 {
191  return HashT()(p.second(), HashT()(p.first(), seed));
192 }
193 
194 
195 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
196 
197 template<class Type>
199 {
200  return Pair<Type>(p.second(), p.first());
201 }
202 
203 
204 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
205 
206 template<class Type>
207 inline bool Foam::operator==(const Pair<Type>& a, const Pair<Type>& b)
208 {
209  return (a.first() == b.first() && a.second() == b.second());
210 }
211 
212 
213 template<class Type>
214 inline bool Foam::operator!=(const Pair<Type>& a, const Pair<Type>& b)
215 {
216  return !(a == b);
217 }
218 
219 
220 template<class Type>
221 inline bool Foam::operator<(const Pair<Type>& a, const Pair<Type>& b)
222 {
223  return
224  (
225  a.first() < b.first()
226  ||
227  (
228  !(b.first() < a.first())
229  && a.second() < b.second()
230  )
231  );
232 }
233 
234 
235 template<class Type>
236 inline bool Foam::operator<=(const Pair<Type>& a, const Pair<Type>& b)
237 {
238  return !(b < a);
239 }
240 
241 
242 template<class Type>
243 inline bool Foam::operator>(const Pair<Type>& a, const Pair<Type>& b)
244 {
245  return (b < a);
246 }
247 
248 
249 template<class Type>
250 inline bool Foam::operator>=(const Pair<Type>& a, const Pair<Type>& b)
251 {
252  return !(a < b);
253 }
254 
255 
256 template<class Type>
257 inline Foam::Istream& Foam::operator>>(Istream& is, Pair<Type>& p)
258 {
259  is.readBegin("Pair");
260  is >> p.first() >> p.second();
261  is.readEnd("Pair");
262 
263  // Check state of Istream
264  is.check("operator>>(Istream&, Pair<Type>&)");
265 
266  return is;
267 }
268 
269 
270 template<class Type>
271 inline Foam::Ostream& Foam::operator<<(Ostream& os, const Pair<Type>& p)
272 {
273  os << token::BEGIN_LIST
274  << p.first() << token::SPACE << p.second()
275  << token::END_LIST;
276 
277  return os;
278 }
279 
280 
281 template<class Type>
282 inline void Foam::writeEntry(Ostream& os, const Pair<Type>& p)
283 {
284  os << p;
285 }
286 
287 
288 // ************************************************************************* //
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
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 <Type> with first() and second() elements.
Definition: Pair.H:66
Type & operator[](const label i)
Access an element by index.
Definition: PairI.H:164
static label size()
Return the size.
Definition: PairI.H:40
static int compare(const Pair< Type > &a, const Pair< Type > &b)
Compare two pairs. Return 0 if they are different, +1 if they are.
Definition: PairI.H:59
static void checkIndex(const label i)
Check index i is within valid range (0 ... 1)
Definition: PairI.H:47
const Type & second() const
Return second.
Definition: PairI.H:121
Type & first()
Return first.
Definition: PairI.H:114
const Type & other(const Type &a) const
Return other.
Definition: PairI.H:135
const Type & first() const
Return first.
Definition: PairI.H:107
Pair()
Null constructor.
Definition: PairI.H:79
static const Pair< Type > & null()
Return a null pair.
Definition: PairI.H:33
@ BEGIN_LIST
Definition: token.H:109
@ END_LIST
Definition: token.H:110
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(lagrangian::Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(lagrangian::Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(lagrangian::Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.name(), lagrangian::cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
volScalarField & b
Definition: createFields.H:27
bool operator!=(const particle &, const particle &)
Definition: particle.C:424
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
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
labelList second(const UList< labelPair > &p)
Definition: patchToPatch.C:49
bool operator<=(const Pair< Type > &a, const Pair< Type > &b)
Compare lexographic order.
Definition: PairI.H:236
void reverse(UList< T > &, const label n)
Definition: UListI.H:334
labelList first(const UList< labelPair > &p)
Definition: patchToPatch.C:39
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
Istream & operator>>(Istream &, pistonPointEdgeData &)
bool operator>(const instant &, const instant &)
Definition: instant.C:85
Ostream & operator<<(Ostream &os, const fvConstraints &constraints)
error FatalError
bool operator>=(const Pair< Type > &a, const Pair< Type > &b)
Compare lexographic order.
Definition: PairI.H:250
labelList f(nPoints)
volScalarField & p