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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/**************************************************************************************************
* Copyright 2018 GrayJack
* All rights reserved.
*
* This Source Code Form is subject to the terms of the BSD 3-Clause License.
**************************************************************************************************/

//! A module for using pattern matching algorithms.
//!

/// **Brute Force:** Search for the pattern in the `find` parameter in a slice.
///
/// It returns `Ok` holding the index of the first character of `find` that was found
/// or `Err` holding the last index it searched if not find.
///
/// |   Case    | Time complexity | Space complexity |
/// |:----------|:---------------:|:----------------:|
/// | Best:     | Ω(m*(n-m+1))    |                  |
/// | Avrg:     | θ(m*(n-m+1))    |                  |
/// | Worst:    | O(m*(n-m+1))    | O(1)             |
///
/// # Example
/// ```rust
/// use algos::pattern;
///
/// let p = "ATCGGATTTCAGAAGCT".as_bytes();
///
/// let find = pattern::bruteforce(&p, &"TTT".as_bytes());
/// assert_eq!(find, Ok(6));
/// ```
pub fn bruteforce(pattern: &[u8], find: &[u8]) -> Result<usize,usize> {
    let (size_patt, size_find) = (pattern.len()-1, find.len()-1);
    let mut cnt = 0; // counter
    for i in 0..=size_patt-size_find {
        let mut j = 0;
        while j < size_find && find[j] == pattern[i+j] {
            if pattern[i+j] != find[j] {
                break;
            }
            j += 1;
        }
        if j == size_find {
            return Ok(i);
        }
        cnt += 1;
    }
    Err(cnt)
}

/// **Karp-Rabin:** Search for the pattern in the `find` parameter in a slice.
///
/// It returns `Ok` holding the index of the first character of `find` that was found
/// or `Err` holding the last index it searched if not find.
///
/// |   Case    | Time complexity | Space complexity |
/// |:----------|:---------------:|:----------------:|
/// | Best:     | Ω(n+m)          |                  |
/// | Avrg:     | θ(n+m)          |                  |
/// | Worst:    | O(m*(n+m))      | O(1)             |
///
/// # Example
/// ```rust
/// use algos::pattern;
///
/// let p = "ATCGGATTTCAGAAGCT".as_bytes();
///
/// let find = pattern::karp_rabin(&p, &"TTT".as_bytes());
/// assert_eq!(find, Ok(6));
/// ```
pub fn karp_rabin(pattern: &[u8], find: &[u8]) -> Result<usize,usize> {
    let (size_patt, size_find) = (pattern.len()-1, find.len()-1);

    // Preprocessing
    // TODO: There is a way to do the preprocessing using dynamic programming, making the
    // preprocessing time linear, making the worst case O(n+m)
    // Using closure cause rust let us use it FUNCTIONAL PROGRAMMING HELL YEAH
    let rehash = |a, b, hash, base| (((hash - a*base) << 1) + b);

    // 2^(m-1)
    let base = {
        let mut x = 1;
        for _ in 1..size_find { x = x<<1 }
        x
    };
    // Calculate the hashes
    let (mut hash_patt, hash_find) = {
        let (mut x, mut y) = (0, 0);
        for i in 0..size_find {
            y = (y << 1) + find[i];
            x = (x << 1) + pattern[i];
        }
        (x, y)
    };

    // Searching
    let (mut i, mut j) = (0, 0);
    while i <= size_patt-size_find {
        if hash_patt == hash_find {
            // Check one by one
            while j < size_find {
                if pattern[i+j] != find[j] {
                    break;
                }
                j += 1;
            }

            if j == size_find {
                return Ok(i);
            }
        }

        hash_patt = rehash(pattern[i], pattern[i+size_find], hash_patt, base);
        i += 1;
    }
    Err(i)
}

/// **Boyer-Moore:** Search for the pattern in the `find` parameter in a slice.
///
/// It returns `Ok` holding the index of the first character of `find` that was found
/// or `Err` holding the last index it searched if not find.
///
/// |   Case    | Time complexity | Space complexity |
/// |:----------|:---------------:|:----------------:|
/// | Best:     | Ω(n/m)          |                  |
/// | Avrg:     | θ(n+m)          |                  |
/// | Worst:    | O(nm)           | O(m+δ)           |
///
///
/// **Obs.:** δ is the max size of u8.
///
/// # Example
/// ```rust
/// use algos::pattern;
///
/// let p = "ATCGGATTTCAGAAGCT".as_bytes();
///
/// let find = pattern::boyer_moore(&p, &"TTT".as_bytes());
/// assert_eq!(find, Ok(6));
/// ```
pub fn boyer_moore(pattern: &[u8], find: &[u8]) -> Result<usize,usize> {
    let (size_patt, size_find) = (pattern.len()-1, find.len());
    let mut good_sufix_table = vec![0usize; size_find];
    let mut bad_char_table   = vec![0usize; 256];

    // Preprocessing
    // Good sufix
    preprocess_good_sufix(find, &mut good_sufix_table[..]);
    preprocess_bad_char(find, &mut bad_char_table[..]);

    // Searching
    let (mut i, mut j) = (size_find-1, 0);
    while j <= size_patt - size_find {
        while find[i] == pattern[i + j] && i > 0 {
            i -= 1;
        }
        if i == 0 {
            let mut k = 0;
            while k < size_find && find[k] == pattern[j+k] {
                k += 1;
            }
            if k == size_find {
                return Ok(j);
            }
            else {
                j += good_sufix_table[0];
            }
        }
        else {
            j += good_sufix_table[i].max(bad_char_table[pattern[i + j] as usize - size_find + 1 + i]);
        }
    }

    Err(j)
}

/// **Horspool:** Search for the pattern in the `find` parameter in a slice.
///
/// It returns `Ok` holding the index of the first character of `find` that was found
/// or `Err` holding the last index it searched if not find.
///
/// It is a variation of the Boyer-Moore algorithm.
///
/// |   Case    | Time complexity | Space complexity |
/// |:----------|:---------------:|:----------------:|
/// | Best:     | Ω(n/m)          |                  |
/// | Avrg:     | θ(n+m)          |                  |
/// | Worst:    | O(nm)           | O(δ)             |
///
///
/// **Obs.:** δ is the max size of u8.
///
/// # Example
/// ```rust
/// use algos::pattern;
///
/// let p = "ATCGGATTTCAGAAGCT".as_bytes();
///
/// let find = pattern::horspool(&p, &"TTT".as_bytes());
/// assert_eq!(find, Ok(6));
/// ```
pub fn horspool(pattern: &[u8], find: &[u8]) -> Result<usize,usize> {
    let (size_patt, size_find) = (pattern.len()-1, find.len()-1);
    let mut bad_char_table = vec![0usize; 256];

    // Preprocessing
    preprocess_bad_char(find, &mut bad_char_table[..]);

    let (mut i, mut j) = (0, 0);
    while i <= size_patt - size_find {
        let c = pattern[i + size_find - 1];
        if find[size_find - 1] == c {
            // Check one by one
            while j < size_find {
                if pattern[i+j] != find[j] {
                    break;
                }
                j += 1;
            }

            if j == size_find {
                return Ok(i);
            }
        }

        i += bad_char_table[c as usize];
    }

    Err(i)
}

fn preprocess_good_sufix(find: &[u8], good_sufix_table: &mut [usize]) {
    let size = find.len() - 1;
    // Good sufix
    let mut suff = vec![0; size];
    let mut good = size-1;
    let mut f    = 0;
    suff[size-1] = size;
    for i in (0..suff.len()).rev() {
        if i > good && suff[i + size-1 + f] < i - good {
            suff[i] = suff[i + size-1 + f];
        }
        else {
            good = if (good) < i { i } else { good };
            f = i;
            while good == 0 && find[good] == find[good + size-1 -f] {
                good -= 1;
            }
            suff[i] = good-f;
        }
    }

    for i in 0..size {
        good_sufix_table[i] = size;
    }
    for i in (0..=size-1).rev() {
        if suff[i] == i+1 {
            for j in 0..size-1 {
                if good_sufix_table[j] == size {
                    good_sufix_table[j] = size - i - 1;
                }
            }
        }
    }
    for i in 0..=size-1 {
        good_sufix_table[size - 1 - suff[i] as usize] = size - 1 - i;
    }
}

fn preprocess_bad_char(find: &[u8], bad_char_table: &mut [usize]) {
    let size = find.len()-1;

    for i in 0..256 {
        bad_char_table[i] = size;
    }
    for i in 0..size-1 {
        bad_char_table[find[i] as usize] = size - i - 1;
    }
}


#[cfg(test)]
pub mod test {
    use pattern::*;

    #[test]
    pub fn test_bruteforce() {
        let p = "ATCGGATTTCAGAAGCT".as_bytes();

        let find = bruteforce(&p, &"TTT".as_bytes());
        let find2 = bruteforce(&p, &"AAG".as_bytes());
        assert_eq!(find, Ok(6));
        assert_eq!(find2, Ok(12));
    }

    #[test]
    pub fn test_karp_rabin() {
        let p = "ATCGGATTTCAGAAGCT".as_bytes();

        let find = karp_rabin(&p, &"TTT".as_bytes());
        let find2 = karp_rabin(&p, &"AAG".as_bytes());
        assert_eq!(find, Ok(6));
        assert_eq!(find2, Ok(12));
    }

    #[test]
    pub fn test_boyer_moore() {
        let p = "ATCGGATTTCAGAAGCT".as_bytes();

        let find = boyer_moore(&p, &"TTT".as_bytes());
        let find2 = boyer_moore(&p, &"AAG".as_bytes());
        assert_eq!(find, Ok(6));
        assert_eq!(find2, Ok(12));
    }

    #[test]
    pub fn test_horspool() {
        let p = "ATCGGATTTCAGAAGCT".as_bytes();

        let find = horspool(&p, &"TTT".as_bytes());
        let find2 = horspool(&p, &"AAG".as_bytes());
        assert_eq!(find, Ok(6));
        assert_eq!(find2, Ok(12));
    }
}