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