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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*use std::cmp;
use std::fmt;
use std::iter::AdditiveIterator;
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
use suffix::SuffixTable;
#[derive(Clone, Debug)]
pub struct DB {
idx: SuffixTable<'static>,
documents: Vec<Document>,
}
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub struct Document {
name: String,
start: usize,
end: usize,
}
#[derive(Debug)]
pub struct SearchResult<'i> {
document: &'i Document,
position: usize,
line: &'i str,
}
impl Decodable for DB {
fn decode<D: Decoder>(d: &mut D) -> Result<DB, D::Error> {
let (table, offsets, texts): (Vec<u32>, Vec<Document>, String) =
try!(Decodable::decode(d));
Ok(DB {
idx: SuffixTable::from_parts(texts, table),
documents: offsets,
})
}
}
impl Encodable for DB {
fn encode<E: Encoder>(&self, e: &mut E) -> Result<(), E::Error> {
(self.idx.table(), &self.documents, self.idx.text()).encode(e)
}
}
impl Decodable for Document {
fn decode<D: Decoder>(d: &mut D) -> Result<Document, D::Error> {
let (name, start, end): (String, usize, usize) =
try!(Decodable::decode(d));
Ok(Document {
name: name,
start: start,
end: end,
})
}
}
impl Encodable for Document {
fn encode<E: Encoder>(&self, e: &mut E) -> Result<(), E::Error> {
(&self.name, self.start, self.end).encode(e)
}
}
impl DB {
pub fn create(documents: Vec<(String, String)>) -> DB {
let mut texts = String::with_capacity(
documents.iter().map(|s| s.1.len()).sum());
let mut offsets = Vec::with_capacity(documents.len());
for (name, text) in documents {
let start = texts.len();
texts.push_str(&text);
offsets.push(Document {
name: name,
start: start,
end: texts.len(),
});
texts.push('\x00');
}
DB {
idx: SuffixTable::new(texts),
documents: offsets,
}
}
pub fn search(&self, query: &str) -> Vec<SearchResult> {
let mut results = Vec::with_capacity(64);
for i in self.idx.positions(query).iter().map(|&i| i as usize) {
results.push(self.new_result(query, i));
}
results
}
pub fn document(&self, position: usize) -> Option<(&Document, usize)> {
// TODO: Change this to binary search. ---AG
for d in &self.documents {
if position >= d.start && position < d.end {
return Some((d, position - d.start));
}
}
None
}
pub fn document_text(&self, doc: &Document) -> &str {
&self.idx.text()[doc.start..doc.end]
}
fn new_result(&self, query: &str, position: usize) -> SearchResult {
let (doc, position) = self.document(position).unwrap();
SearchResult {
document: doc,
position: position,
line: &self.document_text(doc)[position..position+query.len()],
}
}
}
impl<'i> fmt::Display for SearchResult<'i> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}:{}:{}", self.document.name, self.position, self.line)
}
}
impl<'i> Eq for SearchResult<'i> {}
impl<'i> PartialEq for SearchResult<'i> {
fn eq<'j>(&self, o: &SearchResult<'j>) -> bool {
(&self.document.name, self.position) == (&o.document.name, o.position)
}
}
impl<'i> PartialOrd for SearchResult<'i> {
fn partial_cmp<'j>(&self, o: &SearchResult<'j>) -> Option<cmp::Ordering> {
let this = (&self.document.name, self.position);
this.partial_cmp(&(&o.document.name, o.position))
}
}
impl<'i> Ord for SearchResult<'i> {
fn cmp<'j>(&self, o: &SearchResult<'j>) -> cmp::Ordering {
self.partial_cmp(o).unwrap()
}
}*/