lexf 0.1.0

Zero-unsafe multilingual morphological expander and query normalizer for search indexing
Documentation
//! Bounded, insertion-ordered dedup set of emitted forms.

/// Emitted forms packed into one string arena; `end_li[i]` is the end offset of form `i`.
///
/// Linear scan is optimal here: the set never exceeds `max_form` (single digits) entries.
#[derive(Debug, Default, Clone)]
pub(crate) struct Seen {
  buf: String,
  end_li: Vec<usize>,
  max: usize,
}

impl Seen {
  /// Clears all forms and sets the capacity cap.
  pub(crate) fn reset(&mut self, max: usize) {
    self.buf.clear();
    self.end_li.clear();
    self.max = max;
  }

  /// Returns `true` when the capacity cap has been reached.
  #[inline]
  pub(crate) fn is_full(&self) -> bool {
    self.end_li.len() >= self.max
  }

  fn contains(&self, s: &str) -> bool {
    let raw = self.buf.as_bytes();
    let target = s.as_bytes();
    let mut start = 0;
    self.end_li.iter().any(|&end| {
      let hit = end - start == target.len() && raw.get(start..end) == Some(target);
      start = end;
      hit
    })
  }

  /// Records `s`, returning `true` when it is non-empty, unseen, and under the cap.
  pub(crate) fn insert(&mut self, s: &str) -> bool {
    if s.is_empty() || self.is_full() || self.contains(s) {
      return false;
    }
    self.buf.push_str(s);
    self.end_li.push(self.buf.len());
    true
  }
}