RDCode: RangeQuery.h Source File

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