Compare commits

..

3 Commits

4 changed files with 43 additions and 22 deletions

View File

@ -25,11 +25,22 @@ pub fn construct_regex(pattern: &str) -> Regex {
// METHODS values
// ----------------------------------------------------------------
pub fn min<T>(x: T, y: T) -> T
where T: PartialOrd
{
return if y < x { y } else { x };
#[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
}
}
};
}
#[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::core::utils;
use crate::value_min;
use crate::core::log::log_debug;
use crate::stacks::stack::Stack;
use crate::graphs::graph::Graph;
@ -44,10 +44,15 @@ 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
{
if !(ctx.get_state(v) == State::UNTOUCHED) {
return;
}
// do nothing, if v already visited
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);
@ -57,13 +62,12 @@ 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);
// 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);
/****
* 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)));
}
}
ctx.set_state(v, State::FINISHED);
@ -169,8 +173,8 @@ impl<T> Context<T>
}
fn log_info(self: &Self, u: &T) {
if !self.debug { return; }
let info = self.get_info(&u);
if !self.debug { return; }
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 {
where T: Clone + ToString + Eq {
/// Creates new typed instance of stack.
pub fn new() -> Stack<T> {
return Stack {
@ -53,6 +53,11 @@ where T: Clone + ToString {
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,6 +3,7 @@
// ----------------------------------------------------------------
use ads2::core::utils;
use ads2::value_min;
// ----------------------------------------------------------------
// Test regex
@ -21,11 +22,11 @@ fn test_regex() {
#[test]
fn test_min() {
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);
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);
}
// ----------------------------------------------------------------