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 Tuple2 data
137 template<class T1, class T2>
138 struct 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
150 template<class T1, class T2>
151 inline 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 
159 template<class T1, class T2>
160 inline 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 
166 template<class T1, class T2>
167 inline bool operator!=(const Tuple2<T1,T2>& a, const Tuple2<T1,T2>& b)
168 {
169  return !(a == b);
170 }
171 
173 template<class T1, class T2>
174 inline 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  );
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 !(b < a);
188 }
189 
190 
191 template<class T1, class T2>
192 inline bool operator>(const Tuple2<T1,T2>& a, const Tuple2<T1,T2>& b)
193 {
194  return (b < a);
195 }
196 
197 
198 template<class T1, class T2>
199 inline 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
211 template<class T1>
212 struct minFirstOp
213 {
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
229 template<class T1>
230 struct 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>
238  void operator()(Tuple2<T1,T2>& x, const Tuple2<T1,T2>& y) const
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
247 template<class T1>
248 struct 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
258  {
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
265 template<class T1>
266 struct 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;
277  }
278 };
280 
281 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
282 
283 //- Read Tuple2 from Istream
284 template<class T1, class T2>
285 inline Istream& operator>>(Istream& is, Tuple2<T1,T2>& t)
286 {
287  is.readBegin("Tuple2");
288  is >> t.first() >> t.second();
289  is.readEnd("Tuple2");
290 
291  is.check(FUNCTION_NAME);
292  return is;
293 }
294 
295 
296 //- Write Tuple2 to Ostream
297 template<class T1, class T2>
298 inline Ostream& operator<<(Ostream& os, const Tuple2<T1,T2>& t)
299 {
301  << t.first() << token::SPACE << t.second()
302  << token::END_LIST;
303  return os;
304 }
306 
307 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
308 
309 } // End namespace Foam
310 
311 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
312 
313 #endif
314 
315 // ************************************************************************* //
const T & first() const noexcept
Access the first element.
Definition: Pair.H:137
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:54
bool readBegin(const char *funcName)
Begin read of data chunk, starts with &#39;(&#39;.
Definition: Istream.C:134
const Pair< T1 > & operator()(const Pair< T1 > &a, const Pair< T1 > &b) const
Definition: Tuple2.H:238
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:236
bool operator>(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A newer than B.
Begin list [isseparator].
Definition: token.H:161
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:299
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:131
End list [isseparator].
Definition: token.H:162
void reverse(UList< T > &list, const label n)
Reverse the first n elements of the list.
Definition: UListI.H:520
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:56
const direction noexcept
Definition: Scalar.H:258
bool readEnd(const char *funcName)
End read of data chunk, ends with &#39;)&#39;.
Definition: Istream.C:152
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
labelList f(nPoints)
Compare tuple-like containers.
Definition: Tuple2.H:277
void operator()(Pair< T1 > &x, const Pair< T1 > &y) const
Definition: Tuple2.H:258
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:279
bool operator!=(const eddy &a, const eddy &b)
Definition: eddy.H:297
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:297
T2 second_type
Type of member second, the second template parameter (T2)
Definition: Tuple2.H:69
Namespace for OpenFOAM.
Tuple2()=default
Default construct.