MCPcopy Create free account
hub / github.com/douchuan/algorithm / parse

Method parse

src/graph/util/parser.rs:19–60  ·  view source on GitHub ↗
(s: &str, is_weighted: bool)

Source from the content-addressed store, hash-verified

17
18impl GraphDataParser {
19 pub fn parse(s: &str, is_weighted: bool) -> Result<Self, ()> {
20 let lines = s.lines();
21
22 let mut nv = 0;
23 let mut ne = 0;
24 let mut edges: Vec<(usize, usize)> = Vec::new();
25 let mut weighted_edges: Vec<(usize, usize, f32)> = Vec::new();
26 let mut sm = SM::V;
27 for s in lines {
28 if s.is_empty() {
29 continue;
30 }
31
32 match sm {
33 SM::V => {
34 let (_, v) = parse_num(s).ok().ok_or(())?;
35 nv = v;
36 }
37 SM::E => {
38 let (_, v) = parse_num(s).ok().ok_or(())?;
39 ne = v;
40 }
41 SM::Edge => {
42 if is_weighted {
43 let (_, v) = parse_list_float(s).ok().ok_or(())?;
44 weighted_edges.push((v[0] as usize, v[1] as usize, v[2]));
45 } else {
46 let (_, v) = parse_list_num(s).ok().ok_or(())?;
47 edges.push((v[0], v[1]));
48 }
49 }
50 }
51 sm = sm.step();
52 }
53
54 Ok(Self {
55 nv,
56 ne,
57 edges,
58 weighted_edges,
59 })
60 }
61
62 pub fn get_v(&self) -> usize {
63 self.nv

Callers 2

parse_numFunction · 0.80
buildFunction · 0.80

Calls 6

parse_numFunction · 0.85
parse_list_floatFunction · 0.85
parse_list_numFunction · 0.85
pushMethod · 0.80
stepMethod · 0.80
is_emptyMethod · 0.45

Tested by

no test coverage detected