Skip to content

Commit

Permalink
Issue #520: core: replace RedBlackMap and RedBlackSet implementation …
Browse files Browse the repository at this point in the history
…with standard containers

Partial implementation: BingoContext and MoleculeMass are updated
  • Loading branch information
Alexander Stepaniuk authored and Alexander Stepaniuk committed Aug 2, 2022
1 parent 29c6907 commit 3ff4596
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 20 deletions.
4 changes: 3 additions & 1 deletion bingo/bingo-core/src/core/bingo_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#ifndef __bingo_context__
#define __bingo_context__

#include <map>

#include "base_cpp/nullable.h"
#include "bingo_version.h"
#include "lzw/lzw_dictionary.h"
Expand Down Expand Up @@ -81,7 +83,7 @@ namespace indigo

PtrArray<TautomerRule> tautomer_rules;

RedBlackMap<int, double> relative_atomic_mass_map;
std::map<int, double> relative_atomic_mass_map;

void setLoaderSettings(MoleculeAutoLoader& loader);
void setLoaderSettings(ReactionAutoLoader& loader);
Expand Down
10 changes: 5 additions & 5 deletions bingo/oracle/src/oracle/bingo_oracle_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -494,10 +494,10 @@ void BingoOracleContext::atomicMassLoad(OracleEnv& env)
while (sscanf(buffer, "%s%f%n", element_str.ptr(), &mass, &pos) > 1)
{
int elem = Element::fromString(element_str.ptr());
if (relative_atomic_mass_map.find(elem))
if (relative_atomic_mass_map.find(elem) != relative_atomic_mass_map.end())
throw Error("element '%s' duplication in atomic mass list", element_str.ptr());

relative_atomic_mass_map.insert(elem, mass);
relative_atomic_mass_map.insert(std::pair<int, double> {elem, mass});
buffer += pos;

while (*buffer == ' ')
Expand All @@ -510,10 +510,10 @@ void BingoOracleContext::atomicMassLoad(OracleEnv& env)
if (relative_atomic_mass_map.size() != 0)
{
env.dbgPrintfTS("Relative atomic mass read: ");
for (int i = relative_atomic_mass_map.begin(); i != relative_atomic_mass_map.end(); i = relative_atomic_mass_map.next(i))
for (const auto& pair :relative_atomic_mass_map)
{
int elem = relative_atomic_mass_map.key(i);
float mass = relative_atomic_mass_map.value(i);
int elem = pair.first;
float mass = pair.second;
env.dbgPrintf("%s %g; ", Element::toString(elem), mass);
}
env.dbgPrintf("\n");
Expand Down
7 changes: 6 additions & 1 deletion core/indigo-core/molecule/molecule_mass.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
#define __molecule_mass_h__
#include "base_cpp/red_black.h"
#include "molecule/molecule_mass_options.h"

#include <set>
#include <map>

namespace indigo
{

Expand All @@ -31,6 +34,8 @@ namespace indigo
{
DECL_ERROR;

const double* _relativeAtomicMassByNumber(int number) const;

protected:
struct _ElemCounter
{
Expand All @@ -44,7 +49,7 @@ namespace indigo
MoleculeMass();
MassOptions mass_options;

const RedBlackMap<int, double>* relative_atomic_mass_map;
const std::map<int, double>* relative_atomic_mass_map;

/* Mass of a molecule calculated using the average mass of each
* element weighted for its natural isotopic abundance
Expand Down
28 changes: 15 additions & 13 deletions core/indigo-core/molecule/src/molecule_mass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ IMPL_ERROR(MoleculeMass, "mass");

MoleculeMass::MoleculeMass()
{
relative_atomic_mass_map = NULL;
relative_atomic_mass_map = nullptr;
}

double MoleculeMass::molecularWeight(Molecule& mol)
Expand Down Expand Up @@ -70,13 +70,9 @@ double MoleculeMass::molecularWeight(Molecule& mol)

if (isotope == 0)
{
double* value = 0;
if (relative_atomic_mass_map != NULL)
{
value = relative_atomic_mass_map->at2(number);
}
auto* value = _relativeAtomicMassByNumber(number);

if (value == 0)
if (value == nullptr)
{
elements_count[number]++;
}
Expand Down Expand Up @@ -364,12 +360,7 @@ void MoleculeMass::massComposition(Molecule& mol, Array<char>& str)
}
else
{
double* value = 0;
if (relative_atomic_mass_map != NULL)
{
value = relative_atomic_mass_map->at2(number);
}

auto* value = _relativeAtomicMassByNumber(number);
if (value)
{
relativeMass[number] += *value;
Expand Down Expand Up @@ -420,3 +411,14 @@ void MoleculeMass::massComposition(Molecule& mol, Array<char>& str)
}
output.writeChar(0);
}

const double* MoleculeMass::_relativeAtomicMassByNumber(int number) const
{
if (!relative_atomic_mass_map)
{
return nullptr;
}

const auto it = relative_atomic_mass_map->find(number);
return it != relative_atomic_mass_map->end() ? &(it->second) : nullptr;
}

0 comments on commit 3ff4596

Please sign in to comment.