PairI.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) 2017-2020 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "Swap.H"
29 
30 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
31 
32 template<class T>
33 inline int Foam::Pair<T>::compare(const Pair<T>& a, const Pair<T>& b)
34 {
35  if (a.first() == b.first() && a.second() == b.second())
36  {
37  return 1;
38  }
39  if (a.first() == b.second() && a.second() == b.first())
40  {
41  return -1;
42  }
43 
44  return 0;
45 }
46 
47 
48 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
49 
50 template<class T>
51 inline Foam::Pair<T>::Pair(const T& f, const T& s)
52 {
53  first() = f;
54  second() = s;
55 }
56 
57 
58 template<class T>
59 inline Foam::Pair<T>::Pair(T&& f, T&& s)
60 {
61  first() = std::move(f);
62  second() = std::move(s);
63 }
64 
65 
66 template<class T>
67 inline Foam::Pair<T>::Pair(const std::pair<T,T>& vals)
68 {
69  first() = vals.first;
70  second() = vals.second;
71 }
72 
73 
74 template<class T>
75 inline Foam::Pair<T>::Pair(std::pair<T,T>&& vals)
76 {
77  first() = std::move(vals.first);
78  second() = std::move(vals.second);
79 }
80 
81 
82 template<class T>
84 :
85  FixedList<T, 2>(list)
86 {}
87 
88 
89 template<class T>
90 inline Foam::Pair<T>::Pair(const T& f, const T& s, const bool doSort)
91 {
92  if (doSort && s < f)
93  {
94  first() = s;
95  second() = f;
96  }
97  else
98  {
99  first() = f;
100  second() = s;
101  }
102 }
103 
104 
105 template<class T>
106 inline Foam::Pair<T>::Pair(const FixedList<T, 2>& list, const bool doSort)
107 :
108  Pair<T>(list.first(), list.last(), doSort)
109 {}
110 
111 
112 template<class T>
114 :
115  FixedList<T, 2>(is)
116 {}
117 
118 
119 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
120 
121 template<class T>
122 inline const T& Foam::Pair<T>::second() const noexcept
123 {
124  return last();
125 }
126 
127 
128 template<class T>
129 inline T& Foam::Pair<T>::second() noexcept
130 {
131  return last();
132 }
133 
134 
135 template<class T>
136 inline const T& Foam::Pair<T>::other(const T& a) const
137 {
138  if (first() == second())
139  {
141  << "Call to other only valid for Pair with differing elements:"
142  << *this << abort(FatalError);
143  }
144  else if (a == first())
145  {
146  return second();
147  }
148  else if (a != second())
149  {
151  << "Pair " << *this
152  << " does not contain " << a << abort(FatalError);
153  }
154 
155  return first();
156 }
157 
158 
159 template<class T>
160 inline void Foam::Pair<T>::flip()
161 {
162  Foam::Swap(first(), second());
163 }
164 
165 
166 template<class T>
167 inline bool Foam::Pair<T>::sorted() const
168 {
169  return !(second() < first());
170 }
171 
172 
173 template<class T>
174 inline void Foam::Pair<T>::sort()
175 {
176  if (second() < first())
177  {
178  flip();
179  }
180 }
181 
182 
183 // ************************************************************************* //
Foam::Pair::second
const T & second() const noexcept
Return second element, which is also the last element.
Definition: PairI.H:122
Foam::Swap
void Swap(DynamicList< T, SizeMinA > &a, DynamicList< T, SizeMinB > &b)
Definition: DynamicList.H:429
s
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))
Definition: gmvOutputSpray.H:25
Swap.H
Swap arguments as per std::swap, but in Foam namespace.
Foam::Pair::sorted
bool sorted() const
True if first() is less-than second()
Definition: PairI.H:167
Foam::Pair::sort
void sort()
Sort so that first() is less-than second()
Definition: PairI.H:174
Foam::Pair::compare
static int compare(const Pair< T > &a, const Pair< T > &b)
Compare Pairs.
Definition: PairI.H:33
Foam::constant::physicoChemical::b
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::Pair::Pair
Pair()=default
Default construct.
Foam::Pair::other
const T & other(const T &a) const
Return other element.
Definition: PairI.H:136
Foam::FatalError
error FatalError
Foam::Pair::flip
void flip()
Flip the Pair in-place.
Definition: PairI.H:160
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
T
const volScalarField & T
Definition: createFieldRefs.H:2
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::Pair
An ordered pair of two objects of type <T> with first() and second() elements.
Definition: Pair.H:54
f
labelList f(nPoints)
Foam::FixedList
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: HashTable.H:104