RDKit
Open-source cheminformatics and machine learning.
Loading...
Searching...
No Matches
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 {
27inline auto pairGreater = [](const auto &v1, const auto &v2) {
28 return v1.first > v2.first;
29};
30
31//! function for implementing < on two std::pairs. The first entries are
32/// compared.
33inline auto pairLess = [](const auto &v1, const auto &v2) {
34 return v1.first < v2.first;
35};
36
37//! ranks the entries in a vector
38/*!
39 \param vect the vector to rank
40 \param res is used to return the ranks of each entry
41*/
42template <typename T1, typename T2>
43void rankVect(const std::vector<T1> &vect, T2 &res) {
44 PRECONDITION(res.size() >= vect.size(), "vector size mismatch");
45 unsigned int nEntries = rdcast<unsigned int>(vect.size());
46
47 std::vector<unsigned int> indices(nEntries);
48 for (unsigned int i = 0; i < nEntries; ++i) {
49 indices[i] = i;
50 }
51 std::sort(indices.begin(), indices.end(),
52 [&](auto i1, auto i2) { return vect[i1] < vect[i2]; });
53
54 int currRank = 0;
55 unsigned int lastIdx = indices[0];
56 for (auto idx : indices) {
57 if (vect[idx] == vect[lastIdx]) {
58 res[idx] = currRank;
59 } else {
60 res[idx] = ++currRank;
61 lastIdx = idx;
62 }
63 }
64}
65} // namespace Rankers
66#endif
#define PRECONDITION(expr, mess)
Definition Invariant.h:109
Utility functionality used to rank sequences.
Definition Ranking.h:26
auto pairGreater
Definition Ranking.h:27
void rankVect(const std::vector< T1 > &vect, T2 &res)
ranks the entries in a vector
Definition Ranking.h:43
auto pairLess
Definition Ranking.h:33