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 // * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
145 
146 //- Return reverse of a Tuple2
147 template<class T1, class T2>
148 inline Tuple2<T2, T1> reverse(const Tuple2<T1,T2>& t)
149 {
150  return Tuple2<T2, T1>(t.second(), t.first());
151 }
152 
153 
154 // * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * * //
155 
156 template<class T1, class T2>
157 inline bool operator==(const Tuple2<T1,T2>& a, const Tuple2<T1,T2>& b)
158 {
159  return (a.first() == b.first() && a.second() == b.second());
160 }
161 
162 
163 template<class T1, class T2>
164 inline bool operator!=(const Tuple2<T1,T2>& a, const Tuple2<T1,T2>& b)
165 {
166  return !(a == b);
167 }
168 
169 
170 template<class T1, class T2>
171 inline bool operator<(const Tuple2<T1,T2>& a, const Tuple2<T1,T2>& b)
172 {
173  return
174  (
175  a.first() < b.first()
176  || (!(b.first() < a.first()) && a.second() < b.second())
177  );
178 }
179 
180 
181 template<class T1, class T2>
182 inline bool operator<=(const Tuple2<T1,T2>& a, const Tuple2<T1,T2>& b)
183 {
184  return !(b < a);
185 }
186 
187 
188 template<class T1, class T2>
189 inline bool operator>(const Tuple2<T1,T2>& a, const Tuple2<T1,T2>& b)
190 {
191  return (b < a);
192 }
193 
194 
195 template<class T1, class T2>
196 inline bool operator>=(const Tuple2<T1,T2>& a, const Tuple2<T1,T2>& b)
197 {
198  return !(a < b);
199 }
200 
201 
202 // * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
203 
204 // Comparing first only
205 
206 //- Compare tuple-like containers
207 // \return reference to the container with the smaller value of first
208 template<class T1>
209 struct minFirstOp
210 {
211  const Pair<T1>& operator()(const Pair<T1>& a, const Pair<T1>& b) const
212  {
213  return (b.first() < a.first()) ? b : a;
214  }
215 
216  template<class T2>
218  operator()(const Tuple2<T1,T2>& a, const Tuple2<T1,T2>& b) const
219  {
220  return (b.first() < a.first()) ? b : a;
221  }
222 };
223 
224 
225 //- Assign tuple-like container to use the one with the smaller value of first
226 template<class T1>
227 struct minFirstEqOp
228 {
229  void operator()(Pair<T1>& x, const Pair<T1>& y) const
230  {
231  if (y.first() < x.first()) x = y;
232  }
233 
234  template<class T2>
235  void operator()(Tuple2<T1,T2>& x, const Tuple2<T1,T2>& y) const
236  {
237  if (y.first() < x.first()) x = y;
238  }
239 };
240 
241 
242 //- Compare tuple-like containers
243 // \return reference to the container with the larger value of first
244 template<class T1>
245 struct maxFirstOp
246 {
247  const Pair<T1>& operator()(const Pair<T1>& a, const Pair<T1>& b) const
248  {
249  return (a.first() < b.first()) ? b : a;
250  }
251 
252  template<class T2>
254  operator()(const Tuple2<T1,T2>& a, const Tuple2<T1,T2>& b) const
255  {
256  return (a.first() < b.first()) ? b : a;
257  }
258 };
259 
260 
261 //- Assign tuple-like container to use the one with the larger value of first
262 template<class T1>
263 struct maxFirstEqOp
264 {
265  void operator()(Pair<T1>& x, const Pair<T1>& y) const
266  {
267  if (x.first() < y.first()) x = y;
268  }
269 
270  template<class T2>
271  void operator()(Tuple2<T1,T2>& x, const Tuple2<T1,T2>& y) const
272  {
273  if (x.first() < y.first()) x = y;
274  }
275 };
276 
277 
278 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
279 
280 //- Read Tuple2 from Istream, discarding contents of existing Tuple2.
281 template<class T1, class T2>
282 inline Istream& operator>>(Istream& is, Tuple2<T1,T2>& t)
283 {
284  is.readBegin("Tuple2");
285  is >> t.first() >> t.second();
286  is.readEnd("Tuple2");
287 
288  is.check(FUNCTION_NAME);
289  return is;
290 }
291 
292 
293 //- Read std::pair from Istream
294 template<class T1, class T2>
295 inline Istream& operator>>(Istream& is, std::pair<T1,T2>& t)
296 {
297  is.readBegin("std::pair");
298  is >> t.first >> t.second;
299  is.readEnd("std::pair");
300 
301  is.check(FUNCTION_NAME);
302  return is;
303 }
304 
305 
306 //- Write Tuple2 to Ostream.
307 template<class T1, class T2>
308 inline Ostream& operator<<(Ostream& os, const Tuple2<T1,T2>& t)
309 {
310  os << token::BEGIN_LIST
311  << t.first() << token::SPACE << t.second()
312  << token::END_LIST;
313 
314  return os;
315 }
316 
317 
318 //- Write std::pair to Ostream.
319 template<class T1, class T2>
320 inline Ostream& operator<<(Ostream& os, const std::pair<T1,T2>& t)
321 {
322  os << token::BEGIN_LIST
323  << t.first << token::SPACE << t.second
324  << token::END_LIST;
325 
326  return os;
327 }
328 
329 
330 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
331 
332 } // End namespace Foam
333 
334 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
335 
336 #endif
337 
338 // ************************************************************************* //
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:396
Foam::maxFirstOp::operator()
const Pair< T1 > & operator()(const Pair< T1 > &a, const Pair< T1 > &b) const
Definition: Tuple2.H:246
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: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::minFirstEqOp::operator()
void operator()(Pair< T1 > &x, const Pair< T1 > &y) const
Definition: Tuple2.H:228
Foam::minFirstOp::operator()
const Pair< T1 > & operator()(const Pair< T1 > &a, const Pair< T1 > &b) const
Definition: Tuple2.H:210
Foam::maxFirstEqOp::operator()
void operator()(Pair< T1 > &x, const Pair< T1 > &y) const
Definition: Tuple2.H:264
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:127
Foam::maxFirstOp
Compare tuple-like containers.
Definition: Tuple2.H:244
Foam::maxFirstOp::operator()
const Tuple2< T1, T2 > & operator()(const Tuple2< T1, T2 > &a, const Tuple2< T1, T2 > &b) const
Definition: Tuple2.H:253
Foam::Istream::readBegin
bool readBegin(const char *funcName)
Begin read of data chunk, starts with '('.
Definition: Istream.C:109
Foam::operator!=
bool operator!=(const eddy &a, const eddy &b)
Definition: eddy.H:235
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
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:226
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:234
Istream.H
Foam::Tuple2::first
T1 & first() noexcept
Return first.
Definition: Tuple2.H:124
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:51
Foam::minFirstOp
Compare tuple-like containers.
Definition: Tuple2.H:208
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
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:117
x
x
Definition: LISASMDCalcMethod2.H:52
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:270
Foam::Tuple2::second
const T2 & second() const noexcept
Return second.
Definition: Tuple2.H:130
Foam::token::END_LIST
End list [isseparator].
Definition: token.H:123
Foam::maxFirstEqOp::operator()
void operator()(Tuple2< T1, T2 > &x, const Tuple2< T1, T2 > &y) const
Definition: Tuple2.H:270
Foam::operator<
bool operator<(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A older than B.
Definition: IOstreamOption.H:397
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: Tuple2.H:57
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:217
Foam::token::BEGIN_LIST
Begin list [isseparator].
Definition: token.H:122
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:262
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:417
Foam::Tuple2::Tuple2
Tuple2()=default
Default construct.