14
14
#[ legacy_modes] ;
15
15
16
16
extern mod std;
17
- use std:: oldmap;
18
- use std:: oldmap:: HashMap ;
19
17
use std:: sort;
18
+ use core:: hashmap:: linear:: LinearMap ;
20
19
use core:: io:: ReaderUtil ;
21
20
use core:: comm:: { stream, Port , Chan } ;
22
21
use core:: cmp:: Ord ;
23
22
24
23
// given a map, print a sorted version of it
25
- fn sort_and_fmt ( mm : HashMap < ~[ u8 ] , uint > , total : uint ) -> ~str {
24
+ fn sort_and_fmt ( mm : & LinearMap < ~[ u8 ] , uint > , total : uint ) -> ~str {
26
25
fn pct ( xx : uint , yy : uint ) -> float {
27
26
return ( xx as float ) * 100 f / ( yy as float ) ;
28
27
}
@@ -49,7 +48,7 @@ fn sort_and_fmt(mm: HashMap<~[u8], uint>, total: uint) -> ~str {
49
48
let mut pairs = ~[ ] ;
50
49
51
50
// map -> [(k,%)]
52
- for mm. each |& key, & val| {
51
+ for mm. each |& ( & key, & val) | {
53
52
pairs. push ( ( key, pct ( val, total) ) ) ;
54
53
}
55
54
@@ -68,17 +67,21 @@ fn sort_and_fmt(mm: HashMap<~[u8], uint>, total: uint) -> ~str {
68
67
}
69
68
70
69
// given a map, search for the frequency of a pattern
71
- fn find ( mm : HashMap < ~[ u8 ] , uint > , key : ~str ) -> uint {
70
+ fn find ( mm : & LinearMap < ~[ u8 ] , uint > , key : ~str ) -> uint {
72
71
match mm. find ( & str:: to_bytes ( str:: to_lower ( key) ) ) {
73
72
option:: None => { return 0 u; }
74
- option:: Some ( num) => { return num; }
73
+ option:: Some ( & num) => { return num; }
75
74
}
76
75
}
77
76
78
77
// given a map, increment the counter for a key
79
- fn update_freq ( mm : HashMap < ~[ u8 ] , uint > , key : & [ u8 ] ) {
78
+ fn update_freq ( mm : & mut LinearMap < ~[ u8 ] , uint > , key : & [ u8 ] ) {
80
79
let key = vec:: slice ( key, 0 , key. len ( ) ) . to_vec ( ) ;
81
- mm. update ( key, 1 , |v, v1| { v+v1 } ) ;
80
+ let newval = match mm. pop ( & key) {
81
+ Some ( v) => v + 1 ,
82
+ None => 1
83
+ } ;
84
+ mm. insert ( key, newval) ;
82
85
}
83
86
84
87
// given a ~[u8], for each window call a function
@@ -100,7 +103,7 @@ fn windows_with_carry(bb: &[u8], nn: uint,
100
103
fn make_sequence_processor ( sz : uint , from_parent : comm:: Port < ~[ u8 ] > ,
101
104
to_parent : comm:: Chan < ~str > ) {
102
105
103
- let freqs: HashMap < ~[ u8 ] , uint > = oldmap :: HashMap ( ) ;
106
+ let mut freqs: LinearMap < ~[ u8 ] , uint > = LinearMap :: new ( ) ;
104
107
let mut carry: ~[ u8 ] = ~[ ] ;
105
108
let mut total: uint = 0 u;
106
109
@@ -112,19 +115,19 @@ fn make_sequence_processor(sz: uint, from_parent: comm::Port<~[u8]>,
112
115
if line == ~[ ] { break ; }
113
116
114
117
carry = windows_with_carry ( carry + line, sz, |window| {
115
- update_freq ( freqs, window) ;
118
+ update_freq ( & mut freqs, window) ;
116
119
total += 1 u;
117
120
} ) ;
118
121
}
119
122
120
123
let buffer = match sz {
121
- 1 u => { sort_and_fmt ( freqs, total) }
122
- 2 u => { sort_and_fmt ( freqs, total) }
123
- 3 u => { fmt ! ( "%u\t %s" , find( freqs, ~"GGT "), ~" GGT ") }
124
- 4u => { fmt!(" %u\t %s", find(freqs, ~" GGTA "), ~" GGTA ") }
125
- 6u => { fmt!(" %u\t %s", find(freqs, ~" GGTATT "), ~" GGTATT ") }
126
- 12u => { fmt!(" %u\t %s", find(freqs, ~" GGTATTTTAATT "), ~" GGTATTTTAATT ") }
127
- 18u => { fmt!(" %u\t %s", find(freqs, ~" GGTATTTTAATTTATAGT "), ~" GGTATTTTAATTTATAGT ") }
124
+ 1 u => { sort_and_fmt ( & freqs, total) }
125
+ 2 u => { sort_and_fmt ( & freqs, total) }
126
+ 3 u => { fmt ! ( "%u\t %s" , find( & freqs, ~"GGT "), ~" GGT ") }
127
+ 4u => { fmt!(" %u\t %s", find(& freqs, ~" GGTA "), ~" GGTA ") }
128
+ 6u => { fmt!(" %u\t %s", find(& freqs, ~" GGTATT "), ~" GGTATT ") }
129
+ 12u => { fmt!(" %u\t %s", find(& freqs, ~" GGTATTTTAATT "), ~" GGTATTTTAATT ") }
130
+ 18u => { fmt!(" %u\t %s", find(& freqs, ~" GGTATTTTAATTTATAGT "), ~" GGTATTTTAATTTATAGT ") }
128
131
_ => { ~" " }
129
132
};
130
133
0 commit comments