1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use dict::Dict;
use graph_builder::GraphBuilder;
use edge::Edge;
#[derive(Debug, PartialEq)]
pub struct TextRange {
pub s: usize,
pub e: usize,
}
pub struct Graph {
path: Vec<Edge>,
txt: Vec<char>,
}
impl Graph {
pub fn build(_txt: &str, dict: &Dict) -> Graph {
if _txt.len() == 0 {
return Graph {
path: vec![],
txt: vec![],
};
}
let txt: Vec<char> = _txt.chars().collect();
let mut path = Vec::with_capacity(txt.len() + 1);
{
let mut builder = GraphBuilder::new(&txt, &mut path, dict);
builder.build();
}
Graph {
path: path,
txt: txt,
}
}
pub fn to_ranges(&self) -> Vec<TextRange> {
let len = self.path.len();
if len == 0 {
return vec![];
}
let mut ranges: Vec<TextRange> = Vec::with_capacity(len);
let mut e = len - 1;
while e > 0 {
let edge = self.path[e];
let s = edge.p;
ranges.push(TextRange { s: s, e: e });
e = s;
}
ranges.reverse();
return ranges;
}
pub fn to_str_vec(&self) -> Vec<String> {
let ranges = self.to_ranges();
let mut str_vec: Vec<String> = Vec::with_capacity(ranges.len());
for r in ranges {
let mut buf = String::with_capacity(3 * (r.e - r.s + 1));
for i in r.s..r.e {
buf.push(self.txt[i]);
}
str_vec.push(buf)
}
str_vec
}
}