File: Query.h

package info (click to toggle)
rdkit 202503.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 220,160 kB
  • sloc: cpp: 399,240; python: 77,453; ansic: 25,517; java: 8,173; javascript: 4,005; sql: 2,389; yacc: 1,565; lex: 1,263; cs: 1,081; makefile: 580; xml: 229; fortran: 183; sh: 105
file content (210 lines) | stat: -rw-r--r-- 6,855 bytes parent folder | download | duplicates (3)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//
// Copyright (c) 2003-2020 Greg Landrum and Rational Discovery LLC
//
//   @@ 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>
#ifndef RD_QUERY_H
#define RD_QUERY_H

#ifdef _MSC_VER
#pragma warning(disable : 4800)  // warning: converting things to bool
#endif

#include <vector>
#include <string>
#include <RDGeneral/Invariant.h>

namespace Queries {

//! class to allow integer values to pick templates
template <int v>
class Int2Type {
  enum { value = v };
};

//! Base class for all queries
/*!
  Query objects have one or two functions associated with them:
    - <tt>bool matchFunc(MatchFuncArgType other)</tt> returns true or false
      to indicate whether this query matches \c other.
      This is mandatory.

    - <tt>MatchFuncArgType dataFunc(DataFuncArgType other)</tt> converts
      the argument \c other from \c DataFuncArgType to \c MatchFuncArgType.
      This is optional if \c DataFuncArgType is the same as (or implicitly
      convertible to) \c MatchFuncArgType.

*/
template <class MatchFuncArgType, class DataFuncArgType = MatchFuncArgType,
          bool needsConversion = false>
class RDKIT_QUERY_EXPORT Query {
 public:
  using CHILD_TYPE = std::shared_ptr<
      Query<MatchFuncArgType, DataFuncArgType, needsConversion>>;
  using CHILD_VECT = std::vector<CHILD_TYPE>;
  using CHILD_VECT_I = typename CHILD_VECT::iterator;
  using CHILD_VECT_CI = typename CHILD_VECT::const_iterator;
  using MATCH_FUNC_ARG_TYPE = MatchFuncArgType;
  using DATA_FUNC_ARG_TYPE = DataFuncArgType;

  Query() : d_matchFunc(nullptr), d_dataFunc(nullptr) {}
  virtual ~Query() { this->d_children.clear(); }

  //! sets whether or not we are negated
  void setNegation(bool what) { this->df_negate = what; }
  //! returns whether or not we are negated
  bool getNegation() const { return this->df_negate; }

  //! sets our text description
  void setDescription(const std::string &descr) { this->d_description = descr; }
  //! \overload
  void setDescription(const char *descr) {
    this->d_description = std::string(descr);
  }
  //! returns our text description
  const std::string &getDescription() const { return this->d_description; }
  //! returns a fuller text description
  virtual std::string getFullDescription() const {
    if (!getNegation()) {
      return getDescription();
    } else {
      return "not " + getDescription();
    }
  }

  //! sets our type label
  void setTypeLabel(const std::string &typ) { this->d_queryType = typ; }
  //! \overload
  void setTypeLabel(const char *typ) { this->d_queryType = std::string(typ); }
  //! returns our text label.
  const std::string &getTypeLabel() const { return this->d_queryType; }

  //! sets our match function
  void setMatchFunc(bool (*what)(MatchFuncArgType)) {
    this->d_matchFunc = what;
  }
  //! returns our match function:
  bool (*getMatchFunc() const)(MatchFuncArgType) { return this->d_matchFunc; }
  //! sets our data function
  void setDataFunc(MatchFuncArgType (*what)(DataFuncArgType)) {
    this->d_dataFunc = what;
  }
  //! returns our data function:
  MatchFuncArgType (*getDataFunc() const)(DataFuncArgType) {
    return this->d_dataFunc;
  }

  //! adds a child to our list of children
  void addChild(CHILD_TYPE child) { this->d_children.push_back(child); }
  //! returns an iterator for the beginning of our child vector
  CHILD_VECT_CI beginChildren() const { return this->d_children.begin(); }
  //! returns an iterator for the end of our child vector
  CHILD_VECT_CI endChildren() const { return this->d_children.end(); }

  //! returns whether or not we match the argument
  virtual bool Match(const DataFuncArgType arg) const {
    MatchFuncArgType mfArg = TypeConvert(arg, Int2Type<needsConversion>());
    bool tRes;
    if (this->d_matchFunc) {
      tRes = this->d_matchFunc(mfArg);
    } else {
      tRes = static_cast<bool>(mfArg);
    }

    if (this->getNegation()) {
      return !tRes;
    } else {
      return tRes;
    }
  }

  //! returns a copy of this Query
  /*!
     <b>Notes:</b>
       - the caller is responsible for <tt>delete</tt>ing the result
   */
  virtual Query<MatchFuncArgType, DataFuncArgType, needsConversion> *copy()
      const {
    Query<MatchFuncArgType, DataFuncArgType, needsConversion> *res =
        new Query<MatchFuncArgType, DataFuncArgType, needsConversion>();
    for (auto iter = this->beginChildren(); iter != this->endChildren();
         ++iter) {
      res->addChild(CHILD_TYPE(iter->get()->copy()));
    }
    res->d_val = this->d_val;
    res->d_tol = this->d_tol;
    res->df_negate = this->df_negate;
    res->d_matchFunc = this->d_matchFunc;
    res->d_dataFunc = this->d_dataFunc;
    res->d_description = this->d_description;
    res->d_queryType = this->d_queryType;
    return res;
  }

 protected:
  MatchFuncArgType d_val = 0;
  MatchFuncArgType d_tol = 0;
  std::string d_description = "";
  std::string d_queryType = "";
  CHILD_VECT d_children;
  bool df_negate{false};
  bool (*d_matchFunc)(MatchFuncArgType);

  // MSVC complains at compile time when TypeConvert(MatchFuncArgType what,
  // Int2Type<false>) attempts to pass what (which is of type MatchFuncArgType)
  // as parameter of d_dataFunc() (which should be of type DataFuncArgType). The
  // union is but a trick to avoid silly casts and keep MSVC happy when building
  // DLLs
  union {
    MatchFuncArgType (*d_dataFunc)(DataFuncArgType);
    MatchFuncArgType (*d_dataFuncSameType)(MatchFuncArgType);
  };
  //! \brief calls our \c dataFunc (if it's set) on \c what and returns
  //! the result, otherwise returns \c what
  MatchFuncArgType TypeConvert(MatchFuncArgType what,
                               Int2Type<false> /*d*/) const {
    MatchFuncArgType mfArg;
    if (this->d_dataFuncSameType != nullptr &&
        std::is_same<MatchFuncArgType, DataFuncArgType>::value) {
      mfArg = this->d_dataFuncSameType(what);
    } else {
      mfArg = what;
    }
    return mfArg;
  }
  //! calls our \c dataFunc (which must be set) on \c what and returns the
  /// result
  MatchFuncArgType TypeConvert(DataFuncArgType what,
                               Int2Type<true> /*d*/) const {
    PRECONDITION(this->d_dataFunc, "no data function");
    MatchFuncArgType mfArg;
    mfArg = this->d_dataFunc(what);
    return mfArg;
  }
};

//----------------------------
//
// Used within query functions to compare values
//
//----------------------------
template <class T1, class T2>
int queryCmp(const T1 v1, const T2 v2, const T1 tol) {
  T1 diff = v1 - v2;
  if (diff <= tol) {
    if (diff >= -tol) {
      return 0;
    } else {
      return -1;
    }
  } else {
    return 1;
  }
};
}  // namespace Queries
#endif