-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from ajalab/restructure-suffix-array-module
Restructure suffix array module
- Loading branch information
Showing
9 changed files
with
120 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,112 +1,5 @@ | ||
//! Suffix arrays, used to construct the index. | ||
//! | ||
//! Can also be used in sampled fashion to perform locate queries. | ||
use crate::{seal, util}; | ||
use std::fmt; | ||
use serde::{Deserialize, Serialize}; | ||
use vers_vecs::BitVec; | ||
|
||
/// A trait for an index that supports locate queries. | ||
/// | ||
/// This is only supported when [`SuffixOrderSampledArray`] is passed in. | ||
pub trait HasPosition { | ||
#[doc(hidden)] | ||
fn get_sa<L: seal::IsLocal>(&self, i: u64) -> u64; | ||
} | ||
|
||
/// A sampled suffix array, stored within the index. | ||
#[derive(Serialize, Deserialize)] | ||
pub struct SuffixOrderSampledArray { | ||
level: usize, | ||
word_size: usize, | ||
sa: BitVec, | ||
len: usize, | ||
} | ||
|
||
impl SuffixOrderSampledArray { | ||
pub(crate) fn get(&self, i: u64) -> Option<u64> { | ||
debug_assert!(i < self.len as u64); | ||
if i & ((1 << self.level) - 1) == 0 { | ||
Some( | ||
self.sa.get_bits_unchecked( | ||
(i as usize >> self.level) * self.word_size, | ||
self.word_size, | ||
), | ||
) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
pub(crate) fn size(&self) -> usize { | ||
std::mem::size_of::<Self>() + self.sa.heap_size() | ||
} | ||
} | ||
|
||
impl fmt::Debug for SuffixOrderSampledArray { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
for i in 0..self.len { | ||
match self.get(i as u64) { | ||
Some(sa) => write!(f, "{}", sa)?, | ||
None => write!(f, "?")?, | ||
} | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
pub(crate) fn sample(sa: &[u64], level: usize) -> SuffixOrderSampledArray { | ||
let n = sa.len(); | ||
let word_size = (util::log2(n as u64) + 1) as usize; | ||
debug_assert!(n > 0); | ||
debug_assert!( | ||
n > (1 << level), | ||
"sampling level L must satisfy 2^L < text_len (L = {}, text_len = {})", | ||
level, | ||
n, | ||
); | ||
let sa_samples_len = ((n - 1) >> level) + 1; | ||
let mut sa_samples = BitVec::with_capacity(sa_samples_len); | ||
// fid::BitArray::with_word_size(word_size, sa_samples_len); | ||
for i in 0..sa_samples_len { | ||
sa_samples.append_bits(sa[i << level], word_size); | ||
} | ||
SuffixOrderSampledArray { | ||
level, | ||
word_size, | ||
sa: sa_samples, | ||
len: sa.len(), | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_regular() { | ||
let cases = [ | ||
(1, 10), | ||
(1, 25), | ||
(2, 8), | ||
(2, 9), | ||
(2, 10), | ||
(2, 25), | ||
(3, 24), | ||
(3, 25), | ||
]; | ||
for &(level, n) in cases.iter() { | ||
let sa = (0..n).collect::<Vec<u64>>(); | ||
let ssa = sample(&sa, level); | ||
for i in 0..n { | ||
let v = ssa.get(i); | ||
if i & ((1 << level) - 1) == 0 { | ||
assert_eq!(v, Some(i), "ssa[{}] should be Some({})", i, i); | ||
} else { | ||
assert_eq!(v, None, "ssa[{}] should be None", i); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
pub mod sample; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
//! Sampled suffix arrays to perform locate queries. | ||
use crate::util; | ||
use std::fmt; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
use vers_vecs::BitVec; | ||
|
||
/// A sampled suffix array, stored within the index. | ||
#[derive(Serialize, Deserialize)] | ||
pub struct SuffixOrderSampledArray { | ||
level: usize, | ||
word_size: usize, | ||
sa: BitVec, | ||
len: usize, | ||
} | ||
|
||
impl SuffixOrderSampledArray { | ||
pub(crate) fn get(&self, i: u64) -> Option<u64> { | ||
debug_assert!(i < self.len as u64); | ||
if i & ((1 << self.level) - 1) == 0 { | ||
Some( | ||
self.sa.get_bits_unchecked( | ||
(i as usize >> self.level) * self.word_size, | ||
self.word_size, | ||
), | ||
) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
pub(crate) fn size(&self) -> usize { | ||
std::mem::size_of::<Self>() + self.sa.heap_size() | ||
} | ||
} | ||
|
||
impl fmt::Debug for SuffixOrderSampledArray { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
for i in 0..self.len { | ||
match self.get(i as u64) { | ||
Some(sa) => write!(f, "{}", sa)?, | ||
None => write!(f, "?")?, | ||
} | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
pub(crate) fn sample(sa: &[u64], level: usize) -> SuffixOrderSampledArray { | ||
let n = sa.len(); | ||
let word_size = (util::log2(n as u64) + 1) as usize; | ||
debug_assert!(n > 0); | ||
debug_assert!( | ||
n > (1 << level), | ||
"sampling level L must satisfy 2^L < text_len (L = {}, text_len = {})", | ||
level, | ||
n, | ||
); | ||
let sa_samples_len = ((n - 1) >> level) + 1; | ||
let mut sa_samples = BitVec::with_capacity(sa_samples_len); | ||
// fid::BitArray::with_word_size(word_size, sa_samples_len); | ||
for i in 0..sa_samples_len { | ||
sa_samples.append_bits(sa[i << level], word_size); | ||
} | ||
SuffixOrderSampledArray { | ||
level, | ||
word_size, | ||
sa: sa_samples, | ||
len: sa.len(), | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_regular() { | ||
let cases = [ | ||
(1, 10), | ||
(1, 25), | ||
(2, 8), | ||
(2, 9), | ||
(2, 10), | ||
(2, 25), | ||
(3, 24), | ||
(3, 25), | ||
]; | ||
for &(level, n) in cases.iter() { | ||
let sa = (0..n).collect::<Vec<u64>>(); | ||
let ssa = sample(&sa, level); | ||
for i in 0..n { | ||
let v = ssa.get(i); | ||
if i & ((1 << level) - 1) == 0 { | ||
assert_eq!(v, Some(i), "ssa[{}] should be Some({})", i, i); | ||
} else { | ||
assert_eq!(v, None, "ssa[{}] should be None", i); | ||
} | ||
} | ||
} | ||
} | ||
} |