Compare commits
No commits in common. "b30de02973ec6876d0e009f1909a13a49ea91250" and "37f05d1ff0bbca6b83580fe6de2813c3af397bfb" have entirely different histories.
b30de02973
...
37f05d1ff0
@ -25,22 +25,11 @@ pub fn construct_regex(pattern: &str) -> Regex {
|
|||||||
// METHODS values
|
// METHODS values
|
||||||
// ----------------------------------------------------------------
|
// ----------------------------------------------------------------
|
||||||
|
|
||||||
#[macro_export]
|
pub fn min<T>(x: T, y: T) -> T
|
||||||
macro_rules! value_min{
|
where T: PartialOrd
|
||||||
($value:expr $(,)?)=>{ $value };
|
|
||||||
($value:expr $(, $values:expr)+ $(,)?)=>{
|
|
||||||
{
|
{
|
||||||
let x = $crate::value_min!($( $values )+);
|
return if y < x { y } else { x };
|
||||||
if $value < x {
|
|
||||||
$value
|
|
||||||
} else {
|
|
||||||
x
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
#[allow(unused_imports)]
|
|
||||||
pub(crate) use value_min;
|
|
||||||
|
|
||||||
// ----------------------------------------------------------------
|
// ----------------------------------------------------------------
|
||||||
// METHODS Vectors
|
// METHODS Vectors
|
||||||
|
@ -6,7 +6,7 @@ use std::fmt::Display;
|
|||||||
use std::hash::Hash;
|
use std::hash::Hash;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use crate::value_min;
|
use crate::core::utils;
|
||||||
use crate::core::log::log_debug;
|
use crate::core::log::log_debug;
|
||||||
use crate::stacks::stack::Stack;
|
use crate::stacks::stack::Stack;
|
||||||
use crate::graphs::graph::Graph;
|
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>)
|
fn tarjan_visit<T>(gph: &Graph<T>, v: &T, ctx: &mut Context<T>)
|
||||||
where T: Eq + Hash + Clone + Copy + Display
|
where T: Eq + Hash + Clone + Copy + Display
|
||||||
{
|
{
|
||||||
// do nothing, if v already visited
|
if !(ctx.get_state(v) == State::UNTOUCHED) {
|
||||||
if !(ctx.get_state(v) == State::UNTOUCHED) { return; }
|
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.max_index += 1;
|
||||||
ctx.push(v);
|
ctx.push(v);
|
||||||
ctx.set_root(v, ctx.max_index);
|
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:
|
// depth first search:
|
||||||
for u in gph.successors(&v) {
|
for u in gph.successors(&v) {
|
||||||
tarjan_visit(gph, &u, ctx);
|
tarjan_visit(gph, &u, ctx);
|
||||||
/****
|
// remains relevant for v, provided u still in Stack:
|
||||||
* u is in the same component as v, if and only if u is still on the stack.
|
if ctx.stack.elements.contains(&u) {
|
||||||
* In this case, update the associated component-index of v:
|
let root = utils::min(
|
||||||
****/
|
ctx.get_root(&u),
|
||||||
if ctx.stack.contains(&u) {
|
ctx.get_root(v)
|
||||||
ctx.set_root(v, value_min!(ctx.get_root(v), ctx.get_root(&u)));
|
);
|
||||||
|
ctx.set_root(v, root);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ctx.set_state(v, State::FINISHED);
|
ctx.set_state(v, State::FINISHED);
|
||||||
@ -173,8 +169,8 @@ impl<T> Context<T>
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn log_info(self: &Self, u: &T) {
|
fn log_info(self: &Self, u: &T) {
|
||||||
let info = self.get_info(&u);
|
|
||||||
if !self.debug { return; }
|
if !self.debug { return; }
|
||||||
|
let info = self.get_info(&u);
|
||||||
log_debug!("node, index, component: ({}, {}, {})", info.node, info.index, info.root);
|
log_debug!("node, index, component: ({}, {}, {})", info.node, info.index, info.root);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,7 @@ pub struct Stack<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Stack<T>
|
impl<T> Stack<T>
|
||||||
where T: Clone + ToString + Eq {
|
where T: Clone + ToString {
|
||||||
/// Creates new typed instance of stack.
|
/// Creates new typed instance of stack.
|
||||||
pub fn new() -> Stack<T> {
|
pub fn new() -> Stack<T> {
|
||||||
return Stack {
|
return Stack {
|
||||||
@ -53,11 +53,6 @@ where T: Clone + ToString + Eq {
|
|||||||
return element;
|
return element;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// checks if element in stack:
|
|
||||||
pub fn contains(self: &Self, element: &T) -> bool {
|
|
||||||
return self.elements.contains(element);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// convert entries to strings
|
/// convert entries to strings
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
fn repr(self: &Self) -> Vec<String> {
|
fn repr(self: &Self) -> Vec<String> {
|
||||||
|
@ -3,7 +3,6 @@
|
|||||||
// ----------------------------------------------------------------
|
// ----------------------------------------------------------------
|
||||||
|
|
||||||
use ads2::core::utils;
|
use ads2::core::utils;
|
||||||
use ads2::value_min;
|
|
||||||
|
|
||||||
// ----------------------------------------------------------------
|
// ----------------------------------------------------------------
|
||||||
// Test regex
|
// Test regex
|
||||||
@ -22,11 +21,11 @@ fn test_regex() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_min() {
|
fn test_min() {
|
||||||
assert_eq!(value_min!("beth", "alef"), "alef");
|
assert_eq!(utils::min("beth", "alef"), "alef");
|
||||||
assert_eq!(value_min!("alef", "beth"), "alef");
|
assert_eq!(utils::min("alef", "beth"), "alef");
|
||||||
assert_eq!(value_min!("alef", "ale"), "ale");
|
assert_eq!(utils::min("alef", "ale"), "ale");
|
||||||
assert_eq!(value_min!(320, 24), 24);
|
assert_eq!(utils::min(320, 24), 24);
|
||||||
assert_eq!(value_min!(0.03, 0.2), 0.03);
|
assert_eq!(utils::min(0.03, 0.2), 0.03);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------
|
// ----------------------------------------------------------------
|
||||||
|
Loading…
x
Reference in New Issue
Block a user