propagate string_view (#8858)

This commit is contained in:
Ricardo Rodriguez
2025-10-14 19:15:37 +02:00
committed by GitHub
parent 5ea3994bd0
commit af4e8cf01f
13 changed files with 72 additions and 78 deletions

View File

@@ -40,7 +40,8 @@
namespace RDKit {
bool preprocessReaction(ChemicalReaction &rxn, const std::string &propName) {
bool preprocessReaction(ChemicalReaction &rxn,
const std::string_view &propName) {
const bool normalized = true;
return preprocessReaction(
rxn, GetFlattenedFunctionalGroupHierarchy(normalized), propName);
@@ -50,7 +51,7 @@ bool preprocessReaction(
ChemicalReaction &rxn, unsigned int &numWarnings, unsigned int &numErrors,
std::vector<std::vector<std::pair<unsigned int, std::string>>>
&reactantLabels,
const std::string &propName) {
const std::string_view &propName) {
const bool normalized = true;
return preprocessReaction(rxn, numWarnings, numErrors, reactantLabels,
GetFlattenedFunctionalGroupHierarchy(normalized),
@@ -59,7 +60,7 @@ bool preprocessReaction(
bool preprocessReaction(ChemicalReaction &rxn,
const std::map<std::string, ROMOL_SPTR> &queries,
const std::string &propName) {
const std::string_view &propName) {
unsigned int numWarnings, numErrors;
std::vector<std::vector<std::pair<unsigned int, std::string>>> reactantLabels;
@@ -72,7 +73,7 @@ bool preprocessReaction(
std::vector<std::vector<std::pair<unsigned int, std::string>>>
&reactantLabels,
const std::map<std::string, ROMOL_SPTR> &queries,
const std::string &propName) {
const std::string_view &propName) {
rxn.setImplicitPropertiesFlag(true);
rxn.initReactantMatchers();

View File

@@ -35,30 +35,31 @@
#include "Reaction.h"
#include <string>
#include <string_view>
#include <exception>
namespace RDKit {
RDKIT_CHEMREACTIONS_EXPORT bool preprocessReaction(
ChemicalReaction &rxn,
const std::string &propName = common_properties::molFileValue);
const std::string_view &propName = common_properties::molFileValue);
RDKIT_CHEMREACTIONS_EXPORT bool preprocessReaction(
ChemicalReaction &rxn, unsigned int &numWarnings, unsigned int &numErrors,
std::vector<std::vector<std::pair<unsigned int, std::string>>>
&reactantLabels,
const std::string &propName = common_properties::molFileValue);
const std::string_view &propName = common_properties::molFileValue);
RDKIT_CHEMREACTIONS_EXPORT bool preprocessReaction(
ChemicalReaction &rxn, const std::map<std::string, ROMOL_SPTR> &queries,
const std::string &propName = common_properties::molFileValue);
const std::string_view &propName = common_properties::molFileValue);
RDKIT_CHEMREACTIONS_EXPORT bool preprocessReaction(
ChemicalReaction &rxn, unsigned int &numWarnings, unsigned int &numErrors,
std::vector<std::vector<std::pair<unsigned int, std::string>>>
&reactantLabels,
const std::map<std::string, ROMOL_SPTR> &queries,
const std::string &propName = common_properties::molFileValue);
const std::string_view &propName = common_properties::molFileValue);
} // namespace RDKit
#endif

View File

@@ -448,7 +448,7 @@ bool isMoleculeAgentOfReaction(const ChemicalReaction &rxn, const ROMol &mol) {
void addRecursiveQueriesToReaction(
ChemicalReaction &rxn, const std::map<std::string, ROMOL_SPTR> &queries,
const std::string &propName,
const std::string_view &propName,
std::vector<std::vector<std::pair<unsigned int, std::string>>>
*reactantLabels) {
if (!rxn.isInitialized()) {

View File

@@ -39,6 +39,7 @@
#include <GraphMol/RDKitBase.h>
#include <RDGeneral/RDProps.h>
#include <GraphMol/Substruct/SubstructMatch.h>
#include <string_view>
#include <vector>
namespace RDKit {
@@ -468,7 +469,7 @@ getReactingAtoms(const ChemicalReaction &rxn, bool mappedAtomsOnly = false);
*/
RDKIT_CHEMREACTIONS_EXPORT void addRecursiveQueriesToReaction(
ChemicalReaction &rxn, const std::map<std::string, ROMOL_SPTR> &queries,
const std::string &propName,
const std::string_view &propName,
std::vector<std::vector<std::pair<unsigned int, std::string>>>
*reactantLabels = nullptr);

View File

@@ -40,7 +40,7 @@ namespace RxnOps {
// molFileRLabel ==> unsigned int
namespace {
template <class T>
T getMaxProp(ChemicalReaction &rxn, const std::string &prop) {
T getMaxProp(ChemicalReaction &rxn, const std::string_view &prop) {
T max_atom = (T)0;
for (auto it = rxn.beginReactantTemplates(); it != rxn.endReactantTemplates();
++it) {

View File

@@ -891,7 +891,7 @@ ROMol *combineMols(const ROMol &mol1, const ROMol &mol2,
void addRecursiveQueries(
ROMol &mol, const std::map<std::string, ROMOL_SPTR> &queries,
const std::string &propName,
const std::string_view &propName,
std::vector<std::pair<unsigned int, std::string>> *reactantLabels) {
std::string delim = ",";
boost::char_separator<char> sep(delim.c_str());

View File

@@ -12,6 +12,7 @@
#define _RD_CHEMTRANSFORMS_H__
#include <boost/smart_ptr.hpp>
#include <string_view>
#include <vector>
#include <GraphMol/Substruct/SubstructMatch.h>
@@ -228,7 +229,7 @@ RDKIT_CHEMTRANSFORMS_EXPORT ROMol *combineMols(
*/
RDKIT_CHEMTRANSFORMS_EXPORT void addRecursiveQueries(
ROMol &mol, const std::map<std::string, ROMOL_SPTR> &queries,
const std::string &propName,
const std::string_view &propName,
std::vector<std::pair<unsigned int, std::string>> *reactantLabels =
nullptr);

View File

@@ -21,6 +21,7 @@
#include <GraphMol/Substruct/SubstructMatch.h>
#include <map>
#include <sstream>
#include <string_view>
namespace RDKit {
@@ -67,51 +68,35 @@ class RDKIT_FRAGCATALOG_EXPORT FragCatalogEntry
// FUnctions on the property dictionary
template <typename T>
void setProp(const char *key, T &val) const {
void setProp(const std::string_view &key, T &val) const {
dp_props->setVal(key, val);
}
void setProp(const std::string_view &key, int val) const {
dp_props->setVal(key, val);
}
void setProp(const std::string_view &key, float val) const {
dp_props->setVal(key, val);
}
void setProp(const std::string_view &key, std::string &val) const {
dp_props->setVal(key, val);
}
template <typename T>
void setProp(const std::string &key, T &val) const {
setProp(key.c_str(), val);
}
void setProp(const char *key, int val) const { dp_props->setVal(key, val); }
void setProp(const std::string &key, int val) const {
setProp(key.c_str(), val);
}
void setProp(const char *key, float val) const { dp_props->setVal(key, val); }
void setProp(const std::string &key, float val) const {
setProp(key.c_str(), val);
}
void setProp(const std::string &key, std::string &val) const {
setProp(key.c_str(), val);
}
template <typename T>
void getProp(const char *key, T &res) const {
void getProp(const std::string_view &key, T &res) const {
dp_props->getVal(key, res);
}
template <typename T>
void getProp(const std::string &key, T &res) const {
getProp(key.c_str(), res);
}
bool hasProp(const char *key) const {
bool hasProp(const std::string_view &key) const {
if (!dp_props) {
return false;
}
return dp_props->hasVal(key);
}
bool hasProp(const std::string &key) const { return hasProp(key.c_str()); }
void clearProp(const char *key) const { dp_props->clearVal(key); }
void clearProp(const std::string &key) const { clearProp(key.c_str()); }
void clearProp(const std::string_view &key) const { dp_props->clearVal(key); }
void toStream(std::ostream &ss) const override;
std::string Serialize() const override;

View File

@@ -1001,7 +1001,8 @@ int getFormalCharge(const ROMol &mol) {
return accum;
};
unsigned getNumAtomsWithDistinctProperty(const ROMol &mol, std::string prop) {
unsigned getNumAtomsWithDistinctProperty(const ROMol &mol,
const std::string_view &prop) {
unsigned numPropAtoms = 0;
for (const auto atom : mol.atoms()) {
if (atom->hasProp(prop)) {

View File

@@ -1203,7 +1203,7 @@ RDKIT_GRAPHMOL_EXPORT void assignChiralTypesFromMolParity(
//! returns the number of atoms which have a particular property set
RDKIT_GRAPHMOL_EXPORT unsigned getNumAtomsWithDistinctProperty(
const ROMol &mol, std::string prop);
const ROMol &mol, const std::string_view &prop);
//! returns whether or not a molecule needs to have Hs added to it.
RDKIT_GRAPHMOL_EXPORT bool needsHs(const ROMol &mol);

View File

@@ -120,10 +120,10 @@ void MolPickler::_pickleProperties(std::ostream &ss, const RDProps &props,
namespace {
template <typename SAVEAS, typename STOREAS>
void unpickleExplicitProperties(
std::istream &ss, RDProps &props, int version,
const std::vector<std::pair<std::string, std::uint16_t>> &explicitProps) {
template <typename SAVEAS, typename STOREAS, typename EXPLICIT>
inline void unpickleExplicitProperties(std::istream &ss, RDProps &props,
int version,
const EXPLICIT &explicitProps) {
if (version >= 14000) {
std::uint8_t bprops;
streamRead(ss, bprops, version);
@@ -137,10 +137,9 @@ void unpickleExplicitProperties(
}
}
template <typename SAVEAS>
bool pickleExplicitProperties(
std::ostream &ss, const RDProps &props,
const std::vector<std::pair<std::string, std::uint16_t>> &explicitProps) {
template <typename SAVEAS, typename EXPLICIT>
inline bool pickleExplicitProperties(std::ostream &ss, const RDProps &props,
const EXPLICIT &explicitProps) {
std::uint8_t bprops = 0;
std::vector<SAVEAS> ps;
SAVEAS bv;
@@ -166,24 +165,24 @@ class PropTracker {
// this is stored as bitflags in a byte, so don't exceed 8 entries or we need
// to update the pickle format.
// the properties themselves are stored as std::int8_t
const std::vector<std::pair<std::string, std::uint16_t>> explicitBondProps = {
std::array<std::pair<std::string, std::uint16_t>, 5> explicitBondProps{{
{RDKit::common_properties::_MolFileBondType, 0x1},
{RDKit::common_properties::_MolFileBondStereo, 0x2},
{RDKit::common_properties::_MolFileBondCfg, 0x4},
{RDKit::common_properties::_MolFileBondQuery, 0x8},
{RDKit::common_properties::molStereoCare, 0x10},
};
}};
// this is stored as bitflags in a byte, so don't exceed 8 entries or we need
// to update the pickle format.
// the properties themselves are stored as std::int16_t
const std::vector<std::pair<std::string, std::uint16_t>> explicitAtomProps = {
std::array<std::pair<std::string, std::uint16_t>, 4> explicitAtomProps{{
{common_properties::molStereoCare, 0x1},
{common_properties::molParity, 0x2},
{common_properties::molInversionFlag, 0x4},
{common_properties::_ChiralityPossible, 0x8},
};
const std::vector<std::string> ignoreAtomProps = {
}};
std::array<std::string, 2> ignoreAtomProps{
common_properties::molAtomMapNumber,
common_properties::dummyLabel,
};

View File

@@ -18,25 +18,27 @@
#include <GraphMol/Atropisomers.h>
#include <GraphMol/Chirality.h>
#include <algorithm>
#include "SmilesWrite.h"
#include "SmilesParse.h"
#include "SmilesParseOps.h"
#include <GraphMol/MolEnumerator/LinkNode.h>
#include <GraphMol/Chirality.h>
#include <algorithm>
#include <array>
#include <map>
namespace SmilesParseOps {
using namespace RDKit;
const std::string cxsmilesindex = "_cxsmilesindex";
const std::string cxsgTracker = "_sgTracker";
constexpr std::string_view cxsmilesindex = "_cxsmilesindex";
constexpr std::string_view cxsgTracker = "_sgTracker";
// FIX: once this can be automated using constexpr, do so
const std::vector<std::string_view> pseudoatoms{"Pol", "Mod"};
const std::vector<std::string_view> pseudoatoms_p{"Pol_p", "Mod_p"};
constexpr std::array<std::string_view, 2> pseudoatoms{"Pol", "Mod"};
constexpr std::array<std::string_view, 2> pseudoatoms_p{"Pol_p", "Mod_p"};
std::map<std::string, std::string> sgroupTypemap = {
const std::map<std::string, std::string> sgroupTypemap = {
{"n", "SRU"}, {"mon", "MON"}, {"mer", "MER"}, {"co", "COP"},
{"xl", "CRO"}, {"mod", "MOD"}, {"mix", "MIX"}, {"f", "FOR"},
{"any", "ANY"}, {"gen", "GEN"}, {"c", "COM"}, {"grf", "GRA"},
@@ -898,19 +900,20 @@ bool parse_polymer_sgroup(Iterator &first, Iterator last, RDKit::RWMol &mol,
}
first += 3;
std::string typ = read_text_to(first, last, ":");
const auto type_code = read_text_to(first, last, ":");
++first;
if (sgroupTypemap.find(typ) == sgroupTypemap.end()) {
const auto type = sgroupTypemap.find(type_code);
if (type == sgroupTypemap.end()) {
return false;
}
bool keepSGroup = false;
SubstanceGroup sgroup(&mol, sgroupTypemap[typ]);
SubstanceGroup sgroup(&mol, type->second);
sgroup.setProp(cxsmilesindex, nSGroups);
if (typ == "alt") {
if (type_code == "alt") {
sgroup.setProp("SUBTYPE", std::string("ALT"));
} else if (typ == "ran") {
} else if (type_code == "ran") {
sgroup.setProp("SUBTYPE", std::string("RAN"));
} else if (typ == "blk") {
} else if (type_code == "blk") {
sgroup.setProp("SUBTYPE", std::string("BLO"));
}
@@ -1941,7 +1944,7 @@ std::string get_atomlabel_block(const ROMol &mol,
std::string get_value_block(const ROMol &mol,
const std::vector<unsigned int> &atomOrder,
const std::string &prop) {
const std::string_view &prop) {
std::string res = "";
bool first = true;
for (auto idx : atomOrder) {
@@ -2023,9 +2026,11 @@ std::string get_coords_block(const ROMol &mol,
std::string get_atom_props_block(const ROMol &mol,
const std::vector<unsigned int> &atomOrder) {
std::vector<std::string> skip = {common_properties::atomLabel,
common_properties::molFileValue,
common_properties::molParity};
static const std::array<std::string, 3> skip = {
common_properties::atomLabel,
common_properties::molFileValue,
common_properties::molParity,
};
std::string res = "";
unsigned int which = 0;
for (auto idx : atomOrder) {

View File

@@ -48,7 +48,7 @@ class RDProps {
STR_VECT res, computed;
if (!includeComputed &&
getPropIfPresent(RDKit::detail::computedPropName, computed)) {
computed.push_back(RDKit::detail::computedPropName);
computed.emplace_back(RDKit::detail::computedPropName);
}
auto pos = tmp.begin();
@@ -57,7 +57,7 @@ class RDProps {
std::find(computed.begin(), computed.end(), *pos) == computed.end()) {
res.push_back(*pos);
}
pos++;
++pos;
}
return res;
}