Skip to content

Commit

Permalink
fixed tests for libsais with bitpacking
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonVandeVyver committed Oct 14, 2024
1 parent 850b2ee commit c09b61a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
22 changes: 17 additions & 5 deletions libsais64-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<i64>, alphabet_size: i64) -> Option<Vec<i64>> {
pub fn sais64_long(text: &mut Vec<i64>, alphabet_size: i64, sparseness_factor: u8) -> Option<Vec<i64>> {
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)]
Expand All @@ -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]));
}
}
4 changes: 4 additions & 0 deletions sa-builder/src/bitpacking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ pub fn bitpack_text(text: &Vec<u8>, sparseness_factor: u8) -> Vec<i64> {
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;
Expand Down
3 changes: 2 additions & 1 deletion sa-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit c09b61a

Please sign in to comment.