Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
11License
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
27Class
28 Foam::Pair
29
30Description
31 An ordered pair of two objects of type <T> with first() and second()
32 elements.
33
34SourceFiles
35 PairI.H
36
37See 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
52namespace Foam
53{
55// Forward Declarations
56template<class T> class Pair;
57
58// Common pair types
59typedef Pair<label> labelPair;
60typedef Pair<word> wordPair;
61
62
63/*---------------------------------------------------------------------------*\
64 Class Pair Declaration
65\*---------------------------------------------------------------------------*/
66
67template<class T>
68class Pair
69:
70 public FixedList<T, 2>
71{
72public:
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;
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);
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
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>(); }
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
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
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};
179
180// * * * * * * * * * * * * * * * * * Traits * * * * * * * * * * * * * * * * //
181
182//- Pair is contiguous if the type is contiguous
183template<class T>
185
186//- Check for Pair of labels
187template<class T>
189
190//- Check for Pair of scalars
191template<class T>
193
194//- Hashing for Pair of data
195template<class T>
196struct Hash<Pair<T>> : Pair<T>::hasher {};
197
198//- Hashing for std::pair data
199template<class T1, class T2>
200struct Hash<std::pair<T1, T2>>
202 unsigned operator()(const std::pair<T1, T2>& obj, unsigned seed=0) const
204 return Hash<T2>()(obj.second, Hash<T1>()(obj.first, seed));
205 }
206};
207
208
209// * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
210
211//- Return reverse of a Pair
212template<class T>
213Pair<T> reverse(const Pair<T>& p)
214{
215 return Pair<T>(p.second(), p.first());
216}
217
218
219// * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * * //
220
221template<class T>
222bool operator==(const Pair<T>& a, const Pair<T>& b)
223{
224 return (a.first() == b.first() && a.second() == b.second());
226
227
228template<class T>
229bool operator!=(const Pair<T>& a, const Pair<T>& b)
230{
231 return !(a == b);
232}
233
234
235template<class T>
236bool operator<(const Pair<T>& a, const Pair<T>& b)
238 return
239 (
240 a.first() < b.first()
241 || (!(b.first() < a.first()) && a.second() < b.second())
242 );
244
245
246template<class T>
247bool operator<=(const Pair<T>& a, const Pair<T>& b)
248{
249 return !(b < a);
250}
252
253template<class T>
254bool operator>(const Pair<T>& a, const Pair<T>& b)
255{
256 return (b < a);
257}
258
259
260template<class T>
261bool operator>=(const Pair<T>& a, const Pair<T>& b)
262{
263 return !(a < b);
265
266
267// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
268
269//- Read std::pair from Istream
270template<class T1, class T2>
271inline Istream& operator>>(Istream& is, std::pair<T1,T2>& t)
272{
273 is.readBegin("pair");
274 is >> t.first >> t.second;
275 is.readEnd("pair");
276
277 is.check(FUNCTION_NAME);
278 return is;
279}
281
282//- Write std::pair to Ostream
283template<class T1, class T2>
284inline Ostream& operator<<(Ostream& os, const std::pair<T1,T2>& t)
285{
287 << t.first << token::SPACE << t.second
289 return os;
290}
291
292
293// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
294
295} // End namespace Foam
296
297// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
299#include "PairI.H"
300
301// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
302
303#endif
304
305// ************************************************************************* //
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition IOstream.C:45
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
bool readEnd(const char *funcName)
End read of data chunk, ends with ')'.
Definition Istream.C:152
bool readBegin(const char *funcName)
Begin read of data chunk, starts with '('.
Definition Istream.C:134
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
An ordered pair of two objects of type <T> with first() and second() elements.
Definition Pair.H:66
Pair(T &&f, T &&s)
Move construct from components.
Definition PairI.H:50
Pair()=default
Default construct.
Pair(std::pair< T, T > &&vals)
Move construct from std::pair.
Definition PairI.H:66
Pair(const T &f, const T &s)
Copy construct from components.
Definition PairI.H:42
void flip()
Flip the Pair in-place.
Definition PairI.H:137
void back()=delete
The back() accessor (from FixedList) has no purpose.
void front()=delete
The front() accessor (from FixedList) has no purpose.
bool is_sorted() const
True if first() is less-than-equal second().
Definition PairI.H:144
T & first() noexcept
Access the first element.
Definition Pair.H:142
void sort()
Sort so that first() is less-than second().
Definition PairI.H:151
Pair(const FixedList< T, 2 > &list, const bool doSort)
Copy construct, optionally sorted with first less-than second.
Definition PairI.H:97
const T & other(const T &a) const
Return other element.
Definition PairI.H:113
T & second() noexcept
Access the second element.
Definition Pair.H:152
static int compare(const Pair< T > &a, const Pair< T > &b)
Compare Pairs.
Definition PairI.H:24
const T & first() const noexcept
Access the first element.
Definition Pair.H:137
Pair(const std::pair< T, T > &vals)
Copy construct from std::pair.
Definition PairI.H:58
Pair(const FixedList< T, 2 > &list)
Copy construct FixedList of two items.
Definition PairI.H:74
Pair(Istream &is)
Construct from Istream.
Definition PairI.H:104
Pair(const T &f, const T &s, const bool doSort)
Copy construct, optionally sorted with first less-than second.
Definition PairI.H:81
const T & second() const noexcept
Access the second element.
Definition Pair.H:147
@ BEGIN_LIST
Begin list [isseparator].
Definition token.H:174
@ END_LIST
End list [isseparator].
Definition token.H:175
@ SPACE
Space [isspace].
Definition token.H:144
volScalarField & p
OBJstream os(runTime.globalPath()/outputName)
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))
#define FUNCTION_NAME
Namespace for OpenFOAM.
bool operator!=(const eddy &a, const eddy &b)
Definition eddy.H:297
Pair< label > labelPair
A pair of labels.
Definition Pair.H:54
bool operator<=(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A same or older than B.
void reverse(UList< T > &list, const label n)
Reverse the first n elements of the list.
Definition UListI.H:539
bool operator>=(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A same or newer than B.
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces).
tmp< faMatrix< Type > > operator==(const faMatrix< Type > &, const faMatrix< Type > &)
Istream & operator>>(Istream &, directionInfo &)
const direction noexcept
Definition scalarImpl.H:265
bool operator<(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A older than B.
Pair< word > wordPair
A pair of words.
Definition Pair.H:55
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
bool operator>(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A newer than B.
labelList f(nPoints)
volScalarField & b
unsigned operator()(const std::pair< T1, T2 > &obj, unsigned seed=0) const
Definition Pair.H:251
Hash function class. The default definition is for primitives. Non-primitives used to hash entries on...
Definition Hash.H:48
unsigned operator()(const T &obj, unsigned seed=0) const
Definition Hash.H:49
Symmetric hashing functor for Pair, hashes lower value first.
Definition Pair.H:202
unsigned operator()(const Pair< T > &obj, unsigned seed=0) const
Definition Pair.H:203
A template class to specify if a data type is composed solely of Foam::label elements.
Definition contiguous.H:82
A template class to specify if a data type is composed solely of Foam::scalar elements.
Definition contiguous.H:87
A template class to specify that a data type can be considered as being contiguous in memory.
Definition contiguous.H:70