1 // Copyright 2014 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.
16 static TABLE: [u8, ..4] = [ 'A' as u8, 'C' as u8, 'G' as u8, 'T' as u8 ];
17 static TABLE_SIZE: uint = 2 << 16;
19 static OCCURRENCES: [&'static str, ..5] = [
27 // Code implementation
29 #[deriving(Eq, Ord, TotalOrd, TotalEq)]
33 fn hash(&self) -> u64 {
34 let Code(ret) = *self;
38 fn push_char(&self, c: u8) -> Code {
39 Code((self.hash() << 2) + (pack_symbol(c) as u64))
42 fn rotate(&self, c: u8, frame: i32) -> Code {
43 Code(self.push_char(c).hash() & ((1u64 << (2 * (frame as u64))) - 1))
46 fn pack(string: &str) -> Code {
47 string.bytes().fold(Code(0u64), |a, b| a.push_char(b))
50 // FIXME: Inefficient.
51 fn unpack(&self, frame: i32) -> ~str {
52 let mut key = self.hash();
53 let mut result = Vec::new();
54 for _ in range(0, frame) {
55 result.push(unpack_symbol((key as u8) & 3));
60 str::from_utf8_owned(result).unwrap()
64 // Hash table implementation
67 fn f(&self, entry: &mut Entry);
72 impl TableCallback for BumpCallback {
73 fn f(&self, entry: &mut Entry) {
78 struct PrintCallback(&'static str);
80 impl TableCallback for PrintCallback {
81 fn f(&self, entry: &mut Entry) {
82 let PrintCallback(s) = *self;
83 println!("{}\t{}", entry.count as int, s);
95 items: Vec<Option<~Entry>> }
98 cur: Option<&'a Entry>,
99 items: slice::Items<'a, Option<~Entry>>,
106 items: slice::from_fn(TABLE_SIZE, |_| None),
110 fn search_remainder<C:TableCallback>(item: &mut Entry, key: Code, c: C) {
113 let mut entry = ~Entry {
119 item.next = Some(entry);
121 Some(ref mut entry) => {
122 if entry.code == key {
127 Table::search_remainder(*entry, key, c)
132 fn lookup<C:TableCallback>(&mut self, key: Code, c: C) {
133 let index = key.hash() % (TABLE_SIZE as u64);
136 if self.items[index].is_none() {
137 let mut entry = ~Entry {
143 self.items[index] = Some(entry);
149 let entry = &mut *self.items[index].get_mut_ref();
150 if entry.code == key {
155 Table::search_remainder(*entry, key, c)
159 fn iter<'a>(&'a self) -> Items<'a> {
160 Items { cur: None, items: self.items.iter() }
164 impl<'a> Iterator<&'a Entry> for Items<'a> {
165 fn next(&mut self) -> Option<&'a Entry> {
166 let ret = match self.cur {
170 match self.items.next() {
173 Some(&Some(ref a)) => { i = &**a; break }
176 self.cur = Some(&*i);
182 None => { self.cur = None; }
183 Some(ref next) => { self.cur = Some(&**next); }
191 fn pack_symbol(c: u8) -> u8 {
197 _ => fail!("{}", c as char),
201 fn unpack_symbol(c: u8) -> u8 {
205 fn next_char<'a>(mut buf: &'a [u8]) -> &'a [u8] {
207 buf = buf.slice(1, buf.len());
211 if buf[0] != (' ' as u8) && buf[0] != ('\t' as u8) &&
212 buf[0] != ('\n' as u8) && buf[0] != 0 {
219 fn generate_frequencies(frequencies: &mut Table,
222 let mut code = Code(0);
225 for _ in range(0, frame) {
226 code = code.push_char(input[0]);
227 input = next_char(input);
229 frequencies.lookup(code, BumpCallback);
231 while input.len() != 0 && input[0] != ('>' as u8) {
232 code = code.rotate(input[0], frame);
233 frequencies.lookup(code, BumpCallback);
234 input = next_char(input);
238 fn print_frequencies(frequencies: &Table, frame: i32) {
239 let mut vector = Vec::new();
240 for entry in frequencies.iter() {
241 vector.push((entry.code, entry.count));
245 let mut total_count = 0;
246 for &(_, count) in vector.iter() {
247 total_count += count;
250 for &(key, count) in vector.iter() {
251 println!("{} {:.3f}",
253 (count as f32 * 100.0) / (total_count as f32));
257 fn print_occurrences(frequencies: &mut Table, occurrence: &'static str) {
258 frequencies.lookup(Code::pack(occurrence), PrintCallback(occurrence))
262 let input = include_str!("shootout-k-nucleotide.data");
263 let pos = input.find_str(">THREE").unwrap();
264 let pos2 = pos + input.slice_from(pos).find_str("\n").unwrap();
265 let input = input.slice_from(pos2 + 1).as_bytes();
267 let mut frequencies = Table::new();
268 generate_frequencies(&mut frequencies, input, 1);
269 print_frequencies(&frequencies, 1);
271 frequencies = Table::new();
272 generate_frequencies(&mut frequencies, input, 2);
273 print_frequencies(&frequencies, 2);
275 for occurrence in OCCURRENCES.iter() {
276 frequencies = Table::new();
277 generate_frequencies(&mut frequencies,
279 occurrence.len() as i32);
280 print_occurrences(&mut frequencies, *occurrence);