atlas  0.6
comparison.h
Go to the documentation of this file.
1 
5 /*
6  Copyright (C) 2004,2005 Fokko du Cloux
7  part of the Atlas of Lie Groups and Representations
8 
9  For license information see the LICENSE file
10 */
11 
12 #ifndef COMPARISON_H /* guard against multiple inclusions */
13 #define COMPARISON_H
14 
15 /******** type declarations **************************************************/
16 
17 namespace atlas {
18 
19 namespace comparison {
20 
21  template<typename F> class Compare;
22 
23 }
24 
25 namespace comparison {
26 
27 /* The template Compare turns a unary function object into a comparison class
28  The comparison is done by comparing the images under the function object
29  by means of operator<.
30 
31  So if |f| is a unary function object, one can set
32 
33  Compare<F> comp=Compare<F>(f); // or Compare<F> comp(f);
34 
35  after which |comp(x,y)| is equivalent to |f(x)<f(y)|. Rather than being
36  stored in a variable, |Compare<F>(f)| is usually passed as an argument (of
37  type |const Compare<F>&|) to a (STL) function needing a comparison object.
38  */
39 
40 // template function allowing |compare(f)| to abbreviate |Compare<F>(f)|
41 template<typename F>
42  inline Compare<F> compare(const F& f) { return Compare<F>(f);}
43 
44 template<typename F> // F is the type of a unary function object
45 class Compare {
46  private:
47  const F& d_f;
48  public:
49  explicit Compare(const F& f):d_f(f) {}
50 
51  bool operator() (typename F::argument_type x,
52  typename F::argument_type y) const
53  {
54  return d_f(x) < d_f(y); // F::result_type should define operator<
55  }
56 };
57 
58 }
59 
60 }
61 
62 #endif
const F & d_f
Definition: comparison.h:47
Compare(const F &f)
Definition: comparison.h:49
Compare< F > compare(const F &f)
Definition: comparison.h:42
Definition: Atlas.h:38
Definition: comparison.h:21
Definition: common.h:36