#bindings #abc #bc #lr #abcb #b-tree-trans-table #abcbc

general-sam

A general suffix automaton implementation in Rust

17 releases (2 stable)

Uses new Rust 2024

new 1.0.1 Mar 22, 2025
1.0.0 Mar 27, 2024
0.7.0 Feb 29, 2024
0.6.1 Nov 27, 2023

#166 in Data structures

Download history 282/week @ 2024-12-02 236/week @ 2024-12-09 135/week @ 2024-12-16 97/week @ 2024-12-23 15/week @ 2024-12-30 111/week @ 2025-01-06 161/week @ 2025-01-13 67/week @ 2025-01-20 21/week @ 2025-01-27 45/week @ 2025-02-03 46/week @ 2025-02-10 34/week @ 2025-02-17 65/week @ 2025-02-24 47/week @ 2025-03-03 51/week @ 2025-03-10 528/week @ 2025-03-17

695 downloads per month
Used in 4 crates (3 directly)

MIT/Apache

94KB
2.5K SLoC

general-sam

Crates.io Docs.rs License Build status

A general suffix automaton implementation in Rust.

Python bindings and some utilities are also available. Please check out general-sam-py.

flowchart LR
  init((ε))
  a((a))
  b((b))
  ab((ab))
  bc(((bc)))
  abc((abc))
  abcb((abcb))
  abcbc(((abcbc)))

  init -- a --> a
  init -- b --> b
  a -- b --> ab
  b -- c --> bc
  init -- c --> bc
  ab -- c --> abc
  bc -- b --> abcb
  abc -- b --> abcb
  abcb -- c --> abcbc

The suffix automaton of abcbc.

Examples

use general_sam::{GeneralSam, BTreeTransTable};

let sam = GeneralSam::<BTreeTransTable<_>>::from_bytes("abcbc");

// "cbc" is a suffix of "abcbc"
assert!(sam.get_root_state().feed_bytes("cbc").is_accepting());

// "bcb" is not a suffix of "abcbc"
assert!(!sam.get_root_state().feed_bytes("bcb").is_accepting());
use general_sam::{GeneralSam, BTreeTransTable};

let sam = GeneralSam::<BTreeTransTable<_>>::from_chars("abcbc");

let mut state = sam.get_root_state();

// "b" is not a suffix but at least a substring of "abcbc"
state.feed_chars("b");
assert!(!state.is_accepting());

// "bc" is a suffix of "abcbc"
state.feed_chars("c");
assert!(state.is_accepting());

// "bcbc" is a suffix of "abcbc"
state.feed_chars("bc");
assert!(state.is_accepting());

// "bcbcbc" is not a substring, much less a suffix of "abcbc"
state.feed_chars("bc");
assert!(!state.is_accepting() && state.is_nil());
# #[cfg(feature = "trie")] {
use general_sam::{GeneralSam, Trie, BTreeTransTable};

let mut trie = Trie::<BTreeTransTable<_>>::default();
trie.insert("hello".chars());
trie.insert("Chielo".chars());

let sam = GeneralSam::<BTreeTransTable<_>>::from_trie(trie.get_root_state());

assert!(sam.get_root_state().feed_chars("lo").is_accepting());
assert!(sam.get_root_state().feed_chars("ello").is_accepting());
assert!(sam.get_root_state().feed_chars("elo").is_accepting());

assert!(!sam.get_root_state().feed_chars("el").is_accepting());
assert!(!sam.get_root_state().feed_chars("el").is_nil());

assert!(!sam.get_root_state().feed_chars("bye").is_accepting());
assert!(sam.get_root_state().feed_chars("bye").is_nil());
# }

References

License

This project is licensed under either of

at your option.

The SPDX license identifier for this project is MIT OR Apache-2.0.

Dependencies

~0–305KB