Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. use std::io;
  2. use std::io::prelude::*;
  3. use std::collections::HashMap;
  4. use std::hash;
  5. use std::convert::TryInto;
  6.  
  7.  
  8. trait NodeToIndex<K> where K: Eq + hash::Hash {
  9. fn to_index(&mut self, node: K) -> u128;
  10. fn print_table(&self, output: Option<Box<dyn Write>>); // Boxで包んだ
  11. }
  12.  
  13. fn parse_edge(line: &str) -> (String, String) {
  14. let mut iter = line.split("\t");
  15. //TODO ここでsplitができたかエラー処理
  16. (iter.next().unwrap().to_string(), iter.next().unwrap().to_string())
  17. }
  18.  
  19. impl<K> NodeToIndex<K> for HashMap<K, u128> where K: Eq + hash::Hash {
  20. fn to_index(&mut self, node: K) -> u128 {
  21. match self.get(&node) {
  22. Some(index) => *index,
  23. None => {
  24. let index: u128 = self.len().try_into().unwrap();
  25. self.insert(node, index);
  26. index
  27. }
  28. }
  29. }
  30.  
  31. fn print_table(&self, output: Option<Box<dyn Write>>) { // Boxで包んだ
  32. let stdout = io::stdout(); // Rustのlifetimeの仕様により,一旦letする必要があります
  33. let mut output = match output {
  34. Some(output) => output,
  35. None => Box::new(stdout.lock()), // Boxで包んだ
  36. };
  37. writeln!(output, "hello");
  38. }
  39. }
  40.  
  41. fn main() {
  42. let mut converter = HashMap::new();
  43.  
  44. for line in io::stdin().lock().lines() {
  45. let (src_node, dst_node) = parse_edge(&line.unwrap());
  46. //println!("{}\t{}", src_node, dst_node);
  47. let src_id = converter.to_index(src_node);
  48. let dst_id = converter.to_index(dst_node);
  49. println!("{}\t{}", src_id, dst_id);
  50. }
  51. converter.print_table(None); // とりあえずNone
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement