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-2020 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  //- Symmetrical hashing for Pair data.
151  // The lower value is hashed first.
152  template<class HashT=Foam::Hash<T>>
153  struct SymmHash
154  {
155  inline unsigned operator()
156  (
157  const Pair<T>& obj,
158  unsigned seed=0
159  ) const
160  {
161  if (obj.first() < obj.second())
162  {
163  seed = HashT()(obj.first(), seed);
164  seed = HashT()(obj.second(), seed);
165  }
166  else
167  {
168  seed = HashT()(obj.second(), seed);
169  seed = HashT()(obj.first(), seed);
170  }
171  return seed;
172  }
173  };
174 };
175 
176 
177 // * * * * * * * * * * * * * * * * * Traits * * * * * * * * * * * * * * * * //
178 
179 //- Pair is contiguous if the type is contiguous
180 template<class T>
181 struct is_contiguous<Pair<T>> : is_contiguous<T> {};
182 
183 //- Check for Pair of labels
184 template<class T>
186 
187 //- Check for Pair of scalars
188 template<class T>
190 
191 
192 // * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
193 
194 //- Hashing for Pair data, which uses Hasher for contiguous data and
195 //- element-wise incrementally hashing otherwise.
196 template<class T>
197 struct Hash<Pair<T>>
198 {
199  inline unsigned operator()(const Pair<T>& obj, unsigned seed=0) const
200  {
202  {
203  return Hasher(obj.cdata(), sizeof(obj), seed);
204  }
205 
206  seed = Hash<T>()(obj.first(), seed);
207  seed = Hash<T>()(obj.second(), seed);
208 
209  return seed;
210  }
211 };
212 
213 
214 //- Return reverse of a Pair
215 template<class T>
216 Pair<T> reverse(const Pair<T>& p)
217 {
218  return Pair<T>(p.second(), p.first());
219 }
220 
221 
222 // * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * * //
223 
224 template<class T>
225 bool operator==(const Pair<T>& a, const Pair<T>& b)
226 {
227  return (a.first() == b.first() && a.second() == b.second());
228 }
229 
230 
231 template<class T>
232 bool operator!=(const Pair<T>& a, const Pair<T>& b)
233 {
234  return !(a == b);
235 }
236 
237 
238 template<class T>
239 bool operator<(const Pair<T>& a, const Pair<T>& b)
240 {
241  return
242  (
243  a.first() < b.first()
244  || (!(b.first() < a.first()) && a.second() < b.second())
245  );
246 }
247 
248 
249 template<class T>
250 bool operator<=(const Pair<T>& a, const Pair<T>& b)
251 {
252  return !(b < a);
253 }
254 
255 
256 template<class T>
257 bool operator>(const Pair<T>& a, const Pair<T>& b)
258 {
259  return (b < a);
260 }
261 
262 
263 template<class T>
264 bool operator>=(const Pair<T>& a, const Pair<T>& b)
265 {
266  return !(a < b);
267 }
268 
269 
270 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
271 
272 } // End namespace Foam
273 
274 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
275 
276 #include "PairI.H"
277 
278 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
279 
280 #endif
281 
282 // ************************************************************************* //
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:396
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:407
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::Hasher
unsigned Hasher(const void *data, size_t len, unsigned seed=0)
Bob Jenkins's 96-bit mixer hashing function (lookup3)
Definition: Hasher.C:473
Foam::Hash::operator()
unsigned operator()(const T &obj, unsigned seed=0) const
Definition: Hash.H:57
Foam::Pair::SymmHash
Symmetrical hashing for Pair data.
Definition: Pair.H:152
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:235
Foam::Pair::sort
void sort()
Sort so that first() is less-than second()
Definition: PairI.H:174
Foam::Hash
Hash function class. The default definition is for primitives, non-primitives used to hash entries on...
Definition: Hash.H:55
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
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:427
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::Hash< Pair< T > >::operator()
unsigned operator()(const Pair< T > &obj, unsigned seed=0) const
Definition: Pair.H:198
Foam::operator<
bool operator<(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A older than B.
Definition: IOstreamOption.H:397
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:417