RangeQuery.h

Go to the documentation of this file.
00001 //
00002 // Copyright (c) 2003-2006 Greg Landrum and Rational Discovery LLC
00003 //
00004 //   @@ All Rights Reserved @@
00005 //  This file is part of the RDKit.
00006 //  The contents are covered by the terms of the BSD license
00007 //  which is included in the file license.txt, found at the root
00008 //  of the RDKit source tree.
00009 //
00010 #ifndef __RD_RANGEQUERY_H__
00011 #define __RD_RANGEQUERY_H__
00012 #include "Query.h"
00013 #include <utility>
00014 
00015 namespace Queries {
00016 
00017   //! \brief a Query implementing a range: arguments must 
00018   //!  fall in a particular range of values.
00019   //!
00020   //!  The ends of the range default to be open, but they can
00021   //!  individually set to be closed.  
00022   //!
00023   //!  There is also an optional tolerance to be used in comparisons
00024   template <class MatchFuncArgType, class DataFuncArgType=MatchFuncArgType,
00025     bool needsConversion=false>
00026   class RangeQuery :
00027     public Query<MatchFuncArgType, DataFuncArgType,needsConversion> {
00028 
00029   public:
00030     RangeQuery() : d_upper(0), d_lower(0), d_tol(0), df_upperOpen(true), df_lowerOpen(true){
00031       this->df_negate = false;
00032     };
00033     //! construct and set the lower and upper bounds
00034     RangeQuery(MatchFuncArgType lower,MatchFuncArgType upper) : d_upper(upper), d_lower(lower), d_tol(0), df_upperOpen(true), df_lowerOpen(true){
00035       this->df_negate = false;
00036     };
00037 
00038     //! sets our upper bound
00039     void setUpper (MatchFuncArgType what) { this->d_upper = what; };
00040     //! returns our upper bound
00041     const MatchFuncArgType getUpper() const { return this->d_upper; };
00042     //! sets our lower bound
00043     void setLower (MatchFuncArgType what) { this->d_lower = what; };
00044     //! returns our lower bound
00045     const MatchFuncArgType getLower() const { return this->d_lower; };
00046 
00047     //! sets whether or not the ends of the range are open
00048     void setEndsOpen(bool lower, bool upper) {
00049       this->df_lowerOpen = lower;
00050       this->df_upperOpen = upper;
00051     };
00052     //! returns the state of our ends (open or not)
00053     std::pair<bool,bool> getEndsOpen() const {
00054       return std::make_pair(this->df_lowerOpen,this->df_upperOpen);
00055     };
00056   
00057     //! sets our tolerance
00058     void setTol(MatchFuncArgType what) { this->d_tol = what; };
00059     //! returns our tolerance
00060     const MatchFuncArgType getTol() const { return this->d_tol; };
00061   
00062     bool Match(const DataFuncArgType what) const {
00063       MatchFuncArgType mfArg = this->TypeConvert(what,Int2Type<needsConversion>());
00064       int lCmp = queryCmp(this->d_lower,mfArg,this->d_tol);
00065       int uCmp = queryCmp(this->d_upper,mfArg,this->d_tol);
00066       bool lowerRes,upperRes;
00067       if( this->df_lowerOpen ) lowerRes = lCmp < 0;
00068       else lowerRes = lCmp <= 0;
00069       if( this->df_upperOpen ) upperRes = uCmp > 0;
00070       else upperRes = uCmp >= 0;
00071 
00072       bool tempR = !(lowerRes && upperRes);
00073       if( this->getNegation() ) return tempR;
00074       else return !tempR;
00075     };
00076 
00077     Query<MatchFuncArgType,DataFuncArgType,needsConversion> *
00078     copy( ) const {
00079       RangeQuery<MatchFuncArgType,DataFuncArgType,needsConversion> *res =
00080         new RangeQuery<MatchFuncArgType,DataFuncArgType,needsConversion>();
00081       res->setUpper(this->d_upper);
00082       res->setLower(this->d_lower);
00083       res->setTol(this->d_tol);
00084       res->setNegation(this->getNegation());
00085       res->setEndsOpen(this->df_lowerOpen,this->df_upperOpen);
00086       res->setDataFunc(this->d_dataFunc);
00087       res->d_description = this->d_description;
00088       return res;
00089     };
00090 
00091   protected:
00092     MatchFuncArgType d_upper,d_lower;
00093     MatchFuncArgType d_tol;
00094     bool df_upperOpen,df_lowerOpen;
00095   };
00096 
00097 }
00098 #endif