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