File: NullQueryAlgebra.h

package info (click to toggle)
rdkit 202209.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 203,880 kB
  • sloc: cpp: 334,239; python: 80,247; ansic: 24,579; java: 7,667; sql: 2,123; yacc: 1,884; javascript: 1,358; lex: 1,260; makefile: 576; xml: 229; fortran: 183; cs: 181; sh: 101
file content (86 lines) | stat: -rw-r--r-- 2,556 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//
//
//  Copyright (C) 2019 Greg Landrum and T5 Informatics GmbH
//
//   @@ 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.
//
#ifndef RD_NULLQUERYALGEBRA_H
#define RD_NULLQUERYALGEBRA_H

#include <GraphMol/QueryOps.h>

namespace RDKit {
namespace {
template <class T>
void mergeBothNullQ(T *&returnQuery, T *&otherNullQ,
                    Queries::CompositeQueryType how) {
  bool negatedQ = returnQuery->getNegation();
  bool negatedOtherQ = otherNullQ->getNegation();

  if (how == Queries::COMPOSITE_AND) {
    // This is the only case in which we need to do anything
    if (!negatedQ && negatedOtherQ) {
      returnQuery->setNegation(true);
    }
  } else if (how == Queries::COMPOSITE_OR) {
    // This is the only case in which we need to do anything
    if (negatedQ && !negatedOtherQ) {
      returnQuery->setNegation(false);
    }
  } else if (how == Queries::COMPOSITE_XOR) {
    if (!negatedQ && !negatedOtherQ) {
      returnQuery->setNegation(true);
    } else if (negatedQ + negatedOtherQ == 1) {
      returnQuery->setNegation(false);
    }
  }
}

template <class T>
void mergeNullQFirst(T *&returnQuery, T *&otherQ,
                     Queries::CompositeQueryType how) {
  bool negatedQ = returnQuery->getNegation();

  if (how == Queries::COMPOSITE_AND) {
    if (!negatedQ) {
      std::swap(returnQuery, otherQ);
    }
  } else if (how == Queries::COMPOSITE_OR) {
    if (negatedQ) {
      std::swap(returnQuery, otherQ);
    }
  } else if (how == Queries::COMPOSITE_XOR) {
    std::swap(returnQuery, otherQ);
    if (!negatedQ) {
      returnQuery->setNegation(!returnQuery->getNegation());
    }
  }
}

}  // namespace

template <class T>
void mergeNullQueries(T *&returnQuery, bool isQueryNull, T *&otherQuery,
                      bool isOtherQNull, Queries::CompositeQueryType how) {
  PRECONDITION(returnQuery, "bad query");
  PRECONDITION(otherQuery, "bad query");
  PRECONDITION(how == Queries::COMPOSITE_AND || how == Queries::COMPOSITE_OR ||
                   how == Queries::COMPOSITE_XOR,
               "bad combination op");

  if (isQueryNull && isOtherQNull) {
    mergeBothNullQ(returnQuery, otherQuery, how);
  } else if (isQueryNull) {
    mergeNullQFirst(returnQuery, otherQuery, how);
  } else if (isOtherQNull) {
    std::swap(returnQuery, otherQuery);
    mergeNullQFirst(returnQuery, otherQuery, how);
  }
}
}  // namespace RDKit

#endif