Skip to content

Commit

Permalink
Use binary search to find outrank
Browse files Browse the repository at this point in the history
  • Loading branch information
jltsiren committed Apr 20, 2018
1 parent b2359d8 commit 9c3e389
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
6 changes: 3 additions & 3 deletions dynamic_gbwt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ updateRecords(DynamicGBWT& gbwt, std::vector<Sequence>& seqs, size_type iteratio
size_type insert_count = 0;
while(i < seqs.size() && seqs[i].curr == curr)
{
rank_type outrank = current.edgeTo(seqs[i].next);
rank_type outrank = current.edgeToLinear(seqs[i].next);
if(outrank >= current.outdegree()) // Add edge (curr, next) if it does not exist.
{
current.outgoing.push_back(edge_type(seqs[i].next, 0));
Expand Down Expand Up @@ -445,15 +445,15 @@ rebuildOffsets(DynamicGBWT& gbwt, std::vector<Sequence>& seqs)
for(edge_type inedge : gbwt.record(next).incoming)
{
DynamicRecord& predecessor = gbwt.record(inedge.first);
predecessor.offset(predecessor.edgeTo(next)) = offset;
predecessor.offset(predecessor.edgeToLinear(next)) = offset;
offset += inedge.second;
}
}

for(Sequence& seq : seqs)
{
const DynamicRecord& current = gbwt.record(seq.curr);
seq.offset += current.offset(current.edgeTo(seq.next));
seq.offset += current.offset(current.edgeToLinear(seq.next));
}
}

Expand Down
3 changes: 3 additions & 0 deletions include/gbwt/support.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ struct DynamicRecord
// Maps successor nodes to outranks.
rank_type edgeTo(node_type to) const;

// This version works when the edges are not sorted.
rank_type edgeToLinear(node_type to) const;

// These assume that 'outrank' is a valid outgoing edge.
node_type successor(rank_type outrank) const { return this->outgoing[outrank].first; }
#ifdef GBWT_SAVE_MEMORY
Expand Down
22 changes: 20 additions & 2 deletions support.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,20 @@ DynamicRecord::hasEdge(node_type to) const

rank_type
DynamicRecord::edgeTo(node_type to) const
{
rank_type low = 0, high = this->outdegree();
while(low < high)
{
rank_type mid = low + (high - low) / 2;
if(this->successor(mid) == to) { return mid; }
if(this->successor(mid) > to) { high = mid; }
else { low = mid + 1; }
}
return this->outdegree();
}

rank_type
DynamicRecord::edgeToLinear(node_type to) const
{
for(rank_type outrank = 0; outrank < this->outdegree(); outrank++)
{
Expand Down Expand Up @@ -401,9 +415,13 @@ CompressedRecord::hasEdge(node_type to) const
rank_type
CompressedRecord::edgeTo(node_type to) const
{
for(rank_type outrank = 0; outrank < this->outdegree(); outrank++)
rank_type low = 0, high = this->outdegree();
while(low < high)
{
if(this->successor(outrank) == to) { return outrank; }
rank_type mid = low + (high - low) / 2;
if(this->successor(mid) == to) { return mid; }
if(this->successor(mid) > to) { high = mid; }
else { low = mid + 1; }
}
return this->outdegree();
}
Expand Down

0 comments on commit 9c3e389

Please sign in to comment.