1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use std::mem;
use bit_set::BitSet;
use vec_map::VecMap;
use utils::{IntoTextIterator, TextIterator};
pub mod dna;
pub mod protein;
pub type SymbolRanks = VecMap<u8>;
pub struct Alphabet {
pub symbols: BitSet,
}
impl Alphabet {
pub fn new<'a, T: IntoTextIterator<'a>>(symbols: T) -> Self {
let mut s = BitSet::new();
s.extend(symbols.into_iter().map(|&c| c as usize));
Alphabet { symbols: s }
}
pub fn insert(&mut self, a: u8) {
self.symbols.insert(a as usize);
}
pub fn is_word<'a, T: IntoTextIterator<'a>>(&self, text: T) -> bool {
text.into_iter().all(|&c| self.symbols.contains(c as usize))
}
pub fn max_symbol(&self) -> Option<u8> {
self.symbols.iter().max().map(|a| a as u8)
}
pub fn len(&self) -> usize {
self.symbols.len()
}
pub fn is_empty(&self) -> bool {
self.symbols.is_empty()
}
}
#[cfg_attr(feature = "serde_macros", derive(Serialize, Deserialize))]
pub struct RankTransform {
pub ranks: SymbolRanks,
}
impl RankTransform {
pub fn new(alphabet: &Alphabet) -> Self {
let mut ranks = VecMap::new();
for (r, c) in alphabet.symbols.iter().enumerate() {
ranks.insert(c, r as u8);
}
RankTransform { ranks: ranks }
}
pub fn get(&self, a: u8) -> u8 {
*self.ranks.get(a as usize).expect("Unexpected character.")
}
pub fn transform<'a, T: IntoTextIterator<'a>>(&self, text: T) -> Vec<u8> {
text.into_iter()
.map(|&c| *self.ranks.get(c as usize).expect("Unexpected character in text."))
.collect()
}
pub fn qgrams<'a, T: IntoTextIterator<'a>>(&'a self, q: u32, text: T) -> QGrams<T::IntoIter> {
let bits = (self.ranks.len() as f32).log2().ceil() as u32;
assert!((bits * q) as usize <= mem::size_of::<usize>() * 8,
"Expecting q to be smaller than usize / log2(|A|)");
let mut qgrams = QGrams {
text: text.into_iter(),
ranks: self,
bits: bits,
mask: (1 << (q * bits)) - 1,
qgram: 0,
};
for _ in 0..q - 1 {
qgrams.next();
}
qgrams
}
pub fn alphabet(&self) -> Alphabet {
let mut symbols = BitSet::with_capacity(self.ranks.len());
symbols.extend(self.ranks.keys());
Alphabet { symbols: symbols }
}
}
pub struct QGrams<'a, T: TextIterator<'a>> {
text: T,
ranks: &'a RankTransform,
bits: u32,
mask: usize,
qgram: usize,
}
impl<'a, T: TextIterator<'a>> QGrams<'a, T> {
fn qgram_push(&mut self, a: u8) {
self.qgram <<= self.bits;
self.qgram |= a as usize;
self.qgram &= self.mask;
}
}
impl<'a, T: TextIterator<'a>> Iterator for QGrams<'a, T> {
type Item = usize;
fn next(&mut self) -> Option<usize> {
match self.text.next() {
Some(a) => {
let b = self.ranks.get(*a);
self.qgram_push(b);
Some(self.qgram)
}
None => None,
}
}
}
#[cfg(tests)]
mod tests {
#[test]
#[cfg(feature = "nightly")]
fn test_serde() {
use serde::{Serialize, Deserialize};
fn impls_serde_traits<S: Serialize + Deserialize>() {}
impls_serde_traits::<RankTransform>();
}
}