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 -------------------------------------------------------------------------------
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 Foam_Tuple2_H
41 #define Foam_Tuple2_H
42 
43 #include "Istream.H"
44 #include "Ostream.H"
45 #include "Pair.H"
46 
47 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48 
49 namespace Foam
50 {
51 
52 /*---------------------------------------------------------------------------*\
53  Class Tuple2 Declaration
54 \*---------------------------------------------------------------------------*/
55 
56 template<class T1, class T2 = T1>
57 class Tuple2
58 {
59  // Private Data
60 
61  T1 f_;
62  T2 s_;
63 
64 public:
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");
114  is.check(FUNCTION_NAME);
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 std::pair data
137 template<class T1, class T2>
138 struct Hash<std::pair<T1, T2>>
139 {
140  unsigned operator()(const std::pair<T1, T2>& obj, unsigned seed=0) const
141  {
142  return Hash<T2>()(obj.second, Hash<T1>()(obj.first, seed));
143  }
144 };
145 
146 
147 //- Hashing for Tuple2 data
148 template<class T1, class T2>
149 struct Hash<Tuple2<T1, T2>>
150 {
151  unsigned operator()(const Tuple2<T1, T2>& obj, unsigned seed=0) const
152  {
153  return Hash<T2>()(obj.second(), Hash<T1>()(obj.first(), seed));
154  }
155 };
156 
158 // * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
160 //- Return reverse of a Tuple2
161 template<class T1, class T2>
162 inline Tuple2<T2, T1> reverse(const Tuple2<T1,T2>& t)
163 {
164  return Tuple2<T2, T1>(t.second(), t.first());
165 }
166 
167 
168 // * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * * //
169 
170 template<class T1, class T2>
171 inline bool operator==(const Tuple2<T1,T2>& a, const Tuple2<T1,T2>& b)
172 {
173  return (a.first() == b.first() && a.second() == b.second());
174 }
175 
176 
177 template<class T1, class T2>
178 inline bool operator!=(const Tuple2<T1,T2>& a, const Tuple2<T1,T2>& b)
179 {
180  return !(a == b);
181 }
182 
183 
184 template<class T1, class T2>
185 inline bool operator<(const Tuple2<T1,T2>& a, const Tuple2<T1,T2>& b)
186 {
187  return
188  (
189  a.first() < b.first()
190  || (!(b.first() < a.first()) && a.second() < b.second())
191  );
192 }
193 
195 template<class T1, class T2>
196 inline bool operator<=(const Tuple2<T1,T2>& a, const Tuple2<T1,T2>& b)
197 {
198  return !(b < a);
199 }
200 
202 template<class T1, class T2>
203 inline bool operator>(const Tuple2<T1,T2>& a, const Tuple2<T1,T2>& b)
204 {
205  return (b < a);
206 }
207 
209 template<class T1, class T2>
210 inline bool operator>=(const Tuple2<T1,T2>& a, const Tuple2<T1,T2>& b)
211 {
212  return !(a < b);
213 }
214 
215 
216 // * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
217 
218 // Comparing first only
220 //- Compare tuple-like containers
221 // \return reference to the container with the smaller value of first
222 template<class T1>
223 struct minFirstOp
224 {
225  const Pair<T1>& operator()(const Pair<T1>& a, const Pair<T1>& b) const
226  {
227  return (b.first() < a.first()) ? b : a;
228  }
229 
230  template<class T2>
231  const Tuple2<T1,T2>&
232  operator()(const Tuple2<T1,T2>& a, const Tuple2<T1,T2>& b) const
233  {
234  return (b.first() < a.first()) ? b : a;
235  }
236 };
237 
238 
239 //- Assign tuple-like container to use the one with the smaller value of first
240 template<class T1>
241 struct minFirstEqOp
242 {
243  void operator()(Pair<T1>& x, const Pair<T1>& y) const
244  {
245  if (y.first() < x.first()) x = y;
246  }
247 
248  template<class T2>
249  void operator()(Tuple2<T1,T2>& x, const Tuple2<T1,T2>& y) const
250  {
251  if (y.first() < x.first()) x = y;
252  }
253 };
254 
255 
256 //- Compare tuple-like containers
257 // \return reference to the container with the larger value of first
258 template<class T1>
259 struct maxFirstOp
260 {
261  const Pair<T1>& operator()(const Pair<T1>& a, const Pair<T1>& b) const
262  {
263  return (a.first() < b.first()) ? b : a;
264  }
265 
266  template<class T2>
267  const Tuple2<T1,T2>&
268  operator()(const Tuple2<T1,T2>& a, const Tuple2<T1,T2>& b) const
269  {
270  return (a.first() < b.first()) ? b : a;
271  }
272 };
273 
274 
275 //- Assign tuple-like container to use the one with the larger value of first
276 template<class T1>
278 {
279  void operator()(Pair<T1>& x, const Pair<T1>& y) const
280  {
281  if (x.first() < y.first()) x = y;
282  }
283 
284  template<class T2>
285  void operator()(Tuple2<T1,T2>& x, const Tuple2<T1,T2>& y) const
286  {
287  if (x.first() < y.first()) x = y;
288  }
289 };
291 
292 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
293 
294 //- Read std::pair from Istream
295 template<class T1, class T2>
296 inline Istream& operator>>(Istream& is, std::pair<T1,T2>& t)
297 {
298  is.readBegin("pair");
299  is >> t.first >> t.second;
300  is.readEnd("pair");
301 
302  is.check(FUNCTION_NAME);
303  return is;
304 }
305 
306 
307 //- Read Tuple2 from Istream
308 template<class T1, class T2>
309 inline Istream& operator>>(Istream& is, Tuple2<T1,T2>& t)
310 {
311  is.readBegin("Tuple2");
312  is >> t.first() >> t.second();
313  is.readEnd("Tuple2");
314 
315  is.check(FUNCTION_NAME);
316  return is;
317 }
319 
320 //- Write std::pair to Ostream.
321 template<class T1, class T2>
322 inline Ostream& operator<<(Ostream& os, const std::pair<T1,T2>& t)
323 {
325  << t.first << token::SPACE << t.second
326  << token::END_LIST;
327  return os;
328 }
329 
330 
331 //- Write Tuple2 to Ostream.
332 template<class T1, class T2>
333 inline Ostream& operator<<(Ostream& os, const Tuple2<T1,T2>& t)
334 {
336  << t.first() << token::SPACE << t.second()
337  << token::END_LIST;
338  return os;
339 }
340 
341 
342 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
343 
344 } // End namespace Foam
345 
346 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
347 
348 #endif
349 
350 // ************************************************************************* //
const T & first() const noexcept
Access the first element.
Definition: Pair.H:136
T1 first_type
Type of member first, the first template parameter (T1)
Definition: Tuple2.H:64
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:45
A 2-tuple for storing two objects of dissimilar types. The container is similar in purpose to std::pa...
Definition: stringOps.H:55
bool readBegin(const char *funcName)
Begin read of data chunk, starts with &#39;(&#39;.
Definition: Istream.C:104
const Pair< T1 > & operator()(const Pair< T1 > &a, const Pair< T1 > &b) const
Definition: Tuple2.H:251
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
Compare tuple-like containers.
Definition: Tuple2.H:249
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.
void operator()(Pair< T1 > &x, const Pair< T1 > &y) const
Definition: Tuple2.H:312
scalar y
An ordered pair of two objects of type <T> with first() and second() elements.
Definition: instant.H:46
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
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:442
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
bool readEnd(const char *funcName)
End read of data chunk, ends with &#39;)&#39;.
Definition: Istream.C:122
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
labelList f(nPoints)
Compare tuple-like containers.
Definition: Tuple2.H:290
void operator()(Pair< T1 > &x, const Pair< T1 > &y) const
Definition: Tuple2.H:271
Hash function class. The default definition is for primitives. Non-primitives used to hash entries on...
Definition: Hash.H:47
const T2 & second() const noexcept
Access the second element.
Definition: Tuple2.H:142
const Pair< T1 > & operator()(const Pair< T1 > &a, const Pair< T1 > &b) const
Definition: Tuple2.H:292
bool operator!=(const eddy &a, const eddy &b)
Definition: eddy.H:299
tmp< faMatrix< Type > > operator==(const faMatrix< Type > &, const faMatrix< Type > &)
const T1 & first() const noexcept
Access the first element.
Definition: Tuple2.H:132
Tuple2(std::pair< T1, T2 > &&vals)
Move construct from std::pair.
Definition: Tuple2.H:109
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))
Assign tuple-like container to use the one with the larger value of first.
Definition: Tuple2.H:310
T2 second_type
Type of member second, the second template parameter (T2)
Definition: Tuple2.H:69
Namespace for OpenFOAM.
Tuple2()=default
Default construct.