use crate::bitmap::store::array_store::vector::count_set_lanes;
use fearless_simd::{mask16x8, prelude::*, u16x8};
use fearless_simd_macros::simd;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
pub trait BinaryOperationVisitor {
fn visit_vector<S: Simd>(&mut self, value: u16x8<S>, mask: mask16x8<S>);
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 capacity = if capacity < 8 { capacity } else { capacity + 8 };
let vec = Vec::with_capacity(capacity);
VecWriter { vec }
}
pub fn into_inner(self) -> Vec<u16> {
self.vec
}
}
impl BinaryOperationVisitor for VecWriter {
#[simd]
fn visit_vector<S: Simd>(&mut self, value: u16x8<S>, mask: mask16x8<S>) {
let result = value.compress(mask);
let result_size = count_set_lanes(mask);
self.vec.extend_from_slice(result.as_slice());
self.vec.truncate(self.vec.len() - (result.len() - result_size));
}
fn visit_scalar(&mut self, value: u16) {
assert!(self.vec.len() < self.vec.capacity());
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 {
fn visit_vector<S: Simd>(&mut self, _value: u16x8<S>, mask: mask16x8<S>) {
self.count += count_set_lanes(mask);
}
fn visit_scalar(&mut self, _value: u16) {
self.count += 1;
}
fn visit_slice(&mut self, values: &[u16]) {
self.count += values.len();
}
}