use std::slice::Iter;
use std::iter::repeat;
pub mod svec {
use super::BinSetu8;
pub use self::SVec::{Zero, One, Many, ManyBut, Any};
pub enum SVec {
Zero,
One(u8),
Many(Box<BinSetu8>),
ManyBut(Box<BinSetu8>),
Any
}
}
pub struct BinSet {
data: Vec<u64>,
states: Vec<usize>,
pub action: usize
}
#[derive(Clone)]
pub struct BinSetu8 {
pub data: Vec<u64>,
pub states: Vec<u8>
}
impl BinSet {
#[inline(always)]
pub fn contains(&self, state: usize) -> bool {
let chunk = state >> 6;
let idx = state & 0x3F;
((self.data[chunk] >> idx) & 1) != 0
}
#[inline(always)]
pub fn insert(&mut self, state: usize) {
let chunk = state >> 6;
let idx = state & 0x3F;
self.data[chunk] |= 1 << idx;
self.states.push(state);
}
#[inline(always)]
pub fn new(state_count: usize) -> BinSet {
let chunks = (state_count >> 6) + 1;
BinSet {
data: repeat(0u64).take(chunks).collect(),
states: Vec::with_capacity(state_count),
action: 0
}
}
#[inline(always)]
pub fn iter<'a>(&'a self) -> Iter<'a, usize> {
self.states.iter()
}
#[inline(always)]
pub fn is_empty(&self) -> bool {
self.states.is_empty()
}
#[inline(always)]
pub fn action(&self) -> usize {
self.action
}
}
impl BinSetu8 {
#[inline(always)]
#[allow(dead_code)]
pub fn contains(&self, state: u8) -> bool {
let chunk = (state >> 6u64) as usize;
let idx = (state & 0x3Fu8) as u8;
((self.data[chunk] >> idx) & 1) != 0
}
#[inline(always)]
pub fn insert(&mut self, state: u8) {
let chunk = (state >> 6u64) as usize;
let idx = (state & 0x3Fu8) as u8;
self.data[chunk] |= 1 << idx;
self.states.push(state);
}
#[inline(always)]
pub fn new(state_count: usize) -> BinSetu8 {
let chunks = (state_count >> 6) + 1;
BinSetu8 {
data: repeat(0u64).take(chunks).collect(),
states: Vec::with_capacity(state_count)
}
}
}
impl PartialEq for BinSet {
fn eq(&self, other: &BinSet) -> bool {
let len = self.data.len();
let mut i = 0;
while i < len {
if self.data[i] != other.data[i] {
return false;
}
i += 1;
}
true
}
}