All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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-2018 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 #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  {
117  << "Call to other only valid for Pair with differing"
118  << " elements:" << *this << abort(FatalError);
119  return first();
120  }
121  else if (first() == a)
122  {
123  return second();
124  }
125  else
126  {
127  if (second() != a)
128  {
130  << "Pair " << *this
131  << " does not contain " << a << abort(FatalError);
132  }
133  return first();
134  }
135  }
136 
137 
138  //- Compare Pairs
139  // Returning:
140  // - 0: different
141  // - +1: identical
142  // - -1: same pair, but reversed order
143  static inline int compare(const Pair<Type>& a, const Pair<Type>& b)
144  {
145  if (a == b)
146  {
147  return 1;
148  }
149  else if (a == reverse(b))
150  {
151  return -1;
152  }
153  else
154  {
155  return 0;
156  }
157  }
158 };
159 
160 
161 template<class Type>
163 {
164  return Pair<Type>(p.second(), p.first());
165 }
166 
167 
168 template<class Type>
169 bool operator==(const Pair<Type>& a, const Pair<Type>& b)
170 {
171  return (a.first() == b.first() && a.second() == b.second());
172 }
173 
174 
175 template<class Type>
176 bool operator!=(const Pair<Type>& a, const Pair<Type>& b)
177 {
178  return !(a == b);
179 }
180 
181 
182 template<class Type>
183 bool operator<(const Pair<Type>& a, const Pair<Type>& b)
184 {
185  return
186  (
187  a.first() < b.first()
188  ||
189  (
190  !(b.first() < a.first())
191  && a.second() < b.second()
192  )
193  );
194 }
195 
196 
197 template<class Type>
198 bool operator<=(const Pair<Type>& a, const Pair<Type>& b)
199 {
200  return !(b < a);
201 }
202 
203 
204 template<class Type>
205 bool operator>(const Pair<Type>& a, const Pair<Type>& b)
206 {
207  return (b < a);
208 }
209 
210 
211 template<class Type>
212 bool operator>=(const Pair<Type>& a, const Pair<Type>& b)
213 {
214  return !(a < b);
215 }
216 
217 
218 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
219 
220 } // End namespace Foam
221 
222 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
223 
224 #endif
225 
226 // ************************************************************************* //
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
static int compare(const Pair< Type > &a, const Pair< Type > &b)
Compare Pairs.
Definition: Pair.H:142
bool operator>=(const FixedList< Type, Size > &) const
Return true if !(a < b). Takes linear time.
Definition: FixedList.C:116
const Type & other(const Type &a) const
Return other.
Definition: Pair.H:111
bool operator!=(const FixedList< Type, Size > &) const
The opposite of the equality operation. Takes linear time.
Definition: FixedList.C:64
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))
An ordered pair of two objects of type <T> with first() and second() elements.
Definition: contiguous.H:49
const dimensionedScalar & b
Wien displacement law constant: default SI units: [m K].
Definition: createFields.H:27
Type & operator[](const label)
Return element of FixedList.
Definition: FixedListI.H:247
bool operator==(const FixedList< Type, Size > &) const
Equality operation on FixedLists of the same type.
Definition: FixedList.C:48
errorManip< error > abort(error &err)
Definition: errorManip.H:131
void reverse(UList< T > &, const label n)
Definition: UListI.H:322
bool operator>(const FixedList< Type, Size > &) const
Compare two FixedLists lexicographically. Takes linear time.
Definition: FixedList.C:102
labelList f(nPoints)
const Type & second() const
Return second.
Definition: Pair.H:99
Pair()
Null constructor.
Definition: Pair.H:61
volScalarField & p
const Type & first() const
Return first.
Definition: Pair.H:87
Namespace for OpenFOAM.