RDKit
Open-source cheminformatics and machine learning.
Dict.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 /*! \file Dict.h
11 
12  \brief Defines the Dict class
13 
14 */
15 #include <RDGeneral/export.h>
16 #ifndef __RD_DICT_H__
17 #define __RD_DICT_H__
18 
19 #include <map>
20 #include <string>
21 #include <vector>
22 #include "RDValue.h"
23 #include "Exceptions.h"
25 #include <boost/lexical_cast.hpp>
27 
28 namespace RDKit {
29 typedef std::vector<std::string> STR_VECT;
30 
31 //! \brief The \c Dict class can be used to store objects of arbitrary
32 //! type keyed by \c strings.
33 //!
34 //! The actual storage is done using \c RDValue objects.
35 //!
37  public:
38  struct Pair {
39  std::string key;
41 
42  Pair() : key(), val() {}
43  explicit Pair(std::string s) : key(std::move(s)), val() {}
44  Pair(std::string s, const RDValue &v) : key(std::move(s)), val(v) {}
45  };
46 
47  typedef std::vector<Pair> DataType;
48 
49  Dict() : _data(), _hasNonPodData(false){};
50 
51  Dict(const Dict &other) : _data(other._data) {
52  _hasNonPodData = other._hasNonPodData;
53  if (other._hasNonPodData) { // other has non pod data, need to copy
54  std::vector<Pair> data(other._data.size());
55  _data.swap(data);
56  for (size_t i = 0; i < _data.size(); ++i) {
57  _data[i].key = other._data[i].key;
58  copy_rdvalue(_data[i].val, other._data[i].val);
59  }
60  }
61  }
62 
63  ~Dict() {
64  reset(); // to clear pointers if necessary
65  }
66 
67  void update(const Dict &other, bool preserveExisting = false) {
68  if (!preserveExisting) {
69  *this = other;
70  } else {
71  if (other._hasNonPodData) _hasNonPodData = true;
72  for (size_t i = 0; i < other._data.size(); ++i) {
73  const Pair &pair = other._data[i];
74  Pair *target = 0;
75  for (size_t i = 0; i < _data.size(); ++i) {
76  if (_data[i].key == pair.key) {
77  target = &_data[i];
78  break;
79  }
80  }
81 
82  if (!target) {
83  // need to create blank entry and copy
84  _data.push_back(Pair(pair.key));
85  copy_rdvalue(_data.back().val, pair.val);
86  } else {
87  // just copy
88  copy_rdvalue(target->val, pair.val);
89  }
90  }
91  }
92  }
93 
94  Dict &operator=(const Dict &other) {
95  if (this == &other) return *this;
96  if (_hasNonPodData) reset();
97 
98  if (other._hasNonPodData) {
99  std::vector<Pair> data(other._data.size());
100  _data.swap(data);
101  for (size_t i = 0; i < _data.size(); ++i) {
102  _data[i].key = other._data[i].key;
103  copy_rdvalue(_data[i].val, other._data[i].val);
104  }
105  } else {
106  _data = other._data;
107  }
108  _hasNonPodData = other._hasNonPodData;
109  return *this;
110  };
111 
112  //----------------------------------------------------------
113  //! \brief Access to the underlying non-POD containment flag
114  //! This is meant to be used only in bulk updates of _data.
115  bool &getNonPODStatus() { return _hasNonPodData; }
116 
117  //----------------------------------------------------------
118  //! \brief Access to the underlying data.
119  const DataType &getData() const { return _data; }
120  DataType &getData() { return _data; }
121 
122  //----------------------------------------------------------
123 
124  //! \brief Returns whether or not the dictionary contains a particular
125  //! key.
126  bool hasVal(const std::string &what) const {
127  for (const auto &data : _data) {
128  if (data.key == what) return true;
129  }
130  return false;
131  };
132 
133  //----------------------------------------------------------
134  //! Returns the set of keys in the dictionary
135  /*!
136  \return a \c STR_VECT
137  */
138  STR_VECT keys() const {
139  STR_VECT res;
140  DataType::const_iterator item;
141  for (item = _data.begin(); item != _data.end(); item++) {
142  res.push_back(item->key);
143  }
144  return res;
145  }
146 
147  //----------------------------------------------------------
148  //! \brief Gets the value associated with a particular key
149  /*!
150  \param what the key to lookup
151  \param res a reference used to return the result
152 
153  <B>Notes:</b>
154  - If \c res is a \c std::string, every effort will be made
155  to convert the specified element to a string using the
156  \c boost::lexical_cast machinery.
157  - If the dictionary does not contain the key \c what,
158  a KeyErrorException will be thrown.
159  */
160  template <typename T>
161  void getVal(const std::string &what, T &res) const {
162  res = getVal<T>(what);
163  };
164  //! \overload
165  template <typename T>
166  T getVal(const std::string &what) const {
167  for (auto &data : _data) {
168  if (data.key == what) {
169  return from_rdvalue<T>(data.val);
170  }
171  }
172  throw KeyErrorException(what);
173  }
174 
175  //! \overload
176  void getVal(const std::string &what, std::string &res) const;
177 
178  //----------------------------------------------------------
179  //! \brief Potentially gets the value associated with a particular key
180  //! returns true on success/false on failure.
181  /*!
182  \param what the key to lookup
183  \param res a reference used to return the result
184 
185  <B>Notes:</b>
186  - If \c res is a \c std::string, every effort will be made
187  to convert the specified element to a string using the
188  \c boost::lexical_cast machinery.
189  - If the dictionary does not contain the key \c what,
190  a KeyErrorException will be thrown.
191  */
192 
193  template <typename T>
194  bool getValIfPresent(const std::string &what, T &res) const {
195  for (const auto &data : _data) {
196  if (data.key == what) {
197  res = from_rdvalue<T>(data.val);
198  return true;
199  }
200  }
201  return false;
202  };
203 
204  //! \overload
205  bool getValIfPresent(const std::string &what, std::string &res) const;
206 
207  //----------------------------------------------------------
208  //! \brief Sets the value associated with a key
209  /*!
210 
211  \param what the key to set
212  \param val the value to store
213 
214  <b>Notes:</b>
215  - If \c val is a <tt>const char *</tt>, it will be converted
216  to a \c std::string for storage.
217  - If the dictionary already contains the key \c what,
218  the value will be replaced.
219  */
220  template <typename T>
221  void setVal(const std::string &what, T &val) {
222  _hasNonPodData = true;
223  for (auto &&data : _data) {
224  if (data.key == what) {
225  RDValue::cleanup_rdvalue(data.val);
226  data.val = val;
227  return;
228  }
229  }
230  _data.push_back(Pair(what, val));
231  };
232 
233  template <typename T>
234  void setPODVal(const std::string &what, T val) {
235  // don't change the hasNonPodData status
236  for (auto &&data : _data) {
237  if (data.key == what) {
238  RDValue::cleanup_rdvalue(data.val);
239  data.val = val;
240  return;
241  }
242  }
243  _data.push_back(Pair(what, val));
244  };
245 
246  void setVal(const std::string &what, bool val) { setPODVal(what, val); }
247 
248  void setVal(const std::string &what, double val) { setPODVal(what, val); }
249 
250  void setVal(const std::string &what, float val) { setPODVal(what, val); }
251 
252  void setVal(const std::string &what, int val) { setPODVal(what, val); }
253 
254  void setVal(const std::string &what, unsigned int val) {
255  setPODVal(what, val);
256  }
257 
258  //! \overload
259  void setVal(const std::string &what, const char *val) {
260  std::string h(val);
261  setVal(what, h);
262  }
263 
264  //----------------------------------------------------------
265  //! \brief Clears the value associated with a particular key,
266  //! removing the key from the dictionary.
267  /*!
268 
269  \param what the key to clear
270 
271  <b>Notes:</b>
272  - If the dictionary does not contain the key \c what,
273  a KeyErrorException will be thrown.
274  */
275  void clearVal(const std::string &what) {
276  for (DataType::iterator it = _data.begin(); it < _data.end(); ++it) {
277  if (it->key == what) {
278  if (_hasNonPodData) {
279  RDValue::cleanup_rdvalue(it->val);
280  }
281  _data.erase(it);
282  return;
283  }
284  }
285  throw KeyErrorException(what);
286  };
287 
288  //----------------------------------------------------------
289  //! \brief Clears all keys (and values) from the dictionary.
290  //!
291  void reset() {
292  if (_hasNonPodData) {
293  for (auto &&data : _data) {
294  RDValue::cleanup_rdvalue(data.val);
295  }
296  }
297  DataType data;
298  _data.swap(data);
299  };
300 
301  //----------------------------------------------------------
302  //! Converts a \c RDAny to type \c T
303  /*!
304  \param arg a \c RDAny reference
305 
306  \returns the converted object of type \c T
307  */
308  /*
309  template <typename T>
310  T fromany(const RDAny &arg) const {
311  return from_rdany<T>(arg);
312  }
313  */
314  //----------------------------------------------------------
315  //! Converts an instance of type \c T to \c RDAny
316  /*!
317  \param arg the object to be converted
318 
319  \returns a \c RDAny instance
320  */
321  /*
322  template <typename T>
323  RDAny toany(T arg) const {
324  return RDAny(arg);
325  };
326  */
327  private:
328  DataType _data; //!< the actual dictionary
329  bool _hasNonPodData; // if true, need a deep copy
330  // (copy_rdvalue)
331 };
332 
333 template <>
334 RDKIT_RDGENERAL_EXPORT std::string Dict::getVal<std::string>(
335  const std::string &what) const;
336 
337 } // namespace RDKit
338 #endif
RDKit::Dict::setVal
void setVal(const std::string &what, bool val)
Definition: Dict.h:246
RDKit::Dict
The Dict class can be used to store objects of arbitrary type keyed by strings.
Definition: Dict.h:36
RDKit::Dict::update
void update(const Dict &other, bool preserveExisting=false)
Definition: Dict.h:67
RDKit::Dict::setVal
void setVal(const std::string &what, unsigned int val)
Definition: Dict.h:254
RDKit::Dict::Pair::key
std::string key
Definition: Dict.h:39
RDKit::Dict::setVal
void setVal(const std::string &what, double val)
Definition: Dict.h:248
RDKit::Dict::DataType
std::vector< Pair > DataType
Definition: Dict.h:47
RDKit::Dict::setVal
void setVal(const std::string &what, const char *val)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: Dict.h:259
BoostStartInclude.h
RDKit::Dict::Dict
Dict(const Dict &other)
Definition: Dict.h:51
RDKit::RDValue
Definition: RDValue-doublemagic.h:167
RDKit::Dict::setVal
void setVal(const std::string &what, float val)
Definition: Dict.h:250
RDKit::Dict::~Dict
~Dict()
Definition: Dict.h:63
KeyErrorException
Class to allow us to throw a KeyError from C++ and have it make it back to Python.
Definition: Exceptions.h:49
RDKit::Dict::getNonPODStatus
bool & getNonPODStatus()
Access to the underlying non-POD containment flag This is meant to be used only in bulk updates of _d...
Definition: Dict.h:115
RDKit::Dict::hasVal
bool hasVal(const std::string &what) const
Returns whether or not the dictionary contains a particular key.
Definition: Dict.h:126
RDKit::STR_VECT
std::vector< std::string > STR_VECT
Definition: Dict.h:29
RDKit::RDValue::cleanup_rdvalue
static void cleanup_rdvalue(RDValue v)
Definition: RDValue-doublemagic.h:331
RDKit::Dict::getValIfPresent
bool getValIfPresent(const std::string &what, T &res) const
Potentially gets the value associated with a particular key returns true on success/false on failure.
Definition: Dict.h:194
BoostEndInclude.h
RDKit::Dict::Pair::val
RDValue val
Definition: Dict.h:40
RDKit::Dict::setVal
void setVal(const std::string &what, T &val)
Sets the value associated with a key.
Definition: Dict.h:221
RDKit::Dict::getVal
void getVal(const std::string &what, T &res) const
Gets the value associated with a particular key.
Definition: Dict.h:161
RDKit::Dict::Pair::Pair
Pair()
Definition: Dict.h:42
RDKit::Dict::clearVal
void clearVal(const std::string &what)
Clears the value associated with a particular key, removing the key from the dictionary.
Definition: Dict.h:275
RDKit::Dict::reset
void reset()
Clears all keys (and values) from the dictionary.
Definition: Dict.h:291
RDKit::Dict::setPODVal
void setPODVal(const std::string &what, T val)
Definition: Dict.h:234
RDKit::Dict::Pair::Pair
Pair(std::string s, const RDValue &v)
Definition: Dict.h:44
RDKit::Dict::keys
STR_VECT keys() const
Returns the set of keys in the dictionary.
Definition: Dict.h:138
RDKit::Dict::Dict
Dict()
Definition: Dict.h:49
RDKit::Dict::operator=
Dict & operator=(const Dict &other)
Definition: Dict.h:94
RDKit::Dict::Pair::Pair
Pair(std::string s)
Definition: Dict.h:43
RDKit
Std stuff.
Definition: Atom.h:30
RDKit::Dict::getVal
T getVal(const std::string &what) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: Dict.h:166
RDValue.h
RDKit::Dict::Pair
Definition: Dict.h:38
RDKIT_RDGENERAL_EXPORT
#define RDKIT_RDGENERAL_EXPORT
Definition: export.h:502
RDKit::Dict::getData
const DataType & getData() const
Access to the underlying data.
Definition: Dict.h:119
RDKit::Dict::getData
DataType & getData()
Definition: Dict.h:120
RDKit::copy_rdvalue
void copy_rdvalue(RDValue &dest, const RDValue &src)
Definition: RDValue-doublemagic.h:339
RDKit::Dict::setVal
void setVal(const std::string &what, int val)
Definition: Dict.h:252
Exceptions.h
export.h