Skip to content

Commit 8dca3ae

Browse files
committed
auto merge of #13206 : TeXitoi/rust/fix-shootout-k-nucleotide, r=cmr
Correct printing (sort, new lines), reading on stdin.
2 parents d79fbba + a0c366b commit 8dca3ae

File tree

1 file changed

+36
-35
lines changed

1 file changed

+36
-35
lines changed

src/test/bench/shootout-k-nucleotide.rs

+36-35
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
// ignore-pretty
1212

13+
use std::ascii::OwnedStrAsciiExt;
1314
use std::str;
1415
use std::slice;
1516

@@ -39,16 +40,16 @@ impl Code {
3940
Code((self.hash() << 2) + (pack_symbol(c) as u64))
4041
}
4142

42-
fn rotate(&self, c: u8, frame: i32) -> Code {
43-
Code(self.push_char(c).hash() & ((1u64 << (2 * (frame as u64))) - 1))
43+
fn rotate(&self, c: u8, frame: uint) -> Code {
44+
Code(self.push_char(c).hash() & ((1u64 << (2 * frame)) - 1))
4445
}
4546

4647
fn pack(string: &str) -> Code {
4748
string.bytes().fold(Code(0u64), |a, b| a.push_char(b))
4849
}
4950

5051
// FIXME: Inefficient.
51-
fn unpack(&self, frame: i32) -> ~str {
52+
fn unpack(&self, frame: uint) -> ~str {
5253
let mut key = self.hash();
5354
let mut result = Vec::new();
5455
for _ in range(0, frame) {
@@ -86,12 +87,12 @@ impl TableCallback for PrintCallback {
8687

8788
struct Entry {
8889
code: Code,
89-
count: i32,
90+
count: uint,
9091
next: Option<~Entry>,
9192
}
9293

9394
struct Table {
94-
count: i32,
95+
count: uint,
9596
items: Vec<Option<~Entry>> }
9697

9798
struct Items<'a> {
@@ -190,10 +191,10 @@ impl<'a> Iterator<&'a Entry> for Items<'a> {
190191

191192
fn pack_symbol(c: u8) -> u8 {
192193
match c as char {
193-
'a' | 'A' => 0,
194-
'c' | 'C' => 1,
195-
'g' | 'G' => 2,
196-
't' | 'T' => 3,
194+
'A' => 0,
195+
'C' => 1,
196+
'G' => 2,
197+
'T' => 3,
197198
_ => fail!("{}", c as char),
198199
}
199200
}
@@ -202,67 +203,67 @@ fn unpack_symbol(c: u8) -> u8 {
202203
TABLE[c]
203204
}
204205

205-
fn next_char<'a>(mut buf: &'a [u8]) -> &'a [u8] {
206-
loop {
207-
buf = buf.slice(1, buf.len());
208-
if buf.len() == 0 {
209-
break;
210-
}
211-
if buf[0] != (' ' as u8) && buf[0] != ('\t' as u8) &&
212-
buf[0] != ('\n' as u8) && buf[0] != 0 {
213-
break;
214-
}
215-
}
216-
buf
217-
}
218-
219206
fn generate_frequencies(frequencies: &mut Table,
220207
mut input: &[u8],
221-
frame: i32) {
208+
frame: uint) {
209+
if input.len() < frame { return; }
222210
let mut code = Code(0);
223211

224212
// Pull first frame.
225213
for _ in range(0, frame) {
226214
code = code.push_char(input[0]);
227-
input = next_char(input);
215+
input = input.slice_from(1);
228216
}
229217
frequencies.lookup(code, BumpCallback);
230218

231219
while input.len() != 0 && input[0] != ('>' as u8) {
232220
code = code.rotate(input[0], frame);
233221
frequencies.lookup(code, BumpCallback);
234-
input = next_char(input);
222+
input = input.slice_from(1);
235223
}
236224
}
237225

238-
fn print_frequencies(frequencies: &Table, frame: i32) {
226+
fn print_frequencies(frequencies: &Table, frame: uint) {
239227
let mut vector = Vec::new();
240228
for entry in frequencies.iter() {
241-
vector.push((entry.code, entry.count));
229+
vector.push((entry.count, entry.code));
242230
}
243231
vector.as_mut_slice().sort();
244232

245233
let mut total_count = 0;
246-
for &(_, count) in vector.iter() {
234+
for &(count, _) in vector.iter() {
247235
total_count += count;
248236
}
249237

250-
for &(key, count) in vector.iter() {
238+
for &(count, key) in vector.iter().rev() {
251239
println!("{} {:.3f}",
252240
key.unpack(frame),
253241
(count as f32 * 100.0) / (total_count as f32));
254242
}
243+
println!("");
255244
}
256245

257246
fn print_occurrences(frequencies: &mut Table, occurrence: &'static str) {
258247
frequencies.lookup(Code::pack(occurrence), PrintCallback(occurrence))
259248
}
260249

250+
fn get_sequence<R: Buffer>(r: &mut R, key: &str) -> ~[u8] {
251+
let mut res = ~"";
252+
for l in r.lines().map(|l| l.ok().unwrap())
253+
.skip_while(|l| key != l.slice_to(key.len())).skip(1)
254+
{
255+
res.push_str(l.trim());
256+
}
257+
res.into_ascii_upper().into_bytes()
258+
}
259+
261260
fn main() {
262-
let input = include_str!("shootout-k-nucleotide.data");
263-
let pos = input.find_str(">THREE").unwrap();
264-
let pos2 = pos + input.slice_from(pos).find_str("\n").unwrap();
265-
let input = input.slice_from(pos2 + 1).as_bytes();
261+
let input = if std::os::getenv("RUST_BENCH").is_some() {
262+
let fd = std::io::File::open(&Path::new("shootout-k-nucleotide.data"));
263+
get_sequence(&mut std::io::BufferedReader::new(fd), ">THREE")
264+
} else {
265+
get_sequence(&mut std::io::stdin(), ">THREE")
266+
};
266267

267268
let mut frequencies = Table::new();
268269
generate_frequencies(&mut frequencies, input, 1);
@@ -276,7 +277,7 @@ fn main() {
276277
frequencies = Table::new();
277278
generate_frequencies(&mut frequencies,
278279
input,
279-
occurrence.len() as i32);
280+
occurrence.len());
280281
print_occurrences(&mut frequencies, *occurrence);
281282
}
282283
}

0 commit comments

Comments
 (0)