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
26namespace Rankers {
27//! functor for implementing > on two std::pairs. The first entries are
28/// compared.
29template <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.
40template <typename T1, typename T2>
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
49template <typename T>
50class 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*/
64template <typename T1, typename T2>
65void 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) {
71 indices[i] = i;
72 }
73 std::sort(indices.begin(), indices.end(), argless<std::vector<T1>>(vect));
74
75 int currRank = 0;
76 unsigned int lastIdx = indices[0];
77 for (auto idx : indices) {
78 if (vect[idx] == vect[lastIdx]) {
79 res[idx] = currRank;
80 } else {
81 res[idx] = ++currRank;
82 lastIdx = idx;
83 }
84 }
85}
86} // namespace Rankers
87#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