-
Notifications
You must be signed in to change notification settings - Fork 72
/
rabin_karp.rs
143 lines (124 loc) · 3.55 KB
/
rabin_karp.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const MODULUS: u16 = 101;
const BASE: u16 = 256;
pub fn rabin_karp(target: String, pattern: String) -> Vec<usize> {
// Quick exit
if target.is_empty() || pattern.is_empty() || pattern.len() > target.len() {
return vec![];
}
let pattern_hash = hash(pattern.as_str());
// Pre-calculate BASE^(n-1)
let mut pow_rem: u16 = 1;
for _ in 0..pattern.len() - 1 {
pow_rem *= BASE;
pow_rem %= MODULUS;
}
let mut rolling_hash = 0;
let mut ret = vec![];
for i in 0..=target.len() - pattern.len() {
rolling_hash = if i == 0 {
hash(&target[0..pattern.len()])
} else {
recalculate_hash(
target.as_str(),
i - 1,
i + pattern.len() - 1,
rolling_hash,
pow_rem,
)
};
if rolling_hash == pattern_hash && pattern[..] == target[i..i + pattern.len()] {
ret.push(i);
}
}
ret
}
// hash(s) is defined as BASE^(n-1) * s_0 + BASE^(n-2) * s_1 + ... + BASE^0 * s_(n-1)
fn hash(s: &str) -> u16 {
let mut res: u16 = 0;
for &c in s.as_bytes().iter() {
res = (res * BASE % MODULUS + c as u16) % MODULUS;
}
res
}
// new_hash = (old_hash - BASE^(n-1) * s_(i-n)) * BASE + s_i
fn recalculate_hash(
s: &str,
old_index: usize,
new_index: usize,
old_hash: u16,
pow_rem: u16,
) -> u16 {
let mut new_hash = old_hash;
let (old_ch, new_ch) = (
s.as_bytes()[old_index] as u16,
s.as_bytes()[new_index] as u16,
);
new_hash = (new_hash + MODULUS - pow_rem * old_ch % MODULUS) % MODULUS;
new_hash = (new_hash * BASE + new_ch) % MODULUS;
new_hash
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn hi_hash() {
let hash_result = hash("hi");
assert_eq!(hash_result, 65);
}
#[test]
fn abr_hash() {
let hash_result = hash("abr");
assert_eq!(hash_result, 4);
}
#[test]
fn bra_hash() {
let hash_result = hash("bra");
assert_eq!(hash_result, 30);
}
// Attribution to @pgimalac for his tests from Knuth-Morris-Pratt
#[test]
fn each_letter_matches() {
let index = rabin_karp("aaa".to_string(), "a".to_string());
assert_eq!(index, vec![0, 1, 2]);
}
#[test]
fn a_few_separate_matches() {
let index = rabin_karp("abababa".to_string(), "ab".to_string());
assert_eq!(index, vec![0, 2, 4]);
}
#[test]
fn one_match() {
let index = rabin_karp("ABC ABCDAB ABCDABCDABDE".to_string(), "ABCDABD".to_string());
assert_eq!(index, vec![15]);
}
#[test]
fn lots_of_matches() {
let index = rabin_karp("aaabaabaaaaa".to_string(), "aa".to_string());
assert_eq!(index, vec![0, 1, 4, 7, 8, 9, 10]);
}
#[test]
fn lots_of_intricate_matches() {
let index = rabin_karp("ababababa".to_string(), "aba".to_string());
assert_eq!(index, vec![0, 2, 4, 6]);
}
#[test]
fn not_found0() {
let index = rabin_karp("abcde".to_string(), "f".to_string());
assert_eq!(index, vec![]);
}
#[test]
fn not_found1() {
let index = rabin_karp("abcde".to_string(), "ac".to_string());
assert_eq!(index, vec![]);
}
#[test]
fn not_found2() {
let index = rabin_karp("ababab".to_string(), "bababa".to_string());
assert_eq!(index, vec![]);
}
#[test]
fn empty_string() {
let index = rabin_karp("".to_string(), "abcdef".to_string());
assert_eq!(index, vec![]);
}
}