]> git.lizzy.rs Git - rust.git/blob - src/test/bench/shootout-fasta-redux.rs
rollup merge of #19577: aidancully/master
[rust.git] / src / test / bench / shootout-fasta-redux.rs
1 // The Computer Language Benchmarks Game
2 // http://benchmarksgame.alioth.debian.org/
3 //
4 // contributed by the Rust Project Developers
5
6 // Copyright (c) 2013-2014 The Rust Project Developers
7 //
8 // All rights reserved.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions
12 // are met:
13 //
14 // - Redistributions of source code must retain the above copyright
15 //   notice, this list of conditions and the following disclaimer.
16 //
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
20 //   distribution.
21 //
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.
27 //
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.
40
41 #![feature(slicing_syntax)]
42
43 use std::cmp::min;
44 use std::io::{stdout, IoResult};
45 use std::os;
46 use std::slice::bytes::copy_memory;
47
48 const LINE_LEN: uint = 60;
49 const LOOKUP_SIZE: uint = 4 * 1024;
50 const LOOKUP_SCALE: f32 = (LOOKUP_SIZE - 1) as f32;
51
52 // Random number generator constants
53 const IM: u32 = 139968;
54 const IA: u32 = 3877;
55 const IC: u32 = 29573;
56
57 const ALU: &'static str = "GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTG\
58                             GGAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGA\
59                             GACCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAA\
60                             AATACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAAT\
61                             CCCAGCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAAC\
62                             CCGGGAGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTG\
63                             CACTCCAGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA";
64
65 const NULL_AMINO_ACID: AminoAcid = AminoAcid { c: ' ' as u8, p: 0.0 };
66
67 static IUB: [AminoAcid, ..15] = [
68     AminoAcid { c: 'a' as u8, p: 0.27 },
69     AminoAcid { c: 'c' as u8, p: 0.12 },
70     AminoAcid { c: 'g' as u8, p: 0.12 },
71     AminoAcid { c: 't' as u8, p: 0.27 },
72     AminoAcid { c: 'B' as u8, p: 0.02 },
73     AminoAcid { c: 'D' as u8, p: 0.02 },
74     AminoAcid { c: 'H' as u8, p: 0.02 },
75     AminoAcid { c: 'K' as u8, p: 0.02 },
76     AminoAcid { c: 'M' as u8, p: 0.02 },
77     AminoAcid { c: 'N' as u8, p: 0.02 },
78     AminoAcid { c: 'R' as u8, p: 0.02 },
79     AminoAcid { c: 'S' as u8, p: 0.02 },
80     AminoAcid { c: 'V' as u8, p: 0.02 },
81     AminoAcid { c: 'W' as u8, p: 0.02 },
82     AminoAcid { c: 'Y' as u8, p: 0.02 },
83 ];
84
85 static HOMO_SAPIENS: [AminoAcid, ..4] = [
86     AminoAcid { c: 'a' as u8, p: 0.3029549426680 },
87     AminoAcid { c: 'c' as u8, p: 0.1979883004921 },
88     AminoAcid { c: 'g' as u8, p: 0.1975473066391 },
89     AminoAcid { c: 't' as u8, p: 0.3015094502008 },
90 ];
91
92 // FIXME: Use map().
93 fn sum_and_scale(a: &'static [AminoAcid]) -> Vec<AminoAcid> {
94     let mut result = Vec::new();
95     let mut p = 0f32;
96     for a_i in a.iter() {
97         let mut a_i = *a_i;
98         p += a_i.p;
99         a_i.p = p * LOOKUP_SCALE;
100         result.push(a_i);
101     }
102     let result_len = result.len();
103     result[result_len - 1].p = LOOKUP_SCALE;
104     result
105 }
106
107 struct AminoAcid {
108     c: u8,
109     p: f32,
110 }
111
112 impl Copy for AminoAcid {}
113
114 struct RepeatFasta<'a, W:'a> {
115     alu: &'static str,
116     out: &'a mut W
117 }
118
119 impl<'a, W: Writer> RepeatFasta<'a, W> {
120     fn new(alu: &'static str, w: &'a mut W) -> RepeatFasta<'a, W> {
121         RepeatFasta { alu: alu, out: w }
122     }
123
124     fn make(&mut self, n: uint) -> IoResult<()> {
125         let alu_len = self.alu.len();
126         let mut buf = Vec::from_elem(alu_len + LINE_LEN, 0u8);
127         let alu: &[u8] = self.alu.as_bytes();
128
129         copy_memory(buf.as_mut_slice(), alu);
130         let buf_len = buf.len();
131         copy_memory(buf[mut alu_len..buf_len],
132                     alu[..LINE_LEN]);
133
134         let mut pos = 0;
135         let mut bytes;
136         let mut n = n;
137         while n > 0 {
138             bytes = min(LINE_LEN, n);
139             try!(self.out.write(buf.slice(pos, pos + bytes)));
140             try!(self.out.write_u8('\n' as u8));
141             pos += bytes;
142             if pos > alu_len {
143                 pos -= alu_len;
144             }
145             n -= bytes;
146         }
147         Ok(())
148     }
149 }
150
151 fn make_lookup(a: &[AminoAcid]) -> [AminoAcid, ..LOOKUP_SIZE] {
152     let mut lookup = [ NULL_AMINO_ACID, ..LOOKUP_SIZE ];
153     let mut j = 0;
154     for (i, slot) in lookup.iter_mut().enumerate() {
155         while a[j].p < (i as f32) {
156             j += 1;
157         }
158         *slot = a[j];
159     }
160     lookup
161 }
162
163 struct RandomFasta<'a, W:'a> {
164     seed: u32,
165     lookup: [AminoAcid, ..LOOKUP_SIZE],
166     out: &'a mut W,
167 }
168
169 impl<'a, W: Writer> RandomFasta<'a, W> {
170     fn new(w: &'a mut W, a: &[AminoAcid]) -> RandomFasta<'a, W> {
171         RandomFasta {
172             seed: 42,
173             out: w,
174             lookup: make_lookup(a),
175         }
176     }
177
178     fn rng(&mut self, max: f32) -> f32 {
179         self.seed = (self.seed * IA + IC) % IM;
180         max * (self.seed as f32) / (IM as f32)
181     }
182
183     fn nextc(&mut self) -> u8 {
184         let r = self.rng(1.0);
185         for a in self.lookup.iter() {
186             if a.p >= r {
187                 return a.c;
188             }
189         }
190         0
191     }
192
193     fn make(&mut self, n: uint) -> IoResult<()> {
194         let lines = n / LINE_LEN;
195         let chars_left = n % LINE_LEN;
196         let mut buf = [0, ..LINE_LEN + 1];
197
198         for _ in range(0, lines) {
199             for i in range(0u, LINE_LEN) {
200                 buf[i] = self.nextc();
201             }
202             buf[LINE_LEN] = '\n' as u8;
203             try!(self.out.write(&buf));
204         }
205         for i in range(0u, chars_left) {
206             buf[i] = self.nextc();
207         }
208         self.out.write(buf[..chars_left])
209     }
210 }
211
212 fn main() {
213     let args = os::args();
214     let args = args.as_slice();
215     let n = if args.len() > 1 {
216         from_str::<uint>(args[1].as_slice()).unwrap()
217     } else {
218         5
219     };
220
221     let mut out = stdout();
222
223     out.write_line(">ONE Homo sapiens alu").unwrap();
224     {
225         let mut repeat = RepeatFasta::new(ALU, &mut out);
226         repeat.make(n * 2).unwrap();
227     }
228
229     out.write_line(">TWO IUB ambiguity codes").unwrap();
230     let iub = sum_and_scale(&IUB);
231     let mut random = RandomFasta::new(&mut out, iub.as_slice());
232     random.make(n * 3).unwrap();
233
234     random.out.write_line(">THREE Homo sapiens frequency").unwrap();
235     let homo_sapiens = sum_and_scale(&HOMO_SAPIENS);
236     random.lookup = make_lookup(homo_sapiens.as_slice());
237     random.make(n * 5).unwrap();
238
239     random.out.write_str("\n").unwrap();
240 }