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