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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2017-2021 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Class
28  Foam::Pair
29 
30 Description
31  An ordered pair of two objects of type <T> with first() and second()
32  elements.
33 
34 SourceFiles
35  PairI.H
36 
37 See also
38  Foam::Tuple2 for storing two objects of dissimilar types.
39 
40 \*---------------------------------------------------------------------------*/
41 
42 #ifndef Pair_H
43 #define Pair_H
44 
45 #include "FixedList.H"
46 #include "Istream.H"
47 #include <utility>
48 
49 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
50 
51 namespace Foam
52 {
53 
54 // Forward Declarations
55 template<class T> class Pair;
56 
57 // Common pair types
59 typedef Pair<word> wordPair;
60 
61 
62 /*---------------------------------------------------------------------------*\
63  Class Pair Declaration
64 \*---------------------------------------------------------------------------*/
65 
66 template<class T>
67 class Pair
68 :
69  public FixedList<T, 2>
70 {
71 public:
72 
73  // Constructors
74 
75  //- Default construct
76  Pair() = default;
77 
78  //- Copy construct from components
79  inline Pair(const T& f, const T& s);
80 
81  //- Move construct from components
82  inline Pair(T&& f, T&& s);
83 
84  //- Copy construct from std::pair
85  inline Pair(const std::pair<T,T>& vals);
86 
87  //- Move construct from std::pair
88  inline Pair(std::pair<T,T>&& vals);
89 
90  //- Copy construct FixedList of two items
91  inline Pair(const FixedList<T, 2>& list);
92 
93  //- Copy construct, optionally sorted with first less-than second
94  inline Pair(const T& f, const T& s, const bool doSort);
95 
96  //- Copy construct, optionally sorted with first less-than second
97  inline Pair(const FixedList<T, 2>& list, const bool doSort);
98 
99  //- Construct from Istream
100  inline explicit Pair(Istream& is);
101 
102 
103  // Member Functions
104 
105  // Access
106 
107  //- Return first element
109 
110  //- Return last element
111  using FixedList<T, 2>::last;
112 
113  //- Return second element, which is also the last element
114  inline const T& second() const noexcept;
115 
116  //- Return second element, which is also the last element
117  inline T& second() noexcept;
118 
119  //- Return other element
120  inline const T& other(const T& a) const;
121 
122 
123  // Queries
124 
125  //- True if first() is less-than second()
126  inline bool sorted() const;
127 
128 
129  // Editing
130 
131  //- Flip the Pair in-place.
132  inline void flip();
133 
134  //- Sort so that first() is less-than second()
135  inline void sort();
136 
137 
138  // Comparison
139 
140  //- Compare Pairs
141  // \return
142  // - 0: different
143  // - +1: identical values and order used
144  // - -1: identical values, but in reversed order
145  static inline int compare(const Pair<T>& a, const Pair<T>& b);
146 
147 
148  // Hashing
149 
150  //- Symmetric hashing functor for Pair, hashes lower value first
151  // Regular hasher inherited from FixedList
152  struct symmHasher
153  {
154  unsigned operator()(const Pair<T>& obj, unsigned seed=0) const
155  {
156  Foam::Hash<T> op;
157  if (obj.second() < obj.first())
158  {
159  return op(obj.first(), op(obj.second(), seed));
160  }
161  else
162  {
163  return op(obj.second(), op(obj.first(), seed));
164  }
165  }
166  };
167 };
168 
169 
170 // * * * * * * * * * * * * * * * * * Traits * * * * * * * * * * * * * * * * //
171 
172 //- Pair is contiguous if the type is contiguous
173 template<class T>
174 struct is_contiguous<Pair<T>> : is_contiguous<T> {};
175 
176 //- Check for Pair of labels
177 template<class T>
179 
180 //- Check for Pair of scalars
181 template<class T>
183 
184 //- Hashing for Pair of data
185 template<class T>
186 struct Hash<Pair<T>> : Pair<T>::hasher {};
187 
188 
189 // * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
190 
191 //- Return reverse of a Pair
192 template<class T>
193 Pair<T> reverse(const Pair<T>& p)
194 {
195  return Pair<T>(p.second(), p.first());
196 }
197 
198 
199 // * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * * //
200 
201 template<class T>
202 bool operator==(const Pair<T>& a, const Pair<T>& b)
203 {
204  return (a.first() == b.first() && a.second() == b.second());
205 }
206 
207 
208 template<class T>
209 bool operator!=(const Pair<T>& a, const Pair<T>& b)
210 {
211  return !(a == b);
212 }
213 
214 
215 template<class T>
216 bool operator<(const Pair<T>& a, const Pair<T>& b)
217 {
218  return
219  (
220  a.first() < b.first()
221  || (!(b.first() < a.first()) && a.second() < b.second())
222  );
223 }
224 
225 
226 template<class T>
227 bool operator<=(const Pair<T>& a, const Pair<T>& b)
228 {
229  return !(b < a);
230 }
231 
232 
233 template<class T>
234 bool operator>(const Pair<T>& a, const Pair<T>& b)
235 {
236  return (b < a);
237 }
238 
239 
240 template<class T>
241 bool operator>=(const Pair<T>& a, const Pair<T>& b)
242 {
243  return !(a < b);
244 }
245 
246 
247 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
248 
249 } // End namespace Foam
250 
251 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
252 
253 #include "PairI.H"
254 
255 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
256 
257 #endif
258 
259 // ************************************************************************* //
Foam::Pair::second
const T & second() const noexcept
Return second element, which is also the last element.
Definition: PairI.H:122
Foam::reverse
void reverse(UList< T > &list, const label n)
Definition: UListI.H:449
p
volScalarField & p
Definition: createFieldRefs.H:8
Foam::operator<=
bool operator<=(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A same or older than B.
Definition: IOstreamOption.H:408
s
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.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))
Definition: gmvOutputSpray.H:25
Foam::is_contiguous_label
A template class to specify if a data type is composed solely of Foam::label elements.
Definition: contiguous.H:83
Foam::Pair::sorted
bool sorted() const
True if first() is less-than second()
Definition: PairI.H:167
Foam::labelPair
Pair< label > labelPair
A pair of labels.
Definition: Pair.H:54
Foam::wordPair
Pair< word > wordPair
A pair of words.
Definition: Pair.H:58
Foam::operator!=
bool operator!=(const eddy &a, const eddy &b)
Definition: eddy.H:239
Foam::Pair::sort
void sort()
Sort so that first() is less-than second()
Definition: PairI.H:174
Foam::Pair::symmHasher
Symmetric hashing functor for Pair, hashes lower value first.
Definition: Pair.H:151
Foam::Hash
Hash function class. The default definition is for primitives. Non-primitives used to hash entries on...
Definition: Hash.H:53
Foam::Pair::compare
static int compare(const Pair< T > &a, const Pair< T > &b)
Compare Pairs.
Definition: PairI.H:33
Foam::constant::physicoChemical::b
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
Foam::operator==
tmp< faMatrix< Type > > operator==(const faMatrix< Type > &, const faMatrix< Type > &)
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::Pair::Pair
Pair()=default
Default construct.
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
Foam::Pair::symmHasher::operator()
unsigned operator()(const Pair< T > &obj, unsigned seed=0) const
Definition: Pair.H:153
Istream.H
Foam::Pair::other
const T & other(const T &a) const
Return other element.
Definition: PairI.H:136
Foam::Pair::flip
void flip()
Flip the Pair in-place.
Definition: PairI.H:160
Foam::operator>=
bool operator>=(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A same or newer than B.
Definition: IOstreamOption.H:428
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::is_contiguous_scalar
A template class to specify if a data type is composed solely of Foam::scalar elements.
Definition: contiguous.H:91
PairI.H
Foam::Pair
An ordered pair of two objects of type <T> with first() and second() elements.
Definition: Pair.H:54
f
labelList f(nPoints)
Foam::FixedList
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: HashTable.H:104
Foam::operator<
bool operator<(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A older than B.
Definition: IOstreamOption.H:398
FixedList.H
Foam::is_contiguous
A template class to specify that a data type can be considered as being contiguous in memory.
Definition: contiguous.H:75
Foam::operator>
bool operator>(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A newer than B.
Definition: IOstreamOption.H:418