#[cfg(feature = "simd")]
use crate::bitmap::store::array_store::vector::swizzle_to_front;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
pub trait BinaryOperationVisitor {
#[cfg(feature = "simd")]
fn visit_vector(&mut self, value: core::simd::u16x8, mask: u8);
fn visit_scalar(&mut self, value: u16);
fn visit_slice(&mut self, values: &[u16]);
}
pub struct VecWriter {
vec: Vec<u16>,
}
impl VecWriter {
pub fn new(capacity: usize) -> VecWriter {
let vec = Vec::with_capacity(capacity);
VecWriter { vec }
}
pub fn into_inner(self) -> Vec<u16> {
self.vec
}
}
impl BinaryOperationVisitor for VecWriter {
#[cfg(feature = "simd")]
fn visit_vector(&mut self, value: core::simd::u16x8, mask: u8) {
let result = swizzle_to_front(value, mask);
self.vec.extend_from_slice(&result.as_array()[..]);
self.vec.truncate(self.vec.len() - (result.len() - mask.count_ones() as usize));
}
fn visit_scalar(&mut self, value: u16) {
self.vec.push(value)
}
fn visit_slice(&mut self, values: &[u16]) {
self.vec.extend_from_slice(values);
}
}
pub struct CardinalityCounter {
count: usize,
}
impl CardinalityCounter {
pub fn new() -> CardinalityCounter {
CardinalityCounter { count: 0 }
}
pub fn into_inner(self) -> u64 {
self.count as u64
}
}
impl BinaryOperationVisitor for CardinalityCounter {
#[cfg(feature = "simd")]
fn visit_vector(&mut self, _value: core::simd::u16x8, mask: u8) {
self.count += mask.count_ones() as usize;
}
fn visit_scalar(&mut self, _value: u16) {
self.count += 1;
}
fn visit_slice(&mut self, values: &[u16]) {
self.count += values.len();
}
}