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 <cstdint>
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  unsigned int lastIdx = indices[0];
75  for (auto idx : indices) {
76  if (vect[idx] == vect[lastIdx]) {
77  res[idx] = currRank;
78  } else {
79  res[idx] = ++currRank;
80  lastIdx = idx;
81  }
82  }
83 }
84 } // namespace Rankers
85 #endif
#define PRECONDITION(expr, mess)
Definition: Invariant.h:109
bool operator()(unsigned int v1, unsigned int v2) const
Definition: Ranking.h:53
const T & container
Definition: Ranking.h:56
argless(const T &c)
Definition: Ranking.h:52
Utility functionality used to rank sequences.
Definition: Ranking.h:26
void rankVect(const std::vector< T1 > &vect, T2 &res)
ranks the entries in a vector
Definition: Ranking.h:65
bool operator()(const std::pair< T1, T2 > &v1, const std::pair< T1, T2 > &v2) const
Definition: Ranking.h:32
bool operator()(const std::pair< T1, T2 > &v1, const std::pair< T1, T2 > &v2) const
Definition: Ranking.h:43