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-2023 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 Foam_Pair_H
43 #define Foam_Pair_H
44 
45 #include "FixedList.H"
46 #include "Istream.H"
47 #include "Ostream.H"
48 #include <utility> // For std::move, std::pair
49 
50 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
51 
52 namespace Foam
53 {
54 
55 // Forward Declarations
56 template<class T> class Pair;
57 
58 // Common pair types
59 typedef Pair<label> labelPair;
60 typedef Pair<word> wordPair;
61 
62 
63 /*---------------------------------------------------------------------------*\
64  Class Pair Declaration
65 \*---------------------------------------------------------------------------*/
66 
67 template<class T>
68 class Pair
69 :
70  public FixedList<T, 2>
71 {
72 public:
73 
74  // Generated Methods
75 
76  //- Default construct
77  Pair() = default;
78 
79  //- The front() accessor (from FixedList) has no purpose
80  void front() = delete;
81 
82  //- The back() accessor (from FixedList) has no purpose
83  void back() = delete;
84 
85 
86  // Constructors
87 
88  //- Copy construct from components
89  inline Pair(const T& f, const T& s);
90 
91  //- Move construct from components
92  inline Pair(T&& f, T&& s);
93 
94  //- Copy construct from std::pair
95  inline Pair(const std::pair<T,T>& vals);
96 
97  //- Move construct from std::pair
98  inline Pair(std::pair<T,T>&& vals);
99 
100  //- Copy construct FixedList of two items
101  inline Pair(const FixedList<T, 2>& list);
102 
103  //- Copy construct, optionally sorted with first less-than second
104  inline Pair(const T& f, const T& s, const bool doSort);
105 
106  //- Copy construct, optionally sorted with first less-than second
107  inline Pair(const FixedList<T, 2>& list, const bool doSort);
108 
109  //- Construct from Istream
110  inline explicit Pair(Istream& is);
111 
112 
113  // Member Functions
114 
115  // Access
116 
117  //- Access the first element
118  const T& first() const noexcept { return this->template get<0>(); }
119 
120  //- Access the first element
121  T& first() noexcept { return this->template get<0>(); }
122 
123  //- Access the second element
124  const T& second() const noexcept { return this->template get<1>(); }
125 
126  //- Access the second element
127  T& second() noexcept { return this->template get<1>(); }
128 
129  //- Return other element
130  inline const T& other(const T& a) const;
131 
132 
133  // Queries
134 
135  //- True if first() is less-than second()
136  inline bool sorted() const;
138 
139  // Editing
140 
141  //- Flip the Pair in-place.
142  inline void flip();
143 
144  //- Sort so that first() is less-than second()
145  inline void sort();
146 
148  // Comparison
149 
150  //- Compare Pairs
151  // \return
152  // - 0: different
153  // - +1: identical values and order used
154  // - -1: identical values, but in reversed order
155  static inline int compare(const Pair<T>& a, const Pair<T>& b);
156 
157 
158  // Hashing
159 
160  //- Symmetric hashing functor for Pair, hashes lower value first
161  // Regular hasher inherited from FixedList
162  struct symmHasher
163  {
164  unsigned operator()(const Pair<T>& obj, unsigned seed=0) const
165  {
166  Foam::Hash<T> op;
167  if (obj.second() < obj.first())
168  {
169  return op(obj.first(), op(obj.second(), seed));
170  }
171  else
172  {
173  return op(obj.second(), op(obj.first(), seed));
174  }
175  }
176  };
177 };
178 
179 
180 // * * * * * * * * * * * * * * * * * Traits * * * * * * * * * * * * * * * * //
181 
182 //- Pair is contiguous if the type is contiguous
183 template<class T>
184 struct is_contiguous<Pair<T>> : is_contiguous<T> {};
185 
186 //- Check for Pair of labels
187 template<class T>
188 struct is_contiguous_label<Pair<T>> : is_contiguous_label<T> {};
189 
190 //- Check for Pair of scalars
191 template<class T>
192 struct is_contiguous_scalar<Pair<T>> : is_contiguous_scalar<T> {};
193 
194 //- Hashing for Pair of data
195 template<class T>
196 struct Hash<Pair<T>> : Pair<T>::hasher {};
197 
198 //- Hashing for std::pair data
199 template<class T1, class T2>
200 struct Hash<std::pair<T1, T2>>
201 {
202  unsigned operator()(const std::pair<T1, T2>& obj, unsigned seed=0) const
203  {
204  return Hash<T2>()(obj.second, Hash<T1>()(obj.first, seed));
205  }
206 };
207 
208 
209 // * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
210 
211 //- Return reverse of a Pair
212 template<class T>
213 Pair<T> reverse(const Pair<T>& p)
214 {
215  return Pair<T>(p.second(), p.first());
216 }
217 
218 
219 // * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * * //
220 
221 template<class T>
222 bool operator==(const Pair<T>& a, const Pair<T>& b)
223 {
224  return (a.first() == b.first() && a.second() == b.second());
225 }
226 
227 
228 template<class T>
229 bool operator!=(const Pair<T>& a, const Pair<T>& b)
230 {
231  return !(a == b);
232 }
233 
234 
235 template<class T>
236 bool operator<(const Pair<T>& a, const Pair<T>& b)
237 {
238  return
239  (
240  a.first() < b.first()
241  || (!(b.first() < a.first()) && a.second() < b.second())
242  );
243 }
244 
245 
246 template<class T>
247 bool operator<=(const Pair<T>& a, const Pair<T>& b)
248 {
249  return !(b < a);
250 }
252 
253 template<class T>
254 bool operator>(const Pair<T>& a, const Pair<T>& b)
255 {
256  return (b < a);
257 }
258 
259 
260 template<class T>
261 bool operator>=(const Pair<T>& a, const Pair<T>& b)
262 {
263  return !(a < b);
264 }
265 
266 
267 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
268 
269 //- Read std::pair from Istream
270 template<class T1, class T2>
271 inline Istream& operator>>(Istream& is, std::pair<T1,T2>& t)
272 {
273  is.readBegin("pair");
274  is >> t.first >> t.second;
275  is.readEnd("pair");
276 
277  is.check(FUNCTION_NAME);
278  return is;
279 }
281 
282 //- Write std::pair to Ostream
283 template<class T1, class T2>
284 inline Ostream& operator<<(Ostream& os, const std::pair<T1,T2>& t)
285 {
287  << t.first << token::SPACE << t.second
288  << token::END_LIST;
289  return os;
290 }
291 
292 
293 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
294 
295 } // End namespace Foam
296 
297 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
299 #include "PairI.H"
300 
301 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
302 
303 #endif
304 
305 // ************************************************************************* //
const T & first() const noexcept
Access the first element.
Definition: Pair.H:137
void back()=delete
The back() accessor (from FixedList) has no purpose.
void sort()
Sort so that first() is less-than second()
Definition: PairI.H:151
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: HashTable.H:101
bool readBegin(const char *funcName)
Begin read of data chunk, starts with &#39;(&#39;.
Definition: Istream.C:104
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
void flip()
Flip the Pair in-place.
Definition: PairI.H:137
T & first()
Access first element of the list, position [0].
Definition: UList.H:814
bool operator>(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A newer than B.
Begin list [isseparator].
Definition: token.H:158
bool operator>=(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A same or newer than B.
const T & other(const T &a) const
Return other element.
Definition: PairI.H:113
Pair()=default
Default construct.
An ordered pair of two objects of type <T> with first() and second() elements.
Definition: instant.H:46
void front()=delete
The front() accessor (from FixedList) has no purpose.
static int compare(const Pair< T > &a, const Pair< T > &b)
Compare Pairs.
Definition: PairI.H:24
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
Istream & operator>>(Istream &, directionInfo &)
Space [isspace].
Definition: token.H:128
Pair< word > wordPair
A pair of words.
Definition: Pair.H:55
End list [isseparator].
Definition: token.H:159
void reverse(UList< T > &list, const label n)
Reverse the first n elements of the list.
Definition: UListI.H:450
unsigned operator()(const T &obj, unsigned seed=0) const
Definition: Hash.H:49
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:55
const direction noexcept
Definition: Scalar.H:258
Pair< label > labelPair
A pair of labels.
Definition: Pair.H:51
bool sorted() const
True if first() is less-than second()
Definition: PairI.H:144
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
labelList f(nPoints)
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Hash function class. The default definition is for primitives. Non-primitives used to hash entries on...
Definition: Hash.H:47
bool operator!=(const eddy &a, const eddy &b)
Definition: eddy.H:297
volScalarField & p
tmp< faMatrix< Type > > operator==(const faMatrix< Type > &, const faMatrix< Type > &)
const T & second() const noexcept
Access the second element.
Definition: Pair.H:147
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))
Namespace for OpenFOAM.