algorithms_fourth/graph/
symbol_graph.rs1use std::collections::HashMap;
2
3use super::Graph;
4
5type ST = HashMap<String, usize>;
6pub struct SymbolGraph {
7 st: ST,
9 keys: Vec<String>,
11 _g: Graph,
13}
14impl SymbolGraph {
15 pub fn new(stream: &str, sp: &str) -> Self {
16 let mut st = ST::new();
17 let lines = stream.lines();
18 for line in lines {
19 let a: Vec<&str> = line.trim().split(sp).filter(|it| !it.is_empty()).collect();
20 for key in a {
21 if !st.contains_key(key) {
22 st.insert(key.to_string(), st.len());
23 }
24 }
25 }
26 let mut keys = vec!["".to_string(); st.len()];
27 for (key, value) in st.iter() {
28 keys[*value] = key.clone();
29 }
30 let mut g = Graph::new(keys.len());
31 let lines = stream.lines();
32 for line in lines {
33 let a: Vec<&str> = line.trim().split(sp).filter(|it| !it.is_empty()).collect();
34 if !a.is_empty() {
35 let v = *st.get(a[0]).unwrap();
36 for w in a.into_iter().skip(1) {
37 g.add_edge(v, *st.get(w).unwrap());
38 }
39 }
40 }
41 SymbolGraph { st, keys, _g: g }
42 }
43 pub fn contains(&self, key: &str) -> bool {
45 self.st.contains_key(key)
46 }
47 pub fn index(&self, key: &str) -> usize {
49 *self.st.get(key).unwrap()
50 }
51 pub fn name(&self, v: usize) -> &str {
53 &self.keys[v]
54 }
55}
56#[cfg(test)]
57mod test {
58 use super::SymbolGraph;
59
60 #[test]
61 fn test() {
62 let sg = SymbolGraph::new(
63 r#"
64 a b
65 c d
66 e f
67 a d
68 "#,
69 " ",
70 );
71 assert_eq!(sg.contains("a"), true);
72 assert_eq!(sg.contains("f"), true);
73 assert_eq!(sg.contains("aa"), false);
74 assert_eq!(sg.index("a"), 0);
75 assert_eq!(sg.index("b"), 1);
76 assert_eq!(sg.index("c"), 2);
77 assert_eq!(sg.index("d"), 3);
78 assert_eq!(sg.index("e"), 4);
79 assert_eq!(sg.index("f"), 5);
80 assert_eq!(sg.name(0), "a");
81 assert_eq!(sg.name(5), "f");
82 }
83}