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 {
62  MatchFuncArgType mfArg =
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 
86  res->setUpper(this->d_upper);
87  res->setLower(this->d_lower);
88  res->setTol(this->d_tol);
89  res->setNegation(this->getNegation());
90  res->setEndsOpen(this->df_lowerOpen, this->df_upperOpen);
91  res->setDataFunc(this->d_dataFunc);
92  res->d_description = this->d_description;
93  res->d_queryType = this->d_queryType;
94  return res;
95  };
96 
97  std::string getFullDescription() const {
98  std::ostringstream res;
99  res << this->getDescription();
100  if (this->getNegation()) res << " ! ";
101  res << " " << this->d_lower << " val " << this->d_upper;
102  return res.str();
103  };
104 
105  protected:
106  MatchFuncArgType d_upper, d_lower;
107  bool df_upperOpen{true}, df_lowerOpen{true};
108 };
109 } // namespace Queries
110 #endif
class to allow integer values to pick templates
Definition: Query.h:26
Base class for all queries.
Definition: Query.h:45
MatchFuncArgType(* d_dataFunc)(MatchFuncArgType)
Definition: Query.h:162
MatchFuncArgType TypeConvert(MatchFuncArgType what, Int2Type< false >) const
calls our dataFunc (if it's set) on what and returns the result, otherwise returns what
Definition: Query.h:167
std::string d_queryType
Definition: Query.h:151
const std::string & getDescription() const
returns our text description
Definition: Query.h:71
void setDataFunc(MatchFuncArgType(*what)(DataFuncArgType))
sets our data function
Definition: Query.h:94
bool getNegation() const
returns whether or not we are negated
Definition: Query.h:60
void setNegation(bool what)
sets whether or not we are negated
Definition: Query.h:58
std::string d_description
Definition: Query.h:150
a Query implementing a range: arguments must fall in a particular range of values.
Definition: RangeQuery.h:28
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
MatchFuncArgType d_upper
Definition: RangeQuery.h:103
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
bool Match(const DataFuncArgType what) const
Definition: RangeQuery.h:61
const MatchFuncArgType getLower() const
returns our lower bound
Definition: RangeQuery.h:44
MatchFuncArgType d_lower
Definition: RangeQuery.h:106
const MatchFuncArgType getUpper() const
returns our upper bound
Definition: RangeQuery.h:40
const MatchFuncArgType getTol() const
returns our tolerance
Definition: RangeQuery.h:59
std::string getFullDescription() const
returns a fuller text description
Definition: RangeQuery.h:97
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
Query< MatchFuncArgType, DataFuncArgType, needsConversion > * copy() const
returns a copy of this Query
Definition: RangeQuery.h:83
int queryCmp(const T1 v1, const T2 v2, const T1 tol)
Definition: Query.h:195