Skip to content

Commit 1e4f395

Browse files
committed
Optimization: avoid creating duplicate vector of local refs
1 parent bec0172 commit 1e4f395

File tree

7 files changed

+141
-98
lines changed

7 files changed

+141
-98
lines changed

alignment/seqregionsmem.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ auto cmaple::SeqRegionsWithCount::descreaseCount() -> void
2929
if (!count_)
3030
{
3131
lower_regions_ = nullptr;
32+
33+
if (local_ref_vec_.size())
34+
{
35+
std::vector<cmaple::Index>().swap(local_ref_vec_);
36+
}
3237
}
3338
}
3439

@@ -43,3 +48,22 @@ auto cmaple::SeqRegionsWithCount::setSeqRegions(
4348
{
4449
lower_regions_ = std::move(lower_regions);
4550
}
51+
52+
53+
auto cmaple::SeqRegionsWithCount::getLocalRefVec()
54+
-> std::vector<cmaple::Index>&
55+
{
56+
return local_ref_vec_;
57+
}
58+
59+
auto cmaple::SeqRegionsWithCount::setLocalRefVec(
60+
const std::vector<cmaple::Index>& local_ref_vec) -> void
61+
{
62+
local_ref_vec_ = local_ref_vec;
63+
}
64+
65+
auto cmaple::SeqRegionsWithCount::addLocalRefIndex(
66+
const cmaple::Index& local_ref_index) -> void
67+
{
68+
local_ref_vec_.push_back(local_ref_index);
69+
}

alignment/seqregionsmem.h

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ namespace cmaple {
2222
*/
2323
NumSeqsType count_ = 0;
2424

25+
/**
26+
The vector of local refs were integrated to build
27+
the current lower regions from its original regions
28+
*/
29+
std::vector<cmaple::Index> local_ref_vec_;
30+
2531
public:
2632

2733
/**
@@ -59,6 +65,22 @@ namespace cmaple {
5965
Set lower_regions_
6066
*/
6167
auto setSeqRegions(std::unique_ptr<SeqRegions>&& lower_regions) -> void;
68+
69+
/**
70+
Get local_ref_vec_
71+
*/
72+
auto getLocalRefVec() -> std::vector<cmaple::Index>&;
73+
74+
/**
75+
Set local_ref_vec_
76+
*/
77+
auto setLocalRefVec(
78+
const std::vector<cmaple::Index>& local_ref_vec) -> void;
79+
80+
/**
81+
Add a ref index into local_ref_vec_
82+
*/
83+
auto addLocalRefIndex(const cmaple::Index& local_ref_index) -> void;
6284
};
6385

6486
/** A dedicated memory class to store a vector of SeqRegionsWithCounts
@@ -83,6 +105,19 @@ namespace cmaple {
83105
std::unique_ptr<SeqRegions>& mutations,
84106
const bool inverse = false)
85107
-> SeqRegionsWithCount*;
108+
109+
/**
110+
Same as the above but with a local ref index added
111+
to keep track of the list of local refs are being used
112+
*/
113+
template <const cmaple::StateType num_states>
114+
auto getMutIntegratedSeqRegions(
115+
SeqRegionsWithCount* const in_seqregions,
116+
const Alignment* aln,
117+
const cmaple::Index& local_ref_index,
118+
std::unique_ptr<SeqRegions>& mutations,
119+
const bool inverse = false)
120+
-> SeqRegionsWithCount*;
86121
};
87122

88123
template <const cmaple::StateType num_states>
@@ -132,7 +167,37 @@ auto cmaple::SeqRegionsMem::getMutIntegratedSeqRegions(
132167
out_seqregions->setSeqRegions(std::move(mut_integrated_seqregions));
133168
out_seqregions->increaseCount();
134169

170+
// inherit the local_ref_vec
171+
if (in_seqregions->getLocalRefVec().size())
172+
{
173+
out_seqregions->setLocalRefVec(in_seqregions->getLocalRefVec());
174+
}
175+
135176
return out_seqregions;
136177
}
178+
179+
template <const cmaple::StateType num_states>
180+
auto cmaple::SeqRegionsMem::getMutIntegratedSeqRegions(
181+
SeqRegionsWithCount* const in_seqregions,
182+
const Alignment* aln,
183+
const cmaple::Index& local_ref_index,
184+
std::unique_ptr<SeqRegions>& mutations,
185+
const bool inverse)
186+
-> SeqRegionsWithCount*
187+
{
188+
// first call the simple version of this function first
189+
SeqRegionsWithCount* out_seqregion_ptr =
190+
getMutIntegratedSeqRegions<num_states>(
191+
in_seqregions, aln, mutations, inverse);
192+
193+
// if we integrate the mutations/local ref, record it
194+
if (mutations)
195+
{
196+
assert(local_ref_index.getMiniIndex() != UNDEFINED);
197+
out_seqregion_ptr->addLocalRefIndex(local_ref_index);
198+
}
199+
200+
return out_seqregion_ptr;
201+
}
137202

138203
} // namespace cmaple

tree/traversingnode.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,9 @@ auto cmaple::TraversingExtNodev2::descreaseCount()
4343
{
4444
sample_regions_w_count_->descreaseCount();
4545
}
46+
47+
auto cmaple::TraversingExtNodev2::getLocalRefList()
48+
-> std::vector<cmaple::Index>&
49+
{
50+
return sample_regions_w_count_->getLocalRefVec();
51+
}

tree/traversingnode.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ namespace cmaple
9292
Decrease the use count_ by 1
9393
*/
9494
auto descreaseCount() -> void;
95+
96+
/**
97+
Get local_ref_vec_
98+
*/
99+
auto getLocalRefList() -> std::vector<cmaple::Index>&;
95100
};
96101
}
97102

0 commit comments

Comments
 (0)