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-2020 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 inline Istream& operator>>(Istream&, Tuple2<Type1, Type2>&);
53 
54 template<class Type1, class Type2>
55 inline Ostream& operator<<(Ostream&, const Tuple2<Type1, Type2>&);
56 
57 
58 /*---------------------------------------------------------------------------*\
59  class Tuple2 Declaration
60 \*---------------------------------------------------------------------------*/
61 
62 template<class Type1, class Type2>
63 class Tuple2
64 {
65  // Private Data
66 
67  Type1 f_;
68  Type2 s_;
69 
70 
71 public:
72 
73  //- Hashing function class
74  template<class HashT1=Hash<Type1>, class HashT2=Hash<Type2>>
75  class Hash
76  {
77  public:
78  Hash()
79  {}
80 
81  inline unsigned operator()
82  (
83  const Tuple2<Type1, Type2>&,
84  unsigned seed = 0
85  ) const;
86  };
87 
88 
89  // Static Data Members
90 
91  static const char* const typeName;
92 
93 
94  // Constructors
95 
96  //- Null constructor for lists
97  inline Tuple2()
98  {}
99 
100  //- Construct from components
101  inline Tuple2(const Type1& f, const Type2& s)
102  :
103  f_(f),
104  s_(s)
105  {}
106 
107  //- Construct from Istream
108  inline Tuple2(Istream& is)
109  {
110  is >> *this;
111  }
112 
113 
114  // Member Functions
115 
116  //- Return first
117  inline const Type1& first() const
118  {
119  return f_;
120  }
121 
122  //- Return first
123  inline Type1& first()
124  {
125  return f_;
126  }
127 
128  //- Return second
129  inline const Type2& second() const
130  {
131  return s_;
132  }
133 
134  //- Return second
135  inline Type2& second()
136  {
137  return s_;
138  }
139 
140 
141  // IOstream Operators
142 
143  //- Read Tuple2 from Istream, discarding contents of existing Tuple2.
144  friend Istream& operator>> <Type1, Type2>
145  (
146  Istream& is,
148  );
149 
150  // Write Tuple2 to Ostream.
151  friend Ostream& operator<< <Type1, Type2>
152  (
153  Ostream& os,
154  const Tuple2<Type1, Type2>& t2
155  );
156 };
157 
158 
159 template<class Type1, class Type2>
160 template<class HashT1, class HashT2>
162 (
163  const Tuple2<Type1, Type2>& t,
164  unsigned seed
165 ) const
166 {
167  // Hash incrementally
168  unsigned val = seed;
169  val = HashT1()(t.first(), val);
170  val = HashT2()(t.second(), val);
171  return val;
172 }
173 
174 
175 //- Return reverse of a tuple2
176 template<class Type1, class Type2>
178 {
179  return Tuple2<Type2, Type1>(t.second(), t.first());
180 }
181 
182 
183 template<class Type1, class Type2>
184 inline bool operator==
185 (
186  const Tuple2<Type1, Type2>& a,
187  const Tuple2<Type1, Type2>& b
188 )
189 {
190  return (a.first() == b.first() && a.second() == b.second());
191 }
192 
193 
194 template<class Type1, class Type2>
195 inline bool operator!=
196 (
197  const Tuple2<Type1, Type2>& a,
198  const Tuple2<Type1, Type2>& b
199 )
200 {
201  return !(a == b);
202 }
203 
204 
205 template<class Type1, class Type2>
207 {
208  is.readBegin("Tuple2");
209  is >> t2.f_ >> t2.s_;
210  is.readEnd("Tuple2");
211 
212  // Check state of Istream
213  is.check("operator>>(Istream&, Tuple2<Type1, Type2>&)");
214 
215  return is;
216 }
217 
218 
219 template<class Type1, class Type2>
220 inline Ostream& operator<<(Ostream& os, const Tuple2<Type1, Type2>& t2)
221 {
222  os << token::BEGIN_LIST
223  << t2.f_ << token::SPACE << t2.s_
224  << token::END_LIST;
225 
226  return os;
227 }
228 
229 
230 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
231 
232 } // End namespace Foam
233 
234 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
235 
236 #endif
237 
238 // ************************************************************************* //
Istream & readBegin(const char *funcName)
Definition: Istream.C:86
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
A 2-tuple for storing two objects of different types.
Definition: HashTable.H:65
const Type1 & first() const
Return first.
Definition: Tuple2.H:116
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
const dimensionedScalar b
Wien displacement law constant: default SI units: [m K].
Definition: createFields.H:27
friend Ostream & operator(Ostream &os, const Tuple2< Type1, Type2 > &t2)
Istream & readEnd(const char *funcName)
Definition: Istream.C:103
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.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
friend Istream & operator>>(Istream &is, Tuple2< Type1, Type2 > &t2)
Read Tuple2 from Istream, discarding contents of existing Tuple2.
Definition: Tuple2.H:205
Hashing function class.
Definition: Tuple2.H:74
Istream & operator>>(Istream &, directionInfo &)
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
void reverse(UList< T > &, const label n)
Definition: UListI.H:334
labelList f(nPoints)
Tuple2()
Null constructor for lists.
Definition: Tuple2.H:96
const Type2 & second() const
Return second.
Definition: Tuple2.H:128
static const char *const typeName
Definition: Tuple2.H:90
Namespace for OpenFOAM.