Refactor of the Interner

This commit is contained in:
Loïc Lecrenier
2023-03-14 16:37:47 +01:00
parent 31628c5cd4
commit e9cf58d584
21 changed files with 857 additions and 541 deletions

View File

@ -1,9 +1,85 @@
use std::marker::PhantomData;
use super::interner::{FixedSizeInterner, Interned};
pub struct SmallBitmap<T> {
internal: SmallBitmapInternal,
_phantom: PhantomData<T>,
}
impl<T> Clone for SmallBitmap<T> {
fn clone(&self) -> Self {
Self { internal: self.internal.clone(), _phantom: PhantomData }
}
}
impl<T> SmallBitmap<T> {
pub fn for_interned_values_in(interner: &FixedSizeInterner<T>) -> Self {
Self::new(interner.len())
}
pub fn new(universe_length: u16) -> Self {
if universe_length <= 64 {
Self { internal: SmallBitmapInternal::Tiny(0), _phantom: PhantomData }
} else {
Self {
internal: SmallBitmapInternal::Small(
vec![0; 1 + universe_length as usize / 64].into_boxed_slice(),
),
_phantom: PhantomData,
}
}
}
pub fn from_iter(
xs: impl Iterator<Item = Interned<T>>,
for_interner: &FixedSizeInterner<T>,
) -> Self {
Self {
internal: SmallBitmapInternal::from_iter(
xs.map(|x| x.into_inner()),
for_interner.len(),
),
_phantom: PhantomData,
}
}
pub fn is_empty(&self) -> bool {
self.internal.is_empty()
}
pub fn clear(&mut self) {
self.internal.clear()
}
pub fn contains(&self, x: Interned<T>) -> bool {
self.internal.contains(x.into_inner())
}
pub fn insert(&mut self, x: Interned<T>) {
self.internal.insert(x.into_inner())
}
pub fn remove(&mut self, x: Interned<T>) {
self.internal.remove(x.into_inner())
}
pub fn intersection(&mut self, other: &Self) {
self.internal.intersection(&other.internal)
}
pub fn union(&mut self, other: &Self) {
self.internal.union(&other.internal)
}
pub fn subtract(&mut self, other: &Self) {
self.internal.subtract(&other.internal)
}
pub fn is_subset(&self, other: &Self) -> bool {
self.internal.is_subset(&other.internal)
}
pub fn intersects(&self, other: &Self) -> bool {
self.internal.intersects(&other.internal)
}
pub fn iter(&self) -> impl Iterator<Item = Interned<T>> + '_ {
self.internal.iter().map(|x| Interned::new(x))
}
}
#[derive(Clone)]
pub enum SmallBitmap {
pub enum SmallBitmapInternal {
Tiny(u64),
Small(Box<[u64]>),
}
impl SmallBitmap {
impl SmallBitmapInternal {
pub fn new(universe_length: u16) -> Self {
if universe_length <= 64 {
Self::Tiny(0)
@ -20,8 +96,8 @@ impl SmallBitmap {
}
pub fn is_empty(&self) -> bool {
match self {
SmallBitmap::Tiny(set) => *set == 0,
SmallBitmap::Small(sets) => {
SmallBitmapInternal::Tiny(set) => *set == 0,
SmallBitmapInternal::Small(sets) => {
for set in sets.iter() {
if *set != 0 {
return false;
@ -33,8 +109,8 @@ impl SmallBitmap {
}
pub fn clear(&mut self) {
match self {
SmallBitmap::Tiny(set) => *set = 0,
SmallBitmap::Small(sets) => {
SmallBitmapInternal::Tiny(set) => *set = 0,
SmallBitmapInternal::Small(sets) => {
for set in sets.iter_mut() {
*set = 0;
}
@ -43,8 +119,8 @@ impl SmallBitmap {
}
pub fn contains(&self, mut x: u16) -> bool {
let set = match self {
SmallBitmap::Tiny(set) => *set,
SmallBitmap::Small(set) => {
SmallBitmapInternal::Tiny(set) => *set,
SmallBitmapInternal::Small(set) => {
let idx = x / 64;
x %= 64;
set[idx as usize]
@ -54,8 +130,8 @@ impl SmallBitmap {
}
pub fn insert(&mut self, mut x: u16) {
let set = match self {
SmallBitmap::Tiny(set) => set,
SmallBitmap::Small(set) => {
SmallBitmapInternal::Tiny(set) => set,
SmallBitmapInternal::Small(set) => {
let idx = x / 64;
x %= 64;
&mut set[idx as usize]
@ -65,8 +141,8 @@ impl SmallBitmap {
}
pub fn remove(&mut self, mut x: u16) {
let set = match self {
SmallBitmap::Tiny(set) => set,
SmallBitmap::Small(set) => {
SmallBitmapInternal::Tiny(set) => set,
SmallBitmapInternal::Small(set) => {
let idx = x / 64;
x %= 64;
&mut set[idx as usize]
@ -75,20 +151,20 @@ impl SmallBitmap {
*set &= !(0b1 << x);
}
pub fn intersection(&mut self, other: &SmallBitmap) {
pub fn intersection(&mut self, other: &SmallBitmapInternal) {
self.apply_op(other, |a, b| *a &= b);
}
pub fn union(&mut self, other: &SmallBitmap) {
pub fn union(&mut self, other: &SmallBitmapInternal) {
self.apply_op(other, |a, b| *a |= b);
}
pub fn subtract(&mut self, other: &SmallBitmap) {
pub fn subtract(&mut self, other: &SmallBitmapInternal) {
self.apply_op(other, |a, b| *a &= !b);
}
pub fn apply_op(&mut self, other: &SmallBitmap, op: impl Fn(&mut u64, u64)) {
pub fn apply_op(&mut self, other: &SmallBitmapInternal, op: impl Fn(&mut u64, u64)) {
match (self, other) {
(SmallBitmap::Tiny(a), SmallBitmap::Tiny(b)) => op(a, *b),
(SmallBitmap::Small(a), SmallBitmap::Small(b)) => {
(SmallBitmapInternal::Tiny(a), SmallBitmapInternal::Tiny(b)) => op(a, *b),
(SmallBitmapInternal::Small(a), SmallBitmapInternal::Small(b)) => {
assert!(a.len() == b.len(),);
for (a, b) in a.iter_mut().zip(b.iter()) {
op(a, *b);
@ -99,10 +175,14 @@ impl SmallBitmap {
}
}
}
pub fn all_satisfy_op(&self, other: &SmallBitmap, op: impl Fn(u64, u64) -> bool) -> bool {
pub fn all_satisfy_op(
&self,
other: &SmallBitmapInternal,
op: impl Fn(u64, u64) -> bool,
) -> bool {
match (self, other) {
(SmallBitmap::Tiny(a), SmallBitmap::Tiny(b)) => op(*a, *b),
(SmallBitmap::Small(a), SmallBitmap::Small(b)) => {
(SmallBitmapInternal::Tiny(a), SmallBitmapInternal::Tiny(b)) => op(*a, *b),
(SmallBitmapInternal::Small(a), SmallBitmapInternal::Small(b)) => {
assert!(a.len() == b.len());
for (a, b) in a.iter().zip(b.iter()) {
if !op(*a, *b) {
@ -116,10 +196,14 @@ impl SmallBitmap {
}
}
}
pub fn any_satisfy_op(&self, other: &SmallBitmap, op: impl Fn(u64, u64) -> bool) -> bool {
pub fn any_satisfy_op(
&self,
other: &SmallBitmapInternal,
op: impl Fn(u64, u64) -> bool,
) -> bool {
match (self, other) {
(SmallBitmap::Tiny(a), SmallBitmap::Tiny(b)) => op(*a, *b),
(SmallBitmap::Small(a), SmallBitmap::Small(b)) => {
(SmallBitmapInternal::Tiny(a), SmallBitmapInternal::Tiny(b)) => op(*a, *b),
(SmallBitmapInternal::Small(a), SmallBitmapInternal::Small(b)) => {
assert!(a.len() == b.len());
for (a, b) in a.iter().zip(b.iter()) {
if op(*a, *b) {
@ -133,32 +217,32 @@ impl SmallBitmap {
}
}
}
pub fn is_subset(&self, other: &SmallBitmap) -> bool {
pub fn is_subset(&self, other: &SmallBitmapInternal) -> bool {
self.all_satisfy_op(other, |a, b| a & !b == 0)
}
pub fn intersects(&self, other: &SmallBitmap) -> bool {
pub fn intersects(&self, other: &SmallBitmapInternal) -> bool {
self.any_satisfy_op(other, |a, b| a & b != 0)
}
pub fn iter(&self) -> SmallBitmapIter<'_> {
pub fn iter(&self) -> SmallBitmapInternalIter<'_> {
match self {
SmallBitmap::Tiny(x) => SmallBitmapIter::Tiny(*x),
SmallBitmap::Small(xs) => {
SmallBitmapIter::Small { cur: xs[0], next: &xs[1..], base: 0 }
SmallBitmapInternal::Tiny(x) => SmallBitmapInternalIter::Tiny(*x),
SmallBitmapInternal::Small(xs) => {
SmallBitmapInternalIter::Small { cur: xs[0], next: &xs[1..], base: 0 }
}
}
}
}
pub enum SmallBitmapIter<'b> {
pub enum SmallBitmapInternalIter<'b> {
Tiny(u64),
Small { cur: u64, next: &'b [u64], base: u16 },
}
impl<'b> Iterator for SmallBitmapIter<'b> {
impl<'b> Iterator for SmallBitmapInternalIter<'b> {
type Item = u16;
fn next(&mut self) -> Option<Self::Item> {
match self {
SmallBitmapIter::Tiny(set) => {
SmallBitmapInternalIter::Tiny(set) => {
if *set > 0 {
let idx = set.trailing_zeros() as u16;
*set &= *set - 1;
@ -167,7 +251,7 @@ impl<'b> Iterator for SmallBitmapIter<'b> {
None
}
}
SmallBitmapIter::Small { cur, next, base } => {
SmallBitmapInternalIter::Small { cur, next, base } => {
if *cur > 0 {
let idx = cur.trailing_zeros() as u16;
*cur &= *cur - 1;
@ -185,23 +269,23 @@ impl<'b> Iterator for SmallBitmapIter<'b> {
}
}
#[cfg(test)]
mod tests {
use super::SmallBitmap;
// #[cfg(test)]
// mod tests {
// use super::SmallBitmap;
#[test]
fn test_small_bitmap() {
let mut bitmap1 = SmallBitmap::new(32);
for x in 0..16 {
bitmap1.insert(x * 2);
}
let mut bitmap2 = SmallBitmap::new(32);
for x in 0..=10 {
bitmap2.insert(x * 3);
}
bitmap1.intersection(&bitmap2);
for v in bitmap1.iter() {
println!("{v}");
}
}
}
// #[test]
// fn test_small_bitmap() {
// let mut bitmap1 = SmallBitmap::new(32);
// for x in 0..16 {
// bitmap1.insert(x * 2);
// }
// let mut bitmap2 = SmallBitmap::new(32);
// for x in 0..=10 {
// bitmap2.insert(x * 3);
// }
// bitmap1.intersection(&bitmap2);
// for v in bitmap1.iter() {
// println!("{v}");
// }
// }
// }