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