1 // The Computer Language Benchmarks Game
2 // http://benchmarksgame.alioth.debian.org/
4 // contributed by the Rust Project Developers
6 // Copyright (c) 2012-2014 The Rust Project Developers
8 // All rights reserved.
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions
14 // - Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
17 // - Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in
19 // the documentation and/or other materials provided with the
22 // - Neither the name of "The Computer Language Benchmarks Game" nor
23 // the name of "The Computer Language Shootout Benchmarks" nor the
24 // names of its contributors may be used to endorse or promote
25 // products derived from this software without specific prior
26 // written permission.
28 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
31 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
32 // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
33 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 // OF THE POSSIBILITY OF SUCH DAMAGE.
42 use std::io::{BufferedWriter, File};
47 const LINE_LENGTH: uint = 60;
48 const IM: u32 = 139968;
54 fn new() -> MyRandom { MyRandom { last: 42 } }
55 fn normalize(p: f32) -> u32 {(p * IM as f32).floor() as u32}
56 fn gen(&mut self) -> u32 {
57 self.last = (self.last * 3877 + 29573) % IM;
63 rng: &'a mut MyRandom,
67 fn new<'b>(rng: &'b mut MyRandom, aa: &[(char, f32)]) -> AAGen<'b> {
70 .map(|&(ch, p)| { cum += p; (MyRandom::normalize(cum), ch as u8) })
72 AAGen { rng: rng, data: data }
75 impl<'a> Iterator for AAGen<'a> {
78 fn next(&mut self) -> Option<u8> {
79 let r = self.rng.gen();
81 .skip_while(|pc| pc.0 < r)
87 fn make_fasta<W: Writer, I: Iterator<Item=u8>>(
88 wr: &mut W, header: &str, mut it: I, mut n: uint)
89 -> std::io::IoResult<()>
91 try!(wr.write(header.as_bytes()));
92 let mut line = [0u8; LINE_LENGTH + 1];
94 let nb = min(LINE_LENGTH, n);
95 for i in range(0, nb) {
96 line[i] = it.next().unwrap();
99 line[nb] = '\n' as u8;
100 try!(wr.write(&line[..(nb+1)]));
105 fn run<W: Writer>(writer: &mut W) -> std::io::IoResult<()> {
106 let args = os::args();
107 let args = args.as_slice();
108 let n = if os::getenv("RUST_BENCH").is_some() {
110 } else if args.len() <= 1u {
113 args[1].parse().unwrap()
116 let rng = &mut MyRandom::new();
118 "GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGG\
119 GAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGA\
120 CCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAAT\
121 ACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCA\
122 GCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGG\
123 AGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCC\
124 AGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA";
125 let iub = &[('a', 0.27), ('c', 0.12), ('g', 0.12),
126 ('t', 0.27), ('B', 0.02), ('D', 0.02),
127 ('H', 0.02), ('K', 0.02), ('M', 0.02),
128 ('N', 0.02), ('R', 0.02), ('S', 0.02),
129 ('V', 0.02), ('W', 0.02), ('Y', 0.02)];
130 let homosapiens = &[('a', 0.3029549426680),
131 ('c', 0.1979883004921),
132 ('g', 0.1975473066391),
133 ('t', 0.3015094502008)];
135 try!(make_fasta(writer, ">ONE Homo sapiens alu\n",
136 alu.as_bytes().iter().cycle().map(|c| *c), n * 2));
137 try!(make_fasta(writer, ">TWO IUB ambiguity codes\n",
138 AAGen::new(rng, iub), n * 3));
139 try!(make_fasta(writer, ">THREE Homo sapiens frequency\n",
140 AAGen::new(rng, homosapiens), n * 5));
146 let res = if os::getenv("RUST_BENCH").is_some() {
147 let mut file = BufferedWriter::new(File::create(&Path::new("./shootout-fasta.data")));
150 run(&mut io::stdout())