SetQuery.h

Go to the documentation of this file.
00001 //
00002 // Copyright (c) 2003-2008 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_SETQUERY_H__
00011 #define __RD_SETQUERY_H__
00012 #include <set>
00013 #include "Query.h"
00014 
00015 namespace Queries{
00016   //! \brief a Query implementing a set: arguments must 
00017   //!  one of a set of values
00018   //!
00019   template <class MatchFuncArgType, class DataFuncArgType=MatchFuncArgType,
00020     bool needsConversion=false>
00021   class SetQuery :
00022     public Query<MatchFuncArgType, DataFuncArgType,needsConversion> {
00023 
00024   public:
00025     typedef std::set<MatchFuncArgType> CONTAINER_TYPE;
00026 
00027     SetQuery() : Query<MatchFuncArgType,DataFuncArgType,needsConversion>() {};
00028 
00029     //! insert an entry into our \c set
00030     void insert(const MatchFuncArgType what){
00031       //std::cout << "SET QUERY INSERT: " << what << std::endl;
00032       if(d_set.find(what) == this->d_set.end()) this->d_set.insert(what);
00033     }
00034 
00035     //! clears our \c set
00036     void clear(){
00037       //std::cout << "SET QUERY CLEAR " << std::endl;
00038       this->d_set.clear();
00039     }
00040 
00041     bool Match(const DataFuncArgType what) const {
00042       MatchFuncArgType mfArg = this->TypeConvert(what,Int2Type<needsConversion>());
00043       //std::cerr << "SET QUERY SEARCH: " << mfArg << ": "  << (d_set.find(mfArg)==d_set.end()) << std::endl;
00044       return ( this->d_set.find(mfArg) != this->d_set.end() ) ^ this->getNegation();
00045     };
00046 
00047     Query<MatchFuncArgType,DataFuncArgType,needsConversion> *
00048     copy( ) const {
00049       SetQuery<MatchFuncArgType,DataFuncArgType,needsConversion> *res =
00050         new SetQuery<MatchFuncArgType,DataFuncArgType,needsConversion>();
00051       res->setDataFunc(this->d_dataFunc);
00052       typename std::set<MatchFuncArgType>::const_iterator i;
00053       for(i=this->d_set.begin();
00054           i!=this->d_set.end();
00055           ++i){
00056         res->insert(*i);
00057       }
00058       res->setNegation(this->getNegation());
00059       res->d_description = this->d_description;
00060       return res;
00061     };
00062 
00063     typename CONTAINER_TYPE::const_iterator beginSet() const {
00064       return d_set.begin();
00065     };
00066     typename CONTAINER_TYPE::const_iterator endSet() const {
00067       return d_set.end();
00068     };
00069     unsigned int size() const {
00070       return d_set.size();
00071     };
00072   
00073   protected:
00074     CONTAINER_TYPE d_set;
00075   };
00076 
00077 }
00078 #endif