Pair.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) 2017-2023 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::Pair
29 
30 Description
31  An ordered pair of two objects of type <T> with first() and second()
32  elements.
33 
34 SourceFiles
35  PairI.H
36 
37 See also
38  Foam::Tuple2 for storing two objects of dissimilar types.
39 
40 \*---------------------------------------------------------------------------*/
41 
42 #ifndef Foam_Pair_H
43 #define Foam_Pair_H
44 
45 #include "FixedList.H"
46 #include "Istream.H"
47 #include "Ostream.H"
48 #include <utility> // For std::move, std::pair
49 
50 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
51 
52 namespace Foam
53 {
54 
55 // Forward Declarations
56 template<class T> class Pair;
57 
58 // Common pair types
59 typedef Pair<label> labelPair;
60 typedef Pair<word> wordPair;
61 
62 
63 /*---------------------------------------------------------------------------*\
64  Class Pair Declaration
65 \*---------------------------------------------------------------------------*/
66 
67 template<class T>
68 class Pair
69 :
70  public FixedList<T, 2>
71 {
72 public:
73 
74  // Generated Methods
75 
76  //- Default construct
77  Pair() = default;
78 
79  //- The front() accessor (from FixedList) has no purpose
80  void front() = delete;
81 
82  //- The back() accessor (from FixedList) has no purpose
83  void back() = delete;
84 
85 
86  // Constructors
87 
88  //- Copy construct from components
89  inline Pair(const T& f, const T& s);
90 
91  //- Move construct from components
92  inline Pair(T&& f, T&& s);
93 
94  //- Copy construct from std::pair
95  inline Pair(const std::pair<T,T>& vals);
96 
97  //- Move construct from std::pair
98  inline Pair(std::pair<T,T>&& vals);
99 
100  //- Copy construct FixedList of two items
101  inline Pair(const FixedList<T, 2>& list);
102 
103  //- Copy construct, optionally sorted with first less-than second
104  inline Pair(const T& f, const T& s, const bool doSort);
105 
106  //- Copy construct, optionally sorted with first less-than second
107  inline Pair(const FixedList<T, 2>& list, const bool doSort);
108 
109  //- Construct from Istream
110  inline explicit Pair(Istream& is);
111 
112 
113  // Member Functions
114 
115  // Access
116 
117  //- Access the first element
118  const T& first() const noexcept { return this->template get<0>(); }
119 
120  //- Access the first element
121  T& first() noexcept { return this->template get<0>(); }
122 
123  //- Access the second element
124  const T& second() const noexcept { return this->template get<1>(); }
125 
126  //- Access the second element
127  T& second() noexcept { return this->template get<1>(); }
128 
129  //- Return other element
130  inline const T& other(const T& a) const;
131 
132 
133  // Queries
134 
135  //- True if first() is less-than-equal second()
136  inline bool is_sorted() const;
138 
139  // Editing
140 
141  //- Flip the Pair in-place.
142  inline void flip();
143 
144  //- Sort so that first() is less-than second()
145  inline void sort();
146 
148  // Comparison
149 
150  //- Compare Pairs
151  // \return
152  // - 0: different
153  // - +1: identical values and order used
154  // - -1: identical values, but in reversed order
155  static inline int compare(const Pair<T>& a, const Pair<T>& b);
156 
157 
158  // Hashing
159 
160  //- Symmetric hashing functor for Pair, hashes lower value first
161  // Regular hasher inherited from FixedList
162  struct symmHasher
163  {
164  unsigned operator()(const Pair<T>& obj, unsigned seed=0) const
165  {
166  Foam::Hash<T> op;
167  if (obj.second() < obj.first())
168  {
169  return op(obj.first(), op(obj.second(), seed));
170  }
171  else
172  {
173  return op(obj.second(), op(obj.first(), seed));
174  }
175  }
176  };
177 
178 
179  // Housekeeping
180 
181  //- Deprecated(2023-07) Use is_sorted() method
182  // \deprecated(2023-07) Use is_sorted() method
183  bool sorted() const { return is_sorted(); }
184 };
185 
186 
187 // * * * * * * * * * * * * * * * * * Traits * * * * * * * * * * * * * * * * //
188 
189 //- Pair is contiguous if the type is contiguous
190 template<class T>
191 struct is_contiguous<Pair<T>> : is_contiguous<T> {};
192 
193 //- Check for Pair of labels
194 template<class T>
195 struct is_contiguous_label<Pair<T>> : is_contiguous_label<T> {};
196 
197 //- Check for Pair of scalars
198 template<class T>
199 struct is_contiguous_scalar<Pair<T>> : is_contiguous_scalar<T> {};
200 
201 //- Hashing for Pair of data
202 template<class T>
203 struct Hash<Pair<T>> : Pair<T>::hasher {};
204 
205 //- Hashing for std::pair data
206 template<class T1, class T2>
207 struct Hash<std::pair<T1, T2>>
208 {
209  unsigned operator()(const std::pair<T1, T2>& obj, unsigned seed=0) const
210  {
211  return Hash<T2>()(obj.second, Hash<T1>()(obj.first, seed));
212  }
213 };
214 
215 
216 // * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
217 
218 //- Return reverse of a Pair
219 template<class T>
220 Pair<T> reverse(const Pair<T>& p)
221 {
222  return Pair<T>(p.second(), p.first());
223 }
224 
226 // * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * * //
227 
228 template<class T>
229 bool operator==(const Pair<T>& a, const Pair<T>& b)
230 {
231  return (a.first() == b.first() && a.second() == b.second());
232 }
233 
234 
235 template<class T>
236 bool operator!=(const Pair<T>& a, const Pair<T>& b)
237 {
238  return !(a == b);
239 }
240 
242 template<class T>
243 bool operator<(const Pair<T>& a, const Pair<T>& b)
244 {
245  return
246  (
247  a.first() < b.first()
248  || (!(b.first() < a.first()) && a.second() < b.second())
249  );
250 }
251 
252 
253 template<class T>
254 bool operator<=(const Pair<T>& a, const Pair<T>& b)
255 {
256  return !(b < a);
257 }
258 
260 template<class T>
261 bool operator>(const Pair<T>& a, const Pair<T>& b)
262 {
263  return (b < a);
264 }
265 
266 
267 template<class T>
268 bool operator>=(const Pair<T>& a, const Pair<T>& b)
269 {
270  return !(a < b);
271 }
272 
273 
274 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
275 
276 //- Read std::pair from Istream
277 template<class T1, class T2>
278 inline Istream& operator>>(Istream& is, std::pair<T1,T2>& t)
279 {
280  is.readBegin("pair");
281  is >> t.first >> t.second;
282  is.readEnd("pair");
284  is.check(FUNCTION_NAME);
285  return is;
286 }
287 
288 
289 //- Write std::pair to Ostream
290 template<class T1, class T2>
291 inline Ostream& operator<<(Ostream& os, const std::pair<T1,T2>& t)
292 {
294  << t.first << token::SPACE << t.second
295  << token::END_LIST;
296  return os;
297 }
298 
299 
300 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
301 
302 } // End namespace Foam
303 
304 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
305 
306 #include "PairI.H"
307 
308 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
309 
310 #endif
311 
312 // ************************************************************************* //
bool is_sorted() const
True if first() is less-than-equal second()
Definition: PairI.H:144
const T & first() const noexcept
Access the first element.
Definition: Pair.H:137
void back()=delete
The back() accessor (from FixedList) has no purpose.
void sort()
Sort so that first() is less-than second()
Definition: PairI.H:151
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: HashTable.H:107
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:45
bool readBegin(const char *funcName)
Begin read of data chunk, starts with &#39;(&#39;.
Definition: Istream.C:134
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
void flip()
Flip the Pair in-place.
Definition: PairI.H:137
T & first()
Access first element of the list, position [0].
Definition: UList.H:853
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.
const T & other(const T &a) const
Return other element.
Definition: PairI.H:113
Pair()=default
Default construct.
An ordered pair of two objects of type <T> with first() and second() elements.
Definition: instant.H:46
void front()=delete
The front() accessor (from FixedList) has no purpose.
static int compare(const Pair< T > &a, const Pair< T > &b)
Compare Pairs.
Definition: PairI.H:24
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
Pair< word > wordPair
A pair of words.
Definition: Pair.H:55
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
Pair< label > labelPair
A pair of labels.
Definition: Pair.H:51
bool sorted() const
Deprecated(2023-07) Use is_sorted() method.
Definition: Pair.H:225
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)
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Hash function class. The default definition is for primitives. Non-primitives used to hash entries on...
Definition: Hash.H:47
bool operator!=(const eddy &a, const eddy &b)
Definition: eddy.H:297
volScalarField & p
tmp< faMatrix< Type > > operator==(const faMatrix< Type > &, const faMatrix< Type > &)
const T & second() const noexcept
Access the second element.
Definition: Pair.H:147
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))
Namespace for OpenFOAM.