-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathmove_to_front.rs
60 lines (50 loc) · 1.5 KB
/
move_to_front.rs
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
// https://en.wikipedia.org/wiki/Move-to-front_transform
fn blank_char_table() -> Vec<char> {
(0..=255).map(|ch| ch as u8 as char).collect()
}
pub fn move_to_front_encode(text: &str) -> Vec<u8> {
let mut char_table = blank_char_table();
let mut result = Vec::new();
for ch in text.chars() {
if let Some(position) = char_table.iter().position(|&x| x == ch) {
result.push(position as u8);
char_table.remove(position);
char_table.insert(0, ch);
}
}
result
}
pub fn move_to_front_decode(encoded: &[u8]) -> String {
let mut char_table = blank_char_table();
let mut result = String::new();
for &pos in encoded {
let ch = char_table[pos as usize];
result.push(ch);
char_table.remove(pos as usize);
char_table.insert(0, ch);
}
result
}
#[cfg(test)]
mod test {
use super::*;
macro_rules! test_mtf {
($($name:ident: ($text:expr, $encoded:expr),)*) => {
$(
#[test]
fn $name() {
assert_eq!(move_to_front_encode($text), $encoded);
assert_eq!(move_to_front_decode($encoded), $text);
}
)*
}
}
test_mtf! {
empty: ("", &[]),
single_char: ("@", &[64]),
repeated_chars: ("aaba", &[97, 0, 98, 1]),
mixed_chars: ("aZ!", &[97, 91, 35]),
word: ("banana", &[98, 98, 110, 1, 1, 1]),
special_chars: ("\0\n\t", &[0, 10, 10]),
}
}