RDKit
Open-source cheminformatics and machine learning.
Loading...
Searching...
No Matches
DuplicatedSeedCache.h
Go to the documentation of this file.
1//
2// Copyright (C) 2014 Novartis Institutes for BioMedical Research
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#include <RDGeneral/export.h>
11#pragma once
12#include <cstring>
13#include <map>
14#include <vector>
15#include <stdexcept>
16#include <algorithm>
17
18namespace RDKit {
19namespace FMCS {
21 public:
22 typedef bool TValue;
23 class TKey {
24 std::vector<unsigned int> AtomIdx; // sorted
25 std::vector<unsigned int> BondIdx; // sorted
26 public:
27 size_t getNumAtoms() const { return AtomIdx.size(); }
28 size_t getNumBonds() const { return BondIdx.size(); }
29
30 void addAtom(unsigned int i) {
31 auto it = std::lower_bound(AtomIdx.begin(), AtomIdx.end(), i);
32 AtomIdx.insert(it, i);
33 }
34 void addBond(unsigned int i) {
35 auto it = std::lower_bound(BondIdx.begin(), BondIdx.end(), i);
36 BondIdx.insert(it, i);
37 }
38
39 bool operator==(const TKey &right) const { // opt.
40 return AtomIdx.size() == right.AtomIdx.size() &&
41 BondIdx.size() == right.BondIdx.size() &&
42 0 == std::memcmp(&AtomIdx[0], &right.AtomIdx[0],
43 AtomIdx.size() * sizeof(unsigned int)) &&
44 0 == std::memcmp(&BondIdx[0], &right.BondIdx[0],
45 BondIdx.size() * sizeof(unsigned int));
46 }
47
48 bool operator<(const TKey &right) const {
49 if (AtomIdx.size() < right.AtomIdx.size()) {
50 return true;
51 }
52 if (AtomIdx.size() > right.AtomIdx.size()) {
53 return false;
54 }
55
56 if (BondIdx.size() < right.BondIdx.size()) {
57 return true;
58 }
59 if (BondIdx.size() > right.BondIdx.size()) {
60 return false;
61 }
62
63 // everything is equal -> perform straight comparison
64 int diff;
65 diff = std::memcmp(&AtomIdx[0], &right.AtomIdx[0],
66 AtomIdx.size() * sizeof(unsigned int));
67 if (diff < 0) {
68 return true;
69 }
70 if (diff > 0) {
71 return false;
72 }
73 return std::memcmp(&BondIdx[0], &right.BondIdx[0],
74 BondIdx.size() * sizeof(unsigned int)) < 0;
75 }
76 };
77
78 private:
79 std::map<TKey, TValue> Index;
80 size_t MaxAtoms{0}; // max key in the cache for fast failed find
81 public:
83 void clear() {
84 Index.clear();
85 MaxAtoms = 0;
86 }
87
88 bool find(const TKey &key, TValue &value) const {
89 value = false;
90 if (key.getNumAtoms() > MaxAtoms) {
91 return false; // fast check if key greater then max key in the cache
92 }
93
94 const auto entryit = Index.find(key);
95 if (Index.end() != entryit) {
96 value = entryit->second;
97 }
98 return Index.end() != entryit;
99 }
100
101 void add(const TKey &key, TValue found = true) {
102 if (key.getNumAtoms() > MaxAtoms) {
103 MaxAtoms = key.getNumAtoms();
104 }
105
106 Index.insert(std::pair<TKey, bool>(key, found));
107 }
108
109 size_t size() const {
110 return Index.size(); // for statistics only
111 }
112};
113} // namespace FMCS
114} // namespace RDKit
bool operator==(const TKey &right) const
bool operator<(const TKey &right) const
void add(const TKey &key, TValue found=true)
bool find(const TKey &key, TValue &value) const
Std stuff.