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 <functional>
22 #include <algorithm>
23 #include <boost/foreach.hpp>
24 #include <boost/cstdint.hpp>
25 
26 namespace Rankers {
27 //! functor for implementing > on two std::pairs. The first entries are
28 //compared.
29 template <typename T1, typename T2>
31  : public std::binary_function<std::pair<T1, T2>, std::pair<T1, T2>, bool> {
32  bool operator()(const std::pair<T1, T2> &v1,
33  const std::pair<T1, T2> &v2) const {
34  return v1.first > v2.first;
35  }
36 };
37 
38 //! function for implementing < on two std::pairs. The first entries are
39 //compared.
40 template <typename T1, typename T2>
41 struct pairLess
42  : public std::binary_function<std::pair<T1, T2>, std::pair<T1, T2>, bool> {
43  bool operator()(const std::pair<T1, T2> &v1,
44  const std::pair<T1, T2> &v2) const {
45  return v1.first < v2.first;
46  }
47 };
48 
49 template <typename T>
50 class argless : public std::binary_function<T, T, bool> {
51  public:
52  argless(const T &c) : std::binary_function<T, T, bool>(), container(c){};
53  bool operator()(unsigned int v1, unsigned int v2) const {
54  return container[v1] < container[v2];
55  }
56  const T &container;
57 };
58 
59 //! ranks the entries in a vector
60 /*!
61  \param vect the vector to rank
62  \param res is used to return the ranks of each entry
63 */
64 template <typename T1, typename T2>
65 void rankVect(const std::vector<T1> &vect, T2 &res) {
66  PRECONDITION(res.size() >= vect.size(), "vector size mismatch");
67  unsigned int nEntries = rdcast<unsigned int>(vect.size());
68 
69  std::vector<unsigned int> indices(nEntries);
70  for (unsigned int i = 0; i < nEntries; ++i) indices[i] = i;
71  std::sort(indices.begin(), indices.end(), argless<std::vector<T1> >(vect));
72 
73  int currRank = 0;
74  T1 lastV = vect[indices[0]];
75  BOOST_FOREACH (unsigned int idx, indices) {
76  T1 v = vect[idx];
77  if (v == lastV) {
78  res[idx] = currRank;
79  } else {
80  res[idx] = ++currRank;
81  lastV = v;
82  }
83  }
84 }
85 }
86 #endif
Utility functionality used to rank sequences.
Definition: Ranking.h:26
STL namespace.
functor for implementing > on two std::pairs. The first entries are
Definition: Ranking.h:30
function for implementing < on two std::pairs. The first entries are
Definition: Ranking.h:41
const T & container
Definition: Ranking.h:56
argless(const T &c)
Definition: Ranking.h:52
bool operator()(const std::pair< T1, T2 > &v1, const std::pair< T1, T2 > &v2) const
Definition: Ranking.h:43
#define PRECONDITION(expr, mess)
Definition: Invariant.h:107
void rankVect(const std::vector< T1 > &vect, T2 &res)
ranks the entries in a vector
Definition: Ranking.h:65
bool operator()(unsigned int v1, unsigned int v2) const
Definition: Ranking.h:53
bool operator()(const std::pair< T1, T2 > &v1, const std::pair< T1, T2 > &v2) const
Definition: Ranking.h:32