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