From b85574cda4c27d16689c2e22c3743a830a8ebee5 Mon Sep 17 00:00:00 2001 From: raj_mathe Date: Fri, 8 Apr 2022 07:26:56 +0200 Subject: [PATCH] master > master: code-rust - init --- code/rust/src/core/utils.rs | 36 ++++++++++++++-- code/rust/src/graphs/graph.rs | 72 ++++++++++++++++++++++++++++++++ code/rust/src/graphs/mod.rs | 2 + code/rust/src/lib.rs | 2 + code/rust/src/stacks/mod.rs | 1 + code/rust/src/stacks/stack.rs | 77 +++++++++++++++++++++++++++++++++++ 6 files changed, 187 insertions(+), 3 deletions(-) create mode 100644 code/rust/src/graphs/graph.rs create mode 100644 code/rust/src/graphs/mod.rs create mode 100644 code/rust/src/stacks/mod.rs create mode 100644 code/rust/src/stacks/stack.rs diff --git a/code/rust/src/core/utils.rs b/code/rust/src/core/utils.rs index 57b6860..9cc015a 100644 --- a/code/rust/src/core/utils.rs +++ b/code/rust/src/core/utils.rs @@ -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(x: T, y: T) -> T + where T: PartialOrd +{ + return if y < x { y } else { x }; +} + +// ---------------------------------------------------------------- +// METHODS Vectors +// ---------------------------------------------------------------- + +pub fn restrict(x: &mut Vec, i: usize, j: usize) -> Vec + where T: Clone +{ + return x[i..j].iter() + .cloned() + .collect::>(); +} + +pub fn remove_last(x: &mut Vec) -> Vec + where T: Clone +{ + let n = x.len(); + return restrict::(x, 0, n-1); +} + +#[allow(dead_code)] +pub fn remove_first(x: &mut Vec) -> Vec + where T: Clone +{ + let n = x.len(); + return restrict::(x, 0, n); } diff --git a/code/rust/src/graphs/graph.rs b/code/rust/src/graphs/graph.rs new file mode 100644 index 0000000..88de612 --- /dev/null +++ b/code/rust/src/graphs/graph.rs @@ -0,0 +1,72 @@ +// ---------------------------------------------------------------- +// IMPORTS +// ---------------------------------------------------------------- + +// + +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +// CLASS Graph +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +/** + A data structure for graphs + */ +pub struct Graph { + pub nodes: Vec, + pub edges: Vec<(T,T)>, + #[allow(dead_code)] + string: String, +} + +impl Graph +where T: PartialEq + Clone + ToString { + /// Creates new typed instance of stack. + pub fn new(nodes: Vec, edges: Vec<(T,T)>) -> Graph { + 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) -> Graph { + let nodes_ = self.nodes + .iter() + .filter(|&u| (nodes.contains(u))) + .map(|u| u.clone()) + .collect::>(); + let edges_ = self.edges + .iter() + .filter(|&(u, v)| (nodes.contains(u) && nodes.contains(v))) + .map(|(u, v)| (u.clone(), v.clone())) + .collect::>(); + return Graph::new(nodes_, edges_); + } + + /// @returns all successor nodes of a particular node + pub fn successors(self: &Self, u: &T) -> Vec { + return self.edges + .iter() + .filter(|&(u_, _)| (*u_ == *u)) + .map(|(_, v)| (v.clone())) + .collect::>(); + } + + /// @returns all predecessors nodes of a particular node + #[allow(dead_code)] + pub fn predecessors(self: &Self, v: &T) -> Vec { + return self.edges + .iter() + .filter(|&(_, v_)| (*v_ == *v)) + .map(|(u, _)| (u.clone())) + .collect::>(); + } +} diff --git a/code/rust/src/graphs/mod.rs b/code/rust/src/graphs/mod.rs new file mode 100644 index 0000000..33fd8b5 --- /dev/null +++ b/code/rust/src/graphs/mod.rs @@ -0,0 +1,2 @@ +pub mod graph; +pub mod tarjan; diff --git a/code/rust/src/lib.rs b/code/rust/src/lib.rs index 5a7ca06..d731263 100644 --- a/code/rust/src/lib.rs +++ b/code/rust/src/lib.rs @@ -1 +1,3 @@ pub mod core; +pub mod stacks; +pub mod graphs; diff --git a/code/rust/src/stacks/mod.rs b/code/rust/src/stacks/mod.rs new file mode 100644 index 0000000..c93897d --- /dev/null +++ b/code/rust/src/stacks/mod.rs @@ -0,0 +1 @@ +pub mod stack; diff --git a/code/rust/src/stacks/stack.rs b/code/rust/src/stacks/stack.rs new file mode 100644 index 0000000..a71a5a2 --- /dev/null +++ b/code/rust/src/stacks/stack.rs @@ -0,0 +1,77 @@ +// ---------------------------------------------------------------- +// IMPORTS +// ---------------------------------------------------------------- + +use crate::core::utils; + +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +// CLASS Stack +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +/** + A data structure for stacks + */ +#[derive(Clone)] +pub struct Stack { + pub elements: Vec, + string: String, +} + +impl Stack +where T: Clone + ToString { + /// Creates new typed instance of stack. + pub fn new() -> Stack { + 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::(&mut self.elements); + return element; + } + + /// convert entries to strings + #[allow(dead_code)] + fn repr(self: &Self) -> Vec { + return self.elements.iter() + .map(|element| element.to_string()) + .collect::>(); + } + + /// 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(); + } +}