1 // The Computer Language Benchmarks Game
2 // http://benchmarksgame.alioth.debian.org/
4 // contributed by the Rust Project Developers
6 // Copyright (c) 2013-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.
44 use std
::io
::prelude
::*;
46 const LINE_LEN
: usize = 60;
47 const LOOKUP_SIZE
: usize = 4 * 1024;
48 const LOOKUP_SCALE
: f32 = (LOOKUP_SIZE
- 1) as f32;
50 // Random number generator constants
51 const IM
: u32 = 139968;
53 const IC
: u32 = 29573;
55 const ALU
: &'
static str = "GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTG\
56 GGAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGA\
57 GACCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAA\
58 AATACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAAT\
59 CCCAGCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAAC\
60 CCGGGAGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTG\
61 CACTCCAGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA";
63 const NULL_AMINO_ACID
: AminoAcid
= AminoAcid { c: ' ' as u8, p: 0.0 }
;
65 static IUB
: [AminoAcid
;15] = [
66 AminoAcid { c: 'a' as u8, p: 0.27 }
,
67 AminoAcid { c: 'c' as u8, p: 0.12 }
,
68 AminoAcid { c: 'g' as u8, p: 0.12 }
,
69 AminoAcid { c: 't' as u8, p: 0.27 }
,
70 AminoAcid { c: 'B' as u8, p: 0.02 }
,
71 AminoAcid { c: 'D' as u8, p: 0.02 }
,
72 AminoAcid { c: 'H' as u8, p: 0.02 }
,
73 AminoAcid { c: 'K' as u8, p: 0.02 }
,
74 AminoAcid { c: 'M' as u8, p: 0.02 }
,
75 AminoAcid { c: 'N' as u8, p: 0.02 }
,
76 AminoAcid { c: 'R' as u8, p: 0.02 }
,
77 AminoAcid { c: 'S' as u8, p: 0.02 }
,
78 AminoAcid { c: 'V' as u8, p: 0.02 }
,
79 AminoAcid { c: 'W' as u8, p: 0.02 }
,
80 AminoAcid { c: 'Y' as u8, p: 0.02 }
,
83 static HOMO_SAPIENS
: [AminoAcid
;4] = [
84 AminoAcid { c: 'a' as u8, p: 0.3029549426680 }
,
85 AminoAcid { c: 'c' as u8, p: 0.1979883004921 }
,
86 AminoAcid { c: 'g' as u8, p: 0.1975473066391 }
,
87 AminoAcid { c: 't' as u8, p: 0.3015094502008 }
,
91 fn sum_and_scale(a
: &'
static [AminoAcid
]) -> Vec
<AminoAcid
> {
92 let mut result
= Vec
::new();
97 a_i
.p
= p
* LOOKUP_SCALE
;
100 let result_len
= result
.len();
101 result
[result_len
- 1].p
= LOOKUP_SCALE
;
105 #[derive(Copy, Clone)]
111 struct RepeatFasta
<'a
, W
:'a
> {
116 impl<'a
, W
: Write
> RepeatFasta
<'a
, W
> {
117 fn new(alu
: &'
static str, w
: &'a
mut W
) -> RepeatFasta
<'a
, W
> {
118 RepeatFasta { alu: alu, out: w }
121 fn make(&mut self, n
: usize) -> io
::Result
<()> {
122 let alu_len
= self.alu
.len();
123 let mut buf
= vec
![0; alu_len
+ LINE_LEN
];
124 let alu
: &[u8] = self.alu
.as_bytes();
126 for (slot
, val
) in buf
.iter_mut().zip(alu
) {
129 let buf_len
= buf
.len();
130 for (slot
, val
) in buf
[alu_len
..buf_len
].iter_mut().zip(&alu
[..LINE_LEN
]) {
138 bytes
= min(LINE_LEN
, n
);
139 try
!(self.out
.write_all(&buf
[pos
..pos
+ bytes
]));
140 try
!(self.out
.write_all(&[b'
\n'
]));
151 fn make_lookup(a
: &[AminoAcid
]) -> [AminoAcid
;LOOKUP_SIZE
] {
152 let mut lookup
= [ NULL_AMINO_ACID
;LOOKUP_SIZE
];
154 for (i
, slot
) in lookup
.iter_mut().enumerate() {
155 while a
[j
].p
< (i
as f32) {
163 struct RandomFasta
<'a
, W
:'a
> {
165 lookup
: [AminoAcid
;LOOKUP_SIZE
],
169 impl<'a
, W
: Write
> RandomFasta
<'a
, W
> {
170 fn new(w
: &'a
mut W
, a
: &[AminoAcid
]) -> RandomFasta
<'a
, W
> {
174 lookup
: make_lookup(a
),
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)
183 fn nextc(&mut self) -> u8 {
184 let r
= self.rng(1.0);
185 for a
in &self.lookup
[..] {
193 fn make(&mut self, n
: usize) -> io
::Result
<()> {
194 let lines
= n
/ LINE_LEN
;
195 let chars_left
= n
% LINE_LEN
;
196 let mut buf
= [0;LINE_LEN
+ 1];
199 for i
in 0..LINE_LEN
{
200 buf
[i
] = self.nextc();
202 buf
[LINE_LEN
] = '
\n'
as u8;
203 try
!(self.out
.write(&buf
));
205 for i
in 0..chars_left
{
206 buf
[i
] = self.nextc();
208 self.out
.write_all(&buf
[..chars_left
])
213 let mut args
= env
::args();
214 let n
= if args
.len() > 1 {
215 args
.nth(1).unwrap().parse
::<usize>().unwrap()
220 let mut out
= io
::stdout();
222 out
.write_all(b
">ONE Homo sapiens alu\n").unwrap();
224 let mut repeat
= RepeatFasta
::new(ALU
, &mut out
);
225 repeat
.make(n
* 2).unwrap();
228 out
.write_all(b
">TWO IUB ambiguity codes\n").unwrap();
229 let iub
= sum_and_scale(&IUB
);
230 let mut random
= RandomFasta
::new(&mut out
, &iub
);
231 random
.make(n
* 3).unwrap();
233 random
.out
.write_all(b
">THREE Homo sapiens frequency\n").unwrap();
234 let homo_sapiens
= sum_and_scale(&HOMO_SAPIENS
);
235 random
.lookup
= make_lookup(&homo_sapiens
);
236 random
.make(n
* 5).unwrap();
238 random
.out
.write_all(b
"\n").unwrap();