Loading...
Searching...
No Matches
DuplicatedSeedCache.h
Go to the documentation of this file.
//
// Copyright (C) 2014 Novartis Institutes for BioMedical Research
//
// @@ All Rights Reserved @@
// This file is part of the RDKit.
// The contents are covered by the terms of the BSD license
// which is included in the file license.txt, found at the root
// of the RDKit source tree.
//
#include <RDGeneral/export.h>
#pragma once
#include <map>
#include <vector>
#include <stdexcept>
#include <algorithm>
namespace RDKit {
namespace FMCS {
class DuplicatedSeedCache {
public:
typedef bool TValue;
class TKey {
std::vector<unsigned int> AtomIdx; // sorted
std::vector<unsigned int> BondIdx; // sorted
public:
size_t getNumAtoms() const { return AtomIdx.size(); }
size_t getNumBonds() const { return BondIdx.size(); }
void addAtom(unsigned int i) {
auto it = std::lower_bound(AtomIdx.begin(), AtomIdx.end(), i);
AtomIdx.insert(it, i);
}
void addBond(unsigned int i) {
auto it = std::lower_bound(BondIdx.begin(), BondIdx.end(), i);
BondIdx.insert(it, i);
}
bool operator==(const TKey &right) const { // opt.
return AtomIdx.size() == right.AtomIdx.size() &&
BondIdx.size() == right.BondIdx.size() &&
0 == std::memcmp(&AtomIdx[0], &right.AtomIdx[0],
AtomIdx.size() * sizeof(unsigned int)) &&
0 == std::memcmp(&BondIdx[0], &right.BondIdx[0],
BondIdx.size() * sizeof(unsigned int));
}
bool operator<(const TKey &right) const {
if (AtomIdx.size() < right.AtomIdx.size()) {
return true;
}
if (AtomIdx.size() > right.AtomIdx.size()) {
return false;
}
if (BondIdx.size() < right.BondIdx.size()) {
return true;
}
if (BondIdx.size() > right.BondIdx.size()) {
return false;
}
// everything is equal -> perform straight comparison
int diff;
diff = std::memcmp(&AtomIdx[0], &right.AtomIdx[0],
AtomIdx.size() * sizeof(unsigned int));
if (diff < 0) {
return true;
}
if (diff > 0) {
return false;
}
return std::memcmp(&BondIdx[0], &right.BondIdx[0],
BondIdx.size() * sizeof(unsigned int)) < 0;
}
};
private:
std::map<TKey, TValue> Index;
size_t MaxAtoms{0}; // max key in the cache for fast failed find
public:
DuplicatedSeedCache() {}
void clear() {
Index.clear();
MaxAtoms = 0;
}
bool find(const TKey &key, TValue &value) const {
value = false;
if (key.getNumAtoms() > MaxAtoms) {
return false; // fast check if key greater then max key in the cache
}
const auto entryit = Index.find(key);
if (Index.end() != entryit) {
value = entryit->second;
}
return Index.end() != entryit;
}
void add(const TKey &key, TValue found = true) {
if (key.getNumAtoms() > MaxAtoms) {
MaxAtoms = key.getNumAtoms();
}
Index.insert(std::pair<TKey, bool>(key, found));
}
size_t size() const {
return Index.size(); // for statistics only
}
};
} // namespace FMCS
} // namespace RDKit
RDKit::FMCS::DuplicatedSeedCache::TKey
Definition DuplicatedSeedCache.h:22
RDKit::FMCS::DuplicatedSeedCache::TKey::getNumAtoms
size_t getNumAtoms() const
Definition DuplicatedSeedCache.h:26
RDKit::FMCS::DuplicatedSeedCache::TKey::addAtom
void addAtom(unsigned int i)
Definition DuplicatedSeedCache.h:29
RDKit::FMCS::DuplicatedSeedCache::TKey::addBond
void addBond(unsigned int i)
Definition DuplicatedSeedCache.h:33
RDKit::FMCS::DuplicatedSeedCache::TKey::operator==
bool operator==(const TKey &right) const
Definition DuplicatedSeedCache.h:38
RDKit::FMCS::DuplicatedSeedCache::TKey::operator<
bool operator<(const TKey &right) const
Definition DuplicatedSeedCache.h:47
RDKit::FMCS::DuplicatedSeedCache::TKey::getNumBonds
size_t getNumBonds() const
Definition DuplicatedSeedCache.h:27
RDKit::FMCS::DuplicatedSeedCache::TValue
bool TValue
Definition DuplicatedSeedCache.h:21
RDKit::FMCS::DuplicatedSeedCache::add
void add(const TKey &key, TValue found=true)
Definition DuplicatedSeedCache.h:100
RDKit::FMCS::DuplicatedSeedCache::size
size_t size() const
Definition DuplicatedSeedCache.h:108
RDKit::FMCS::DuplicatedSeedCache::DuplicatedSeedCache
DuplicatedSeedCache()
Definition DuplicatedSeedCache.h:81
RDKit::FMCS::DuplicatedSeedCache::clear
void clear()
Definition DuplicatedSeedCache.h:82
RDKit::FMCS::DuplicatedSeedCache::find
bool find(const TKey &key, TValue &value) const
Definition DuplicatedSeedCache.h:87
export.h
RDKit::FMCS
Definition Composition2N.h:13
RDKit
Std stuff.
Definition Abbreviations.h:19