Tuple2.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2024 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::Tuple2
26 
27 Description
28  A 2-tuple for storing two objects of different types.
29 
30 See also
31  Foam::Pair for storing two objects of identical types.
32 
33 \*---------------------------------------------------------------------------*/
34 
35 #ifndef Tuple2_H
36 #define Tuple2_H
37 
38 #include "Istream.H"
39 #include "Hash.H"
40 
41 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
42 
43 namespace Foam
44 {
45 
46 // Forward declaration of friend functions and operators
47 
48 template<class Type1, class Type2>
49 class Tuple2;
50 
51 template<class Type1, class Type2>
52 void writeEntry(Ostream& os, const Tuple2<Type1, Type2>&);
53 
54 template<class Type1, class Type2>
55 inline Istream& operator>>(Istream&, Tuple2<Type1, Type2>&);
56 
57 template<class Type1, class Type2>
58 inline Ostream& operator<<(Ostream&, const Tuple2<Type1, Type2>&);
59 
60 
61 /*---------------------------------------------------------------------------*\
62  Class Tuple2 Declaration
63 \*---------------------------------------------------------------------------*/
64 
65 template<class Type1, class Type2>
66 class Tuple2
67 {
68  // Private Data
69 
70  Type1 f_;
71  Type2 s_;
72 
73 
74 public:
75 
76  //- Hashing function class
77  template<class HashT1=Hash<Type1>, class HashT2=Hash<Type2>>
78  class Hash
79  {
80  public:
81  Hash()
82  {}
83 
84  inline unsigned operator()
85  (
86  const Tuple2<Type1, Type2>&,
87  unsigned seed = 0
88  ) const;
89  };
90 
91 
92  // Static Data Members
93 
94  static const char* const typeName;
95 
96 
97  // Constructors
98 
99  //- Null constructor for lists
100  inline Tuple2()
101  {}
102 
103  //- Construct from components
104  inline Tuple2(const Type1& f, const Type2& s)
105  :
106  f_(f),
107  s_(s)
108  {}
109 
110  //- Construct from Istream
111  inline Tuple2(Istream& is)
112  {
113  is >> *this;
114  }
115 
116 
117  // Member Functions
118 
119  //- Return first
120  inline const Type1& first() const
121  {
122  return f_;
123  }
124 
125  //- Return first
126  inline Type1& first()
127  {
128  return f_;
129  }
130 
131  //- Return second
132  inline const Type2& second() const
133  {
134  return s_;
135  }
136 
137  //- Return second
138  inline Type2& second()
139  {
140  return s_;
141  }
142 
143 
144  // IOstream Operators
145 
146  //- Read Tuple2 from Istream, discarding contents of existing Tuple2.
147  friend Istream& operator>> <Type1, Type2>
148  (
149  Istream& is,
151  );
152 
153  // Write Tuple2 to Ostream.
154  friend Ostream& operator<< <Type1, Type2>
155  (
156  Ostream& os,
157  const Tuple2<Type1, Type2>& t2
158  );
159 };
160 
161 
162 template<class Type1, class Type2>
163 template<class HashT1, class HashT2>
165 (
166  const Tuple2<Type1, Type2>& t,
167  unsigned seed
168 ) const
169 {
170  // Hash incrementally
171  unsigned val = seed;
172  val = HashT1()(t.first(), val);
173  val = HashT2()(t.second(), val);
174  return val;
175 }
176 
177 
178 //- Return reverse of a tuple2
179 template<class Type1, class Type2>
181 {
182  return Tuple2<Type2, Type1>(t.second(), t.first());
183 }
184 
185 
186 template<class Type1, class Type2>
187 inline bool operator==
188 (
189  const Tuple2<Type1, Type2>& a,
190  const Tuple2<Type1, Type2>& b
191 )
192 {
193  return (a.first() == b.first() && a.second() == b.second());
194 }
195 
196 
197 template<class Type1, class Type2>
198 inline bool operator!=
199 (
200  const Tuple2<Type1, Type2>& a,
201  const Tuple2<Type1, Type2>& b
202 )
203 {
204  return !(a == b);
205 }
206 
207 
208 template<class Type1, class Type2>
210 {
211  is.readBegin("Tuple2");
212  is >> t2.f_ >> t2.s_;
213  is.readEnd("Tuple2");
214 
215  // Check state of Istream
216  is.check("operator>>(Istream&, Tuple2<Type1, Type2>&)");
217 
218  return is;
219 }
220 
221 
222 template<class Type1, class Type2>
223 inline Ostream& operator<<(Ostream& os, const Tuple2<Type1, Type2>& t2)
224 {
225  os << token::BEGIN_LIST
226  << t2.f_ << token::SPACE << t2.s_
227  << token::END_LIST;
228 
229  return os;
230 }
231 
232 
233 template<class Type1, class Type2>
234 void writeEntry(Ostream& os, const Tuple2<Type1, Type2>& t2)
235 {
236  os << t2;
237 }
238 
239 
240 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
241 
242 } // End namespace Foam
243 
244 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
245 
246 #endif
247 
248 // ************************************************************************* //
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
Istream & readEnd(const char *funcName)
Definition: Istream.C:103
Istream & readBegin(const char *funcName)
Definition: Istream.C:86
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
Hashing function class.
Definition: Tuple2.H:78
A 2-tuple for storing two objects of different types.
Definition: Tuple2.H:66
static const char *const typeName
Definition: Tuple2.H:93
const Type2 & second() const
Return second.
Definition: Tuple2.H:131
const Type1 & first() const
Return first.
Definition: Tuple2.H:119
friend Ostream & operator(Ostream &os, const Tuple2< Type1, Type2 > &t2)
Tuple2()
Null constructor for lists.
Definition: Tuple2.H:99
T & first()
Return the first element of the list.
Definition: UListI.H:114
@ BEGIN_LIST
Definition: token.H:109
@ END_LIST
Definition: token.H:110
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.name(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
volScalarField & b
Definition: createFields.H:25
Namespace for OpenFOAM.
void reverse(UList< T > &, const label n)
Definition: UListI.H:334
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
Istream & operator>>(Istream &, pistonPointEdgeData &)
Ostream & operator<<(Ostream &os, const fvConstraints &constraints)
labelList f(nPoints)