libsais_sys/
lib.rs

1/*!
2Raw bindings to the [`libsais`](https://github.com/IlyaGrebnov/libsais) C library by Ilya Grebnov.
3
4The `libsais` library provides fast linear-time construction of suffix array (SA),
5generalized suffix array (GSA), longest common prefix (LCP) array, permuted LCP (PLCP) array,
6Burrows-Wheeler transform (BWT) and inverse BWT, based on the induced sorting algorithm
7(with optional OpenMP support for multi-core parallel construction).
8
9The OpenMP support is guarded by the `openmp` feature of this crate, which is enabled by default.
10*/
11
12#![allow(non_camel_case_types)]
13
14extern crate openmp_sys;
15
16/// Version of the library for 8-bit inputs smaller than 2GB (2147483648 bytes).
17/// Also allows integer array inputs in some instances.
18pub mod libsais;
19
20/// Extension of the library for inputs larger or equal to 2GB (2147483648 bytes).
21pub mod libsais64;
22
23/// Independent version of `libsais` for 16-bit inputs.
24pub mod libsais16;
25
26/// Independent version of `libsais64` for 16-bit inputs.
27pub mod libsais16x64;
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32
33    fn is_suffix_array(text: &[u8], maybe_suffix_array: &[i32]) -> bool {
34        if text.is_empty() && maybe_suffix_array.is_empty() {
35            return true;
36        }
37
38        for indices in maybe_suffix_array.windows(2) {
39            let previous = indices[0] as usize;
40            let current = indices[1] as usize;
41
42            if &text[previous..] > &text[current..] {
43                return false;
44            }
45        }
46
47        true
48    }
49
50    #[test]
51    fn libsais_basic() {
52        let text = b"abababcabba";
53        let mut suffix_array = [0; 11];
54        let res = unsafe {
55            libsais::libsais(
56                text.as_ptr(),
57                suffix_array.as_mut_ptr(),
58                text.len() as i32,
59                0,
60                std::ptr::null_mut(),
61            )
62        };
63
64        assert_eq!(res, 0);
65        assert!(is_suffix_array(text, &suffix_array))
66    }
67
68    #[cfg(feature = "openmp")]
69    #[test]
70    fn libsais_omp() {
71        let text = b"abababcabba";
72        let mut suffix_array = [0; 11];
73        let res = unsafe {
74            libsais::libsais_omp(
75                text.as_ptr(),
76                suffix_array.as_mut_ptr(),
77                text.len() as i32,
78                0,
79                std::ptr::null_mut(),
80                2,
81            )
82        };
83
84        assert_eq!(res, 0);
85        assert!(is_suffix_array(text, &suffix_array))
86    }
87}