 |
RDKit
Open-source cheminformatics and machine learning.
|
Go to the documentation of this file.
16 #ifndef RD_DICT_H_012020
17 #define RD_DICT_H_012020
25 #include <boost/lexical_cast.hpp>
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) {}
49 Dict() : _data(), _hasNonPodData(false) {}
52 _hasNonPodData = other._hasNonPodData;
53 if (other._hasNonPodData) {
54 std::vector<Pair> data(other._data.size());
56 for (
size_t i = 0; i < _data.size(); ++i) {
57 _data[i].key = other._data[i].key;
67 void update(
const Dict &other,
bool preserveExisting =
false) {
68 if (!preserveExisting) {
71 if (other._hasNonPodData) _hasNonPodData =
true;
72 for (
size_t i = 0; i < other._data.size(); ++i) {
73 const Pair &pair = other._data[i];
75 for (
size_t i = 0; i < _data.size(); ++i) {
76 if (_data[i].key == pair.
key) {
84 _data.push_back(
Pair(pair.
key));
95 if (
this == &other)
return *
this;
96 if (_hasNonPodData) reset();
98 if (other._hasNonPodData) {
99 std::vector<Pair> data(other._data.size());
101 for (
size_t i = 0; i < _data.size(); ++i) {
102 _data[i].key = other._data[i].key;
108 _hasNonPodData = other._hasNonPodData;
126 bool hasVal(
const std::string &what)
const {
127 for (
const auto &data : _data) {
128 if (data.key == what)
return true;
140 res.reserve(_data.size());
141 for (
const auto &item : _data) {
142 res.push_back(item.key);
160 template <
typename T>
161 void getVal(
const std::string &what, T &res)
const {
162 res = getVal<T>(what);
166 template <
typename T>
167 T
getVal(
const std::string &what)
const {
168 for (
auto &data : _data) {
169 if (data.key == what) {
170 return from_rdvalue<T>(data.val);
177 void getVal(
const std::string &what, std::string &res)
const {
178 for (
const auto &i : _data) {
201 template <
typename T>
203 for (
const auto &data : _data) {
204 if (data.key == what) {
205 res = from_rdvalue<T>(data.val);
214 for (
const auto &i : _data) {
236 template <
typename T>
237 void setVal(
const std::string &what, T &val) {
238 _hasNonPodData =
true;
239 for (
auto &&data : _data) {
240 if (data.key == what) {
246 _data.push_back(
Pair(what, val));
249 template <
typename T>
252 for (
auto &&data : _data) {
253 if (data.key == what) {
259 _data.push_back(
Pair(what, val));
262 void setVal(
const std::string &what,
bool val) { setPODVal(what, val); }
264 void setVal(
const std::string &what,
double val) { setPODVal(what, val); }
266 void setVal(
const std::string &what,
float val) { setPODVal(what, val); }
268 void setVal(
const std::string &what,
int val) { setPODVal(what, val); }
270 void setVal(
const std::string &what,
unsigned int val) {
271 setPODVal(what, val);
275 void setVal(
const std::string &what,
const char *val) {
289 for (DataType::iterator it = _data.begin(); it < _data.end(); ++it) {
290 if (it->key == what) {
291 if (_hasNonPodData) {
304 if (_hasNonPodData) {
305 for (
auto &&data : _data) {
320 inline std::string Dict::getVal<std::string>(
const std::string &what)
const {
void setVal(const std::string &what, bool val)
The Dict class can be used to store objects of arbitrary type keyed by strings.
void update(const Dict &other, bool preserveExisting=false)
void setVal(const std::string &what, unsigned int val)
void setVal(const std::string &what, double val)
std::vector< Pair > DataType
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...
void setVal(const std::string &what, float val)
Class to allow us to throw a KeyError from C++ and have it make it back to Python.
bool & getNonPODStatus()
Access to the underlying non-POD containment flag This is meant to be used only in bulk updates of _d...
bool hasVal(const std::string &what) const
Returns whether or not the dictionary contains a particular key.
std::vector< std::string > STR_VECT
static void cleanup_rdvalue(RDValue v)
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.
void getVal(const std::string &what, std::string &res) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
void setVal(const std::string &what, T &val)
Sets the value associated with a key.
void getVal(const std::string &what, T &res) const
Gets the value associated with a particular key.
void clearVal(const std::string &what)
Clears the value associated with a particular key, removing the key from the dictionary.
void reset()
Clears all keys (and values) from the dictionary.
void setPODVal(const std::string &what, T val)
Pair(std::string s, const RDValue &v)
STR_VECT keys() const
Returns the set of keys in the dictionary.
Dict & operator=(const Dict &other)
bool rdvalue_tostring(RDValue_cast_t val, std::string &res)
T getVal(const std::string &what) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
bool getValIfPresent(const std::string &what, std::string &res) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
#define RDKIT_RDGENERAL_EXPORT
const DataType & getData() const
Access to the underlying data.
void copy_rdvalue(RDValue &dest, const RDValue &src)
void setVal(const std::string &what, int val)