diff --git a/code/rust/src/graphs/tarjan.rs b/code/rust/src/graphs/tarjan.rs index dcba891..b2e417b 100644 --- a/code/rust/src/graphs/tarjan.rs +++ b/code/rust/src/graphs/tarjan.rs @@ -48,7 +48,7 @@ fn tarjan_visit(gph: &Graph, &u: &T, ctx: &mut Context) { // Place node on stack + initialise visit-index + component-index. ctx.max_index += 1; - ctx.push(&u); + ctx.stack_push(&u); ctx.set_root(&u, ctx.max_index); ctx.set_index(&u, ctx.max_index); ctx.set_state(&u, State::PENDING); @@ -63,18 +63,19 @@ fn tarjan_visit(gph: &Graph, &u: &T, ctx: &mut Context) tarjan_visit(gph, &v, ctx); } // 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_state(&u, State::FINISHED); 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) { let mut component: Vec = Vec::new(); loop { - let v = ctx.top(); - ctx.pop(); + let v = ctx.stack_top(); + ctx.stack_pop(); component.push(v.clone()); if u == v { break; } } @@ -119,15 +120,15 @@ impl Context }; } - fn push(self: &mut Self, u: &T) { + fn stack_push(self: &mut Self, u: &T) { self.stack.push(u.clone()); } - fn top(self: &mut Self) -> T { + fn stack_top(self: &mut Self) -> T { return self.stack.top(); } - fn pop(self: &mut Self) -> T { + fn stack_pop(self: &mut Self) -> T { return self.stack.pop(); } @@ -153,6 +154,10 @@ impl Context 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 { return *self.infos.get(u).unwrap(); } diff --git a/code/rust/tests/test_graphs/test_tarjan.rs b/code/rust/tests/test_graphs/test_tarjan.rs index 81cbbaf..ec26e7d 100644 --- a/code/rust/tests/test_graphs/test_tarjan.rs +++ b/code/rust/tests/test_graphs/test_tarjan.rs @@ -55,7 +55,7 @@ fn fixture_graph3() -> graph::Graph { } // ---------------------------------------------------------------- -// Test Graph +// Test Tarjan-Algorithm // ---------------------------------------------------------------- #[rstest] diff --git a/code/rust/tests/test_stacks/test_stack.rs b/code/rust/tests/test_stacks/test_stack.rs index de6ef42..021d9cc 100644 --- a/code/rust/tests/test_stacks/test_stack.rs +++ b/code/rust/tests/test_stacks/test_stack.rs @@ -7,10 +7,6 @@ extern crate closure; use ads2::stacks::stack; -// ---------------------------------------------------------------- -// Test regex -// ---------------------------------------------------------------- - // ---------------------------------------------------------------- // Test stack // ----------------------------------------------------------------