Tuple2.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) 2019-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::Tuple2
29 
30 Description
31  A 2-tuple for storing two objects of dissimilar types.
32  The container is similar in purpose to std::pair, but does not expose
33  its members directly.
34 
35 See also
36  Foam::Pair for storing two objects of identical types.
37 
38 \*---------------------------------------------------------------------------*/
39 
40 #ifndef Tuple2_H
41 #define Tuple2_H
42 
43 #include "Istream.H"
44 #include "Ostream.H"
45 #include "Pair.H"
46 #include <utility>
47 
48 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
49 
50 namespace Foam
51 {
52 
53 /*---------------------------------------------------------------------------*\
54  Class Tuple2 Declaration
55 \*---------------------------------------------------------------------------*/
56 
57 template<class T1, class T2 = T1>
58 class Tuple2
59 {
60  // Private Data
61 
62  T1 f_;
63  T2 s_;
64 
65 public:
66 
67  // Typedefs (cf. std::pair)
68 
69  //- Type of member first, the first template parameter (T1)
70  typedef T1 first_type;
71 
72  //- Type of member second, the second template parameter (T2)
73  typedef T2 second_type;
74 
75 
76  // Constructors
77 
78  //- Default construct
79  Tuple2() = default;
80 
81  //- Copy construct from components
82  inline Tuple2(const T1& f, const T2& s)
83  :
84  f_(f),
85  s_(s)
86  {}
87 
88  //- Move construct from components
89  inline Tuple2(T1&& f, T2&& s)
90  :
91  f_(std::move(f)),
92  s_(std::move(s))
93  {}
94 
95  //- Copy construct from std::pair
96  inline Tuple2(const std::pair<T1,T2>& vals)
97  :
98  f_(vals.first),
99  s_(vals.second)
100  {}
101 
102  //- Move construct from std::pair
103  inline Tuple2(std::pair<T1,T2>&& vals)
104  :
105  f_(std::move(vals.first)),
106  s_(std::move(vals.second))
107  {}
108 
109  //- Construct from Istream
110  inline explicit Tuple2(Istream& is)
111  {
112  is >> *this;
113  }
114 
115 
116  // Member Functions
117 
118  //- Return first
119  inline const T1& first() const noexcept
120  {
121  return f_;
122  }
123 
124  //- Return first
125  inline T1& first() noexcept
126  {
127  return f_;
128  }
129 
130  //- Return second
131  inline const T2& second() const noexcept
132  {
133  return s_;
134  }
135 
136  //- Return second
137  inline T2& second() noexcept
138  {
139  return s_;
140  }
141 };
142 
143 
144 // * * * * * * * * * * * * * * * * * Traits * * * * * * * * * * * * * * * * //
145 
146 //- Hashing for Tuple2 data
147 template<class T1, class T2>
148 struct Hash<Tuple2<T1, T2>>
149 {
150  unsigned operator()(const Tuple2<T1, T2>& obj, unsigned seed=0) const
151  {
152  return Hash<T2>()(obj.second(), Hash<T1>()(obj.first(), seed));
153  }
154 };
155 
156 //- Hashing for std::pair data
157 template<class T1, class T2>
158 struct Hash<std::pair<T1, T2>>
159 {
160  unsigned operator()(const std::pair<T1, T2>& obj, unsigned seed=0) const
161  {
162  return Hash<T2>()(obj.second, Hash<T1>()(obj.first, seed));
163  }
164 };
165 
166 
167 // * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
168 
169 //- Return reverse of a Tuple2
170 template<class T1, class T2>
171 inline Tuple2<T2, T1> reverse(const Tuple2<T1,T2>& t)
172 {
173  return Tuple2<T2, T1>(t.second(), t.first());
174 }
175 
176 
177 // * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * * //
178 
179 template<class T1, class T2>
180 inline bool operator==(const Tuple2<T1,T2>& a, const Tuple2<T1,T2>& b)
181 {
182  return (a.first() == b.first() && a.second() == b.second());
183 }
184 
185 
186 template<class T1, class T2>
187 inline bool operator!=(const Tuple2<T1,T2>& a, const Tuple2<T1,T2>& b)
188 {
189  return !(a == b);
190 }
191 
192 
193 template<class T1, class T2>
194 inline bool operator<(const Tuple2<T1,T2>& a, const Tuple2<T1,T2>& b)
195 {
196  return
197  (
198  a.first() < b.first()
199  || (!(b.first() < a.first()) && a.second() < b.second())
200  );
201 }
202 
203 
204 template<class T1, class T2>
205 inline bool operator<=(const Tuple2<T1,T2>& a, const Tuple2<T1,T2>& b)
206 {
207  return !(b < a);
208 }
209 
210 
211 template<class T1, class T2>
212 inline bool operator>(const Tuple2<T1,T2>& a, const Tuple2<T1,T2>& b)
213 {
214  return (b < a);
215 }
216 
217 
218 template<class T1, class T2>
219 inline bool operator>=(const Tuple2<T1,T2>& a, const Tuple2<T1,T2>& b)
220 {
221  return !(a < b);
222 }
223 
224 
225 // * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
226 
227 // Comparing first only
228 
229 //- Compare tuple-like containers
230 // \return reference to the container with the smaller value of first
231 template<class T1>
232 struct minFirstOp
233 {
234  const Pair<T1>& operator()(const Pair<T1>& a, const Pair<T1>& b) const
235  {
236  return (b.first() < a.first()) ? b : a;
237  }
238 
239  template<class T2>
241  operator()(const Tuple2<T1,T2>& a, const Tuple2<T1,T2>& b) const
242  {
243  return (b.first() < a.first()) ? b : a;
244  }
245 };
246 
247 
248 //- Assign tuple-like container to use the one with the smaller value of first
249 template<class T1>
250 struct minFirstEqOp
251 {
252  void operator()(Pair<T1>& x, const Pair<T1>& y) const
253  {
254  if (y.first() < x.first()) x = y;
255  }
256 
257  template<class T2>
258  void operator()(Tuple2<T1,T2>& x, const Tuple2<T1,T2>& y) const
259  {
260  if (y.first() < x.first()) x = y;
261  }
262 };
263 
264 
265 //- Compare tuple-like containers
266 // \return reference to the container with the larger value of first
267 template<class T1>
268 struct maxFirstOp
269 {
270  const Pair<T1>& operator()(const Pair<T1>& a, const Pair<T1>& b) const
271  {
272  return (a.first() < b.first()) ? b : a;
273  }
274 
275  template<class T2>
277  operator()(const Tuple2<T1,T2>& a, const Tuple2<T1,T2>& b) const
278  {
279  return (a.first() < b.first()) ? b : a;
280  }
281 };
282 
283 
284 //- Assign tuple-like container to use the one with the larger value of first
285 template<class T1>
286 struct maxFirstEqOp
287 {
288  void operator()(Pair<T1>& x, const Pair<T1>& y) const
289  {
290  if (x.first() < y.first()) x = y;
291  }
292 
293  template<class T2>
294  void operator()(Tuple2<T1,T2>& x, const Tuple2<T1,T2>& y) const
295  {
296  if (x.first() < y.first()) x = y;
297  }
298 };
299 
300 
301 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
302 
303 //- Read Tuple2 from Istream, discarding contents of existing Tuple2.
304 template<class T1, class T2>
305 inline Istream& operator>>(Istream& is, Tuple2<T1,T2>& t)
306 {
307  is.readBegin("Tuple2");
308  is >> t.first() >> t.second();
309  is.readEnd("Tuple2");
310 
311  is.check(FUNCTION_NAME);
312  return is;
313 }
314 
315 
316 //- Read std::pair from Istream
317 template<class T1, class T2>
318 inline Istream& operator>>(Istream& is, std::pair<T1,T2>& t)
319 {
320  is.readBegin("std::pair");
321  is >> t.first >> t.second;
322  is.readEnd("std::pair");
323 
324  is.check(FUNCTION_NAME);
325  return is;
326 }
327 
328 
329 //- Write Tuple2 to Ostream.
330 template<class T1, class T2>
331 inline Ostream& operator<<(Ostream& os, const Tuple2<T1,T2>& t)
332 {
334  << t.first() << token::SPACE << t.second()
335  << token::END_LIST;
336 
337  return os;
338 }
339 
340 
341 //- Write std::pair to Ostream.
342 template<class T1, class T2>
343 inline Ostream& operator<<(Ostream& os, const std::pair<T1,T2>& t)
344 {
346  << t.first << token::SPACE << t.second
347  << token::END_LIST;
348 
349  return os;
350 }
351 
352 
353 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
354 
355 } // End namespace Foam
356 
357 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
358 
359 #endif
360 
361 // ************************************************************************* //
Foam::Tuple2::Tuple2
Tuple2(T1 &&f, T2 &&s)
Move construct from components.
Definition: Tuple2.H:88
Foam::reverse
void reverse(UList< T > &list, const label n)
Definition: UListI.H:449
Foam::maxFirstOp::operator()
const Pair< T1 > & operator()(const Pair< T1 > &a, const Pair< T1 > &b) const
Definition: Tuple2.H:269
Foam::Tuple2::second
T2 & second() noexcept
Return second.
Definition: Tuple2.H:136
Foam::Tuple2::Tuple2
Tuple2(const T1 &f, const T2 &s)
Copy construct from components.
Definition: Tuple2.H:81
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::minFirstEqOp::operator()
void operator()(Pair< T1 > &x, const Pair< T1 > &y) const
Definition: Tuple2.H:251
Foam::minFirstOp::operator()
const Pair< T1 > & operator()(const Pair< T1 > &a, const Pair< T1 > &b) const
Definition: Tuple2.H:233
Foam::maxFirstEqOp::operator()
void operator()(Pair< T1 > &x, const Pair< T1 > &y) const
Definition: Tuple2.H:287
Foam::Hash< Tuple2< T1, T2 > >::operator()
unsigned operator()(const Tuple2< T1, T2 > &obj, unsigned seed=0) const
Definition: Tuple2.H:149
Foam::Hash::operator()
unsigned operator()(const T &obj, unsigned seed=0) const
Definition: Hash.H:55
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
Pair.H
Foam::Istream::readEnd
bool readEnd(const char *funcName)
End read of data chunk, ends with ')'.
Definition: Istream.C:129
Foam::maxFirstOp
Compare tuple-like containers.
Definition: Tuple2.H:267
Foam::maxFirstOp::operator()
const Tuple2< T1, T2 > & operator()(const Tuple2< T1, T2 > &a, const Tuple2< T1, T2 > &b) const
Definition: Tuple2.H:276
Foam::Istream::readBegin
bool readBegin(const char *funcName)
Begin read of data chunk, starts with '('.
Definition: Istream.C:111
Foam::operator!=
bool operator!=(const eddy &a, const eddy &b)
Definition: eddy.H:239
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Foam::Hash
Hash function class. The default definition is for primitives. Non-primitives used to hash entries on...
Definition: Hash.H:53
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::minFirstEqOp
Assign tuple-like container to use the one with the smaller value of first.
Definition: Tuple2.H:249
Foam::Tuple2::Tuple2
Tuple2(const std::pair< T1, T2 > &vals)
Copy construct from std::pair.
Definition: Tuple2.H:95
Foam::minFirstEqOp::operator()
void operator()(Tuple2< T1, T2 > &x, const Tuple2< T1, T2 > &y) const
Definition: Tuple2.H:257
Istream.H
Foam::Tuple2::first
T1 & first() noexcept
Return first.
Definition: Tuple2.H:124
Foam::Hash< std::pair< T1, T2 > >::operator()
unsigned operator()(const std::pair< T1, T2 > &obj, unsigned seed=0) const
Definition: Tuple2.H:159
Foam::Tuple2::second_type
T2 second_type
Type of member second, the second template parameter (T2)
Definition: Tuple2.H:72
Foam::IOstream::check
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:58
Foam::minFirstOp
Compare tuple-like containers.
Definition: Tuple2.H:231
os
OBJstream os(runTime.globalPath()/outputName)
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
Ostream.H
Foam::Pair
An ordered pair of two objects of type <T> with first() and second() elements.
Definition: Pair.H:54
Foam::Tuple2::Tuple2
Tuple2(Istream &is)
Construct from Istream.
Definition: Tuple2.H:109
f
labelList f(nPoints)
Foam::token::SPACE
Space [isspace].
Definition: token.H:125
x
x
Definition: LISASMDCalcMethod2.H:52
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:295
Foam::Tuple2::second
const T2 & second() const noexcept
Return second.
Definition: Tuple2.H:130
Foam::token::END_LIST
End list [isseparator].
Definition: token.H:156
Foam::maxFirstEqOp::operator()
void operator()(Tuple2< T1, T2 > &x, const Tuple2< T1, T2 > &y) const
Definition: Tuple2.H:293
Foam::operator<
bool operator<(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A older than B.
Definition: IOstreamOption.H:398
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::Tuple2
A 2-tuple for storing two objects of dissimilar types. The container is similar in purpose to std::pa...
Definition: stringOps.H:60
Foam::Tuple2::Tuple2
Tuple2(std::pair< T1, T2 > &&vals)
Move construct from std::pair.
Definition: Tuple2.H:102
Foam::minFirstOp::operator()
const Tuple2< T1, T2 > & operator()(const Tuple2< T1, T2 > &a, const Tuple2< T1, T2 > &b) const
Definition: Tuple2.H:240
Foam::token::BEGIN_LIST
Begin list [isseparator].
Definition: token.H:155
Foam::Tuple2::first
const T1 & first() const noexcept
Return first.
Definition: Tuple2.H:118
Foam::Tuple2::first_type
T1 first_type
Type of member first, the first template parameter (T1)
Definition: Tuple2.H:69
Foam::maxFirstEqOp
Assign tuple-like container to use the one with the larger value of first.
Definition: Tuple2.H:285
y
scalar y
Definition: LISASMDCalcMethod1.H:14
Foam::operator>
bool operator>(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A newer than B.
Definition: IOstreamOption.H:418
Foam::Tuple2::Tuple2
Tuple2()=default
Default construct.