rivet is hosted by Hepforge, IPPP Durham
Rivet  2.7.0
Cmp.hh
1 // -*- C++ -*-
2 #ifndef RIVET_Cmp_HH
3 #define RIVET_Cmp_HH
4 
5 #include "Rivet/Config/RivetCommon.hh"
6 #include "Rivet/Projection.hh"
7 #include "Cmp.fhh"
8 #include <typeinfo>
9 
10 
11 namespace Rivet {
12 
13 
26  template <typename T>
27  class Cmp {
28  public:
29 
31 
32 
34  Cmp(const T& t1, const T& t2)
35  : _value(UNDEFINED), _objects(&t1, &t2) { }
36 
38  template <typename U>
39  Cmp(const Cmp<U>& x)
40  : _value(x), _objects(0, 0) { }
41 
43  ~Cmp() { };
44 
46  template <typename U>
47  const Cmp<T>& operator=(const Cmp<U>& x) {
48  _value = x;
49  return *this;
50  }
51 
53 
54  public:
55 
57  operator CmpState() const {
58  _compare();
59  return _value;
60  }
61 
63  operator int() const {
64  _compare();
65  return _value;
66  }
67 
69  template <typename U>
70  const Cmp<T>& operator||(const Cmp<U>& c) const {
71  _compare();
72  if (_value == EQUIVALENT) _value = c;
73  return *this;
74  }
75 
76  private:
77 
79  void _compare() const {
80  if (_value == UNDEFINED) {
81  less<T> l;
82  if ( l(*_objects.first, *_objects.second) ) _value = ORDERED;
83  else if ( l(*_objects.second, *_objects.first) ) _value = UNORDERED;
84  else _value = EQUIVALENT;
85  }
86  }
87 
89  mutable CmpState _value;
90 
92  pair<const T*, const T*> _objects;
93 
94  };
95 
96 
111  template <>
112  class Cmp<Projection> {
113  public:
114 
116 
117  Cmp(const Projection& p1, const Projection& p2)
119  : _value(UNDEFINED), _objects(&p1, &p2)
120  { }
121 
123  template <typename U>
124  Cmp(const Cmp<U>& x)
125  : _value(x), _objects(0, 0)
126  { }
127 
129  ~Cmp() { };
130 
132  template <typename U>
133  const Cmp<Projection>& operator=(const Cmp<U>& x) {
134  _value = x;
135  return *this;
136  }
138 
139  public:
140 
142  operator CmpState() const {
143  _compare();
144  return _value;
145  }
146 
147 
149  operator int() const {
150  _compare();
151  return _value;
152  }
153 
155  template <typename U>
156  const Cmp<Projection>& operator||(const Cmp<U>& c) const {
157  _compare();
158  if (_value == EQUIVALENT) _value = c;
159  return *this;
160  }
161 
162  private:
163 
165  void _compare() const {
166  if (_value == UNDEFINED) {
167  const std::type_info& id1 = typeid(*_objects.first);
168  const std::type_info& id2 = typeid(*_objects.second);
169  if (id1.before(id2)) _value = ORDERED;
170  else if (id2.before(id1)) _value = UNORDERED;
171  else {
172  int c = _objects.first->compare(*_objects.second);
173  if (c < 0) _value = ORDERED;
174  else if (c > 0) _value = UNORDERED;
175  else _value = EQUIVALENT;
176  }
177  }
178  }
179 
180  private:
181 
183  mutable CmpState _value;
184 
186  pair<const Projection*, const Projection*> _objects;
187 
188  };
189 
190 
191 
192 
206  template <>
207  class Cmp<double> {
208  public:
209 
211 
212  Cmp(const double p1, const double p2)
214  : _value(UNDEFINED), _numA(p1), _numB(p2)
215  { }
216 
218  template <typename U>
219  Cmp(const Cmp<U>& x)
220  : _value(x), _numA(0.0), _numB(0.0)
221  { }
222 
224  ~Cmp() { }
225 
227  template <typename U>
228  const Cmp<double>& operator=(const Cmp<U>& x) {
229  _value = x;
230  return *this;
231  }
233 
234  public:
235 
237  operator CmpState() const {
238  _compare();
239  return _value;
240  }
241 
243  operator int() const {
244  _compare();
245  return _value;
246  }
247 
249  template <typename U>
250  const Cmp<double>& operator||(const Cmp<U>& c) const {
251  _compare();
252  if (_value == EQUIVALENT) _value = c;
253  return *this;
254  }
255 
256  private:
257 
259  void _compare() const {
260  if (_value == UNDEFINED) {
261  if (fuzzyEquals(_numA,_numB)) _value = EQUIVALENT;
262  else if (_numA < _numB) _value = ORDERED;
263  else _value = UNORDERED;
264  }
265  }
266 
267  private:
268 
270  mutable CmpState _value;
271 
273  double _numA, _numB;
274 
275  };
276 
277 
278 
280 
281 
282 
284  template <typename T>
285  inline Cmp<T> cmp(const T& t1, const T& t2) {
286  return Cmp<T>(t1, t2);
287  }
288 
289 
292 
293 
295  inline Cmp<Projection> pcmp(const Projection& p1, const Projection& p2) {
296  return Cmp<Projection>(p1, p2);
297  }
298 
301  inline Cmp<Projection> pcmp(const Projection& parent1, const Projection& parent2, const string& pname) {
302  return Cmp<Projection>(parent1.getProjection(pname), parent2.getProjection(pname));
303  }
304 
308  inline Cmp<Projection> pcmp(const Projection* parent1, const Projection& parent2, const string& pname) {
309  assert(parent1);
310  return Cmp<Projection>(parent1->getProjection(pname), parent2.getProjection(pname));
311  }
312 
316  inline Cmp<Projection> pcmp(const Projection& parent1, const Projection* parent2, const string& pname) {
317  assert(parent2);
318  return Cmp<Projection>(parent1.getProjection(pname), parent2->getProjection(pname));
319  }
320 
323  inline Cmp<Projection> pcmp(const Projection* parent1, const Projection* parent2, const string& pname) {
324  assert(parent1);
325  assert(parent2);
326  return Cmp<Projection>(parent1->getProjection(pname), parent2->getProjection(pname));
327  }
328 
329 
330 }
331 
332 
333 #endif
Definition: ALICE_2010_I880049.cc:13
Cmp(const Cmp< U > &x)
The copy constructor.
Definition: Cmp.hh:39
const Cmp< double > & operator||(const Cmp< U > &c) const
If this state is equivalent, set this state to the state of c.
Definition: Cmp.hh:250
Cmp(const Cmp< U > &x)
The copy constructor.
Definition: Cmp.hh:219
~Cmp()
The destructor is not virtual since this is not intended to be a base class.
Definition: Cmp.hh:224
Cmp(const T &t1, const T &t2)
The default constructor.
Definition: Cmp.hh:34
~Cmp()
The destructor is not virtual since this is not intended to be a base class.
Definition: Cmp.hh:43
const Cmp< double > & operator=(const Cmp< U > &x)
The assignment operator.
Definition: Cmp.hh:228
const Cmp< Projection > & operator=(const Cmp< U > &x)
The assignment operator.
Definition: Cmp.hh:133
const Cmp< T > & operator||(const Cmp< U > &c) const
If this state is equivalent, set this state to the state of c.
Definition: Cmp.hh:70
~Cmp()
The destructor is not virtual since this is not intended to be a base class.
Definition: Cmp.hh:129
Specialization of Cmp for checking the ordering of two {Projection}s.
Definition: Cmp.hh:112
Cmp< Projection > pcmp(const Projection &p1, const Projection &p2)
Global helper function for easy creation of Cmp<Projection> objects.
Definition: Cmp.hh:295
Cmp< Projection > PCmp
Typedef for Cmp<Projection>
Definition: Cmp.hh:291
Specialization of Cmp for checking the ordering of two floating point numbers.
Definition: Cmp.hh:207
std::enable_if< std::is_arithmetic< N1 >::value &&std::is_arithmetic< N2 >::value &&(std::is_floating_point< N1 >::value||std::is_floating_point< N2 >::value), bool >::type fuzzyEquals(N1 a, N2 b, double tolerance=1e-5)
Compare two numbers for equality with a degree of fuzziness.
Definition: MathUtils.hh:45
const PROJ & getProjection(const std::string &name) const
Definition: ProjectionApplier.hh:56
Definition: Cmp.hh:27
const Cmp< T > & operator=(const Cmp< U > &x)
The assignment operator.
Definition: Cmp.hh:47
Base class for all Rivet projections.
Definition: Projection.hh:29
const Cmp< Projection > & operator||(const Cmp< U > &c) const
If this state is equivalent, set this state to the state of c.
Definition: Cmp.hh:156
Cmp(const Cmp< U > &x)
The copy constructor.
Definition: Cmp.hh:124
Cmp< T > cmp(const T &t1, const T &t2)
Global helper function for easy creation of Cmp objects.
Definition: Cmp.hh:285