master > master: code-rust - init
This commit is contained in:
		
							parent
							
								
									3338b255a1
								
							
						
					
					
						commit
						b85574cda4
					
				@ -11,15 +11,45 @@ use self::regex::Regex;
 | 
			
		||||
// ----------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
/// Constructs RegEx and panics if error.
 | 
			
		||||
#[allow(dead_code)]
 | 
			
		||||
pub fn construct_regex(pattern: &str) -> Regex {
 | 
			
		||||
    return Regex::new(pattern)
 | 
			
		||||
        .expect("Invalid regex construction!");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ----------------------------------------------------------------
 | 
			
		||||
// METHOD hello world
 | 
			
		||||
// METHODS values
 | 
			
		||||
// ----------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
pub fn greet() {
 | 
			
		||||
    println!("Hello world!");
 | 
			
		||||
pub fn min<T>(x: T, y: T) -> T
 | 
			
		||||
    where T: PartialOrd
 | 
			
		||||
{
 | 
			
		||||
    return if y < x { y } else { x };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ----------------------------------------------------------------
 | 
			
		||||
// METHODS Vectors
 | 
			
		||||
// ----------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
pub fn restrict<T>(x: &mut Vec<T>, i: usize, j: usize) -> Vec<T>
 | 
			
		||||
    where T: Clone
 | 
			
		||||
{
 | 
			
		||||
    return x[i..j].iter()
 | 
			
		||||
        .cloned()
 | 
			
		||||
        .collect::<Vec<T>>();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn remove_last<T>(x: &mut Vec<T>) -> Vec<T>
 | 
			
		||||
    where T: Clone
 | 
			
		||||
{
 | 
			
		||||
    let n = x.len();
 | 
			
		||||
    return restrict::<T>(x, 0, n-1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[allow(dead_code)]
 | 
			
		||||
pub fn remove_first<T>(x: &mut Vec<T>) -> Vec<T>
 | 
			
		||||
    where T: Clone
 | 
			
		||||
{
 | 
			
		||||
    let n = x.len();
 | 
			
		||||
    return restrict::<T>(x, 0, n);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										72
									
								
								code/rust/src/graphs/graph.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										72
									
								
								code/rust/src/graphs/graph.rs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,72 @@
 | 
			
		||||
// ----------------------------------------------------------------
 | 
			
		||||
// IMPORTS
 | 
			
		||||
// ----------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 | 
			
		||||
// CLASS Graph
 | 
			
		||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 A data structure for graphs
 | 
			
		||||
 */
 | 
			
		||||
pub struct Graph<T> {
 | 
			
		||||
    pub nodes: Vec<T>,
 | 
			
		||||
    pub edges: Vec<(T,T)>,
 | 
			
		||||
    #[allow(dead_code)]
 | 
			
		||||
    string: String,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl<T> Graph<T>
 | 
			
		||||
where T: PartialEq + Clone + ToString {
 | 
			
		||||
    /// Creates new typed instance of stack.
 | 
			
		||||
    pub fn new(nodes: Vec<T>, edges: Vec<(T,T)>) -> Graph<T> {
 | 
			
		||||
        return Graph {
 | 
			
		||||
            nodes: nodes,
 | 
			
		||||
            edges: edges,
 | 
			
		||||
            string: String::from(""),
 | 
			
		||||
        };
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// @returns number of elements in stack.
 | 
			
		||||
    #[allow(dead_code)]
 | 
			
		||||
    pub fn len(self: &Self) -> usize {
 | 
			
		||||
        return self.nodes.len();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// @returns graph induced by subset of nodes
 | 
			
		||||
    #[allow(dead_code)]
 | 
			
		||||
    pub fn subgraph(self: Self, nodes: Vec<T>) -> Graph<T> {
 | 
			
		||||
        let nodes_ = self.nodes
 | 
			
		||||
            .iter()
 | 
			
		||||
            .filter(|&u| (nodes.contains(u)))
 | 
			
		||||
            .map(|u| u.clone())
 | 
			
		||||
            .collect::<Vec<T>>();
 | 
			
		||||
        let edges_ = self.edges
 | 
			
		||||
            .iter()
 | 
			
		||||
            .filter(|&(u, v)| (nodes.contains(u) && nodes.contains(v)))
 | 
			
		||||
            .map(|(u, v)| (u.clone(), v.clone()))
 | 
			
		||||
            .collect::<Vec<(T,T)>>();
 | 
			
		||||
        return Graph::new(nodes_, edges_);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// @returns all successor nodes of a particular node
 | 
			
		||||
    pub fn successors(self: &Self, u: &T) -> Vec<T> {
 | 
			
		||||
        return self.edges
 | 
			
		||||
            .iter()
 | 
			
		||||
            .filter(|&(u_, _)| (*u_ == *u))
 | 
			
		||||
            .map(|(_, v)| (v.clone()))
 | 
			
		||||
            .collect::<Vec<T>>();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// @returns all predecessors nodes of a particular node
 | 
			
		||||
    #[allow(dead_code)]
 | 
			
		||||
    pub fn predecessors(self: &Self, v: &T) -> Vec<T> {
 | 
			
		||||
        return self.edges
 | 
			
		||||
            .iter()
 | 
			
		||||
            .filter(|&(_, v_)| (*v_ == *v))
 | 
			
		||||
            .map(|(u, _)| (u.clone()))
 | 
			
		||||
            .collect::<Vec<T>>();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										2
									
								
								code/rust/src/graphs/mod.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								code/rust/src/graphs/mod.rs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,2 @@
 | 
			
		||||
pub mod graph;
 | 
			
		||||
pub mod tarjan;
 | 
			
		||||
@ -1 +1,3 @@
 | 
			
		||||
pub mod core;
 | 
			
		||||
pub mod stacks;
 | 
			
		||||
pub mod graphs;
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										1
									
								
								code/rust/src/stacks/mod.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								code/rust/src/stacks/mod.rs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1 @@
 | 
			
		||||
pub mod stack;
 | 
			
		||||
							
								
								
									
										77
									
								
								code/rust/src/stacks/stack.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										77
									
								
								code/rust/src/stacks/stack.rs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,77 @@
 | 
			
		||||
// ----------------------------------------------------------------
 | 
			
		||||
// IMPORTS
 | 
			
		||||
// ----------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
use crate::core::utils;
 | 
			
		||||
 | 
			
		||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 | 
			
		||||
// CLASS Stack
 | 
			
		||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 A data structure for stacks
 | 
			
		||||
 */
 | 
			
		||||
#[derive(Clone)]
 | 
			
		||||
pub struct Stack<T> {
 | 
			
		||||
    pub elements: Vec<T>,
 | 
			
		||||
    string: String,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl<T> Stack<T>
 | 
			
		||||
where T: Clone + ToString {
 | 
			
		||||
    /// Creates new typed instance of stack.
 | 
			
		||||
    pub fn new() -> Stack<T> {
 | 
			
		||||
        return Stack {
 | 
			
		||||
            elements: Vec::new(),
 | 
			
		||||
            string: String::from(""),
 | 
			
		||||
        };
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// @returns number of elements in stack.
 | 
			
		||||
    pub fn len(self: &Self) -> usize {
 | 
			
		||||
        return self.elements.len();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// adds element to stack
 | 
			
		||||
    pub fn push(self: &mut Self, x: T) {
 | 
			
		||||
        self.elements.push(x.clone());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// obtains top element of stack without removing it
 | 
			
		||||
    pub fn top(self: &mut Self) -> T {
 | 
			
		||||
        let n = self.len();
 | 
			
		||||
        match self.elements.get(n-1) {
 | 
			
		||||
            Some(element) => { return element.clone(); },
 | 
			
		||||
            None =>  { panic!("Stack is empty!"); }
 | 
			
		||||
        };
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// obtains top element of stack and removes it
 | 
			
		||||
    pub fn pop(self: &mut Self) -> T {
 | 
			
		||||
        let element: T = self.top();
 | 
			
		||||
        self.elements = utils::remove_last::<T>(&mut self.elements);
 | 
			
		||||
        return element;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// convert entries to strings
 | 
			
		||||
    #[allow(dead_code)]
 | 
			
		||||
    fn repr(self: &Self) -> Vec<String> {
 | 
			
		||||
        return self.elements.iter()
 | 
			
		||||
            .map(|element| element.to_string())
 | 
			
		||||
            .collect::<Vec<String>>();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// String representation
 | 
			
		||||
    #[allow(dead_code)]
 | 
			
		||||
    pub fn to_string(self: &mut Self) -> String {
 | 
			
		||||
        self.string = format!("{} |", self.repr().join("; "));
 | 
			
		||||
        return self.string.clone();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// &str representation
 | 
			
		||||
    #[allow(dead_code)]
 | 
			
		||||
    pub fn as_string(self: &mut Self) -> &str {
 | 
			
		||||
        self.to_string();
 | 
			
		||||
        return self.string.as_str();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user