RDKit: Ranking.h Source File

| RDKit Open-source cheminformatics and machine learning.

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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