Skip to content

Commit

Permalink
Issue #520: core: replace RedBlackMap and RedBlackSet implementation
Browse files Browse the repository at this point in the history
Partial implementation: replace RedBlackMap in BaseMolecule class
  • Loading branch information
Mikalai Sukhikh authored and Mikalai Sukhikh committed Aug 4, 2022
1 parent 774ebd6 commit 2e952b6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 19 deletions.
2 changes: 1 addition & 1 deletion core/indigo-core/layout/src/molecule_layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void MoleculeLayout::_init(bool smart_layout)

// modify the atom mapping
for (int j = 0; j < _atomMapping.size(); ++j)
if (atomMapCollapse.find(_atomMapping[j]))
if (atomMapCollapse.find(_atomMapping[j]) != atomMapCollapse.end())
_atomMapping[j] = atomMapCollapse.at(_atomMapping[j]);
}
}
Expand Down
5 changes: 3 additions & 2 deletions core/indigo-core/molecule/base_molecule.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "molecule/molecule_standardize.h"
#include "molecule/molecule_stereocenters.h"
#include "molecule/molecule_tgroups.h"
#include <map>
#include <set>

#ifdef _WIN32
Expand Down Expand Up @@ -104,7 +105,7 @@ namespace indigo
class DLLEXPORT BaseMolecule : public Graph
{
public:
typedef RedBlackMap<int, int> Mapping;
typedef std::map<int, int> Mapping;

BaseMolecule();
~BaseMolecule() override;
Expand Down Expand Up @@ -462,7 +463,7 @@ namespace indigo
Array<int> _bond_directions;

Array<Vec3f> _xyz;
RedBlackMap<int, Vec3f> _stereo_flag_positions;
std::map<int, Vec3f> _stereo_flag_positions;

ObjArray<Array<int>> _rsite_attachment_points;
bool _rGroupFragment;
Expand Down
24 changes: 12 additions & 12 deletions core/indigo-core/molecule/src/base_molecule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1231,10 +1231,10 @@ void BaseMolecule::collapse(BaseMolecule& bm, int id, Mapping& mapAtom, Mapping&
int from = group.atoms[j];
int to = group.atoms[k];

int* to_ = mapAtom.at2(from);
if (to_ == 0)
mapAtom.insert(from, to);
else if (*to_ != to)
auto it = mapAtom.find(from);
if (it == mapAtom.end())
mapAtom.emplace(from, to);
else if (it->second != to)
throw Error("Invalid mapping in MultipleGroup::collapse");

if (k != j)
Expand All @@ -1244,15 +1244,15 @@ void BaseMolecule::collapse(BaseMolecule& bm, int id, Mapping& mapAtom, Mapping&
for (int j = bm.edgeBegin(); j < bm.edgeEnd(); j = bm.edgeNext(j))
{
const Edge& edge = bm.getEdge(j);
bool in1 = mapAtom.find(edge.beg), in2 = mapAtom.find(edge.end), p1 = in1 && mapAtom.at(edge.beg) == edge.beg,
p2 = in2 && mapAtom.at(edge.end) == edge.end;
bool in1 = mapAtom.find(edge.beg) != mapAtom.end(), in2 = mapAtom.find(edge.end) != mapAtom.end();
bool p1 = in1 && mapAtom.at(edge.beg) == edge.beg, p2 = in2 && mapAtom.at(edge.end) == edge.end;
if ((in1 && !p1 && !in2) || (!in1 && !p2 && in2))
{
int beg = in1 ? mapAtom.at(edge.beg) : edge.beg;
int end = in2 ? mapAtom.at(edge.end) : edge.end;
int bid = copyBaseBond(bm, beg, end, j);
if (!mapBondInv.find(bid))
mapBondInv.insert(bid, j);
if (mapBondInv.find(bid) == mapBondInv.end())
mapBondInv.emplace(bid, j);
}
}

Expand Down Expand Up @@ -4137,7 +4137,7 @@ void BaseMolecule::setStereoFlagPosition(int frag_index, const Vec3f& pos)
{
try
{
_stereo_flag_positions.insert(frag_index, pos);
_stereo_flag_positions.emplace(frag_index, pos);
}
catch (Exception& ex)
{
Expand All @@ -4146,10 +4146,10 @@ void BaseMolecule::setStereoFlagPosition(int frag_index, const Vec3f& pos)

bool BaseMolecule::getStereoFlagPosition(int frag_index, Vec3f& pos)
{
auto pval = _stereo_flag_positions.at2(frag_index);
if (pval)
auto it = _stereo_flag_positions.find(frag_index);
if (it != _stereo_flag_positions.end())
{
pos = *pval;
pos = it->second;
return true;
}
return false;
Expand Down
8 changes: 4 additions & 4 deletions core/render2d/src/render_internal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@ void MoleculeRenderInternal::_cloneAndFillMappings()
_bondMappingInv.clear();
for (int i = clone->edgeBegin(); i < clone->edgeEnd(); i = clone->edgeNext(i))
{
_bondMappingInv.insert(i, BaseMolecule::findMappedEdge(*clone, *_mol, i, _atomMappingInv.ptr()));
_bondMappingInv.emplace(i, BaseMolecule::findMappedEdge(*clone, *_mol, i, _atomMappingInv.ptr()));
}
_mol = clone;
_own_mol = true;
Expand Down Expand Up @@ -928,9 +928,9 @@ void MoleculeRenderInternal::_prepareSGroups()
bid = amol.addBond(said, naid, amol.getBondOrder(nbid));
amol.setEdgeTopology(bid, amol.getBondTopology(nbid));
}
if (_bondMappingInv.find(bid))
_bondMappingInv.remove(bid);
_bondMappingInv.insert(bid, _bondMappingInv.at(nbid));
if (_bondMappingInv.find(bid) != _bondMappingInv.end())
_bondMappingInv.erase(bid);
_bondMappingInv.emplace(bid, _bondMappingInv.at(nbid));
}
}
}
Expand Down

0 comments on commit 2e952b6

Please sign in to comment.