forked from TheAlgorithms/Rust
-
Notifications
You must be signed in to change notification settings - Fork 1
/
burrows_wheeler_transform.rs
122 lines (111 loc) · 3.44 KB
/
burrows_wheeler_transform.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
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
pub fn burrows_wheeler_transform(input: String) -> (String, usize) {
let len = input.len();
let mut table = Vec::<String>::with_capacity(len);
for i in 0..len {
table.push(input[i..].to_owned() + &input[..i]);
}
table.sort_by_key(|a| a.to_lowercase());
let mut encoded = String::new();
let mut index: usize = 0;
for (i, item) in table.iter().enumerate().take(len) {
encoded.push(item.chars().last().unwrap());
if item.eq(&input) {
index = i;
}
}
(encoded, index)
}
pub fn inv_burrows_wheeler_transform(input: (String, usize)) -> String {
let len = input.0.len();
let mut table = Vec::<(usize, char)>::with_capacity(len);
for i in 0..len {
table.push((i, input.0.chars().nth(i).unwrap()));
}
table.sort_by(|a, b| a.1.cmp(&b.1));
let mut decoded = String::new();
let mut idx = input.1;
for _ in 0..len {
decoded.push(table[idx].1);
idx = table[idx].0;
}
decoded
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
//Ensure function stand-alone legitimacy
fn stand_alone_function() {
assert_eq!(
burrows_wheeler_transform("CARROT".to_string()),
("CTRRAO".to_string(), 1usize)
);
assert_eq!(
inv_burrows_wheeler_transform(("CTRRAO".to_string(), 1usize)),
("CARROT".to_string())
);
assert_eq!(
burrows_wheeler_transform("THEALGORITHMS".to_string()),
("EHLTTRAHGOMSI".to_string(), 11usize)
);
assert_eq!(
inv_burrows_wheeler_transform(("EHLTTRAHGOMSI".to_string(), 11usize)),
("THEALGORITHMS".to_string())
);
assert_eq!(
burrows_wheeler_transform("!.!.!??.=::".to_string()),
(":..!!?:=.?!".to_string(), 0usize)
);
assert_eq!(
inv_burrows_wheeler_transform((":..!!?:=.?!".to_string(), 0usize)),
"!.!.!??.=::"
);
}
#[test]
fn basic_characters() {
assert_eq!(
inv_burrows_wheeler_transform(burrows_wheeler_transform("CARROT".to_string())),
"CARROT"
);
assert_eq!(
inv_burrows_wheeler_transform(burrows_wheeler_transform("TOMATO".to_string())),
"TOMATO"
);
assert_eq!(
inv_burrows_wheeler_transform(burrows_wheeler_transform("THISISATEST".to_string())),
"THISISATEST"
);
assert_eq!(
inv_burrows_wheeler_transform(burrows_wheeler_transform("THEALGORITHMS".to_string())),
"THEALGORITHMS"
);
assert_eq!(
inv_burrows_wheeler_transform(burrows_wheeler_transform("RUST".to_string())),
"RUST"
);
}
#[test]
fn special_characters() {
assert_eq!(
inv_burrows_wheeler_transform(burrows_wheeler_transform("!.!.!??.=::".to_string())),
"!.!.!??.=::"
);
assert_eq!(
inv_burrows_wheeler_transform(burrows_wheeler_transform(
"!{}{}(((&&%%!??.=::".to_string()
)),
"!{}{}(((&&%%!??.=::"
);
assert_eq!(
inv_burrows_wheeler_transform(burrows_wheeler_transform("//&$[]".to_string())),
"//&$[]"
);
}
#[test]
fn empty() {
assert_eq!(
inv_burrows_wheeler_transform(burrows_wheeler_transform("".to_string())),
""
);
}
}