diff --git a/libsais64-rs/src/lib.rs b/libsais64-rs/src/lib.rs index c2c9369..93d7158 100644 --- a/libsais64-rs/src/lib.rs +++ b/libsais64-rs/src/lib.rs @@ -13,10 +13,16 @@ include!(concat!(env!("OUT_DIR"), "/bindings.rs")); /// /// Returns Some with the suffix array build over the text if construction succeeds /// Returns None if construction of the suffix array failed -pub fn sais64_long(text: &mut Vec, alphabet_size: i64) -> Option> { +pub fn sais64_long(text: &mut Vec, alphabet_size: i64, sparseness_factor: u8) -> Option> { let mut sa = vec![0; text.len()]; let exit_code = unsafe { libsais64_long(text.as_mut_ptr(), sa.as_mut_ptr(), text.len() as i64, alphabet_size, 0) }; - if exit_code == 0 { Some(sa) } else { None } + if exit_code == 0 { + let sparseness_factor = sparseness_factor as i64; + for elem in sa.iter_mut() { + *elem *= sparseness_factor; + } + Some(sa) + } else { None } } #[cfg(test)] @@ -25,8 +31,14 @@ mod tests { #[test] fn check_build_sa_with_libsais64() { - let text = "banana$"; - let sa = sais64(text.as_bytes()); - assert_eq!(sa, Some(vec![6, 5, 3, 1, 0, 4, 2])); + let bits_per_char = 5; + let sparseness_factor = 4; + let mut text = [100834, // BANA + 493603, // NA-B + 80975, // ANAN + 65536 // A$ + ].to_vec(); + let sa = sais64_long(&mut text, 1 << (bits_per_char * sparseness_factor), sparseness_factor); + assert_eq!(sa, Some(vec![12, 8, 0, 4])); } } diff --git a/sa-builder/src/bitpacking.rs b/sa-builder/src/bitpacking.rs index 192d918..2c1404b 100644 --- a/sa-builder/src/bitpacking.rs +++ b/sa-builder/src/bitpacking.rs @@ -15,6 +15,10 @@ pub fn bitpack_text(text: &Vec, sparseness_factor: u8) -> Vec { let num_ints = (text.len() + (sparseness_factor-1)) / sparseness_factor; let mut text_packed = vec![0; num_ints]; + if text.len() == 0 { + return text_packed; + } + for i in 0..(num_ints-1) { let ti = i * sparseness_factor; let mut element = 0i64; diff --git a/sa-builder/src/lib.rs b/sa-builder/src/lib.rs index 97dcb93..9601f86 100644 --- a/sa-builder/src/lib.rs +++ b/sa-builder/src/lib.rs @@ -60,7 +60,8 @@ pub fn build_ssa( let mut sa = match construction_algorithm { SAConstructionAlgorithm::LibSais => { let mut packed_text = bitpack_text(text, sparseness_factor); - libsais64_rs::sais64_long(&mut packed_text, 1 << (BITS_PER_CHAR * sparseness_factor as usize)) + + libsais64_rs::sais64_long(&mut packed_text, 1 << (BITS_PER_CHAR * sparseness_factor as usize), sparseness_factor) }, SAConstructionAlgorithm::LibDivSufSort => libdivsufsort_rs::divsufsort64(text) }