cdivsufsort/
lib.rs

1extern "C" {
2    fn divsufsort(T: *const u8, SA: *mut i32, n: i32) -> i32;
3    pub fn dss_flush();
4}
5
6/// Sort suffixes of `text` and store their lexographic order
7/// in the given suffix array `sa`.
8/// Will panic if `sa.len()` != `text.len()`
9pub fn sort_in_place(text: &[u8], sa: &mut [i32]) {
10    assert_eq!(
11        text.len(),
12        sa.len(),
13        "text and suffix array should have same len"
14    );
15    assert!(
16        text.len() < i32::max_value() as usize,
17        "text too large, should not exceed {} bytes",
18        i32::max_value() - 1
19    );
20
21    let ret = unsafe { divsufsort(text.as_ptr(), sa.as_mut_ptr(), text.len() as i32) };
22    assert_eq!(0, ret);
23}
24
25//// Sort suffixes
26pub fn sort<'a>(text: &'a [u8]) -> sacabase::SuffixArray<i32> {
27    let mut sa = vec![0; text.len()];
28    sort_in_place(text, &mut sa);
29    sacabase::SuffixArray::new(text, sa)
30}