RDKit
Open-source cheminformatics and machine learning.
SetQuery.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2003-2008 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_SETQUERY_H__
11 #define __RD_SETQUERY_H__
12 #include <set>
13 #include "Query.h"
14 #include <sstream>
15 #include <algorithm>
16 #include <iterator>
17 
18 namespace Queries{
19  //! \brief a Query implementing a set: arguments must
20  //! one of a set of values
21  //!
22  template <class MatchFuncArgType, class DataFuncArgType=MatchFuncArgType,
23  bool needsConversion=false>
24  class SetQuery :
25  public Query<MatchFuncArgType, DataFuncArgType,needsConversion> {
26 
27  public:
28  typedef std::set<MatchFuncArgType> CONTAINER_TYPE;
29 
30  SetQuery() : Query<MatchFuncArgType,DataFuncArgType,needsConversion>() {};
31 
32  //! insert an entry into our \c set
33  void insert(const MatchFuncArgType what){
34  if(d_set.find(what) == this->d_set.end()) this->d_set.insert(what);
35  }
36 
37  //! clears our \c set
38  void clear(){
39  this->d_set.clear();
40  }
41 
42  bool Match(const DataFuncArgType what) const {
43  MatchFuncArgType mfArg = this->TypeConvert(what,Int2Type<needsConversion>());
44  return ( this->d_set.find(mfArg) != this->d_set.end() ) ^ this->getNegation();
45  };
46 
48  copy( ) const {
51  res->setDataFunc(this->d_dataFunc);
52  typename std::set<MatchFuncArgType>::const_iterator i;
53  for(i=this->d_set.begin();
54  i!=this->d_set.end();
55  ++i){
56  res->insert(*i);
57  }
58  res->setNegation(this->getNegation());
59  res->d_description = this->d_description;
60  return res;
61  };
62 
63  typename CONTAINER_TYPE::const_iterator beginSet() const {
64  return d_set.begin();
65  };
66  typename CONTAINER_TYPE::const_iterator endSet() const {
67  return d_set.end();
68  };
69  unsigned int size() const {
70  return d_set.size();
71  };
72 
73  std::string getFullDescription() const {
74  std::ostringstream res;
75  res<<this->getDescription()<<" val";
76  if(this->getNegation()) res<<" not in ";
77  else res<<" in (";
78  std::copy(d_set.begin(),d_set.end(),std::ostream_iterator<MatchFuncArgType>(res,", "));
79  res<<")";
80  return res.str();
81  }
82 
83 
84  protected:
85  CONTAINER_TYPE d_set;
86  };
87 
88 }
89 #endif
bool getNegation() const
returns whether or not we are negated
Definition: Query.h:60
void clear()
clears our set
Definition: SetQuery.h:38
std::string getFullDescription() const
returns a fuller text description
Definition: SetQuery.h:73
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
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
CONTAINER_TYPE d_set
Definition: SetQuery.h:85
bool Match(const DataFuncArgType what) const
returns whether or not we match the argument
Definition: SetQuery.h:42
CONTAINER_TYPE::const_iterator beginSet() const
Definition: SetQuery.h:63
Query< MatchFuncArgType, DataFuncArgType, needsConversion > * copy() const
returns a copy of this Query
Definition: SetQuery.h:48
class to allow integer values to pick templates
Definition: Query.h:26
MatchFuncArgType(* d_dataFunc)(DataFuncArgType)
Definition: Query.h:130
void insert(const MatchFuncArgType what)
insert an entry into our set
Definition: SetQuery.h:33
void setDataFunc(MatchFuncArgType(*what)(DataFuncArgType))
sets our data function
Definition: Query.h:81
CONTAINER_TYPE::const_iterator endSet() const
Definition: SetQuery.h:66
std::set< MatchFuncArgType > CONTAINER_TYPE
Definition: SetQuery.h:28
std::string d_description
Definition: Query.h:123
unsigned int size() const
Definition: SetQuery.h:69
Base class for all queries.
Definition: Query.h:46
a Query implementing a set: arguments must one of a set of values
Definition: SetQuery.h:24