11 releases

0.1.10 Oct 5, 2023
0.1.9 Oct 5, 2023
0.1.8 Jun 6, 2022
0.1.7 Sep 2, 2021
0.1.1 Jun 6, 2020

#93 in Data structures

Download history 42946/week @ 2024-07-01 41850/week @ 2024-07-08 35107/week @ 2024-07-15 40384/week @ 2024-07-22 35983/week @ 2024-07-29 36918/week @ 2024-08-05 34313/week @ 2024-08-12 36775/week @ 2024-08-19 38186/week @ 2024-08-26 36920/week @ 2024-09-02 41147/week @ 2024-09-09 41387/week @ 2024-09-16 43115/week @ 2024-09-23 46293/week @ 2024-09-30 45397/week @ 2024-10-07 52183/week @ 2024-10-14

189,875 downloads per month
Used in 100 crates (2 directly)

Apache-2.0

175KB
857 lines

esaxx-rs

This code implements a fast suffix tree / suffix array.

This code is taken from sentencepiece and to be used by hugging face.

Small wrapper around sentencepiece's esaxx suffix array C++ library. Usage

let string = "abracadabra";
let suffix = esaxx_rs::suffix(string).unwrap();
let chars: Vec<_> = string.chars().collect();
let mut iter = suffix.iter();
assert_eq!(iter.next().unwrap(), (&chars[..4], 2)); // abra
assert_eq!(iter.next(), Some((&chars[..1], 5))); // a
assert_eq!(iter.next(), Some((&chars[1..4], 2))); // bra
assert_eq!(iter.next(), Some((&chars[2..4], 2))); // ra
assert_eq!(iter.next(), Some((&chars[..0], 11))); // ''
assert_eq!(iter.next(), None);

lib.rs:

Small wrapper around sentencepiece's esaxx suffix array C++ library. Usage

#[cfg(feature="cpp")]
{
let string = "abracadabra";
let suffix = esaxx_rs::suffix(string).unwrap();
let chars: Vec<_> = string.chars().collect();
let mut iter = suffix.iter();
assert_eq!(iter.next().unwrap(), (&chars[..4], 2)); // abra
assert_eq!(iter.next(), Some((&chars[..1], 5))); // a
assert_eq!(iter.next(), Some((&chars[1..4], 2))); // bra
assert_eq!(iter.next(), Some((&chars[2..4], 2))); // ra
assert_eq!(iter.next(), Some((&chars[..0], 11))); // ''
assert_eq!(iter.next(), None);
}

The previous version uses unsafe optimized c++ code. There exists another implementation a bit slower (~2x slower) that uses safe rust. It's a bit slower because it uses usize (mostly 64bit) instead of i32 (32bit). But it does seems to fix a few OOB issues in the cpp version (which never seemed to cause real problems in tests but still.)

let string = "abracadabra";
let suffix = esaxx_rs::suffix_rs(string).unwrap();
let chars: Vec<_> = string.chars().collect();
let mut iter = suffix.iter();
assert_eq!(iter.next().unwrap(), (&chars[..4], 2)); // abra
assert_eq!(iter.next(), Some((&chars[..1], 5))); // a
assert_eq!(iter.next(), Some((&chars[1..4], 2))); // bra
assert_eq!(iter.next(), Some((&chars[2..4], 2))); // ra
assert_eq!(iter.next(), Some((&chars[..0], 11))); // ''
assert_eq!(iter.next(), None);

No runtime deps