RDKit: RangeQuery.h Source File

| RDKit Open-source cheminformatics and machine learning.

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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 public:
29 RangeQuery()
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 =
73 this->TypeConvert(what, Int2Type<needsConversion>());
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
93 Query<MatchFuncArgType, DataFuncArgType, needsConversion> *copy() const {
94 RangeQuery<MatchFuncArgType, DataFuncArgType, needsConversion> *res =
95 new RangeQuery<MatchFuncArgType, DataFuncArgType, needsConversion>();
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;
117 bool df_upperOpen, df_lowerOpen;
118 };
119 }
120 #endif
Queries::Query::getNegation
bool getNegation() const
returns whether or not we are negated
Definition: Query.h:63
Queries::RangeQuery::RangeQuery
RangeQuery()
Definition: RangeQuery.h:29
Queries
Definition: AndQuery.h:15
Queries::RangeQuery::Match
bool Match(const DataFuncArgType what) const
returns whether or not we match the argument
Definition: RangeQuery.h:71
Queries::RangeQuery::RangeQuery
RangeQuery(MatchFuncArgType lower, MatchFuncArgType upper)
construct and set the lower and upper bounds
Definition: RangeQuery.h:38
Queries::queryCmp
int queryCmp(const T1 v1, const T2 v2, const T1 tol)
Definition: Query.h:177
Queries::Query::getDescription
const std::string & getDescription() const
returns our text description
Definition: Query.h:74
Queries::Query::setNegation
void setNegation(bool what)
sets whether or not we are negated
Definition: Query.h:61
Queries::RangeQuery::getLower
const MatchFuncArgType getLower() const
returns our lower bound
Definition: RangeQuery.h:54
Queries::RangeQuery::setUpper
void setUpper(MatchFuncArgType what)
sets our upper bound
Definition: RangeQuery.h:48
Queries::Query::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:150
Queries::RangeQuery::d_tol
MatchFuncArgType d_tol
Definition: RangeQuery.h:116
Queries::RangeQuery::getEndsOpen
std::pair< bool, bool > getEndsOpen() const
returns the state of our ends (open or not)
Definition: RangeQuery.h:62
Query.h
Queries::RangeQuery::getFullDescription
std::string getFullDescription() const
returns a fuller text description
Definition: RangeQuery.h:106
Queries::RangeQuery::setTol
void setTol(MatchFuncArgType what)
sets our tolerance
Definition: RangeQuery.h:67
Queries::RangeQuery
a Query implementing a range: arguments must fall in a particular range of values.
Definition: RangeQuery.h:26
Queries::RangeQuery::d_lower
MatchFuncArgType d_lower
Definition: RangeQuery.h:112
Queries::Int2Type
class to allow integer values to pick templates
Definition: Query.h:26
Queries::RangeQuery::getTol
const MatchFuncArgType getTol() const
returns our tolerance
Definition: RangeQuery.h:69
Queries::RangeQuery::setLower
void setLower(MatchFuncArgType what)
sets our lower bound
Definition: RangeQuery.h:52
Queries::Query::d_dataFunc
MatchFuncArgType(* d_dataFunc)(DataFuncArgType)
Definition: Query.h:146
Queries::RangeQuery::df_lowerOpen
bool df_lowerOpen
Definition: RangeQuery.h:117
Queries::RangeQuery::setEndsOpen
void setEndsOpen(bool lower, bool upper)
sets whether or not the ends of the range are open
Definition: RangeQuery.h:57
Queries::Query::df_negate
bool df_negate
Definition: Query.h:144
Queries::Query::setDataFunc
void setDataFunc(MatchFuncArgType(*what)(DataFuncArgType))
sets our data function
Definition: Query.h:90
Queries::Query::d_description
std::string d_description
Definition: Query.h:139
Queries::RangeQuery::copy
Query< MatchFuncArgType, DataFuncArgType, needsConversion > * copy() const
returns a copy of this Query
Definition: RangeQuery.h:93
Queries::RangeQuery::getUpper
const MatchFuncArgType getUpper() const
returns our upper bound
Definition: RangeQuery.h:50
Queries::RangeQuery::d_upper
MatchFuncArgType d_upper
Definition: RangeQuery.h:112
Queries::RangeQuery::df_upperOpen
bool df_upperOpen
Definition: RangeQuery.h:117
Queries::Query
Base class for all queries.
Definition: Query.h:45