diff --git a/Code/GraphMol/ChemReactions/PreprocessRxn.cpp b/Code/GraphMol/ChemReactions/PreprocessRxn.cpp index fe2b888ba..75edcfd3b 100644 --- a/Code/GraphMol/ChemReactions/PreprocessRxn.cpp +++ b/Code/GraphMol/ChemReactions/PreprocessRxn.cpp @@ -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>> &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 &queries, - const std::string &propName) { + const std::string_view &propName) { unsigned int numWarnings, numErrors; std::vector>> reactantLabels; @@ -72,7 +73,7 @@ bool preprocessReaction( std::vector>> &reactantLabels, const std::map &queries, - const std::string &propName) { + const std::string_view &propName) { rxn.setImplicitPropertiesFlag(true); rxn.initReactantMatchers(); diff --git a/Code/GraphMol/ChemReactions/PreprocessRxn.h b/Code/GraphMol/ChemReactions/PreprocessRxn.h index 9e3f81670..d3cdca5ef 100644 --- a/Code/GraphMol/ChemReactions/PreprocessRxn.h +++ b/Code/GraphMol/ChemReactions/PreprocessRxn.h @@ -35,30 +35,31 @@ #include "Reaction.h" #include +#include #include 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>> &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 &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>> &reactantLabels, const std::map &queries, - const std::string &propName = common_properties::molFileValue); + const std::string_view &propName = common_properties::molFileValue); } // namespace RDKit #endif diff --git a/Code/GraphMol/ChemReactions/Reaction.cpp b/Code/GraphMol/ChemReactions/Reaction.cpp index 4fb41c3b3..ec1cdef45 100644 --- a/Code/GraphMol/ChemReactions/Reaction.cpp +++ b/Code/GraphMol/ChemReactions/Reaction.cpp @@ -448,7 +448,7 @@ bool isMoleculeAgentOfReaction(const ChemicalReaction &rxn, const ROMol &mol) { void addRecursiveQueriesToReaction( ChemicalReaction &rxn, const std::map &queries, - const std::string &propName, + const std::string_view &propName, std::vector>> *reactantLabels) { if (!rxn.isInitialized()) { diff --git a/Code/GraphMol/ChemReactions/Reaction.h b/Code/GraphMol/ChemReactions/Reaction.h index bd5e4d7ac..1768832c6 100644 --- a/Code/GraphMol/ChemReactions/Reaction.h +++ b/Code/GraphMol/ChemReactions/Reaction.h @@ -39,6 +39,7 @@ #include #include #include +#include #include namespace RDKit { @@ -468,7 +469,7 @@ getReactingAtoms(const ChemicalReaction &rxn, bool mappedAtomsOnly = false); */ RDKIT_CHEMREACTIONS_EXPORT void addRecursiveQueriesToReaction( ChemicalReaction &rxn, const std::map &queries, - const std::string &propName, + const std::string_view &propName, std::vector>> *reactantLabels = nullptr); diff --git a/Code/GraphMol/ChemReactions/SanitizeRxn.cpp b/Code/GraphMol/ChemReactions/SanitizeRxn.cpp index 7ec3f3317..13e6e3b1c 100644 --- a/Code/GraphMol/ChemReactions/SanitizeRxn.cpp +++ b/Code/GraphMol/ChemReactions/SanitizeRxn.cpp @@ -40,7 +40,7 @@ namespace RxnOps { // molFileRLabel ==> unsigned int namespace { template -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) { diff --git a/Code/GraphMol/ChemTransforms/ChemTransforms.cpp b/Code/GraphMol/ChemTransforms/ChemTransforms.cpp index 70c6e65cb..f4d1d0601 100644 --- a/Code/GraphMol/ChemTransforms/ChemTransforms.cpp +++ b/Code/GraphMol/ChemTransforms/ChemTransforms.cpp @@ -891,7 +891,7 @@ ROMol *combineMols(const ROMol &mol1, const ROMol &mol2, void addRecursiveQueries( ROMol &mol, const std::map &queries, - const std::string &propName, + const std::string_view &propName, std::vector> *reactantLabels) { std::string delim = ","; boost::char_separator sep(delim.c_str()); diff --git a/Code/GraphMol/ChemTransforms/ChemTransforms.h b/Code/GraphMol/ChemTransforms/ChemTransforms.h index fc29cc31e..d7f551a97 100644 --- a/Code/GraphMol/ChemTransforms/ChemTransforms.h +++ b/Code/GraphMol/ChemTransforms/ChemTransforms.h @@ -12,6 +12,7 @@ #define _RD_CHEMTRANSFORMS_H__ #include +#include #include #include @@ -228,7 +229,7 @@ RDKIT_CHEMTRANSFORMS_EXPORT ROMol *combineMols( */ RDKIT_CHEMTRANSFORMS_EXPORT void addRecursiveQueries( ROMol &mol, const std::map &queries, - const std::string &propName, + const std::string_view &propName, std::vector> *reactantLabels = nullptr); diff --git a/Code/GraphMol/FragCatalog/FragCatalogEntry.h b/Code/GraphMol/FragCatalog/FragCatalogEntry.h index 42b5b8aa9..a013da812 100644 --- a/Code/GraphMol/FragCatalog/FragCatalogEntry.h +++ b/Code/GraphMol/FragCatalog/FragCatalogEntry.h @@ -21,6 +21,7 @@ #include #include #include +#include namespace RDKit { @@ -67,51 +68,35 @@ class RDKIT_FRAGCATALOG_EXPORT FragCatalogEntry // FUnctions on the property dictionary template - 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 - 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 - void getProp(const char *key, T &res) const { + void getProp(const std::string_view &key, T &res) const { dp_props->getVal(key, res); } - template - 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; diff --git a/Code/GraphMol/MolOps.cpp b/Code/GraphMol/MolOps.cpp index c5130accf..3497cd9fc 100644 --- a/Code/GraphMol/MolOps.cpp +++ b/Code/GraphMol/MolOps.cpp @@ -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)) { diff --git a/Code/GraphMol/MolOps.h b/Code/GraphMol/MolOps.h index ed25e4ba9..24dc1b286 100644 --- a/Code/GraphMol/MolOps.h +++ b/Code/GraphMol/MolOps.h @@ -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); diff --git a/Code/GraphMol/MolPickler.cpp b/Code/GraphMol/MolPickler.cpp index e79307c3a..33edc21a9 100644 --- a/Code/GraphMol/MolPickler.cpp +++ b/Code/GraphMol/MolPickler.cpp @@ -120,10 +120,10 @@ void MolPickler::_pickleProperties(std::ostream &ss, const RDProps &props, namespace { -template -void unpickleExplicitProperties( - std::istream &ss, RDProps &props, int version, - const std::vector> &explicitProps) { +template +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 -bool pickleExplicitProperties( - std::ostream &ss, const RDProps &props, - const std::vector> &explicitProps) { +template +inline bool pickleExplicitProperties(std::ostream &ss, const RDProps &props, + const EXPLICIT &explicitProps) { std::uint8_t bprops = 0; std::vector 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> explicitBondProps = { + std::array, 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> explicitAtomProps = { + std::array, 4> explicitAtomProps{{ {common_properties::molStereoCare, 0x1}, {common_properties::molParity, 0x2}, {common_properties::molInversionFlag, 0x4}, {common_properties::_ChiralityPossible, 0x8}, - }; - const std::vector ignoreAtomProps = { + }}; + std::array ignoreAtomProps{ common_properties::molAtomMapNumber, common_properties::dummyLabel, }; diff --git a/Code/GraphMol/SmilesParse/CXSmilesOps.cpp b/Code/GraphMol/SmilesParse/CXSmilesOps.cpp index 5359680ab..e6c0a24d4 100644 --- a/Code/GraphMol/SmilesParse/CXSmilesOps.cpp +++ b/Code/GraphMol/SmilesParse/CXSmilesOps.cpp @@ -18,25 +18,27 @@ #include #include -#include #include "SmilesWrite.h" #include "SmilesParse.h" #include "SmilesParseOps.h" #include #include + +#include +#include #include 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 pseudoatoms{"Pol", "Mod"}; -const std::vector pseudoatoms_p{"Pol_p", "Mod_p"}; +constexpr std::array pseudoatoms{"Pol", "Mod"}; +constexpr std::array pseudoatoms_p{"Pol_p", "Mod_p"}; -std::map sgroupTypemap = { +const std::map 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 &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 &atomOrder) { - std::vector skip = {common_properties::atomLabel, - common_properties::molFileValue, - common_properties::molParity}; + static const std::array skip = { + common_properties::atomLabel, + common_properties::molFileValue, + common_properties::molParity, + }; std::string res = ""; unsigned int which = 0; for (auto idx : atomOrder) { diff --git a/Code/RDGeneral/RDProps.h b/Code/RDGeneral/RDProps.h index 07b3af740..2eec9699e 100644 --- a/Code/RDGeneral/RDProps.h +++ b/Code/RDGeneral/RDProps.h @@ -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; }