RDKit
Open-source cheminformatics and machine learning.
Ranking.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2004-2015 Greg Landrum and Rational Discovery LLC
3 //
4 // @@ All Rights Reserved @@
5 // This file is part of the RDKit.
6 // The contents are covered by the terms of the BSD license
7 // which is included in the file license.txt, found at the root
8 // of the RDKit source tree.
9 //
10 
11 //! \file Ranking.h
12 /*!
13  \brief Utility functionality used to rank sequences
14 
15  Much of this used to be in GraphMol/RankAtoms.h
16 */
17 #ifndef RD_RANKING_H
18 #define RD_RANKING_H
19 
20 #include <vector>
21 #include <algorithm>
22 #include <boost/foreach.hpp>
23 #include <boost/cstdint.hpp>
24 
25 namespace Rankers {
26  //! functor for implementing > on two std::pairs. The first entries are compared.
27  template <typename T1, typename T2>
28  struct pairGreater : public std::binary_function<std::pair<T1,T2>,std::pair<T1,T2>,bool> {
29  bool operator() (const std::pair<T1,T2> &v1,const std::pair<T1,T2> &v2) const {
30  return v1.first > v2.first;
31  }
32  };
33 
34  //! function for implementing < on two std::pairs. The first entries are compared.
35  template <typename T1, typename T2>
36  struct pairLess : public std::binary_function<std::pair<T1,T2>,std::pair<T1,T2>,bool> {
37  bool operator() (const std::pair<T1,T2> &v1,const std::pair<T1,T2> &v2) const {
38  return v1.first < v2.first;
39  }
40  };
41 
42  template <typename T>
43  class argless : public std::binary_function<T,T,bool> {
44  public:
45  argless(const T& c) : std::binary_function<T,T,bool>(), container(c) {};
46  bool operator() (unsigned int v1,unsigned int v2) const {
47  return container[v1]<container[v2];
48  }
49  const T &container;
50  };
51 
52 
53  //! ranks the entries in a vector
54  /*!
55  \param vect the vector to rank
56  \param res is used to return the ranks of each entry
57  */
58  template <typename T1,typename T2>
59  void rankVect(const std::vector<T1> &vect,T2 &res){
60  PRECONDITION(res.size()>=vect.size(),"vector size mismatch");
61  unsigned int nEntries = vect.size();
62 
63  std::vector< unsigned int > indices(nEntries);
64  for(unsigned int i=0;i<nEntries;++i) indices[i]=i;
65  std::sort(indices.begin(),indices.end(),argless< std::vector<T1> >(vect) );
66 
67  int currRank=0;
68  T1 lastV = vect[indices[0]];
69  BOOST_FOREACH(unsigned int idx,indices){
70  T1 v = vect[idx];
71  if(v==lastV){
72  res[idx] = currRank;
73  } else {
74  res[idx] = ++currRank;
75  lastV = v;
76  }
77  }
78  }
79 
80 }
81 #endif
Utility functionality used to rank sequences.
Definition: Ranking.h:25
STL namespace.
functor for implementing > on two std::pairs. The first entries are compared.
Definition: Ranking.h:28
function for implementing < on two std::pairs. The first entries are compared.
Definition: Ranking.h:36
const T & container
Definition: Ranking.h:49
argless(const T &c)
Definition: Ranking.h:45
#define PRECONDITION(expr, mess)
Definition: Invariant.h:119
void rankVect(const std::vector< T1 > &vect, T2 &res)
ranks the entries in a vector
Definition: Ranking.h:59
bool operator()(const std::pair< T1, T2 > &v1, const std::pair< T1, T2 > &v2) const
Definition: Ranking.h:29