use std::cmp::Ordering;
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub(crate) struct IdColumn {
text: String,
ends: Vec<u32>,
}
impl IdColumn {
pub(crate) fn from_sorted<'a>(ids: impl IntoIterator<Item = &'a str>) -> Self {
let mut column = Self::default();
for id in ids {
column.push(id);
}
column
}
pub(crate) fn push(&mut self, id: &str) {
self.text.push_str(id);
self.ends
.push(u32::try_from(self.text.len()).unwrap_or(u32::MAX));
}
pub(crate) fn len(&self) -> usize {
self.ends.len()
}
pub(crate) fn get(&self, row: usize) -> &str {
let start = if row == 0 {
0
} else {
self.ends[row - 1] as usize
};
&self.text[start..self.ends[row] as usize]
}
pub(crate) fn heap_bytes(&self) -> usize {
self.text.len() + self.ends.len() * 4
}
pub(crate) fn contains(&self, id: &str) -> bool {
let (mut lo, mut hi) = (0, self.len());
while lo < hi {
let mid = lo + (hi - lo) / 2;
match self.get(mid).cmp(id) {
Ordering::Less => lo = mid + 1,
Ordering::Greater => hi = mid,
Ordering::Equal => return true,
}
}
false
}
pub(crate) fn is_strictly_ascending(&self) -> bool {
(1..self.len()).all(|row| self.get(row - 1) < self.get(row))
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Bitmap {
words: Vec<u64>,
len: usize,
}
impl Bitmap {
pub(crate) fn new(len: usize) -> Self {
Self {
words: vec![0; len.div_ceil(64)],
len,
}
}
#[cfg(test)]
pub(crate) fn len(&self) -> usize {
self.len
}
pub(crate) fn set(&mut self, i: usize) {
self.words[i / 64] |= 1 << (i % 64);
}
pub(crate) fn get(&self, i: usize) -> bool {
self.words[i / 64] & (1 << (i % 64)) != 0
}
pub(crate) fn count(&self) -> usize {
self.words.iter().map(|w| w.count_ones() as usize).sum()
}
pub(crate) fn full(len: usize) -> Self {
let mut words = vec![u64::MAX; len.div_ceil(64)];
if !len.is_multiple_of(64) {
if let Some(last) = words.last_mut() {
*last = (1u64 << (len % 64)) - 1;
}
}
Self { words, len }
}
pub(crate) fn is_empty(&self) -> bool {
self.words.iter().all(|w| *w == 0)
}
pub(crate) fn union_with(&mut self, other: &Bitmap) {
debug_assert_eq!(self.len, other.len);
self.words
.iter_mut()
.zip(&other.words)
.for_each(|(a, b)| *a |= b);
}
pub(crate) fn subtract(&mut self, other: &Bitmap) {
debug_assert_eq!(self.len, other.len);
self.words
.iter_mut()
.zip(&other.words)
.for_each(|(a, b)| *a &= !b);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn an_id_column_gives_back_its_ids_and_finds_them() {
let ids = ["a", "bb", "c\u{e9}", "zz"];
let column = IdColumn::from_sorted(ids);
assert_eq!(column.len(), 4);
for (row, id) in ids.iter().enumerate() {
assert_eq!(column.get(row), *id);
assert!(column.contains(id));
}
assert!(!column.contains("b"));
assert!(!column.contains(""));
assert!(column.is_strictly_ascending());
assert!(!IdColumn::from_sorted(["b", "a"]).is_strictly_ascending());
assert!(!IdColumn::from_sorted(["a", "a"]).is_strictly_ascending());
assert_eq!(IdColumn::from_sorted([]).len(), 0);
}
#[test]
fn a_bitmap_sets_and_counts_across_words() {
let mut bits = Bitmap::new(130);
for i in [0, 63, 64, 129] {
bits.set(i);
}
assert_eq!(bits.len(), 130);
assert_eq!(bits.count(), 4);
assert!(bits.get(63) && bits.get(64) && !bits.get(65));
}
#[test]
fn a_bitmap_combines_with_union_subtract_and_full() {
let mut full = Bitmap::full(130);
assert_eq!(full.count(), 130);
assert!(!full.is_empty() && Bitmap::new(130).is_empty() && Bitmap::full(0).is_empty());
let mut odd = Bitmap::new(130);
(1..130).step_by(2).for_each(|i| odd.set(i));
full.subtract(&odd);
assert_eq!(full.count(), 65);
assert!(full.get(0) && !full.get(1) && full.get(128));
full.union_with(&odd);
assert_eq!(full, Bitmap::full(130));
}
}