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 #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:
31  : d_upper(0),
32  d_lower(0),
33  d_tol(0),
34  df_upperOpen(true),
35  df_lowerOpen(true) {
36  this->df_negate = false;
37  };
38  //! construct and set the lower and upper bounds
39  RangeQuery(MatchFuncArgType lower, MatchFuncArgType upper)
40  : d_upper(upper),
41  d_lower(lower),
42  d_tol(0),
43  df_upperOpen(true),
44  df_lowerOpen(true) {
45  this->df_negate = false;
46  };
47 
48  //! sets our upper bound
49  void setUpper(MatchFuncArgType what) { this->d_upper = what; };
50  //! returns our upper bound
51  const MatchFuncArgType getUpper() const { return this->d_upper; };
52  //! sets our lower bound
53  void setLower(MatchFuncArgType what) { this->d_lower = what; };
54  //! returns our lower bound
55  const MatchFuncArgType getLower() const { return this->d_lower; };
56 
57  //! sets whether or not the ends of the range are open
58  void setEndsOpen(bool lower, bool upper) {
59  this->df_lowerOpen = lower;
60  this->df_upperOpen = upper;
61  };
62  //! returns the state of our ends (open or not)
63  std::pair<bool, bool> getEndsOpen() const {
64  return std::make_pair(this->df_lowerOpen, this->df_upperOpen);
65  };
66 
67  //! sets our tolerance
68  void setTol(MatchFuncArgType what) { this->d_tol = what; };
69  //! returns our tolerance
70  const MatchFuncArgType getTol() const { return this->d_tol; };
71 
72  bool Match(const DataFuncArgType what) const {
73  MatchFuncArgType mfArg =
75  int lCmp = queryCmp(this->d_lower, mfArg, this->d_tol);
76  int uCmp = queryCmp(this->d_upper, mfArg, this->d_tol);
77  bool lowerRes, upperRes;
78  if (this->df_lowerOpen)
79  lowerRes = lCmp < 0;
80  else
81  lowerRes = lCmp <= 0;
82  if (this->df_upperOpen)
83  upperRes = uCmp > 0;
84  else
85  upperRes = uCmp >= 0;
86 
87  bool tempR = !(lowerRes && upperRes);
88  if (this->getNegation())
89  return tempR;
90  else
91  return !tempR;
92  };
93 
97  res->setUpper(this->d_upper);
98  res->setLower(this->d_lower);
99  res->setTol(this->d_tol);
100  res->setNegation(this->getNegation());
101  res->setEndsOpen(this->df_lowerOpen, this->df_upperOpen);
102  res->setDataFunc(this->d_dataFunc);
103  res->d_description = this->d_description;
104  return res;
105  };
106 
107  std::string getFullDescription() const {
108  std::ostringstream res;
109  res << this->getDescription();
110  if (this->getNegation()) res << " ! ";
111  res << " " << this->d_lower << " val " << this->d_upper;
112  return res.str();
113  };
114 
115  protected:
116  MatchFuncArgType d_upper, d_lower;
117  MatchFuncArgType d_tol;
119 };
120 }
121 #endif
RangeQuery(MatchFuncArgType lower, MatchFuncArgType upper)
construct and set the lower and upper bounds
Definition: RangeQuery.h:39
int queryCmp(const T1 v1, const T2 v2, const T1 tol)
Definition: Query.h:185
const MatchFuncArgType getTol() const
returns our tolerance
Definition: RangeQuery.h:70
void setNegation(bool what)
sets whether or not we are negated
Definition: Query.h:62
void setUpper(MatchFuncArgType what)
sets our upper bound
Definition: RangeQuery.h:49
Query< MatchFuncArgType, DataFuncArgType, needsConversion > * copy() const
returns a copy of this Query
Definition: RangeQuery.h:94
bool getNegation() const
returns whether or not we are negated
Definition: Query.h:64
MatchFuncArgType d_tol
Definition: RangeQuery.h:117
void setTol(MatchFuncArgType what)
sets our tolerance
Definition: RangeQuery.h:68
std::pair< bool, bool > getEndsOpen() const
returns the state of our ends (open or not)
Definition: RangeQuery.h:63
const MatchFuncArgType getLower() const
returns our lower bound
Definition: RangeQuery.h:55
a Query implementing a range: arguments must fall in a particular range of values.
Definition: RangeQuery.h:27
MatchFuncArgType d_lower
Definition: RangeQuery.h:113
class to allow integer values to pick templates
Definition: Query.h:27
void setLower(MatchFuncArgType what)
sets our lower bound
Definition: RangeQuery.h:53
std::string getFullDescription() const
returns a fuller text description
Definition: RangeQuery.h:107
MatchFuncArgType(* d_dataFunc)(DataFuncArgType)
Definition: Query.h:153
void setEndsOpen(bool lower, bool upper)
sets whether or not the ends of the range are open
Definition: RangeQuery.h:58
bool df_negate
Definition: Query.h:145
void setDataFunc(MatchFuncArgType(*what)(DataFuncArgType))
sets our data function
Definition: Query.h:91
const MatchFuncArgType getUpper() const
returns our upper bound
Definition: RangeQuery.h:51
std::string d_description
Definition: Query.h:140
const std::string & getDescription() const
returns our text description
Definition: Query.h:75
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:158
bool Match(const DataFuncArgType what) const
returns whether or not we match the argument
Definition: RangeQuery.h:72
MatchFuncArgType d_upper
Definition: RangeQuery.h:113
Base class for all queries.
Definition: Query.h:46