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-2021 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 Pair_H
43#define Pair_H
44
45#include "FixedList.H"
46#include "Istream.H"
47#include <utility>
48
49// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
50
51namespace Foam
52{
53
54// Forward Declarations
55template<class T> class Pair;
56
57// Common pair types
59typedef Pair<word> wordPair;
60
61
62/*---------------------------------------------------------------------------*\
63 Class Pair Declaration
64\*---------------------------------------------------------------------------*/
65
66template<class T>
67class Pair
68:
69 public FixedList<T, 2>
70{
71public:
72
73 // Constructors
74
75 //- Default construct
76 Pair() = default;
77
78 //- Copy construct from components
79 inline Pair(const T& f, const T& s);
80
81 //- Move construct from components
82 inline Pair(T&& f, T&& s);
83
84 //- Copy construct from std::pair
85 inline Pair(const std::pair<T,T>& vals);
86
87 //- Move construct from std::pair
88 inline Pair(std::pair<T,T>&& vals);
89
90 //- Copy construct FixedList of two items
91 inline Pair(const FixedList<T, 2>& list);
92
93 //- Copy construct, optionally sorted with first less-than second
94 inline Pair(const T& f, const T& s, const bool doSort);
95
96 //- Copy construct, optionally sorted with first less-than second
97 inline Pair(const FixedList<T, 2>& list, const bool doSort);
98
99 //- Construct from Istream
100 inline explicit Pair(Istream& is);
101
102
103 // Member Functions
104
105 // Access
106
107 //- Return first element
108 using FixedList<T, 2>::first;
109
110 //- Return last element
111 using FixedList<T, 2>::last;
112
113 //- Return second element, which is also the last element
114 inline const T& second() const noexcept;
115
116 //- Return second element, which is also the last element
117 inline T& second() noexcept;
118
119 //- Return other element
120 inline const T& other(const T& a) const;
121
122
123 // Queries
124
125 //- True if first() is less-than second()
126 inline bool sorted() const;
127
128
129 // Editing
130
131 //- Flip the Pair in-place.
132 inline void flip();
133
134 //- Sort so that first() is less-than second()
135 inline void sort();
136
137
138 // Comparison
139
140 //- Compare Pairs
141 // \return
142 // - 0: different
143 // - +1: identical values and order used
144 // - -1: identical values, but in reversed order
145 static inline int compare(const Pair<T>& a, const Pair<T>& b);
146
147
148 // Hashing
149
150 //- Symmetric hashing functor for Pair, hashes lower value first
151 // Regular hasher inherited from FixedList
152 struct symmHasher
154 unsigned operator()(const Pair<T>& obj, unsigned seed=0) const
155 {
156 Foam::Hash<T> op;
157 if (obj.second() < obj.first())
158 {
159 return op(obj.first(), op(obj.second(), seed));
160 }
161 else
162 {
163 return op(obj.second(), op(obj.first(), seed));
164 }
165 }
166 };
167};
168
169
170// * * * * * * * * * * * * * * * * * Traits * * * * * * * * * * * * * * * * //
171
172//- Pair is contiguous if the type is contiguous
173template<class T>
174struct is_contiguous<Pair<T>> : is_contiguous<T> {};
175
176//- Check for Pair of labels
177template<class T>
179
180//- Check for Pair of scalars
181template<class T>
183
184//- Hashing for Pair of data
185template<class T>
186struct Hash<Pair<T>> : Pair<T>::hasher {};
187
188
189// * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
190
191//- Return reverse of a Pair
192template<class T>
193Pair<T> reverse(const Pair<T>& p)
194{
195 return Pair<T>(p.second(), p.first());
196}
197
198
199// * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * * //
200
201template<class T>
202bool operator==(const Pair<T>& a, const Pair<T>& b)
203{
204 return (a.first() == b.first() && a.second() == b.second());
205}
206
207
208template<class T>
209bool operator!=(const Pair<T>& a, const Pair<T>& b)
210{
211 return !(a == b);
212}
213
214
215template<class T>
216bool operator<(const Pair<T>& a, const Pair<T>& b)
217{
218 return
219 (
220 a.first() < b.first()
221 || (!(b.first() < a.first()) && a.second() < b.second())
222 );
223}
224
225
226template<class T>
227bool operator<=(const Pair<T>& a, const Pair<T>& b)
228{
229 return !(b < a);
230}
231
232
233template<class T>
234bool operator>(const Pair<T>& a, const Pair<T>& b)
235{
236 return (b < a);
237}
238
239
240template<class T>
241bool operator>=(const Pair<T>& a, const Pair<T>& b)
242{
243 return !(a < b);
244}
245
246
247// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
248
249} // End namespace Foam
250
251// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
252
253#include "PairI.H"
254
255// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
256
257#endif
258
259// ************************************************************************* //
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: FixedList.H:81
T & first() noexcept
The first element of the list, position [0].
Definition: FixedListI.H:207
T & last() noexcept
The last element of the list, position [N-1].
Definition: FixedListI.H:221
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
An ordered pair of two objects of type <T> with first() and second() elements.
Definition: Pair.H:69
Pair()=default
Default construct.
void flip()
Flip the Pair in-place.
Definition: PairI.H:158
bool sorted() const
True if first() is less-than second()
Definition: PairI.H:165
void sort()
Sort so that first() is less-than second()
Definition: PairI.H:172
const T & other(const T &a) const
Return other element.
Definition: PairI.H:134
T & second() noexcept
Return second element, which is also the last element.
Definition: PairI.H:127
static int compare(const Pair< T > &a, const Pair< T > &b)
Compare Pairs.
Definition: PairI.H:31
const T & second() const noexcept
Return second element, which is also the last element.
Definition: PairI.H:120
T & first()
Return the first element of the list.
Definition: UListI.H:202
volScalarField & p
const volScalarField & T
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.
bool operator!=(const eddy &a, const eddy &b)
Definition: eddy.H:239
Pair< label > labelPair
A pair of labels.
Definition: Pair.H:57
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:449
bool operator>=(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A same or newer than B.
tmp< faMatrix< Type > > operator==(const faMatrix< Type > &, const faMatrix< Type > &)
const direction noexcept
Definition: Scalar.H:223
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:58
bool operator>(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A newer than B.
labelList f(nPoints)
volScalarField & b
Definition: createFields.H:27
Hash function class. The default definition is for primitives. Non-primitives used to hash entries on...
Definition: Hash.H:54
Symmetric hashing functor for Pair, hashes lower value first.
Definition: Pair.H:152
unsigned operator()(const Pair< T > &obj, unsigned seed=0) const
Definition: Pair.H:153
A template class to specify if a data type is composed solely of Foam::label elements.
Definition: contiguous.H:86
A template class to specify if a data type is composed solely of Foam::scalar elements.
Definition: contiguous.H:94
A template class to specify that a data type can be considered as being contiguous in memory.
Definition: contiguous.H:78