Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::io;
- use std::io::prelude::*;
- use std::collections::HashMap;
- use std::hash;
- use std::convert::TryInto;
- trait NodeToIndex<K> where K: Eq + hash::Hash {
- fn to_index(&mut self, node: K) -> u128;
- fn print_table(&self, output: Option<Box<dyn Write>>); // Boxで包んだ
- }
- fn parse_edge(line: &str) -> (String, String) {
- let mut iter = line.split("\t");
- //TODO ここでsplitができたかエラー処理
- (iter.next().unwrap().to_string(), iter.next().unwrap().to_string())
- }
- impl<K> NodeToIndex<K> for HashMap<K, u128> where K: Eq + hash::Hash {
- fn to_index(&mut self, node: K) -> u128 {
- match self.get(&node) {
- Some(index) => *index,
- None => {
- let index: u128 = self.len().try_into().unwrap();
- self.insert(node, index);
- index
- }
- }
- }
- fn print_table(&self, output: Option<Box<dyn Write>>) { // Boxで包んだ
- let stdout = io::stdout(); // Rustのlifetimeの仕様により,一旦letする必要があります
- let mut output = match output {
- Some(output) => output,
- None => Box::new(stdout.lock()), // Boxで包んだ
- };
- writeln!(output, "hello");
- }
- }
- fn main() {
- let mut converter = HashMap::new();
- for line in io::stdin().lock().lines() {
- let (src_node, dst_node) = parse_edge(&line.unwrap());
- //println!("{}\t{}", src_node, dst_node);
- let src_id = converter.to_index(src_node);
- let dst_id = converter.to_index(dst_node);
- println!("{}\t{}", src_id, dst_id);
- }
- converter.print_table(None); // とりあえずNone
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement