use crate::driver::route;
const WORDS: usize = 4;
#[derive(Clone, Copy, Default, PartialEq, Eq)]
pub struct Routes([u64; WORDS]);
impl Routes {
pub const EMPTY: Self = Self([0; WORDS]);
pub const fn singleton<T: route::App>() -> Self {
Self::singleton_id(T::ID)
}
pub const fn singleton_id(id: route::Id) -> Self {
let mut words = [0; WORDS];
let word = id.raw() as usize / u64::BITS as usize;
let bit = id.raw() as usize % u64::BITS as usize;
words[word] |= 1u64 << bit;
Self(words)
}
#[must_use]
pub const fn has_route(self, route: route::Route) -> bool {
let Some(id) = route.app() else {
return false;
};
let word = id.raw() as usize / u64::BITS as usize;
let bit = id.raw() as usize % u64::BITS as usize;
self.0[word] & (1u64 << bit) != 0
}
pub(in crate::driver::settings) const fn overlap(self, other: Self) -> Option<route::Id> {
const { assert!(WORDS * (u64::BITS as usize) == route::CAPACITY) };
let mut word = 0;
while word < WORDS {
let overlap = self.0[word] & other.0[word];
if overlap != 0 {
let base = (word as u8) * (u64::BITS as u8);
return route::Route::from_bits(base + overlap.trailing_zeros() as u8).app();
}
word += 1;
}
None
}
pub const fn join(self, other: Self) -> Self {
Self([
self.0[0] | other.0[0],
self.0[1] | other.0[1],
self.0[2] | other.0[2],
self.0[3] | other.0[3],
])
}
}