master > master: code-rs - minor
This commit is contained in:
parent
dc831a91c7
commit
5be654e5db
@ -48,7 +48,7 @@ fn tarjan_visit<T>(gph: &Graph<T>, &u: &T, ctx: &mut Context<T>)
|
|||||||
{
|
{
|
||||||
// Place node on stack + initialise visit-index + component-index.
|
// Place node on stack + initialise visit-index + component-index.
|
||||||
ctx.max_index += 1;
|
ctx.max_index += 1;
|
||||||
ctx.push(&u);
|
ctx.stack_push(&u);
|
||||||
ctx.set_root(&u, ctx.max_index);
|
ctx.set_root(&u, ctx.max_index);
|
||||||
ctx.set_index(&u, ctx.max_index);
|
ctx.set_index(&u, ctx.max_index);
|
||||||
ctx.set_state(&u, State::PENDING);
|
ctx.set_state(&u, State::PENDING);
|
||||||
@ -63,18 +63,19 @@ fn tarjan_visit<T>(gph: &Graph<T>, &u: &T, ctx: &mut Context<T>)
|
|||||||
tarjan_visit(gph, &v, ctx);
|
tarjan_visit(gph, &v, ctx);
|
||||||
}
|
}
|
||||||
// Update associated component-index of parent node, if in same component as child:
|
// Update associated component-index of parent node, if in same component as child:
|
||||||
if ctx.stack.contains(&v) {
|
if ctx.stack_contains(&v) {
|
||||||
ctx.set_root(&u, value_min!(ctx.get_root(&u), ctx.get_root(&v)));
|
ctx.set_root(&u, value_min!(ctx.get_root(&u), ctx.get_root(&v)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ctx.set_state(&u, State::FINISHED);
|
ctx.set_state(&u, State::FINISHED);
|
||||||
ctx.log_info(&u);
|
ctx.log_info(&u);
|
||||||
|
|
||||||
|
// If at root of component pop everything from stack up to root and add component:
|
||||||
if ctx.get_index(&u) == ctx.get_root(&u) {
|
if ctx.get_index(&u) == ctx.get_root(&u) {
|
||||||
let mut component: Vec<T> = Vec::new();
|
let mut component: Vec<T> = Vec::new();
|
||||||
loop {
|
loop {
|
||||||
let v = ctx.top();
|
let v = ctx.stack_top();
|
||||||
ctx.pop();
|
ctx.stack_pop();
|
||||||
component.push(v.clone());
|
component.push(v.clone());
|
||||||
if u == v { break; }
|
if u == v { break; }
|
||||||
}
|
}
|
||||||
@ -119,15 +120,15 @@ impl<T> Context<T>
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn push(self: &mut Self, u: &T) {
|
fn stack_push(self: &mut Self, u: &T) {
|
||||||
self.stack.push(u.clone());
|
self.stack.push(u.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn top(self: &mut Self) -> T {
|
fn stack_top(self: &mut Self) -> T {
|
||||||
return self.stack.top();
|
return self.stack.top();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn pop(self: &mut Self) -> T {
|
fn stack_pop(self: &mut Self) -> T {
|
||||||
return self.stack.pop();
|
return self.stack.pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -153,6 +154,10 @@ impl<T> Context<T>
|
|||||||
self.update_infos(u, info);
|
self.update_infos(u, info);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn stack_contains(self: &Self, u: &T) -> bool {
|
||||||
|
return self.stack.contains(u);
|
||||||
|
}
|
||||||
|
|
||||||
fn get_info(self: &Self, u: &T) -> NodeInformation<T> {
|
fn get_info(self: &Self, u: &T) -> NodeInformation<T> {
|
||||||
return *self.infos.get(u).unwrap();
|
return *self.infos.get(u).unwrap();
|
||||||
}
|
}
|
||||||
|
@ -55,7 +55,7 @@ fn fixture_graph3() -> graph::Graph<i32> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------
|
// ----------------------------------------------------------------
|
||||||
// Test Graph
|
// Test Tarjan-Algorithm
|
||||||
// ----------------------------------------------------------------
|
// ----------------------------------------------------------------
|
||||||
|
|
||||||
#[rstest]
|
#[rstest]
|
||||||
|
@ -7,10 +7,6 @@ extern crate closure;
|
|||||||
|
|
||||||
use ads2::stacks::stack;
|
use ads2::stacks::stack;
|
||||||
|
|
||||||
// ----------------------------------------------------------------
|
|
||||||
// Test regex
|
|
||||||
// ----------------------------------------------------------------
|
|
||||||
|
|
||||||
// ----------------------------------------------------------------
|
// ----------------------------------------------------------------
|
||||||
// Test stack
|
// Test stack
|
||||||
// ----------------------------------------------------------------
|
// ----------------------------------------------------------------
|
||||||
|
Loading…
x
Reference in New Issue
Block a user