RDKit: NullQueryAlgebra.h Source File

| RDKit Open-source cheminformatics and machine learning.

Loading...
Searching...
No Matches
NullQueryAlgebra.h
Go to the documentation of this file.
1//
2//
3// Copyright (C) 2019 Greg Landrum and T5 Informatics GmbH
4//
5// @@ All Rights Reserved @@
6// This file is part of the RDKit.
7// The contents are covered by the terms of the BSD license
8// which is included in the file license.txt, found at the root
9// of the RDKit source tree.
10//
11#ifndef RD_NULLQUERYALGEBRA_H
12#define RD_NULLQUERYALGEBRA_H
13
14#include <GraphMol/QueryOps.h>
15
16namespace RDKit {
17namespace {
18template <class T>
19void mergeBothNullQ(T *&returnQuery, T *&otherNullQ,
20 Queries::CompositeQueryType how) {
21 bool negatedQ = returnQuery->getNegation();
22 bool negatedOtherQ = otherNullQ->getNegation();
23
24 if (how == Queries::COMPOSITE_AND) {
25 // This is the only case in which we need to do anything
26 if (!negatedQ && negatedOtherQ) {
27 returnQuery->setNegation(true);
28 }
29 } else if (how == Queries::COMPOSITE_OR) {
30 // This is the only case in which we need to do anything
31 if (negatedQ && !negatedOtherQ) {
32 returnQuery->setNegation(false);
33 }
34 } else if (how == Queries::COMPOSITE_XOR) {
35 if (!negatedQ && !negatedOtherQ) {
36 returnQuery->setNegation(true);
37 } else if (negatedQ + negatedOtherQ == 1) {
38 returnQuery->setNegation(false);
39 }
40 }
41}
42
43template <class T>
44void mergeNullQFirst(T *&returnQuery, T *&otherQ,
45 Queries::CompositeQueryType how) {
46 bool negatedQ = returnQuery->getNegation();
47
48 if (how == Queries::COMPOSITE_AND) {
49 if (!negatedQ) {
50 std::swap(returnQuery, otherQ);
51 }
52 } else if (how == Queries::COMPOSITE_OR) {
53 if (negatedQ) {
54 std::swap(returnQuery, otherQ);
55 }
56 } else if (how == Queries::COMPOSITE_XOR) {
57 std::swap(returnQuery, otherQ);
58 if (!negatedQ) {
59 returnQuery->setNegation(!returnQuery->getNegation());
60 }
61 }
62}
63
64} // namespace
65
66template <class T>
67 void mergeNullQueries(T *&returnQuery, bool isQueryNull, T *&otherQuery,
68 bool isOtherQNull, Queries::CompositeQueryType how) {
69 PRECONDITION(returnQuery, "bad query");
70 PRECONDITION(otherQuery, "bad query");
71 PRECONDITION(how == Queries::COMPOSITE_AND || how == Queries::COMPOSITE_OR ||
72 how == Queries::COMPOSITE_XOR,
73 "bad combination op");
74
75 if (isQueryNull && isOtherQNull) {
76 mergeBothNullQ(returnQuery, otherQuery, how);
77 } else if (isQueryNull) {
78 mergeNullQFirst(returnQuery, otherQuery, how);
79 } else if (isOtherQNull) {
80 std::swap(returnQuery, otherQuery);
81 mergeNullQFirst(returnQuery, otherQuery, how);
82 }
83}
84} // namespace RDKit
85
86#endif
PRECONDITION
#define PRECONDITION(expr, mess)
Definition Invariant.h:108
QueryOps.h
Queries::CompositeQueryType
CompositeQueryType
Definition QueryObjects.h:36
Queries::COMPOSITE_XOR
@ COMPOSITE_XOR
Definition QueryObjects.h:36
Queries::COMPOSITE_OR
@ COMPOSITE_OR
Definition QueryObjects.h:36
Queries::COMPOSITE_AND
@ COMPOSITE_AND
Definition QueryObjects.h:36
RDKit
Std stuff.
Definition Abbreviations.h:19
RDKit::mergeNullQueries
void mergeNullQueries(T *&returnQuery, bool isQueryNull, T *&otherQuery, bool isOtherQNull, Queries::CompositeQueryType how)
Definition NullQueryAlgebra.h:67