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