RDKit
Open-source cheminformatics and machine learning.
RangeQuery.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2003-2020 Greg Landrum and Rational Discovery LLC
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 #ifndef RD_RANGEQUERY_H
12 #define RD_RANGEQUERY_H
13 #include "Query.h"
14 #include <utility>
15 
16 namespace Queries {
17 
18 //! \brief a Query implementing a range: arguments must
19 //! fall in a particular range of values.
20 //!
21 //! The ends of the range default to be open, but they can
22 //! individually set to be closed.
23 //!
24 //! There is also an optional tolerance to be used in comparisons
25 template <class MatchFuncArgType, class DataFuncArgType = MatchFuncArgType,
26  bool needsConversion = false>
28  : public Query<MatchFuncArgType, DataFuncArgType, needsConversion> {
29  public:
30  RangeQuery() : d_upper(0), d_lower(0) { this->df_negate = false; }
31  //! construct and set the lower and upper bounds
32  RangeQuery(MatchFuncArgType lower, MatchFuncArgType upper)
33  : d_upper(upper), d_lower(lower), df_upperOpen(true), df_lowerOpen(true) {
34  this->df_negate = false;
35  }
36 
37  //! sets our upper bound
38  void setUpper(MatchFuncArgType what) { this->d_upper = what; }
39  //! returns our upper bound
40  const MatchFuncArgType getUpper() const { return this->d_upper; }
41  //! sets our lower bound
42  void setLower(MatchFuncArgType what) { this->d_lower = what; }
43  //! returns our lower bound
44  const MatchFuncArgType getLower() const { return this->d_lower; }
45 
46  //! sets whether or not the ends of the range are open
47  void setEndsOpen(bool lower, bool upper) {
48  this->df_lowerOpen = lower;
49  this->df_upperOpen = upper;
50  }
51  //! returns the state of our ends (open or not)
52  std::pair<bool, bool> getEndsOpen() const {
53  return std::make_pair(this->df_lowerOpen, this->df_upperOpen);
54  }
55 
56  //! sets our tolerance
57  void setTol(MatchFuncArgType what) { this->d_tol = what; }
58  //! returns our tolerance
59  const MatchFuncArgType getTol() const { return this->d_tol; }
60 
61  bool Match(const DataFuncArgType what) const override {
62  MatchFuncArgType mfArg =
63  this->TypeConvert(what, Int2Type<needsConversion>());
64  int lCmp = queryCmp(this->d_lower, mfArg, this->d_tol);
65  int uCmp = queryCmp(this->d_upper, mfArg, this->d_tol);
66  bool lowerRes, upperRes;
67  if (this->df_lowerOpen) {
68  lowerRes = lCmp < 0;
69  } else {
70  lowerRes = lCmp <= 0;
71  }
72  if (this->df_upperOpen) {
73  upperRes = uCmp > 0;
74  } else {
75  upperRes = uCmp >= 0;
76  }
77 
78  bool tempR = !(lowerRes && upperRes);
79  if (this->getNegation()) {
80  return tempR;
81  } else {
82  return !tempR;
83  }
84  }
85 
87  const override {
90  res->setUpper(this->d_upper);
91  res->setLower(this->d_lower);
92  res->setTol(this->d_tol);
93  res->setNegation(this->getNegation());
94  res->setEndsOpen(this->df_lowerOpen, this->df_upperOpen);
95  res->setDataFunc(this->d_dataFunc);
96  res->d_description = this->d_description;
97  res->d_queryType = this->d_queryType;
98  return res;
99  }
100 
101  std::string getFullDescription() const override {
102  std::ostringstream res;
103  res << this->getDescription();
104  if (this->getNegation()) {
105  res << " ! ";
106  }
107  res << " " << this->d_lower << " val " << this->d_upper;
108  return res.str();
109  }
110 
111  protected:
112  MatchFuncArgType d_upper, d_lower;
113  bool df_upperOpen{true}, df_lowerOpen{true};
114 };
115 } // namespace Queries
116 #endif
class to allow integer values to pick templates
Definition: Query.h:26
Base class for all queries.
Definition: Query.h:45
std::string d_queryType
Definition: Query.h:153
void setDataFunc(MatchFuncArgType(*what)(DataFuncArgType))
sets our data function
Definition: Query.h:94
void setNegation(bool what)
sets whether or not we are negated
Definition: Query.h:59
std::string d_description
Definition: Query.h:152
a Query implementing a range: arguments must fall in a particular range of values.
Definition: RangeQuery.h:28
std::string getFullDescription() const override
returns a fuller text description
Definition: RangeQuery.h:101
RangeQuery(MatchFuncArgType lower, MatchFuncArgType upper)
construct and set the lower and upper bounds
Definition: RangeQuery.h:32
void setTol(MatchFuncArgType what)
sets our tolerance
Definition: RangeQuery.h:57
void setUpper(MatchFuncArgType what)
sets our upper bound
Definition: RangeQuery.h:38
std::pair< bool, bool > getEndsOpen() const
returns the state of our ends (open or not)
Definition: RangeQuery.h:52
Query< MatchFuncArgType, DataFuncArgType, needsConversion > * copy() const override
returns a copy of this Query
Definition: RangeQuery.h:86
bool Match(const DataFuncArgType what) const override
Definition: RangeQuery.h:61
const MatchFuncArgType getLower() const
returns our lower bound
Definition: RangeQuery.h:44
MatchFuncArgType d_lower
Definition: RangeQuery.h:112
const MatchFuncArgType getUpper() const
returns our upper bound
Definition: RangeQuery.h:40
const MatchFuncArgType getTol() const
returns our tolerance
Definition: RangeQuery.h:59
void setEndsOpen(bool lower, bool upper)
sets whether or not the ends of the range are open
Definition: RangeQuery.h:47
void setLower(MatchFuncArgType what)
sets our lower bound
Definition: RangeQuery.h:42
#define RDKIT_QUERY_EXPORT
Definition: export.h:557
int queryCmp(const T1 v1, const T2 v2, const T1 tol)
Definition: Query.h:197