From f65bdcf6572a89d3ea4087f5e9da29b3ced7744c Mon Sep 17 00:00:00 2001 From: Aliaksander Stsepaniuk Date: Tue, 20 Sep 2022 23:17:44 +0300 Subject: [PATCH 1/3] Issue #847 Make Array properly movable --- core/indigo-core/common/base_cpp/array.h | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/core/indigo-core/common/base_cpp/array.h b/core/indigo-core/common/base_cpp/array.h index bb83161e89..82f03552e0 100644 --- a/core/indigo-core/common/base_cpp/array.h +++ b/core/indigo-core/common/base_cpp/array.h @@ -44,11 +44,9 @@ namespace indigo { } - Array(Array&& other) : _reserved(other._reserved), _length(other._length), _array(other._array) + Array(Array&& other) noexcept : Array() { - other._array = nullptr; - other._length = 0; - other._reserved = 0; + swap(other); } ~Array() @@ -62,6 +60,12 @@ namespace indigo } } + Array& operator=(Array&& src) noexcept + { + swap(src); + return *this; + } + void clear() { _length = 0; @@ -358,7 +362,7 @@ namespace indigo _length = newsize; } - void swap(Array& other) + void swap(Array& other) noexcept { std::swap(_array, other._array); std::swap(_reserved, other._reserved); From d37c17c372e1ad0ec6c87115690182622a001fe2 Mon Sep 17 00:00:00 2001 From: Aliaksander Stsepaniuk Date: Wed, 21 Sep 2022 10:02:23 +0300 Subject: [PATCH 2/3] Issue #851 Make molecule sgroups classes movable --- api/c/indigo/src/indigo_molecule.cpp | 6 +- .../common/base_cpp/string_pool.cpp | 2 +- .../indigo-core/common/base_cpp/string_pool.h | 2 +- core/indigo-core/molecule/base_molecule.h | 2 +- core/indigo-core/molecule/molecule_sgroups.h | 58 ++++-- .../molecule/src/base_molecule.cpp | 101 ++++++----- .../molecule/src/molecule_sgroups.cpp | 171 +++++++++--------- .../molecule/src/molfile_loader.cpp | 8 +- .../molecule/src/molfile_saver.cpp | 26 +-- 9 files changed, 203 insertions(+), 173 deletions(-) diff --git a/api/c/indigo/src/indigo_molecule.cpp b/api/c/indigo/src/indigo_molecule.cpp index 493283a8d5..35c6f68d86 100644 --- a/api/c/indigo/src/indigo_molecule.cpp +++ b/api/c/indigo/src/indigo_molecule.cpp @@ -3174,8 +3174,8 @@ CEXPORT int indigoAddSGroupAttachmentPoint(int sgroup, int aidx, int lvidx, cons INDIGO_BEGIN { Superatom& sup = IndigoSuperatom::cast(self.getObject(sgroup)).get(); - int ap_idx = sup.attachment_points.add(); - Superatom::_AttachmentPoint& ap = sup.attachment_points.at(ap_idx); + int ap_idx = sup.getAttachmentPoints().add(); + Superatom::_AttachmentPoint& ap = sup.getAttachmentPoints().at(ap_idx); ap.aidx = aidx; ap.lvidx = lvidx; ap.apid.readString(apid, true); @@ -3189,7 +3189,7 @@ CEXPORT int indigoDeleteSGroupAttachmentPoint(int sgroup, int ap_idx) INDIGO_BEGIN { Superatom& sup = IndigoSuperatom::cast(self.getObject(sgroup)).get(); - sup.attachment_points.remove(ap_idx); + sup.getAttachmentPoints().remove(ap_idx); return 1; } INDIGO_END(-1); diff --git a/core/indigo-core/common/base_cpp/string_pool.cpp b/core/indigo-core/common/base_cpp/string_pool.cpp index d555c5d17d..d6979cb12a 100644 --- a/core/indigo-core/common/base_cpp/string_pool.cpp +++ b/core/indigo-core/common/base_cpp/string_pool.cpp @@ -65,7 +65,7 @@ int StringPool::add(int size) return _add(0, size); } -int StringPool::add(Array& str) +int StringPool::add(const Array& str) { return _add(str.ptr(), str.size()); } diff --git a/core/indigo-core/common/base_cpp/string_pool.h b/core/indigo-core/common/base_cpp/string_pool.h index 8987576552..4a3ae51758 100644 --- a/core/indigo-core/common/base_cpp/string_pool.h +++ b/core/indigo-core/common/base_cpp/string_pool.h @@ -39,7 +39,7 @@ namespace indigo ~StringPool(); int add(const char* str); - int add(Array& str); + int add(const Array& str); int add(int size); void remove(int idx); int size() const; diff --git a/core/indigo-core/molecule/base_molecule.h b/core/indigo-core/molecule/base_molecule.h index ca879b4aaf..e748b56c87 100644 --- a/core/indigo-core/molecule/base_molecule.h +++ b/core/indigo-core/molecule/base_molecule.h @@ -176,7 +176,7 @@ namespace indigo int getTemplateAtomAttachmentPoint(int atom_idx, int order); void getTemplateAtomAttachmentPointId(int atom_idx, int order, Array& apid); int getTemplateAtomAttachmentPointsCount(int atom_idx); - int getTemplateAtomAttachmentPointById(int atom_idx, Array& att_id); + int getTemplateAtomAttachmentPointById(int atom_idx, const Array& att_id); void addAttachmentPoint(int order, int atom_index); diff --git a/core/indigo-core/molecule/molecule_sgroups.h b/core/indigo-core/molecule/molecule_sgroups.h index 76fd07453d..97865f598c 100644 --- a/core/indigo-core/molecule/molecule_sgroups.h +++ b/core/indigo-core/molecule/molecule_sgroups.h @@ -19,6 +19,8 @@ #ifndef __molecule_sgroups__ #define __molecule_sgroups__ +#include + #include "base_cpp/array.h" #include "base_cpp/obj_pool.h" #include "base_cpp/ptr_pool.h" @@ -99,7 +101,13 @@ namespace indigo }; SGroup(); - virtual ~SGroup(); + SGroup(const SGroup&) = delete; + SGroup(SGroup&&) noexcept = default; + + virtual ~SGroup() = default; + + SGroup& operator=(const SGroup&) = delete; + SGroup& operator=(SGroup&&) noexcept = default; int sgroup_type; // group type, represnted with STY in Molfile format int sgroup_subtype; // group subtype, represnted with SST in Molfile format @@ -116,16 +124,17 @@ namespace indigo static const char* typeToString(int sg_type); static int getType(const char* sg_type); - - private: - SGroup(const SGroup&); }; class DLLEXPORT DataSGroup : public SGroup { public: DataSGroup(); - ~DataSGroup() override; + DataSGroup(const DataSGroup&) = delete; + DataSGroup(DataSGroup&&) noexcept = default; + + DataSGroup& operator=(const DataSGroup&) = delete; + DataSGroup& operator=(DataSGroup&&) noexcept = default; Array description; // SDT in Molfile format (filed units or format) Array name; // SDT in Molfile format (field name) @@ -140,15 +149,17 @@ namespace indigo int num_chars; // number of characters int dasp_pos; char tag; // tag - private: - DataSGroup(const DataSGroup&); }; class DLLEXPORT Superatom : public SGroup { public: Superatom(); - ~Superatom() override; + Superatom(const Superatom&) = delete; + Superatom(Superatom&&) noexcept = default; + + Superatom& operator=(const Superatom&) = delete; + Superatom& operator=(Superatom&&) noexcept = default; Array subscript; // SMT in Molfile format Array sa_class; // SCL in Molfile format @@ -163,7 +174,9 @@ namespace indigo int lvidx; Array apid; }; - ObjPool<_AttachmentPoint> attachment_points; // SAP in Molfile format + const ObjPool<_AttachmentPoint>& getAttachmentPoints() const; + ObjPool<_AttachmentPoint>& getAttachmentPoints(); + bool hasAttachmentPoints() const; struct _BondConnection { @@ -173,32 +186,35 @@ namespace indigo Array<_BondConnection> bond_connections; // SBV in Molfile format private: - Superatom(const Superatom&); + mutable std::unique_ptr> _attachment_points; // SAP in Molfile format }; class DLLEXPORT RepeatingUnit : public SGroup { public: RepeatingUnit(); - ~RepeatingUnit() override; + RepeatingUnit(const RepeatingUnit&) = delete; + RepeatingUnit(RepeatingUnit&&) noexcept = default; + + RepeatingUnit& operator=(const RepeatingUnit&) = delete; + RepeatingUnit& operator=(RepeatingUnit&&) noexcept = default; int connectivity; Array subscript; // SMT in Molfile format - private: - RepeatingUnit(const RepeatingUnit&); }; class DLLEXPORT MultipleGroup : public SGroup { public: MultipleGroup(); - ~MultipleGroup() override; + MultipleGroup(const MultipleGroup&) = delete; + MultipleGroup(MultipleGroup&&) noexcept = default; + + MultipleGroup& operator=(const MultipleGroup&) = delete; + MultipleGroup& operator=(MultipleGroup&&) noexcept = default; Array parent_atoms; int multiplier; - - private: - MultipleGroup(const MultipleGroup&); }; class Tree; @@ -206,7 +222,11 @@ namespace indigo { public: MoleculeSGroups(); - ~MoleculeSGroups(); + MoleculeSGroups(const MoleculeSGroups&) = delete; + MoleculeSGroups(MoleculeSGroups&&) noexcept = default; + + MoleculeSGroups& operator=(const MoleculeSGroups&) = delete; + MoleculeSGroups& operator=(MoleculeSGroups&&) noexcept = default; DECL_ERROR; @@ -248,7 +268,7 @@ namespace indigo void registerUnfoldedHydrogen(int idx, int new_h_idx); protected: - PtrPool _sgroups; + std::unique_ptr> _sgroups; private: int _findSGroupById(int id); diff --git a/core/indigo-core/molecule/src/base_molecule.cpp b/core/indigo-core/molecule/src/base_molecule.cpp index 705976f0ac..07702a9c2e 100644 --- a/core/indigo-core/molecule/src/base_molecule.cpp +++ b/core/indigo-core/molecule/src/base_molecule.cpp @@ -185,24 +185,26 @@ void BaseMolecule::mergeSGroupsWithSubmolecule(BaseMolecule& mol, Array& ma sa.sa_class.copy(supersa.sa_class); sa.sa_natreplace.copy(supersa.sa_natreplace); sa.contracted = supersa.contracted; - if (supersa.attachment_points.size() > 0) + if (supersa.hasAttachmentPoints()) { - for (int j = supersa.attachment_points.begin(); j < supersa.attachment_points.end(); j = supersa.attachment_points.next(j)) + const auto& super_aps = supersa.getAttachmentPoints(); + auto& aps = sa.getAttachmentPoints(); + for (int j = super_aps.begin(); j < super_aps.end(); j = super_aps.next(j)) { - int ap_idx = sa.attachment_points.add(); - Superatom::_AttachmentPoint& ap = sa.attachment_points.at(ap_idx); - int a_idx = supersa.attachment_points[j].aidx; + int ap_idx = aps.add(); + Superatom::_AttachmentPoint& ap = aps.at(ap_idx); + int a_idx = super_aps[j].aidx; if (a_idx > -1) ap.aidx = mapping[a_idx]; else ap.aidx = a_idx; - int leave_idx = supersa.attachment_points[j].lvidx; + int leave_idx = super_aps[j].lvidx; if (leave_idx > -1) ap.lvidx = mapping[leave_idx]; else ap.lvidx = leave_idx; - ap.apid.copy(supersa.attachment_points[j].apid); + ap.apid.copy(super_aps[j].apid); } } } @@ -421,11 +423,12 @@ void BaseMolecule::_flipSuperatomBond(Superatom& sa, int src_bond_idx, int new_b bond.bond_idx = new_bond_idx; } } - if (sa.attachment_points.size() > 0) + if (sa.hasAttachmentPoints()) { - for (int j = sa.attachment_points.begin(); j != sa.attachment_points.end(); j = sa.attachment_points.next(j)) + auto& aps = sa.getAttachmentPoints(); + for (int j = aps.begin(); j != aps.end(); j = aps.next(j)) { - Superatom::_AttachmentPoint& ap = sa.attachment_points.at(j); + Superatom::_AttachmentPoint& ap = aps.at(j); const Edge& edge = getEdge(new_bond_idx); if ((edge.beg == ap.aidx) || (edge.end == ap.aidx)) @@ -1089,7 +1092,7 @@ void BaseMolecule::getTemplateAtomAttachmentPointId(int atom_idx, int order, Arr throw Error("attachment point order %d is out of range (%d)", order, ap_count); } -int BaseMolecule::getTemplateAtomAttachmentPointById(int atom_idx, Array& att_id) +int BaseMolecule::getTemplateAtomAttachmentPointById(int atom_idx, const Array& att_id) { QS_DEF(Array, tmp); int aidx = -1; @@ -1382,11 +1385,12 @@ int BaseMolecule::transformFullCTABtoSCSR(ObjArray& templates) { Superatom& su = (Superatom&)sg; - if (su.attachment_points.size() > 0) + if (su.hasAttachmentPoints()) { - for (int k = su.attachment_points.begin(); k < su.attachment_points.end(); k = su.attachment_points.next(k)) + const auto& aps = su.getAttachmentPoints(); + for (int k = aps.begin(); k < aps.end(); k = aps.next(k)) { - Superatom::_AttachmentPoint& ap = su.attachment_points.at(k); + const Superatom::_AttachmentPoint& ap = aps.at(k); ap_points_atoms.push(ap.aidx); ap_lgrp_atoms.push(ap.lvidx); ap_ids.push(ap_points_ids.add(ap.apid)); @@ -2152,11 +2156,12 @@ int BaseMolecule::transformFullCTABtoSCSR(ObjArray& templates) { Superatom& su = (Superatom&)sg; - if (su.attachment_points.size() > 0) + if (su.hasAttachmentPoints()) { - for (int k = su.attachment_points.begin(); k < su.attachment_points.end(); k = su.attachment_points.next(k)) + const auto& aps = su.getAttachmentPoints(); + for (int k = aps.begin(); k < aps.end(); k = aps.next(k)) { - Superatom::_AttachmentPoint& ap = su.attachment_points.at(k); + const Superatom::_AttachmentPoint& ap = aps.at(k); ap_points_atoms.push(ap.aidx); ap_lgrp_atoms.push(ap.lvidx); ap_ids.push(ap_points_ids.add(ap.apid)); @@ -2912,11 +2917,12 @@ int BaseMolecule::_transformTGroupToSGroup(int idx, int t_idx) // printf("Template = %s (%d)\n", tgroup.tgroup_name.ptr(), idx); - if (su.attachment_points.size() > 0) + if (su.hasAttachmentPoints()) { - for (int j = su.attachment_points.begin(); j < su.attachment_points.end(); j = su.attachment_points.next(j)) + const auto& aps = su.getAttachmentPoints(); + for (int j = aps.begin(); j < aps.end(); j = aps.next(j)) { - Superatom::_AttachmentPoint& ap = su.attachment_points.at(j); + const Superatom::_AttachmentPoint& ap = aps.at(j); int att_atom_idx = getTemplateAtomAttachmentPointById(idx, ap.apid); if (att_atom_idx > -1) @@ -3031,14 +3037,18 @@ int BaseMolecule::_transformTGroupToSGroup(int idx, int t_idx) { su.bonds.push(bond_idx); - for (int j = su.attachment_points.begin(); j < su.attachment_points.end(); j = su.attachment_points.next(j)) + if (su.hasAttachmentPoints()) { - Superatom::_AttachmentPoint& ap = su.attachment_points.at(j); + auto& aps = su.getAttachmentPoints(); + for (int j = aps.begin(); j < aps.end(); j = aps.next(j)) + { + Superatom::_AttachmentPoint& ap = aps.at(j); - // printf("SUP AP ap.aidx = %d, tg_atoms[i] = %d, mapping[tg_atoms[i]] = %d\n", ap.aidx, tg_atoms[i], mapping[tg_atoms[i]]); + // printf("SUP AP ap.aidx = %d, tg_atoms[i] = %d, mapping[tg_atoms[i]] = %d\n", ap.aidx, tg_atoms[i], mapping[tg_atoms[i]]); - if (ap.aidx == mapping[tg_atoms[i]]) - ap.lvidx = att_atoms[i]; + if (ap.aidx == mapping[tg_atoms[i]]) + ap.lvidx = att_atoms[i]; + } } } } @@ -3085,14 +3095,15 @@ int BaseMolecule::_transformSGroupToTGroup(int sg_idx, int& tg_idx) ap_points_ids.clear(); ap_ids.clear(); - if (su.attachment_points.size() == 0 && su.bonds.size() == 0) + if (!su.hasAttachmentPoints() && su.bonds.size() == 0) return -1; - if (su.attachment_points.size() > 0) + if (su.hasAttachmentPoints()) { - for (int k = su.attachment_points.begin(); k < su.attachment_points.end(); k = su.attachment_points.next(k)) + const auto& aps = su.getAttachmentPoints(); + for (int k = aps.begin(); k < aps.end(); k = aps.next(k)) { - Superatom::_AttachmentPoint& ap = su.attachment_points.at(k); + const Superatom::_AttachmentPoint& ap = aps.at(k); ap_points_atoms.push(ap.aidx); ap_ids.push(ap_points_ids.add(ap.apid)); } @@ -3119,8 +3130,8 @@ int BaseMolecule::_transformSGroupToTGroup(int sg_idx, int& tg_idx) continue; } - int idap = su.attachment_points.add(); - Superatom::_AttachmentPoint& ap = su.attachment_points.at(idap); + int idap = su.getAttachmentPoints().add(); + Superatom::_AttachmentPoint& ap = su.getAttachmentPoints().at(idap); if (_isNTerminus(su, ap_aidx)) // N-terminus ? ap.apid.readString("Al", true); @@ -3179,7 +3190,9 @@ int BaseMolecule::_transformSGroupToTGroup(int sg_idx, int& tg_idx) else { Superatom& sup_new = (Superatom&)sg; - if ((strcmp(su.subscript.ptr(), sup_new.subscript.ptr()) == 0) && (su.attachment_points.size() == sup_new.attachment_points.size())) + if ((strcmp(su.subscript.ptr(), sup_new.subscript.ptr()) == 0) + && sup_new.hasAttachmentPoints() + && (su.getAttachmentPoints().size() == sup_new.getAttachmentPoints().size())) { new_sg_idx = sgs[j]; } @@ -3381,11 +3394,12 @@ int BaseMolecule::_createSGroupFromFragment(Array& sg_atoms, const TGroup& Superatom& su = (Superatom&)sg; - if (su.attachment_points.size() > 0) + if (su.hasAttachmentPoints()) { - for (int j = su.attachment_points.begin(); j < su.attachment_points.end(); j = su.attachment_points.next(j)) + const auto& aps = su.getAttachmentPoints(); + for (int j = aps.begin(); j < aps.end(); j = aps.next(j)) { - Superatom::_AttachmentPoint& ap = su.attachment_points.at(j); + const Superatom::_AttachmentPoint& ap = aps.at(j); tg_atoms.push(ap.aidx); lvgroups.push(ap.lvidx); @@ -3418,8 +3432,8 @@ int BaseMolecule::_createSGroupFromFragment(Array& sg_atoms, const TGroup& if (fragment.getBondOrder(q_xbond_idx) == this->asMolecule().getBondOrder(t_xbond_idx)) { su_new.bonds.push(t_xbond_idx); - int idap = su_new.attachment_points.add(); - Superatom::_AttachmentPoint& ap = su_new.attachment_points.at(idap); + int idap = su_new.getAttachmentPoints().add(); + Superatom::_AttachmentPoint& ap = su_new.getAttachmentPoints().at(idap); ap.aidx = att_point_idx; ap.lvidx = v.neiVertex(k); ap.apid.readString(ap_points_ids.at(l), true); @@ -3476,13 +3490,14 @@ void BaseMolecule::_removeAtomsFromSuperatom(Superatom& sa, Array& mapping) sa.bond_connections.remove(j); } } - if (sa.attachment_points.size() > 0) + if (sa.hasAttachmentPoints()) { - for (int j = sa.attachment_points.begin(); j < sa.attachment_points.end(); j = sa.attachment_points.next(j)) + auto& aps = sa.getAttachmentPoints(); + for (int j = aps.begin(); j < aps.end(); j = aps.next(j)) { - Superatom::_AttachmentPoint& ap = sa.attachment_points.at(j); + Superatom::_AttachmentPoint& ap = aps.at(j); if (ap.aidx >= 0 && mapping[ap.aidx] == -1) - sa.attachment_points.remove(j); + aps.remove(j); else if (ap.lvidx >= 0 && mapping[ap.lvidx] == -1) ap.lvidx = -1; } @@ -4111,8 +4126,8 @@ int BaseMolecule::transformHELMtoSGroups(Array& helm_class, Array& n { throw Error("internal error: attachment point was not found"); } - int idap = sg.attachment_points.add(); - Superatom::_AttachmentPoint& ap = sg.attachment_points.at(idap); + int idap = sg.getAttachmentPoints().add(); + Superatom::_AttachmentPoint& ap = sg.getAttachmentPoints().at(idap); ap.aidx = ap_idx; ap.lvidx = i; if (r_num == 0) diff --git a/core/indigo-core/molecule/src/molecule_sgroups.cpp b/core/indigo-core/molecule/src/molecule_sgroups.cpp index 3c1bb766e1..431e8e4a7d 100644 --- a/core/indigo-core/molecule/src/molecule_sgroups.cpp +++ b/core/indigo-core/molecule/src/molecule_sgroups.cpp @@ -62,10 +62,6 @@ SGroup::SGroup() parent_idx = -1; } -SGroup::~SGroup() -{ -} - DataSGroup::DataSGroup() { sgroup_type = SGroup::SG_TYPE_DAT; @@ -77,87 +73,84 @@ DataSGroup::DataSGroup() tag = ' '; } -DataSGroup::~DataSGroup() -{ -} - -Superatom::Superatom() +Superatom::Superatom() : _attachment_points{nullptr} { sgroup_type = SGroup::SG_TYPE_SUP; contracted = -1; seqid = -1; - attachment_points.clear(); bond_connections.clear(); } -Superatom::~Superatom() +const ObjPool& Superatom::getAttachmentPoints() const { + if (!_attachment_points) + { + _attachment_points = std::make_unique>(); + } + + return *_attachment_points; } -RepeatingUnit::RepeatingUnit() +ObjPool& Superatom::getAttachmentPoints() { - sgroup_type = SGroup::SG_TYPE_SRU; - connectivity = 0; + return const_cast&>(static_cast(*this).getAttachmentPoints()); } -RepeatingUnit::~RepeatingUnit() +bool Superatom::hasAttachmentPoints() const { + return _attachment_points && _attachment_points->size() > 0; } -MultipleGroup::MultipleGroup() +RepeatingUnit::RepeatingUnit() { - sgroup_type = SGroup::SG_TYPE_MUL; - multiplier = 1; + sgroup_type = SGroup::SG_TYPE_SRU; + connectivity = 0; } -MultipleGroup::~MultipleGroup() +MultipleGroup::MultipleGroup() { + sgroup_type = SGroup::SG_TYPE_MUL; + multiplier = 1; } IMPL_ERROR(MoleculeSGroups, "molecule sgroups"); -MoleculeSGroups::MoleculeSGroups() -{ - _sgroups.clear(); -} - -MoleculeSGroups::~MoleculeSGroups() +MoleculeSGroups::MoleculeSGroups() : _sgroups{std::make_unique>()} { - _sgroups.clear(); } void MoleculeSGroups::clear() { - _sgroups.clear(); + _sgroups->clear(); } void MoleculeSGroups::clear(int sg_type) { - for (int i = _sgroups.begin(); i != _sgroups.end(); i = _sgroups.next(i)) + for (int i = _sgroups->begin(); i != _sgroups->end(); i = _sgroups->next(i)) { - if (_sgroups.at(i)->sgroup_type == sg_type) + if (_sgroups->at(i)->sgroup_type == sg_type) remove(i); } } void MoleculeSGroups::remove(int idx) { - _sgroups.remove(idx); + _sgroups->remove(idx); } int MoleculeSGroups::begin() { - return _sgroups.begin(); + return _sgroups->begin(); } int MoleculeSGroups::end() { - return _sgroups.end(); + return _sgroups->end(); } int MoleculeSGroups::next(int i) { - return _sgroups.next(i); + return _sgroups->next(i); } int MoleculeSGroups::addSGroup(const char* sg_type) @@ -175,56 +168,56 @@ int MoleculeSGroups::addSGroup(int sg_type) int idx = -1; if (sg_type == SGroup::SG_TYPE_GEN) { - idx = _sgroups.add(new SGroup()); + idx = _sgroups->add(new SGroup()); } else if (sg_type == SGroup::SG_TYPE_DAT) { - idx = _sgroups.add(new DataSGroup()); + idx = _sgroups->add(new DataSGroup()); } else if (sg_type == SGroup::SG_TYPE_SUP) { - idx = _sgroups.add(new Superatom()); + idx = _sgroups->add(new Superatom()); } else if (sg_type == SGroup::SG_TYPE_SRU) { - idx = _sgroups.add(new RepeatingUnit()); + idx = _sgroups->add(new RepeatingUnit()); } else if (sg_type == SGroup::SG_TYPE_MUL) { - idx = _sgroups.add(new MultipleGroup()); + idx = _sgroups->add(new MultipleGroup()); } else { - idx = _sgroups.add(new SGroup()); + idx = _sgroups->add(new SGroup()); if (idx != -1) - _sgroups.at(idx)->sgroup_type = sg_type; + _sgroups->at(idx)->sgroup_type = sg_type; } return idx; } SGroup& MoleculeSGroups::getSGroup(int idx) { - if (_sgroups.hasElement(idx)) - return *_sgroups.at(idx); + if (_sgroups->hasElement(idx)) + return *_sgroups->at(idx); throw Error("Sgroup with index %d is not found", idx); } bool MoleculeSGroups::hasSGroup(int idx) { - return _sgroups.hasElement(idx); + return _sgroups->hasElement(idx); } SGroup& MoleculeSGroups::getSGroup(int idx, int sg_type) { int count = -1; - for (int i = _sgroups.begin(); i != _sgroups.end(); i = _sgroups.next(i)) + for (int i = _sgroups->begin(); i != _sgroups->end(); i = _sgroups->next(i)) { - if (_sgroups.at(i)->sgroup_type == sg_type) + if (_sgroups->at(i)->sgroup_type == sg_type) { count++; if (count == idx) - return *_sgroups.at(i); + return *_sgroups->at(i); } } throw Error("Sgroup index %d or type %d wrong", idx, sg_type); @@ -232,15 +225,15 @@ SGroup& MoleculeSGroups::getSGroup(int idx, int sg_type) int MoleculeSGroups::getSGroupCount() { - return _sgroups.size(); + return _sgroups->size(); } int MoleculeSGroups::getSGroupCount(int sg_type) { int count = 0; - for (int i = _sgroups.begin(); i != _sgroups.end(); i = _sgroups.next(i)) + for (int i = _sgroups->begin(); i != _sgroups->end(); i = _sgroups->next(i)) { - if (_sgroups.at(i)->sgroup_type == sg_type) + if (_sgroups->at(i)->sgroup_type == sg_type) count++; } return count; @@ -387,9 +380,9 @@ void MoleculeSGroups::findSGroups(int property, int value, Array& sgs) int i; if (property == SGroup::SG_TYPE) { - for (i = _sgroups.begin(); i != _sgroups.end(); i = _sgroups.next(i)) + for (i = _sgroups->begin(); i != _sgroups->end(); i = _sgroups->next(i)) { - SGroup& sg = *_sgroups.at(i); + SGroup& sg = *_sgroups->at(i); if (sg.sgroup_type == value) { sgs.push(i); @@ -398,9 +391,9 @@ void MoleculeSGroups::findSGroups(int property, int value, Array& sgs) } else if (property == SGroup::SG_BRACKET_STYLE) { - for (i = _sgroups.begin(); i != _sgroups.end(); i = _sgroups.next(i)) + for (i = _sgroups->begin(); i != _sgroups->end(); i = _sgroups->next(i)) { - SGroup& sg = *_sgroups.at(i); + SGroup& sg = *_sgroups->at(i); if (sg.brk_style == value) { sgs.push(i); @@ -409,9 +402,9 @@ void MoleculeSGroups::findSGroups(int property, int value, Array& sgs) } else if (property == SGroup::SG_DISPLAY_OPTION) { - for (i = _sgroups.begin(); i != _sgroups.end(); i = _sgroups.next(i)) + for (i = _sgroups->begin(); i != _sgroups->end(); i = _sgroups->next(i)) { - SGroup& sg = *_sgroups.at(i); + SGroup& sg = *_sgroups->at(i); if (sg.sgroup_type == SGroup::SG_TYPE_SUP) { Superatom& sup = (Superatom&)sg; @@ -424,9 +417,9 @@ void MoleculeSGroups::findSGroups(int property, int value, Array& sgs) } else if (property == SGroup::SG_PARENT) { - for (i = _sgroups.begin(); i != _sgroups.end(); i = _sgroups.next(i)) + for (i = _sgroups->begin(); i != _sgroups->end(); i = _sgroups->next(i)) { - SGroup& sg = *_sgroups.at(i); + SGroup& sg = *_sgroups->at(i); if (sg.parent_group == value) { sgs.push(i); @@ -435,10 +428,10 @@ void MoleculeSGroups::findSGroups(int property, int value, Array& sgs) } else if (property == SGroup::SG_CHILD) { - if (!_sgroups.hasElement(value)) + if (!_sgroups->hasElement(value)) return; - SGroup& sg = *_sgroups.at(value); + SGroup& sg = *_sgroups->at(value); if (sg.parent_group != 0) { int idx = _findSGroupById(sg.parent_group); @@ -455,9 +448,9 @@ void MoleculeSGroups::findSGroups(int property, const char* str, Array& sgs int i; if (property == SGroup::SG_CLASS) { - for (i = _sgroups.begin(); i != _sgroups.end(); i = _sgroups.next(i)) + for (i = _sgroups->begin(); i != _sgroups->end(); i = _sgroups->next(i)) { - SGroup& sg = *_sgroups.at(i); + SGroup& sg = *_sgroups->at(i); if (sg.sgroup_type == SGroup::SG_TYPE_SUP) { Superatom& sa = (Superatom&)sg; @@ -471,9 +464,9 @@ void MoleculeSGroups::findSGroups(int property, const char* str, Array& sgs } else if (property == SGroup::SG_LABEL) { - for (i = _sgroups.begin(); i != _sgroups.end(); i = _sgroups.next(i)) + for (i = _sgroups->begin(); i != _sgroups->end(); i = _sgroups->next(i)) { - SGroup& sg = *_sgroups.at(i); + SGroup& sg = *_sgroups->at(i); if (sg.sgroup_type == SGroup::SG_TYPE_SUP) { Superatom& sa = (Superatom&)sg; @@ -496,9 +489,9 @@ void MoleculeSGroups::findSGroups(int property, const char* str, Array& sgs } else if (property == SGroup::SG_DATA) { - for (i = _sgroups.begin(); i != _sgroups.end(); i = _sgroups.next(i)) + for (i = _sgroups->begin(); i != _sgroups->end(); i = _sgroups->next(i)) { - SGroup& sg = *_sgroups.at(i); + SGroup& sg = *_sgroups->at(i); if (sg.sgroup_type == SGroup::SG_TYPE_DAT) { DataSGroup& dg = (DataSGroup&)sg; @@ -512,9 +505,9 @@ void MoleculeSGroups::findSGroups(int property, const char* str, Array& sgs } else if (property == SGroup::SG_DATA_NAME) { - for (i = _sgroups.begin(); i != _sgroups.end(); i = _sgroups.next(i)) + for (i = _sgroups->begin(); i != _sgroups->end(); i = _sgroups->next(i)) { - SGroup& sg = *_sgroups.at(i); + SGroup& sg = *_sgroups->at(i); if (sg.sgroup_type == SGroup::SG_TYPE_DAT) { DataSGroup& dg = (DataSGroup&)sg; @@ -528,9 +521,9 @@ void MoleculeSGroups::findSGroups(int property, const char* str, Array& sgs } else if (property == SGroup::SG_DATA_TYPE) { - for (i = _sgroups.begin(); i != _sgroups.end(); i = _sgroups.next(i)) + for (i = _sgroups->begin(); i != _sgroups->end(); i = _sgroups->next(i)) { - SGroup& sg = *_sgroups.at(i); + SGroup& sg = *_sgroups->at(i); if (sg.sgroup_type == SGroup::SG_TYPE_DAT) { DataSGroup& dg = (DataSGroup&)sg; @@ -544,9 +537,9 @@ void MoleculeSGroups::findSGroups(int property, const char* str, Array& sgs } else if (property == SGroup::SG_DATA_DESCRIPTION) { - for (i = _sgroups.begin(); i != _sgroups.end(); i = _sgroups.next(i)) + for (i = _sgroups->begin(); i != _sgroups->end(); i = _sgroups->next(i)) { - SGroup& sg = *_sgroups.at(i); + SGroup& sg = *_sgroups->at(i); if (sg.sgroup_type == SGroup::SG_TYPE_DAT) { DataSGroup& dg = (DataSGroup&)sg; @@ -560,9 +553,9 @@ void MoleculeSGroups::findSGroups(int property, const char* str, Array& sgs } else if (property == SGroup::SG_DATA_DISPLAY) { - for (i = _sgroups.begin(); i != _sgroups.end(); i = _sgroups.next(i)) + for (i = _sgroups->begin(); i != _sgroups->end(); i = _sgroups->next(i)) { - SGroup& sg = *_sgroups.at(i); + SGroup& sg = *( _sgroups->at(i)); if (sg.sgroup_type == SGroup::SG_TYPE_DAT) { DataSGroup& dg = (DataSGroup&)sg; @@ -575,9 +568,9 @@ void MoleculeSGroups::findSGroups(int property, const char* str, Array& sgs } else if (property == SGroup::SG_DATA_LOCATION) { - for (i = _sgroups.begin(); i != _sgroups.end(); i = _sgroups.next(i)) + for (i = _sgroups->begin(); i != _sgroups->end(); i = _sgroups->next(i)) { - SGroup& sg = *_sgroups.at(i); + SGroup& sg = *_sgroups->at(i); if (sg.sgroup_type == SGroup::SG_TYPE_DAT) { DataSGroup& dg = (DataSGroup&)sg; @@ -590,9 +583,9 @@ void MoleculeSGroups::findSGroups(int property, const char* str, Array& sgs } else if (property == SGroup::SG_DATA_TAG) { - for (i = _sgroups.begin(); i != _sgroups.end(); i = _sgroups.next(i)) + for (i = _sgroups->begin(); i != _sgroups->end(); i = _sgroups->next(i)) { - SGroup& sg = *_sgroups.at(i); + SGroup& sg = *_sgroups->at(i); if (sg.sgroup_type == SGroup::SG_TYPE_DAT) { DataSGroup& dg = (DataSGroup&)sg; @@ -605,9 +598,9 @@ void MoleculeSGroups::findSGroups(int property, const char* str, Array& sgs } else if (property == SGroup::SG_QUERY_CODE) { - for (i = _sgroups.begin(); i != _sgroups.end(); i = _sgroups.next(i)) + for (i = _sgroups->begin(); i != _sgroups->end(); i = _sgroups->next(i)) { - SGroup& sg = *_sgroups.at(i); + SGroup& sg = *_sgroups->at(i); if (sg.sgroup_type == SGroup::SG_TYPE_DAT) { DataSGroup& dg = (DataSGroup&)sg; @@ -621,9 +614,9 @@ void MoleculeSGroups::findSGroups(int property, const char* str, Array& sgs } else if (property == SGroup::SG_QUERY_OPER) { - for (i = _sgroups.begin(); i != _sgroups.end(); i = _sgroups.next(i)) + for (i = _sgroups->begin(); i != _sgroups->end(); i = _sgroups->next(i)) { - SGroup& sg = *_sgroups.at(i); + SGroup& sg = *_sgroups->at(i); if (sg.sgroup_type == SGroup::SG_TYPE_DAT) { DataSGroup& dg = (DataSGroup&)sg; @@ -644,9 +637,9 @@ void MoleculeSGroups::findSGroups(int property, Array& indices, Array& int i; if (property == SGroup::SG_ATOMS) { - for (i = _sgroups.begin(); i != _sgroups.end(); i = _sgroups.next(i)) + for (i = _sgroups->begin(); i != _sgroups->end(); i = _sgroups->next(i)) { - SGroup& sg = *_sgroups.at(i); + SGroup& sg = *_sgroups->at(i); if (_cmpIndices(sg.atoms, indices)) { sgs.push(i); @@ -655,9 +648,9 @@ void MoleculeSGroups::findSGroups(int property, Array& indices, Array& } else if (property == SGroup::SG_BONDS) { - for (i = _sgroups.begin(); i != _sgroups.end(); i = _sgroups.next(i)) + for (i = _sgroups->begin(); i != _sgroups->end(); i = _sgroups->next(i)) { - SGroup& sg = *_sgroups.at(i); + SGroup& sg = *_sgroups->at(i); if (_cmpIndices(sg.bonds, indices)) { sgs.push(i); @@ -670,9 +663,9 @@ void MoleculeSGroups::findSGroups(int property, Array& indices, Array& void MoleculeSGroups::registerUnfoldedHydrogen(int idx, int new_h_idx) { - for (int i = _sgroups.begin(); i != _sgroups.end(); i = _sgroups.next(i)) + for (int i = _sgroups->begin(); i != _sgroups->end(); i = _sgroups->next(i)) { - SGroup& sg = *_sgroups.at(i); + SGroup& sg = *_sgroups->at(i); if (sg.atoms.find(idx) != -1) { sg.atoms.push(new_h_idx); @@ -682,9 +675,9 @@ void MoleculeSGroups::registerUnfoldedHydrogen(int idx, int new_h_idx) int MoleculeSGroups::_findSGroupById(int id) { - for (int i = _sgroups.begin(); i != _sgroups.end(); i = _sgroups.next(i)) + for (int i = _sgroups->begin(); i != _sgroups->end(); i = _sgroups->next(i)) { - SGroup& sg = *_sgroups.at(i); + SGroup& sg = *_sgroups->at(i); if (sg.original_group == id) { return i; diff --git a/core/indigo-core/molecule/src/molfile_loader.cpp b/core/indigo-core/molecule/src/molfile_loader.cpp index 467387b600..42096528f5 100644 --- a/core/indigo-core/molecule/src/molfile_loader.cpp +++ b/core/indigo-core/molecule/src/molfile_loader.cpp @@ -1360,8 +1360,8 @@ void MolfileLoader::_readCtab2000() int n = _scanner.readIntFix(3); while (n-- > 0) { - int idap = sup.attachment_points.add(); - Superatom::_AttachmentPoint& ap = sup.attachment_points.at(idap); + int idap = sup.getAttachmentPoints().add(); + Superatom::_AttachmentPoint& ap = sup.getAttachmentPoints().at(idap); _scanner.skip(1); ap.aidx = _scanner.readIntFix(3) - 1; _scanner.skip(1); @@ -3573,8 +3573,8 @@ void MolfileLoader::_readSGroup3000(const char* str) throw Error("SAP number is %d (must be 3)", n); scanner.skipSpace(); int idx = scanner.readInt() - 1; - int idap = sup->attachment_points.add(); - Superatom::_AttachmentPoint& ap = sup->attachment_points.at(idap); + int idap = sup->getAttachmentPoints().add(); + Superatom::_AttachmentPoint& ap = sup->getAttachmentPoints().at(idap); ap.aidx = idx; scanner.skipSpace(); ap.lvidx = scanner.readInt() - 1; diff --git a/core/indigo-core/molecule/src/molfile_saver.cpp b/core/indigo-core/molecule/src/molfile_saver.cpp index d12da6366e..2272e6572f 100644 --- a/core/indigo-core/molecule/src/molfile_saver.cpp +++ b/core/indigo-core/molecule/src/molfile_saver.cpp @@ -798,15 +798,16 @@ void MolfileSaver::_writeCtab(Output& output, BaseMolecule& mol, bool query) out.printf(" CLASS=%s", sup.sa_class.ptr()); if (sup.contracted == 0) out.printf(" ESTATE=E"); - if (sup.attachment_points.size() > 0) + if (sup.hasAttachmentPoints()) { - for (int j = sup.attachment_points.begin(); j < sup.attachment_points.end(); j = sup.attachment_points.next(j)) + const auto& aps = sup.getAttachmentPoints(); + for (int j = aps.begin(); j < aps.end(); j = aps.next(j)) { int leave_idx = 0; - if (sup.attachment_points[j].lvidx > -1) - leave_idx = _atom_mapping[sup.attachment_points[j].lvidx]; + if (aps[j].lvidx > -1) + leave_idx = _atom_mapping[aps[j].lvidx]; - out.printf(" SAP=(3 %d %d %s)", _atom_mapping[sup.attachment_points[j].aidx], leave_idx, sup.attachment_points[j].apid.ptr()); + out.printf(" SAP=(3 %d %d %s)", _atom_mapping[aps[j].aidx], leave_idx, aps[j].apid.ptr()); } } if (sup.seqid > 0) @@ -1637,12 +1638,13 @@ void MolfileSaver::_writeCtab2000(Output& output, BaseMolecule& mol, bool query) { output.printfCR("M SDS EXP 1 %3d", superatom.original_group); } - if (superatom.attachment_points.size() > 0) + if (superatom.hasAttachmentPoints()) { bool next_line = true; - int nrem = superatom.attachment_points.size(); + const auto& aps = superatom.getAttachmentPoints(); + int nrem = aps.size(); int k = 0; - for (int j = superatom.attachment_points.begin(); j < superatom.attachment_points.end(); j = superatom.attachment_points.next(j)) + for (int j = aps.begin(); j < aps.end(); j = aps.next(j)) { if (next_line) { @@ -1651,10 +1653,10 @@ void MolfileSaver::_writeCtab2000(Output& output, BaseMolecule& mol, bool query) } int leave_idx = 0; - if (superatom.attachment_points[j].lvidx > -1) - leave_idx = _atom_mapping[superatom.attachment_points[j].lvidx]; - output.printf(" %3d %3d %c%c", _atom_mapping[superatom.attachment_points[j].aidx], leave_idx, superatom.attachment_points[j].apid[0], - superatom.attachment_points[j].apid[1]); + if (aps[j].lvidx > -1) + leave_idx = _atom_mapping[aps[j].lvidx]; + output.printf(" %3d %3d %c%c", _atom_mapping[aps[j].aidx], leave_idx, aps[j].apid[0], + aps[j].apid[1]); k++; nrem--; if ((k == 6) || (nrem == 0)) From 4288ab6e96c653d8e976b03f67b2c663b3608c1c Mon Sep 17 00:00:00 2001 From: Alexander Stepaniuk Date: Fri, 23 Sep 2022 22:25:22 +0300 Subject: [PATCH 3/3] Static analyzer issues fixes --- core/indigo-core/molecule/src/base_molecule.cpp | 5 ++--- core/indigo-core/molecule/src/molecule_sgroups.cpp | 2 +- core/indigo-core/molecule/src/molfile_saver.cpp | 3 +-- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/core/indigo-core/molecule/src/base_molecule.cpp b/core/indigo-core/molecule/src/base_molecule.cpp index 07702a9c2e..d9d48d2661 100644 --- a/core/indigo-core/molecule/src/base_molecule.cpp +++ b/core/indigo-core/molecule/src/base_molecule.cpp @@ -3190,9 +3190,8 @@ int BaseMolecule::_transformSGroupToTGroup(int sg_idx, int& tg_idx) else { Superatom& sup_new = (Superatom&)sg; - if ((strcmp(su.subscript.ptr(), sup_new.subscript.ptr()) == 0) - && sup_new.hasAttachmentPoints() - && (su.getAttachmentPoints().size() == sup_new.getAttachmentPoints().size())) + if ((strcmp(su.subscript.ptr(), sup_new.subscript.ptr()) == 0) && sup_new.hasAttachmentPoints() && + (su.getAttachmentPoints().size() == sup_new.getAttachmentPoints().size())) { new_sg_idx = sgs[j]; } diff --git a/core/indigo-core/molecule/src/molecule_sgroups.cpp b/core/indigo-core/molecule/src/molecule_sgroups.cpp index 431e8e4a7d..6f7f4cba24 100644 --- a/core/indigo-core/molecule/src/molecule_sgroups.cpp +++ b/core/indigo-core/molecule/src/molecule_sgroups.cpp @@ -555,7 +555,7 @@ void MoleculeSGroups::findSGroups(int property, const char* str, Array& sgs { for (i = _sgroups->begin(); i != _sgroups->end(); i = _sgroups->next(i)) { - SGroup& sg = *( _sgroups->at(i)); + SGroup& sg = *(_sgroups->at(i)); if (sg.sgroup_type == SGroup::SG_TYPE_DAT) { DataSGroup& dg = (DataSGroup&)sg; diff --git a/core/indigo-core/molecule/src/molfile_saver.cpp b/core/indigo-core/molecule/src/molfile_saver.cpp index 2272e6572f..ed54c51d57 100644 --- a/core/indigo-core/molecule/src/molfile_saver.cpp +++ b/core/indigo-core/molecule/src/molfile_saver.cpp @@ -1655,8 +1655,7 @@ void MolfileSaver::_writeCtab2000(Output& output, BaseMolecule& mol, bool query) int leave_idx = 0; if (aps[j].lvidx > -1) leave_idx = _atom_mapping[aps[j].lvidx]; - output.printf(" %3d %3d %c%c", _atom_mapping[aps[j].aidx], leave_idx, aps[j].apid[0], - aps[j].apid[1]); + output.printf(" %3d %3d %c%c", _atom_mapping[aps[j].aidx], leave_idx, aps[j].apid[0], aps[j].apid[1]); k++; nrem--; if ((k == 6) || (nrem == 0))