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
use pattern_matching::shift_and::masks;
use utils::{TextSlice, IntoTextIterator};
pub struct BNDM {
    m: usize,
    masks: [u64; 256],
    accept: u64,
}
impl BNDM {
    
    pub fn new<'a, P: IntoTextIterator<'a>>(pattern: P) -> Self where
        P::IntoIter: DoubleEndedIterator + ExactSizeIterator {
        let pattern = pattern.into_iter();
        let m = pattern.len();
        assert!(m <= 64, "Expecting a pattern of at most 64 symbols.");
        
        
        let (masks, accept) = masks(pattern.rev());
        BNDM {
            m: m,
            masks: masks,
            accept: accept,
        }
    }
    
    pub fn find_all<'a>(&'a self, text: TextSlice<'a>) -> Matches {
        Matches {
            bndm: self,
            window: self.m,
            text: text,
        }
    }
}
pub struct Matches<'a> {
    bndm: &'a BNDM,
    window: usize,
    text: TextSlice<'a>,
}
impl<'a> Iterator for Matches<'a> {
    type Item = usize;
    fn next(&mut self) -> Option<usize> {
        while self.window <= self.text.len() {
            let mut occ = None;
            
            let mut active = (1u64 << self.bndm.m) - 1;
            let (mut j, mut lastsuffix) = (1, 0);
            
            while active != 0 {
                
                active &= self.bndm.masks[self.text[self.window - j] as usize];
                if active & self.bndm.accept != 0 {
                    
                    if j == self.bndm.m {
                        occ = Some(self.window - self.bndm.m);
                        break;
                    } else {
                        
                        
                        
                        
                        
                        
                        
                        lastsuffix = j;
                    }
                }
                j += 1;
                active <<= 1;
            }
            
            self.window += self.bndm.m - lastsuffix;
            if occ.is_some() {
                return occ;
            }
        }
        None
    }
}