use std::cell::RefCell;
use std::collections::HashMap;
pub type BitCounts = [u32; 3];
pub const UNCOUNTED: BitCounts = [u32::MAX; 3];
thread_local! {
static COUNTS: RefCell<HashMap<u64, Vec<BitCounts>>> = RefCell::default();
static SIDES: RefCell<HashMap<u64, Vec<SideBit>>> = RefCell::default();
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct SideBit {
pub points: [u16; 2],
pub active: [u8; 2],
pub present: [bool; 2],
}
pub fn reset_driver_counts_for_new_run() {
COUNTS.with(|c| c.borrow_mut().clear());
SIDES.with(|c| c.borrow_mut().clear());
}
pub fn set_driver_counts(key: u64, counts: &[BitCounts]) {
COUNTS.with(|c| {
let mut m = c.borrow_mut();
match m.get_mut(&key) {
Some(v) if v.as_slice() == counts => {}
Some(v) => *v = counts.to_vec(),
None => {
m.insert(key, counts.to_vec());
}
}
});
}
pub fn driver_counts_of(key: u64, bit: u32) -> Option<BitCounts> {
COUNTS.with(|c| {
c.borrow()
.get(&key)
.and_then(|v| v.get(bit as usize).copied())
})
}
pub fn set_sides(key: u64, bits: &[SideBit]) {
SIDES.with(|c| {
c.borrow_mut().insert(key, bits.to_vec());
});
}
pub fn sides_of(key: u64, bit: u32) -> Option<SideBit> {
SIDES.with(|c| {
c.borrow()
.get(&key)
.and_then(|v| v.get(bit as usize).copied())
})
}