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 MoleculeGrossFormula class
  • Loading branch information
Mikalai Sukhikh authored and Mikalai Sukhikh committed Aug 4, 2022
1 parent 774ebd6 commit 6e2f689
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
5 changes: 3 additions & 2 deletions core/indigo-core/molecule/molecule_gross_formula.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#ifndef __molecule_gross_formula__
#define __molecule_gross_formula__

#include <map>
#include <memory>
#include <set>

Expand All @@ -43,7 +44,7 @@ namespace indigo
{
public:
Array<char> multiplier;
RedBlackMap<int, int> isotopes;
std::map<int, int> isotopes;
};

// Represents array of superunits gross formulas.
Expand Down Expand Up @@ -74,7 +75,7 @@ namespace indigo
};

static void _toString(const Array<int>& gross, ArrayOutput& output, int (*cmp)(_ElemCounter&, _ElemCounter&, void*), bool add_rsites);
static void _toString(const RedBlackMap<int, int>& gross, ArrayOutput& output, int (*cmp)(_ElemCounter&, _ElemCounter&, void*), bool add_rsites);
static void _toString(const std::map<int, int>& gross, ArrayOutput& output, int (*cmp)(_ElemCounter&, _ElemCounter&, void*), bool add_rsites);
static int _cmp(_ElemCounter& ec1, _ElemCounter& ec2, void* context);
static int _cmp_hill(_ElemCounter& ec1, _ElemCounter& ec2, void* context);
static int _cmp_hill_no_carbon(_ElemCounter& ec1, _ElemCounter& ec2, void* context);
Expand Down
44 changes: 26 additions & 18 deletions core/indigo-core/molecule/src/molecule_gross_formula.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ void MoleculeGrossFormula::collect(BaseMolecule& molecule, Array<int>& gross_out
auto& unit = gross[0];
int number = 0;

for (int i = unit.isotopes.begin(); i < unit.isotopes.end(); i = unit.isotopes.next(i))
for (auto it = unit.isotopes.begin(); it != unit.isotopes.end(); it++)
{
number = unit.isotopes.key(i) & 0xFF;
number = it->first & 0xFF;
if (number < ELEM_RSITE + 1)
gross_out[number] += unit.isotopes.value(i);
gross_out[number] += it->second;
}
}

Expand Down Expand Up @@ -204,8 +204,10 @@ std::unique_ptr<GROSS_UNITS> MoleculeGrossFormula::collect(BaseMolecule& mol, bo
else
continue;

if ((val = unit.isotopes.at2(key)) == 0)
unit.isotopes.insert(key, 1);
auto it = unit.isotopes.find(key);
val = &(it->second);
if (it == unit.isotopes.end())
unit.isotopes.emplace(key, 1);
else
*val += 1;

Expand All @@ -216,8 +218,10 @@ std::unique_ptr<GROSS_UNITS> MoleculeGrossFormula::collect(BaseMolecule& mol, bo
if (implicit_h >= 0)
{
key = ELEM_H;
if ((val = unit.isotopes.at2(key)) == 0)
unit.isotopes.insert(key, implicit_h);
auto it = unit.isotopes.find(key);
val = &(it->second);
if (it == unit.isotopes.end())
unit.isotopes.emplace(key, implicit_h);
else
*val += implicit_h;
}
Expand Down Expand Up @@ -255,7 +259,9 @@ void MoleculeGrossFormula::toString_Hill(GROSS_UNITS& gross, Array<char>& str, b
int* val;

// First base molecule
if ((val = gross[0].isotopes.at2(ELEM_C)) == 0)
auto it = gross[0].isotopes.find(ELEM_C);
val = &(it->second);
if (it == gross[0].isotopes.end())
_toString(gross[0].isotopes, output, _cmp_hill_no_carbon, add_rsites);
else
_toString(gross[0].isotopes, output, _cmp_hill, add_rsites);
Expand All @@ -264,7 +270,9 @@ void MoleculeGrossFormula::toString_Hill(GROSS_UNITS& gross, Array<char>& str, b
for (int i = 1; i < gross.size(); i++)
{
output.writeChar('(');
if ((val = gross[i].isotopes.at2(ELEM_C)) == 0)
auto it = gross[0].isotopes.find(ELEM_C);
val = &(it->second);
if (it == gross[0].isotopes.end())
_toString(gross[i].isotopes, output, _cmp_hill_no_carbon, add_rsites);
else
_toString(gross[i].isotopes, output, _cmp_hill, add_rsites);
Expand Down Expand Up @@ -314,28 +322,27 @@ void MoleculeGrossFormula::_toString(const Array<int>& gross, ArrayOutput& outpu
}
}

void MoleculeGrossFormula::_toString(const RedBlackMap<int, int>& isotopes, ArrayOutput& output, int (*cmp)(_ElemCounter&, _ElemCounter&, void*),
bool add_rsites)
void MoleculeGrossFormula::_toString(const std::map<int, int>& isotopes, ArrayOutput& output, int (*cmp)(_ElemCounter&, _ElemCounter&, void*), bool add_rsites)
{
QS_DEF(Array<_ElemCounter>, counters);

int i;
int number;
int isotope;

for (i = isotopes.begin(); i < isotopes.end(); i = isotopes.next(i))
for (auto it = isotopes.begin(); it != isotopes.end(); it++)
{
if (isotopes.key(i) == ELEM_RSITE)
if (it->first == ELEM_RSITE)
continue;

_ElemCounter& ec = counters.push();

number = isotopes.key(i) & 0xFF;
isotope = isotopes.key(i) >> 8;
number = it->first & 0xFF;
isotope = it->first >> 8;

ec.elem = number;
ec.isotope = isotope;
ec.counter = isotopes.value(i);
ec.counter = it->second;
}

counters.qsort(cmp, 0);
Expand Down Expand Up @@ -371,8 +378,9 @@ void MoleculeGrossFormula::_toString(const RedBlackMap<int, int>& isotopes, Arra
first_written = true;
}

int* val;
if (add_rsites && (val = isotopes.at2(ELEM_RSITE)) != 0)
auto it = isotopes.find(ELEM_RSITE);
const int* val = &(it->second);
if (add_rsites && it != isotopes.end())
{
output.writeString(" R#");
if (*val > 1)
Expand Down

0 comments on commit 6e2f689

Please sign in to comment.