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