Compare commits

..

No commits in common. "b30de02973ec6876d0e009f1909a13a49ea91250" and "37f05d1ff0bbca6b83580fe6de2813c3af397bfb" have entirely different histories.

4 changed files with 22 additions and 43 deletions

View File

@ -25,22 +25,11 @@ pub fn construct_regex(pattern: &str) -> Regex {
// METHODS values
// ----------------------------------------------------------------
#[macro_export]
macro_rules! value_min{
($value:expr $(,)?)=>{ $value };
($value:expr $(, $values:expr)+ $(,)?)=>{
{
let x = $crate::value_min!($( $values )+);
if $value < x {
$value
} else {
x
}
}
};
pub fn min<T>(x: T, y: T) -> T
where T: PartialOrd
{
return if y < x { y } else { x };
}
#[allow(unused_imports)]
pub(crate) use value_min;
// ----------------------------------------------------------------
// METHODS Vectors

View File

@ -6,7 +6,7 @@ use std::fmt::Display;
use std::hash::Hash;
use std::collections::HashMap;
use crate::value_min;
use crate::core::utils;
use crate::core::log::log_debug;
use crate::stacks::stack::Stack;
use crate::graphs::graph::Graph;
@ -44,15 +44,10 @@ pub fn tarjan_algorithm<T>(gph: &Graph<T>, debug: bool) -> Vec<Vec<T>>
fn tarjan_visit<T>(gph: &Graph<T>, v: &T, ctx: &mut Context<T>)
where T: Eq + Hash + Clone + Copy + Display
{
// do nothing, if v already visited
if !(ctx.get_state(v) == State::UNTOUCHED) { return; }
if !(ctx.get_state(v) == State::UNTOUCHED) {
return;
}
/****************
* First visit of v.
* - Place v on stack.
* - Initialise visit-index + component-index.
* - After each rekursiv call, only those all
****************/
ctx.max_index += 1;
ctx.push(v);
ctx.set_root(v, ctx.max_index);
@ -62,12 +57,13 @@ fn tarjan_visit<T>(gph: &Graph<T>, v: &T, ctx: &mut Context<T>)
// depth first search:
for u in gph.successors(&v) {
tarjan_visit(gph, &u, ctx);
/****
* u is in the same component as v, if and only if u is still on the stack.
* In this case, update the associated component-index of v:
****/
if ctx.stack.contains(&u) {
ctx.set_root(v, value_min!(ctx.get_root(v), ctx.get_root(&u)));
// remains relevant for v, provided u still in Stack:
if ctx.stack.elements.contains(&u) {
let root = utils::min(
ctx.get_root(&u),
ctx.get_root(v)
);
ctx.set_root(v, root);
}
}
ctx.set_state(v, State::FINISHED);
@ -173,8 +169,8 @@ impl<T> Context<T>
}
fn log_info(self: &Self, u: &T) {
let info = self.get_info(&u);
if !self.debug { return; }
let info = self.get_info(&u);
log_debug!("node, index, component: ({}, {}, {})", info.node, info.index, info.root);
}
}

View File

@ -18,7 +18,7 @@ pub struct Stack<T> {
}
impl<T> Stack<T>
where T: Clone + ToString + Eq {
where T: Clone + ToString {
/// Creates new typed instance of stack.
pub fn new() -> Stack<T> {
return Stack {
@ -53,11 +53,6 @@ where T: Clone + ToString + Eq {
return element;
}
/// checks if element in stack:
pub fn contains(self: &Self, element: &T) -> bool {
return self.elements.contains(element);
}
/// convert entries to strings
#[allow(dead_code)]
fn repr(self: &Self) -> Vec<String> {

View File

@ -3,7 +3,6 @@
// ----------------------------------------------------------------
use ads2::core::utils;
use ads2::value_min;
// ----------------------------------------------------------------
// Test regex
@ -22,11 +21,11 @@ fn test_regex() {
#[test]
fn test_min() {
assert_eq!(value_min!("beth", "alef"), "alef");
assert_eq!(value_min!("alef", "beth"), "alef");
assert_eq!(value_min!("alef", "ale"), "ale");
assert_eq!(value_min!(320, 24), 24);
assert_eq!(value_min!(0.03, 0.2), 0.03);
assert_eq!(utils::min("beth", "alef"), "alef");
assert_eq!(utils::min("alef", "beth"), "alef");
assert_eq!(utils::min("alef", "ale"), "ale");
assert_eq!(utils::min(320, 24), 24);
assert_eq!(utils::min(0.03, 0.2), 0.03);
}
// ----------------------------------------------------------------