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-2022 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 <utility> // For std::move
48 
49 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
50 
51 namespace Foam
52 {
53 
54 // Forward Declarations
55 template<class T> class Pair;
56 
57 // Common pair types
58 typedef Pair<label> labelPair;
59 typedef Pair<word> wordPair;
60 
61 
62 /*---------------------------------------------------------------------------*\
63  Class Pair Declaration
64 \*---------------------------------------------------------------------------*/
65 
66 template<class T>
67 class Pair
68 :
69  public FixedList<T, 2>
70 {
71 public:
72 
73  // Generated Methods
74 
75  //- Default construct
76  Pair() = default;
77 
78  //- The front() accessor (from FixedList) has no purpose
79  void front() = delete;
80 
81  //- The back() accessor (from FixedList) has no purpose
82  void back() = delete;
83 
84 
85  // Constructors
86 
87  //- Copy construct from components
88  inline Pair(const T& f, const T& s);
89 
90  //- Move construct from components
91  inline Pair(T&& f, T&& s);
92 
93  //- Copy construct from std::pair
94  inline Pair(const std::pair<T,T>& vals);
95 
96  //- Move construct from std::pair
97  inline Pair(std::pair<T,T>&& vals);
98 
99  //- Copy construct FixedList of two items
100  inline Pair(const FixedList<T, 2>& list);
101 
102  //- Copy construct, optionally sorted with first less-than second
103  inline Pair(const T& f, const T& s, const bool doSort);
104 
105  //- Copy construct, optionally sorted with first less-than second
106  inline Pair(const FixedList<T, 2>& list, const bool doSort);
107 
108  //- Construct from Istream
109  inline explicit Pair(Istream& is);
110 
111 
112  // Member Functions
113 
114  // Access
115 
116  //- Access the first element
117  const T& first() const noexcept { return this->template get<0>(); }
118 
119  //- Access the first element
120  T& first() noexcept { return this->template get<0>(); }
121 
122  //- Access the second element
123  const T& second() const noexcept { return this->template get<1>(); }
124 
125  //- Access the second element
126  T& second() noexcept { return this->template get<1>(); }
127 
128  //- Return other element
129  inline const T& other(const T& a) const;
130 
131 
132  // Queries
133 
134  //- True if first() is less-than second()
135  inline bool sorted() const;
137 
138  // Editing
139 
140  //- Flip the Pair in-place.
141  inline void flip();
142 
143  //- Sort so that first() is less-than second()
144  inline void sort();
145 
147  // Comparison
148 
149  //- Compare Pairs
150  // \return
151  // - 0: different
152  // - +1: identical values and order used
153  // - -1: identical values, but in reversed order
154  static inline int compare(const Pair<T>& a, const Pair<T>& b);
155 
156 
157  // Hashing
158 
159  //- Symmetric hashing functor for Pair, hashes lower value first
160  // Regular hasher inherited from FixedList
161  struct symmHasher
162  {
163  unsigned operator()(const Pair<T>& obj, unsigned seed=0) const
164  {
165  Foam::Hash<T> op;
166  if (obj.second() < obj.first())
167  {
168  return op(obj.first(), op(obj.second(), seed));
169  }
170  else
171  {
172  return op(obj.second(), op(obj.first(), seed));
173  }
174  }
175  };
176 };
177 
178 
179 // * * * * * * * * * * * * * * * * * Traits * * * * * * * * * * * * * * * * //
180 
181 //- Pair is contiguous if the type is contiguous
182 template<class T>
183 struct is_contiguous<Pair<T>> : is_contiguous<T> {};
184 
185 //- Check for Pair of labels
186 template<class T>
187 struct is_contiguous_label<Pair<T>> : is_contiguous_label<T> {};
188 
189 //- Check for Pair of scalars
190 template<class T>
191 struct is_contiguous_scalar<Pair<T>> : is_contiguous_scalar<T> {};
192 
193 //- Hashing for Pair of data
194 template<class T>
195 struct Hash<Pair<T>> : Pair<T>::hasher {};
196 
197 
198 // * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
199 
200 //- Return reverse of a Pair
201 template<class T>
203 {
204  return Pair<T>(p.second(), p.first());
205 }
206 
207 
208 // * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * * //
209 
210 template<class T>
211 bool operator==(const Pair<T>& a, const Pair<T>& b)
212 {
213  return (a.first() == b.first() && a.second() == b.second());
214 }
215 
216 
217 template<class T>
218 bool operator!=(const Pair<T>& a, const Pair<T>& b)
219 {
220  return !(a == b);
221 }
222 
223 
224 template<class T>
225 bool operator<(const Pair<T>& a, const Pair<T>& b)
226 {
227  return
228  (
229  a.first() < b.first()
230  || (!(b.first() < a.first()) && a.second() < b.second())
231  );
232 }
233 
234 
235 template<class T>
236 bool operator<=(const Pair<T>& a, const Pair<T>& b)
237 {
238  return !(b < a);
239 }
240 
241 
242 template<class T>
243 bool operator>(const Pair<T>& a, const Pair<T>& b)
244 {
245  return (b < a);
246 }
247 
248 
249 template<class T>
250 bool operator>=(const Pair<T>& a, const Pair<T>& b)
251 {
252  return !(a < b);
253 }
254 
255 
256 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
257 
258 } // End namespace Foam
259 
260 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
261 
262 #include "PairI.H"
263 
264 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
265 
266 #endif
268 // ************************************************************************* //
const T & first() const noexcept
Access the first element.
Definition: Pair.H:136
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:101
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:798
bool operator>(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A newer than B.
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
Pair< word > wordPair
A pair of words.
Definition: Pair.H:54
void reverse(UList< T > &list, const label n)
Reverse the first n elements of the list.
Definition: UListI.H:442
const direction noexcept
Definition: Scalar.H:258
Pair< label > labelPair
A pair of labels.
Definition: Pair.H:50
bool sorted() const
True if first() is less-than second()
Definition: PairI.H:144
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:299
volScalarField & p
tmp< faMatrix< Type > > operator==(const faMatrix< Type > &, const faMatrix< Type > &)
const T & second() const noexcept
Access the second element.
Definition: Pair.H:146
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.