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-------------------------------------------------------------------------------
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 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
50namespace Foam
51{
52
53/*---------------------------------------------------------------------------*\
54 Class Tuple2 Declaration
55\*---------------------------------------------------------------------------*/
56
57template<class T1, class T2 = T1>
58class Tuple2
59{
60 // Private Data
61
62 T1 f_;
63 T2 s_;
64
65public:
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
147template<class T1, class T2>
148struct Hash<Tuple2<T1, T2>>
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
157template<class T1, class T2>
158struct Hash<std::pair<T1, T2>>
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
170template<class T1, class T2>
171inline 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
179template<class T1, class T2>
180inline 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
186template<class T1, class T2>
187inline bool operator!=(const Tuple2<T1,T2>& a, const Tuple2<T1,T2>& b)
188{
189 return !(a == b);
190}
191
192
193template<class T1, class T2>
194inline 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
204template<class T1, class T2>
205inline bool operator<=(const Tuple2<T1,T2>& a, const Tuple2<T1,T2>& b)
206{
207 return !(b < a);
208}
209
210
211template<class T1, class T2>
212inline bool operator>(const Tuple2<T1,T2>& a, const Tuple2<T1,T2>& b)
213{
214 return (b < a);
215}
216
217
218template<class T1, class T2>
219inline 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
231template<class T1>
232struct minFirstOp
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
249template<class T1>
250struct minFirstEqOp
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
267template<class T1>
268struct maxFirstOp
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
285template<class T1>
286struct maxFirstEqOp
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.
304template<class T1, class T2>
306{
307 is.readBegin("Tuple2");
308 is >> t.first() >> t.second();
309 is.readEnd("Tuple2");
310
312 return is;
313}
314
315
316//- Read std::pair from Istream
317template<class T1, class T2>
318inline 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
325 return is;
326}
327
328
329//- Write Tuple2 to Ostream.
330template<class T1, class T2>
331inline Ostream& operator<<(Ostream& os, const Tuple2<T1,T2>& t)
332{
334 << t.first() << token::SPACE << t.second()
336
337 return os;
338}
339
340
341//- Write std::pair to Ostream.
342template<class T1, class T2>
343inline Ostream& operator<<(Ostream& os, const std::pair<T1,T2>& t)
344{
346 << t.first << token::SPACE << t.second
348
349 return os;
350}
351
352
353// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
354
355} // End namespace Foam
356
357// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
358
359#endif
360
361// ************************************************************************* //
scalar y
T & first() noexcept
The first element of the list, position [0].
Definition: FixedListI.H:207
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:58
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
bool readEnd(const char *funcName)
End read of data chunk, ends with ')'.
Definition: Istream.C:129
bool readBegin(const char *funcName)
Begin read of data chunk, starts with '('.
Definition: Istream.C:111
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
An ordered pair of two objects of type <T> with first() and second() elements.
Definition: Pair.H:69
A 2-tuple for storing two objects of dissimilar types. The container is similar in purpose to std::pa...
Definition: Tuple2.H:58
Tuple2(const T1 &f, const T2 &s)
Copy construct from components.
Definition: Tuple2.H:81
T1 first_type
Type of member first, the first template parameter (T1)
Definition: Tuple2.H:69
Tuple2(std::pair< T1, T2 > &&vals)
Move construct from std::pair.
Definition: Tuple2.H:102
Tuple2(Istream &is)
Construct from Istream.
Definition: Tuple2.H:109
const T1 & first() const noexcept
Return first.
Definition: Tuple2.H:118
Tuple2(T1 &&f, T2 &&s)
Move construct from components.
Definition: Tuple2.H:88
T2 second_type
Type of member second, the second template parameter (T2)
Definition: Tuple2.H:72
T2 & second() noexcept
Return second.
Definition: Tuple2.H:136
Tuple2()=default
Default construct.
Tuple2(const std::pair< T1, T2 > &vals)
Copy construct from std::pair.
Definition: Tuple2.H:95
T1 & first() noexcept
Return first.
Definition: Tuple2.H:124
const T2 & second() const noexcept
Return second.
Definition: Tuple2.H:130
T & first()
Return the first element of the list.
Definition: UListI.H:202
@ BEGIN_LIST
Begin list [isseparator].
Definition: token.H:155
@ END_LIST
End list [isseparator].
Definition: token.H:156
@ SPACE
Space [isspace].
Definition: token.H:125
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:239
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:449
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)
Definition: boundaryPatch.C:83
tmp< faMatrix< Type > > operator==(const faMatrix< Type > &, const faMatrix< Type > &)
Istream & operator>>(Istream &, directionInfo &)
const direction noexcept
Definition: Scalar.H:223
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
Definition: createFields.H:27
unsigned operator()(const Tuple2< T1, T2 > &obj, unsigned seed=0) const
Definition: Tuple2.H:149
unsigned operator()(const std::pair< 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:54
unsigned operator()(const T &obj, unsigned seed=0) const
Definition: Hash.H:55
Assign tuple-like container to use the one with the larger value of first.
Definition: Tuple2.H:286
void operator()(Pair< T1 > &x, const Pair< T1 > &y) const
Definition: Tuple2.H:287
void operator()(Tuple2< T1, T2 > &x, const Tuple2< T1, T2 > &y) const
Definition: Tuple2.H:293
Compare tuple-like containers.
Definition: Tuple2.H:268
const Tuple2< T1, T2 > & operator()(const Tuple2< T1, T2 > &a, const Tuple2< T1, T2 > &b) const
Definition: Tuple2.H:276
const Pair< T1 > & operator()(const Pair< T1 > &a, const Pair< T1 > &b) const
Definition: Tuple2.H:269
Assign tuple-like container to use the one with the smaller value of first.
Definition: Tuple2.H:250
void operator()(Pair< T1 > &x, const Pair< T1 > &y) const
Definition: Tuple2.H:251
void operator()(Tuple2< T1, T2 > &x, const Tuple2< T1, T2 > &y) const
Definition: Tuple2.H:257
Compare tuple-like containers.
Definition: Tuple2.H:232
const Tuple2< T1, T2 > & operator()(const Tuple2< T1, T2 > &a, const Tuple2< T1, T2 > &b) const
Definition: Tuple2.H:240
const Pair< T1 > & operator()(const Pair< T1 > &a, const Pair< T1 > &b) const
Definition: Tuple2.H:233