From f414fcb89dd0e9e103200c2548ea545221035989 Mon Sep 17 00:00:00 2001 From: Akira Hayakawa Date: Sun, 20 Jul 2014 12:19:38 +0900 Subject: [PATCH] Add tsort Signed-off-by: Akira Hayakawa --- Cargo.toml | 4 ++ Makefile | 1 + tsort/tsort.rs | 189 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 194 insertions(+) create mode 100644 tsort/tsort.rs diff --git a/Cargo.toml b/Cargo.toml index 85254dd82..76d3eb79f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -203,6 +203,10 @@ path = "true/true.rs" name = "truncate" path = "truncate/truncate.rs" +[[bin]] +name = "tsort" +path = "tsort/tsort.rs" + [[bin]] name = "tty" path = "tty/tty.rs" diff --git a/Makefile b/Makefile index 400d1e268..9993a8fa0 100644 --- a/Makefile +++ b/Makefile @@ -53,6 +53,7 @@ PROGS := \ tr \ true \ truncate \ + tsort \ unlink \ uniq \ wc \ diff --git a/tsort/tsort.rs b/tsort/tsort.rs new file mode 100644 index 000000000..fe77e0f71 --- /dev/null +++ b/tsort/tsort.rs @@ -0,0 +1,189 @@ +#![crate_name = "tsort"] + +/* + * This file is part of the uutils coreutils package. + * + * (c) Ben Eggers + * (c) Akira Hayakawa + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +#![feature(macro_rules)] + +extern crate getopts; +extern crate libc; + +use std::io; +use std::collections::{HashSet, HashMap}; + +#[path = "../common/util.rs"] +mod util; + +static NAME: &'static str = "tsort"; +static VERSION: &'static str = "1.0.0"; + +pub fn uumain(args: Vec) -> int { + let opts = [ + getopts::optflag("h", "help", "display this help and exit"), + getopts::optflag("V", "version", "output version information and exit"), + ]; + + let matches = match getopts::getopts(args.tail(), opts) { + Ok(m) => m, + Err(f) => crash!(1, "{}", f) + }; + + if matches.opt_present("h") { + println!("{} v{}", NAME, VERSION); + println!(""); + println!("Usage:"); + println!(" {} [OPTIONS] FILE", NAME); + println!(""); + io::print(getopts::usage("Topological sort the strings in FILE. Strings are defined as any sequence of tokens separated by whitespace (tab, space, or newline). If FILE is not passed in, stdin is used instead.", opts).as_slice()); + return 0; + } + + if matches.opt_present("V") { + println!("{} v{}", NAME, VERSION); + return 0; + } + + let files = matches.free.clone(); + let input = if files.len() > 1 { + crash!(1, "{}, extra operand '{}'", NAME, matches.free[1]); + } else if files.is_empty() { + "-".to_string() + } else { + files[0].to_string() + }; + + let mut stdin_buf; + let mut file_buf; + let mut reader = io::BufferedReader::new( + if input.as_slice() == "-" { + stdin_buf = io::stdio::stdin_raw(); + &mut stdin_buf as &mut Reader + } else { + file_buf = match io::File::open(&Path::new(input.as_slice())) { + Ok(a) => a, + _ => { + show_error!("{}: No such file or directory", input); + return 1; + } + }; + &mut file_buf as &mut Reader + } + ); + + let mut g = Graph::new(); + loop { + match reader.read_line() { + Ok(line) => { + let ab: Vec<&str> = line.as_slice().trim_right_chars('\n').split(' ').collect(); + if ab.len() > 2 { + crash!(1, "{}: input contains an odd number of tokens", input); + } + g.add_edge(&ab[0].to_string(), &ab[1].to_string()); + }, + _ => break + } + } + + g.run_tsort(); + + if !g.is_acyclic() { + crash!(1, "{}, input contains a loop:", input); + } + + for x in g.result.iter() { + println!("{}", x); + } + + return 0 +} + +// We use String as a representation of node here +// but using integer may improve performance. +struct Graph { + in_edges: HashMap>, + out_edges: HashMap>, + result: Vec +} + +impl Graph { + fn new() -> Graph { + Graph { + in_edges: HashMap::new(), + out_edges: HashMap::new(), + result: vec!(), + } + } + + fn has_node(&self, n: &String) -> bool { + self.in_edges.contains_key(n) + } + + fn has_edge(&self, from: &String, to: &String) -> bool { + self.in_edges.find(to).unwrap().contains(from) + } + + fn init_node(&mut self, n: &String) { + self.in_edges.insert(n.clone(), HashSet::new()); + self.out_edges.insert(n.clone(), vec!()); + } + + fn add_edge(&mut self, from: &String, to: &String) { + if !self.has_node(to) { + self.init_node(to); + } + + if !self.has_node(from) { + self.init_node(from); + } + + if !self.has_edge(from, to) { + self.in_edges.find_mut(to).unwrap().insert(from.clone()); + self.out_edges.find_mut(from).unwrap().push(to.clone()); + } + } + + // Kahn's algorithm + // O(|V|+|E|) + fn run_tsort(&mut self) { + let mut start_nodes = vec!(); + for (n, edges) in self.in_edges.iter() { + if edges.is_empty() { + start_nodes.push(n.clone()); + } + } + + while !start_nodes.is_empty() { + let n = start_nodes.shift().unwrap(); + + self.result.push(n.clone()); + + let n_out_edges = self.out_edges.find_mut(&n).unwrap(); + for m in n_out_edges.iter() { + let m_in_edges = self.in_edges.find_mut(m).unwrap(); + m_in_edges.remove(&n); + + // If m doesn't have other in-coming edges add it to start_nodes + if m_in_edges.is_empty() { + start_nodes.push(m.clone()); + } + } + n_out_edges.clear(); + } + } + + fn is_acyclic(&self) -> bool { + for (_, edges) in self.out_edges.iter() { + if !edges.is_empty() { + return false + } + } + true + } +}