Skip to content

Commit

Permalink
Share ROOT_STATE_IDX and DEAD_STATE_IDX (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
vbkaisetsu authored Jun 7, 2022
1 parent f618d3e commit 8335137
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
22 changes: 12 additions & 10 deletions src/charwise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -910,20 +910,22 @@ impl CharwiseDoubleArrayAhoCorasick {
/// `state_id` must be smaller than the length of states.
#[inline(always)]
unsafe fn get_next_state_id_leftmost_unchecked(&self, mut state_id: u32, c: char) -> u32 {
loop {
if let Some(mapped_c) = self.mapper.get(c) {
if let Some(mapped_c) = self.mapper.get(c) {
loop {
if let Some(state_id) = self.get_child_index_unchecked(state_id, mapped_c) {
return state_id;
}
if state_id == ROOT_STATE_IDX {
return ROOT_STATE_IDX;
}
let fail_id = self.states.get_unchecked(state_id as usize).fail();
if fail_id == DEAD_STATE_IDX {
return ROOT_STATE_IDX;
}
state_id = fail_id;
}
if state_id == ROOT_STATE_IDX {
return ROOT_STATE_IDX;
}
let fail_id = self.states.get_unchecked(state_id as usize).fail();
if fail_id == DEAD_STATE_IDX {
return DEAD_STATE_IDX;
}
state_id = fail_id;
} else {
ROOT_STATE_IDX
}
}
}
Expand Down
18 changes: 13 additions & 5 deletions src/charwise/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use core::num::NonZeroU32;

use crate::charwise::CharwiseDoubleArrayAhoCorasick;
use crate::Match;
use crate::{DEAD_STATE_IDX, ROOT_STATE_IDX};
use crate::ROOT_STATE_IDX;

/// Iterator for some struct that implements [`AsRef<str>`].
pub struct StrIterator<P> {
Expand Down Expand Up @@ -244,7 +244,7 @@ where
#[inline(always)]
fn next(&mut self) -> Option<Self::Item> {
let mut state_id = ROOT_STATE_IDX;
let mut last_output_pos = None;
let mut last_output_pos: Option<NonZeroU32> = None;

let mut skips = 0;
for c in unsafe { self.haystack.as_ref().get_unchecked(self.pos..) }.chars() {
Expand All @@ -253,9 +253,17 @@ where
// state_id is always smaller than self.pma.states.len() because
// self.pma.get_next_state_id_leftmost_unchecked() ensures to return such a value.
state_id = unsafe { self.pma.get_next_state_id_leftmost_unchecked(state_id, c) };
if state_id == DEAD_STATE_IDX {
debug_assert!(last_output_pos.is_some());
break;
if state_id == ROOT_STATE_IDX {
if let Some(output_pos) = last_output_pos {
// last_output_pos is always smaller than self.pma.outputs.len() because
// State::output_pos() ensures to return such a value when it is Some.
let out = unsafe { self.pma.outputs.get_unchecked(output_pos.get() as usize) };
return Some(Match {
length: out.length() as usize,
end: self.pos,
value: out.value() as usize,
});
}
}

// state_id is always smaller than self.pma.states.len() because
Expand Down

0 comments on commit 8335137

Please sign in to comment.