npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
CompareByIndex.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_COMPAREBYINDEX_HH_
2 #define NPSTAT_COMPAREBYINDEX_HH_
3 
4 /*!
5 // \file CompareByIndex.hh
6 //
7 // \brief Compare subscriptable objects (e.g., vectors) by their k-th element
8 //
9 // Author: I. Volobouev
10 //
11 // March 2010
12 */
13 
14 namespace npstat {
15  /**
16  // Comparison functor for classes which support subscripting and for
17  // pointers to such classes. To be used with std::sort and such.
18  */
19  template <typename T>
21  {
22  public:
23  /** Constructor takes the index of the element to be compared */
24  inline explicit LessByIndex(const unsigned i) : i_(i) {}
25 
26  inline bool operator()(const T& x, const T& y) const
27  {return x[i_] < y[i_];}
28  inline bool operator()(const T* x, const T* y) const
29  {return (*x)[i_] < (*y)[i_];}
30  private:
31  LessByIndex();
32  unsigned i_;
33  };
34 
35  /**
36  // Comparison functor for classes which support subscripting and for
37  // pointers to such classes. To be used with std::sort and such.
38  */
39  template <typename T>
41  {
42  public:
43  /** Constructor takes the index of the element to be compared */
44  inline explicit GreaterByIndex(const unsigned i) : i_(i) {}
45 
46  inline bool operator()(const T& x, const T& y) const
47  {return y[i_] < x[i_];}
48  inline bool operator()(const T* x, const T* y) const
49  {return (*y)[i_] < (*x)[i_];}
50  private:
52  unsigned i_;
53  };
54 }
55 
56 #endif // NPSTAT_COMPAREBYINDEX_HH_
Definition: CompareByIndex.hh:41
GreaterByIndex(const unsigned i)
Definition: CompareByIndex.hh:44
Definition: CompareByIndex.hh:21
LessByIndex(const unsigned i)
Definition: CompareByIndex.hh:24
Definition: AbsArrayProjector.hh:14