Pair.H
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #ifndef Pair_H
00038 #define Pair_H
00039
00040 #include "FixedList.H"
00041 #include "Istream.H"
00042
00043
00044
00045 namespace Foam
00046 {
00047
00048
00049
00050
00051
00052 template<class Type>
00053 class Pair
00054 :
00055 public FixedList<Type, 2>
00056 {
00057
00058 public:
00059
00060
00061
00062
00063 inline Pair()
00064 {}
00065
00066
00067 inline Pair(const Type& f, const Type& s)
00068 {
00069 first() = f;
00070 second() = s;
00071 }
00072
00073
00074 inline Pair(Istream& is)
00075 :
00076 FixedList<Type, 2>(is)
00077 {}
00078
00079
00080
00081
00082
00083 inline const Type& first() const
00084 {
00085 return this->operator[](0);
00086 }
00087
00088
00089 inline Type& first()
00090 {
00091 return this->operator[](0);
00092 }
00093
00094
00095 inline const Type& second() const
00096 {
00097 return this->operator[](1);
00098 }
00099
00100
00101 inline Type& second()
00102 {
00103 return this->operator[](1);
00104 }
00105
00106
00107 inline Pair<Type> reversePair() const
00108 {
00109 return Pair<Type>(second(), first());
00110 }
00111
00112
00113 inline const Type& other(const Type& a) const
00114 {
00115 if (first() == second())
00116 {
00117 FatalErrorIn("Pair<Type>::other(const Type&) const")
00118 << "Call to other only valid for Pair with differing"
00119 << " elements:" << *this << abort(FatalError);
00120 }
00121 else if (first() == a)
00122 {
00123 return second();
00124 }
00125 else
00126 {
00127 if (second() != a)
00128 {
00129 FatalErrorIn("Pair<Type>::other(const Type&) const")
00130 << "Pair " << *this
00131 << " does not contain " << a << abort(FatalError);
00132 }
00133 return first();
00134 }
00135 }
00136
00137
00138
00139
00140
00141
00142 static inline int compare(const Pair<Type>& a, const Pair<Type>& b)
00143 {
00144 if (a[0] == b[0] && a[1] == b[1])
00145 {
00146 return 1;
00147 }
00148 else if (a[0] == b[1] && a[1] == b[0])
00149 {
00150 return -1;
00151 }
00152 else
00153 {
00154 return 0;
00155 }
00156 }
00157
00158
00159
00160
00161 friend bool operator==(const Pair<Type>& a, const Pair<Type>& b)
00162 {
00163 return (a.first() == b.first() && a.second() == b.second());
00164 }
00165
00166 friend bool operator!=(const Pair<Type>& a, const Pair<Type>& b)
00167 {
00168 return !(a == b);
00169 }
00170 };
00171
00172
00173
00174
00175 }
00176
00177
00178
00179 #endif
00180
00181
Copyright © 2000-2009 OpenCFD Ltd