]> git.lizzy.rs Git - rust.git/blob - src/test/bench/shootout-reverse-complement.rs
auto merge of #17109 : brson/rust/win64snap, r=alexcrichton
[rust.git] / src / test / bench / shootout-reverse-complement.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 // ignore-pretty very bad with line comments
42 // ignore-android doesn't terminate?
43
44 use std::iter::range_step;
45 use std::io::{stdin, stdout, File};
46
47 static LINE_LEN: uint = 60;
48
49 fn make_complements() -> [u8, ..256] {
50     let transforms = [
51         ('A', 'T'), ('C', 'G'), ('G', 'C'), ('T', 'A'),
52         ('U', 'A'), ('M', 'K'), ('R', 'Y'), ('W', 'W'),
53         ('S', 'S'), ('Y', 'R'), ('K', 'M'), ('V', 'B'),
54         ('H', 'D'), ('D', 'H'), ('B', 'V'), ('N', 'N'),
55         ('\n', '\n')];
56     let mut complements: [u8, ..256] = [0, ..256];
57     for (i, c) in complements.mut_iter().enumerate() {
58         *c = i as u8;
59     }
60     let lower = 'A' as u8 - 'a' as u8;
61     for &(from, to) in transforms.iter() {
62         complements[from as uint] = to as u8;
63         complements[(from as u8 - lower) as uint] = to as u8;
64     }
65     complements
66 }
67
68 fn main() {
69     let complements = make_complements();
70     let data = if std::os::getenv("RUST_BENCH").is_some() {
71         File::open(&Path::new("shootout-k-nucleotide.data")).read_to_end()
72     } else {
73         stdin().read_to_end()
74     };
75     let mut data = data.unwrap();
76
77     for seq in data.as_mut_slice().mut_split(|c| *c == '>' as u8) {
78         // skip header and last \n
79         let begin = match seq.iter().position(|c| *c == '\n' as u8) {
80             None => continue,
81             Some(c) => c
82         };
83         let len = seq.len();
84         let seq = seq.mut_slice(begin + 1, len - 1);
85
86         // arrange line breaks
87         let len = seq.len();
88         let off = LINE_LEN - len % (LINE_LEN + 1);
89         for i in range_step(LINE_LEN, len, LINE_LEN + 1) {
90             for j in std::iter::count(i, -1).take(off) {
91                 seq[j] = seq[j - 1];
92             }
93             seq[i - off] = '\n' as u8;
94         }
95
96         // reverse complement, as
97         //    seq.reverse(); for c in seq.mut_iter() {*c = complements[*c]}
98         // but faster:
99         let mut it = seq.mut_iter();
100         loop {
101             match (it.next(), it.next_back()) {
102                 (Some(front), Some(back)) => {
103                     let tmp = complements[*front as uint];
104                     *front = complements[*back as uint];
105                     *back = tmp;
106                 }
107                 (Some(last), None) => *last = complements[*last as uint], // last element
108                 _ => break // vector exhausted.
109             }
110         }
111     }
112
113     stdout().write(data.as_slice()).unwrap();
114 }