Skip to content

Commit

Permalink
Fixes issue #2
Browse files Browse the repository at this point in the history
  • Loading branch information
cartoonist committed Jun 10, 2017
1 parent 875b02b commit 94c6975
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
25 changes: 17 additions & 8 deletions src/vargraph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,7 @@ namespace grem
template < >
bool at_end ( GraphIter < VarGraph, Backtracker<> > &it )
{
return it.visiting_buffer.empty() &&
!it.vargraph_ptr->has_fwd_edge(*it);
return it.state.end;
} /* ----- end of template function at_end ----- */

template < >
Expand All @@ -349,7 +348,9 @@ namespace grem

begin_itr.vargraph_ptr = &g;
begin_itr.itr_value = start_node_id;
begin_itr.visited.push_back(0); // Next node ID from current node. 0 = nothing buffered.
begin_itr.state.buffer = 0;
begin_itr.state.end = false;
begin_itr.state.start = start_node_id;

return begin_itr;
} /* ----- end of template function begin ----- */
Expand All @@ -360,9 +361,9 @@ namespace grem
GraphIter < VarGraph, Backtracker <> > &
GraphIter < VarGraph, Backtracker <> >::operator++ ( )
{
if ( this->visited[0] != 0 ) { // Any node buffered?
this->itr_value = this->visited[0]; // Use it.
this->visited[0] = 0; // Clear up buffer.
if ( this->state.buffer != 0 ) { // Any node buffered?
this->itr_value = this->state.buffer; // Use it.
this->state.buffer = 0; // Clear up buffer.
}
else { // else
Backtracker<>::Value cnode_id = this->itr_value;
Expand All @@ -375,6 +376,9 @@ namespace grem
this->visiting_buffer.push_back ( std::make_pair(cnode_id, edges[i]->to()) );
}
}
else {
this->state.end = true;
}
}

return *this;
Expand All @@ -384,18 +388,23 @@ namespace grem
GraphIter < VarGraph, Backtracker <> > &
GraphIter < VarGraph, Backtracker <> >::operator-- ( )
{
if ( this->visited[0] != 0 ) { // Any node buffered?
if ( this->state.buffer != 0 ) { // Any node buffered?
while ( // Remove all buffered branches of the current node.
!this->visiting_buffer.empty() &&
this->visiting_buffer.back().first == this->itr_value ) {
this->visiting_buffer.pop_back();
}
this->state.buffer = 0;
}

if ( !this->visiting_buffer.empty() ) { // Go back in buffer.
this->itr_value = this->visiting_buffer.back().first;
this->visited[0] = this->visiting_buffer.back().second;
this->state.buffer = this->visiting_buffer.back().second;
this->visiting_buffer.pop_back();
this->state.end = false; // Reset at-end flag.
}
else {
this->state.end = true; // Set at-end flag.
}

return *this;
Expand Down
7 changes: 6 additions & 1 deletion src/vargraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,12 @@ namespace grem
typedef VarGraph::NodeID Value;
typedef Value Level;
typedef std::deque< std::pair< Value, Value > > TContainer;
typedef std::vector< Value > TSet; /**< @brief Used as a buffer. */
typedef void* TSet;
typedef struct {
VarGraph::NodeID start; /**< @brief Start node ID. */
VarGraph::NodeID buffer; /**< @brief Buffer node ID. 0=nothing */
bool end; /**< @brief End flag. */
} TState;
}; /* ---------- end of struct Backtracker ---------- */

template < typename TSpec = void >
Expand Down

0 comments on commit 94c6975

Please sign in to comment.