Skip to content

Commit

Permalink
#815 Replace ObjList with std::vector in IndigoDeconvolution
Browse files Browse the repository at this point in the history
  • Loading branch information
Aliaksander Stsepaniuk authored and Aliaksander Stsepaniuk committed Sep 14, 2022
1 parent 18424d1 commit 5eac32b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
44 changes: 28 additions & 16 deletions api/c/indigo/src/indigo_deconvolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* limitations under the License.
***************************************************************************/

#include <algorithm>

#include "indigo_deconvolution.h"
#include "base_cpp/array.h"
#include "base_cpp/obj_array.h"
Expand Down Expand Up @@ -690,13 +692,13 @@ void IndigoDecompositionMatch::removeRsitesFromMaps(Graph& query_graph)
}
}

void IndigoDecompositionMatch::copyScafAutoMaps(ObjList<Array<int>>& autoMaps)
void IndigoDecompositionMatch::copyScafAutoMaps(const std::vector<Array<int>>& autoMaps)
{
scafAutoMaps.clear();
for (int i = autoMaps.begin(); i != autoMaps.end(); i = autoMaps.next(i))
for (const auto& src: autoMaps)
{
int idx = scafAutoMaps.add();
scafAutoMaps.at(idx).copy(autoMaps[i]);
scafAutoMaps.emplace_back();
scafAutoMaps.back().copy(src);
}
}

Expand Down Expand Up @@ -953,7 +955,7 @@ void IndigoDeconvolution::addCompleteRGroup(IndigoDecompositionMatch& deco_match
if (deco_match.scafAutoMaps.size() == 0)
throw Error("internal error: can not calculate scaffold matchings for null automorphism");

for (int aut_idx = deco_match.scafAutoMaps.begin(); aut_idx != deco_match.scafAutoMaps.end(); aut_idx = deco_match.scafAutoMaps.next(aut_idx))
for (int aut_idx = 0; aut_idx < deco_match.scafAutoMaps.size(); ++aut_idx)
{
int new_rg_num = _createRgMap(deco_match, aut_idx, match_rgroups, &rg_map_buf, false);
/*
Expand Down Expand Up @@ -1239,8 +1241,8 @@ void IndigoDeconvolution::DecompositionEnumerator::calculateAutoMaps(Graph& sub)
*/
_scafAutoMaps.clear();

int l_idx = _scafAutoMaps.add();
Array<int>& d_map = _scafAutoMaps.at(l_idx);
_scafAutoMaps.emplace_back();
Array<int>& d_map = _scafAutoMaps.back();
d_map.resize(sub.vertexEnd());
for (int i = 0; i < d_map.size(); ++i)
{
Expand Down Expand Up @@ -1342,9 +1344,8 @@ void IndigoDeconvolution::DecompositionEnumerator::addMatch(IndigoDecompositionM
{
direct_order.push(pair.first);
}
for (int auto_idx = _autoMaps.begin(); auto_idx != _autoMaps.end(); auto_idx = _autoMaps.next(auto_idx))
for (const Array<int>& auto_map: _autoMaps)
{
Array<int>& auto_map = _autoMaps[auto_idx];
/*
* Check for correctness and condition
*/
Expand Down Expand Up @@ -1436,15 +1437,15 @@ void IndigoDeconvolution::DecompositionEnumerator::_swapIndexes(IndigoDecomposit
}
}

void IndigoDeconvolution::DecompositionEnumerator::_refineAutoMaps(ObjList<Array<int>>& auto_maps, Graph& sub_in, Graph& super, Array<int>& scaf_map)
void IndigoDeconvolution::DecompositionEnumerator::_refineAutoMaps(std::vector<Array<int>>& auto_maps, Graph& sub_in, Graph& super, Array<int>& scaf_map)
{
QS_DEF(Array<int>, indices_to_remove);
indices_to_remove.clear();

BaseMolecule& sub = (BaseMolecule&)sub_in;

int sub_idx, super_idx, ae_idx_beg, ae_idx_end, e_beg, e_end;
for (int auto_idx = auto_maps.begin(); auto_idx != auto_maps.end(); auto_idx = auto_maps.next(auto_idx))
for (int auto_idx = 0; auto_idx < auto_maps.size(); ++auto_idx)
{
Array<int>& auto_map = auto_maps.at(auto_idx);

Expand Down Expand Up @@ -1515,10 +1516,21 @@ void IndigoDeconvolution::DecompositionEnumerator::_refineAutoMaps(ObjList<Array
indices_to_remove.push(auto_idx);
}
}
for (int i = 0; i < indices_to_remove.size(); ++i)

// clean-up auto_maps according to indices_to_remove
auto tmp_maps = std::move(auto_maps);
auto_maps.clear();

int start = 0;
for (int idx: indices_to_remove)
{
auto_maps.remove(indices_to_remove[i]);
const auto startIt = tmp_maps.begin() + start;
const auto endIt = tmp_maps.begin() + idx;
std::move(startIt, endIt, std::back_inserter(auto_maps));

start = idx + 1;
}
std::move(tmp_maps.begin() + start, tmp_maps.end(), std::back_inserter(auto_maps));
}

void IndigoDeconvolution::DecompositionEnumerator::_addAllRsites(QueryMolecule& r_molecule, IndigoDecompositionMatch& deco_match, std::map<int, int>& r_sites)
Expand Down Expand Up @@ -1560,9 +1572,9 @@ void IndigoDeconvolution::DecompositionEnumerator::_addAllRsites(QueryMolecule&

bool IndigoDeconvolution::DecompositionEnumerator::_cbAutoCheckAutomorphism(Graph&, const Array<int>& mapping, const void* context)
{
ObjList<Array<int>>& auto_maps = *(ObjList<Array<int>>*)context;
int l_idx = auto_maps.add();
auto_maps.at(l_idx).copy(mapping);
std::vector<Array<int>>& auto_maps = *(std::vector<Array<int>>*)context;
auto_maps.emplace_back();
auto_maps.back().copy(mapping);
return false;
}

Expand Down
10 changes: 5 additions & 5 deletions api/c/indigo/src/indigo_deconvolution.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ class DLLEXPORT IndigoDeconvolution : public IndigoObject
DecompositionEnumerator(const DecompositionEnumerator&); // no implicit copy
bool _foundOrder(ObjArray<Array<int>>& rsite_orders, Array<int>& swap_order);
void _swapIndexes(IndigoDecompositionMatch&, int old_idx, int new_idx);
void _refineAutoMaps(ObjList<Array<int>>& auto_maps, Graph& sub, Graph& super, Array<int>& scaf_map);
void _refineAutoMaps(std::vector<Array<int>>& auto_maps, Graph& sub, Graph& super, Array<int>& scaf_map);
void _addAllRsites(QueryMolecule&, IndigoDecompositionMatch&, std::map<int, int>&);

static bool _cbAutoCheckAutomorphism(Graph& graph, const Array<int>& mapping, const void* context);
ObjList<Array<int>> _autoMaps;
ObjList<Array<int>> _scafAutoMaps;
std::vector<Array<int>> _autoMaps;
std::vector<Array<int>> _scafAutoMaps;
};

void addCompleteRGroup(IndigoDecompositionMatch& emb_context, bool change_scaffold, Array<int>* rg_map);
Expand Down Expand Up @@ -181,7 +181,7 @@ class DLLEXPORT IndigoDecompositionMatch : public IndigoObject
Array<int> lastInvMapping;
std::vector<Array<int>> attachmentOrder;
std::vector<Array<int>> attachmentIndex;
ObjList<Array<int>> scafAutoMaps;
std::vector<Array<int>> scafAutoMaps;

int getRgroupNumber() const
{
Expand All @@ -191,7 +191,7 @@ class DLLEXPORT IndigoDecompositionMatch : public IndigoObject
void renumber(Array<int>& map, Array<int>& inv_map);
void copy(IndigoDecompositionMatch& other);
void removeRsitesFromMaps(Graph& query_graph);
void copyScafAutoMaps(ObjList<Array<int>>& autoMaps);
void copyScafAutoMaps(const std::vector<Array<int>>& autoMaps);
void completeScaffold();

Molecule mol_out;
Expand Down
2 changes: 1 addition & 1 deletion core/indigo-core/common/base_cpp/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace indigo
{
}

Array(Array&& other) : _reserved(other._reserved), _length(other._length), _array(other._array)
Array(Array&& other) noexcept : _reserved(other._reserved), _length(other._length), _array(other._array)
{
other._array = nullptr;
other._length = 0;
Expand Down

0 comments on commit 5eac32b

Please sign in to comment.