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  if (this->df_upperOpen)
72  upperRes = uCmp > 0;
73  else
74  upperRes = uCmp >= 0;
75 
76  bool tempR = !(lowerRes && upperRes);
77  if (this->getNegation())
78  return tempR;
79  else
80  return !tempR;
81  }
82 
84  const override {
87  res->setUpper(this->d_upper);
88  res->setLower(this->d_lower);
89  res->setTol(this->d_tol);
90  res->setNegation(this->getNegation());
91  res->setEndsOpen(this->df_lowerOpen, this->df_upperOpen);
92  res->setDataFunc(this->d_dataFunc);
93  res->d_description = this->d_description;
94  res->d_queryType = this->d_queryType;
95  return res;
96  }
97 
98  std::string getFullDescription() const override {
99  std::ostringstream res;
100  res << this->getDescription();
101  if (this->getNegation()) res << " ! ";
102  res << " " << this->d_lower << " val " << this->d_upper;
103  return res.str();
104  }
105 
106  protected:
107  MatchFuncArgType d_upper, d_lower;
108  bool df_upperOpen{true}, df_lowerOpen{true};
109 };
110 } // namespace Queries
111 #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:150
void setDataFunc(MatchFuncArgType(*what)(DataFuncArgType))
sets our data function
Definition: Query.h:93
void setNegation(bool what)
sets whether or not we are negated
Definition: Query.h:59
std::string d_description
Definition: Query.h:149
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:98
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:83
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:107
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:549
int queryCmp(const T1 v1, const T2 v2, const T1 tol)
Definition: Query.h:194