1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 /* -*- mode: rust; indent-tabs-mode: nil -*-
12 * Implementation of 'fasta' benchmark from
13 * Computer Language Benchmarks Game
14 * http://shootout.alioth.debian.org/
18 use std::io::buffered::BufferedWriter;
23 static LINE_LENGTH: uint = 60;
24 static IM: u32 = 139968;
30 fn new() -> MyRandom { MyRandom { last: 42 } }
31 fn normalize(p: f32) -> u32 {(p * IM as f32).floor() as u32}
32 fn gen(&mut self) -> u32 {
33 self.last = (self.last * 3877 + 29573) % IM;
39 rng: &'a mut MyRandom,
43 fn new<'b>(rng: &'b mut MyRandom, aa: &[(char, f32)]) -> AAGen<'b> {
46 .map(|&(ch, p)| { cum += p; (MyRandom::normalize(cum), ch as u8) })
48 AAGen { rng: rng, data: data }
51 impl<'a> Iterator<u8> for AAGen<'a> {
52 fn next(&mut self) -> Option<u8> {
53 let r = self.rng.gen();
55 .skip_while(|pc| pc.n0() < r)
61 fn make_fasta<W: Writer, I: Iterator<u8>>(
62 wr: &mut W, header: &str, mut it: I, mut n: uint)
64 wr.write(header.as_bytes());
65 let mut line = [0u8, .. LINE_LENGTH + 1];
67 let nb = min(LINE_LENGTH, n);
68 for i in range(0, nb) {
69 line[i] = it.next().unwrap();
72 line[nb] = '\n' as u8;
73 wr.write(line.slice_to(nb + 1));
77 fn run<W: Writer>(writer: &mut W) {
78 let args = os::args();
79 let n = if os::getenv("RUST_BENCH").is_some() {
81 } else if args.len() <= 1u {
84 from_str(args[1]).unwrap()
87 let rng = &mut MyRandom::new();
89 "GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGG\
90 GAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGA\
91 CCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAAT\
92 ACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCA\
93 GCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGG\
94 AGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCC\
95 AGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA";
96 let iub = &[('a', 0.27), ('c', 0.12), ('g', 0.12),
97 ('t', 0.27), ('B', 0.02), ('D', 0.02),
98 ('H', 0.02), ('K', 0.02), ('M', 0.02),
99 ('N', 0.02), ('R', 0.02), ('S', 0.02),
100 ('V', 0.02), ('W', 0.02), ('Y', 0.02)];
101 let homosapiens = &[('a', 0.3029549426680),
102 ('c', 0.1979883004921),
103 ('g', 0.1975473066391),
104 ('t', 0.3015094502008)];
106 make_fasta(writer, ">ONE Homo sapiens alu\n",
107 alu.as_bytes().iter().cycle().map(|c| *c), n * 2);
108 make_fasta(writer, ">TWO IUB ambiguity codes\n",
109 AAGen::new(rng, iub), n * 3);
110 make_fasta(writer, ">THREE Homo sapiens frequency\n",
111 AAGen::new(rng, homosapiens), n * 5);
117 if os::getenv("RUST_BENCH").is_some() {
118 let mut file = BufferedWriter::new(File::create(&Path::new("./shootout-fasta.data")));
121 run(&mut BufferedWriter::new(io::stdout()));