Skip to main content

Fsa

Struct Fsa 

Source
pub struct Fsa<D> { /* private fields */ }
Expand description

A read-only minimal-FSA map over a byte container.

Implementations§

Source§

impl<D: AsRef<[u8]>> Fsa<D>

Source

pub fn new(data: D) -> Result<Self, FsaError>

Load an FSA from a serialized byte buffer. Accepts anything that implements AsRef<[u8]> (&[u8], Vec<u8>, mmap::Mmap, etc.). The buffer is parsed header-only — no allocation per entry.

Returns FsaError::BadMagic / FsaError::BadVersion / FsaError::Truncated on invalid input; never panics.

use inputx_fsa::{Builder, Fsa, FsaError};
let mut b = Builder::new();
b.insert(b"hello", 42);
let bytes = b.finish();

let fsa = Fsa::new(&bytes[..]).unwrap();
assert_eq!(fsa.len(), 1);

// Corrupt magic → graceful error, no panic.
let mut bad = bytes.clone();
bad[0] = 0;
assert!(matches!(Fsa::new(&bad[..]), Err(FsaError::BadMagic)));
Source

pub fn len(&self) -> u64

Number of keys stored.

Source

pub fn is_empty(&self) -> bool

Source

pub fn state_count(&self) -> u32

Total number of states in the automaton (diagnostics).

Source

pub fn get(&self, key: &[u8]) -> Option<u64>

Look up keySome(value) for an exact match, None otherwise. Constant cost per byte; the buffer is never copied.

use inputx_fsa::{Builder, Fsa};
let mut b = Builder::new();
b.insert(b"ni", 1);
b.insert(b"nihao", 100);
let fsa = Fsa::new(b.finish()).unwrap();
assert_eq!(fsa.get(b"ni"), Some(1));
assert_eq!(fsa.get(b"nihao"), Some(100));
assert_eq!(fsa.get(b"niha"), None);  // prefix-only, not a key
assert_eq!(fsa.get(b"absent"), None);
Source

pub fn contains_prefix(&self, prefix: &[u8]) -> bool

true if any stored key starts with prefix.

Source

pub fn prefix(&self, prefix: &[u8]) -> Vec<(Vec<u8>, u64)>

All (key, value) pairs whose key starts with prefix, sorted.

Source

pub fn prefix_for_each<F: FnMut(&[u8], u64)>(&self, prefix: &[u8], visit: F)

Streaming variant: invoke visit(key, value) for every (key, value) whose key starts with prefix, in sorted order, without allocating a result vector. The key slice is valid only for the call. This is the hot-path entry — a bare-letter prefix can match tens of thousands of keys, and materializing them all would dominate cost.

use inputx_fsa::{Builder, Fsa};
let mut b = Builder::new();
for (k, v) in [(&b"apple"[..], 1u64), (b"apply", 2), (b"banana", 3)] {
    b.insert(k, v);
}
let fsa = Fsa::new(b.finish()).unwrap();

let mut count = 0;
let mut last_value = 0;
fsa.prefix_for_each(b"app", |_key, value| {
    count += 1;
    last_value = value;
});
assert_eq!(count, 2);          // apple + apply
assert_eq!(last_value, 2);     // "apply" sorts last
Source

pub fn iter(&self) -> FsaIter<'_, D>

Lazy iterator over all (key, value) pairs in sorted order.

Source

pub fn range(&self, prefix: &[u8]) -> FsaIter<'_, D>

Lazy iterator over (key, value) pairs whose key starts with prefix, in sorted order. Unlike prefix it allocates no result vector and supports early termination (.take, .find, break).

Auto Trait Implementations§

§

impl<D> Freeze for Fsa<D>
where D: Freeze,

§

impl<D> RefUnwindSafe for Fsa<D>
where D: RefUnwindSafe,

§

impl<D> Send for Fsa<D>
where D: Send,

§

impl<D> Sync for Fsa<D>
where D: Sync,

§

impl<D> Unpin for Fsa<D>
where D: Unpin,

§

impl<D> UnsafeUnpin for Fsa<D>
where D: UnsafeUnpin,

§

impl<D> UnwindSafe for Fsa<D>
where D: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.