use super::error::Dyadic;
use crate::hash::{HashMap, HashSet};
use crate::linear::occurrences::Member;
use crate::linear::sequents::Eigen;
pub(super) fn table_bytes(most: usize, entry: usize) -> u64 {
if most == 0 {
return 0;
}
let slots = (most as u64 * 8).div_ceil(7).next_power_of_two().max(4);
slots * (entry as u64 + 1) + 16
}
#[derive(Clone, Debug, Default)]
pub(super) struct Zone {
pub(super) members: HashSet<Member>,
pub(super) most: usize,
}
impl Zone {
pub(super) fn len(&self) -> usize {
self.members.len()
}
pub(super) fn is_empty(&self) -> bool {
self.members.is_empty()
}
pub(super) fn iter(&self) -> impl Iterator<Item = Member> + '_ {
self.members.iter().copied()
}
pub(super) fn insert(&mut self, o: Member) -> bool {
if self.members.contains(&o) {
return false;
}
self.members.insert(o);
self.most = self.most.max(self.members.len());
true
}
pub(super) fn remove(&mut self, o: Member) -> bool {
self.members.remove(&o)
}
pub(super) fn bytes(&self) -> u64 {
table_bytes(self.most, size_of::<Member>())
}
}
#[derive(Clone, Debug, Default)]
pub(super) struct Bag {
pub(super) counts: HashMap<Member, u32>,
pub(super) len: usize,
pub(super) most: usize,
}
impl PartialEq for Bag {
fn eq(&self, other: &Self) -> bool {
self.len == other.len && self.counts == other.counts
}
}
impl Eq for Bag {}
impl Bag {
pub(super) const MOST: usize = (u32::MAX - 1) as usize;
pub(super) fn of(ids: impl IntoIterator<Item = Member>) -> Self {
let mut bag = Self::default();
for o in ids {
bag.insert(o);
}
bag
}
pub(super) fn is_empty(&self) -> bool {
self.len == 0
}
pub(super) fn counts(&self) -> impl Iterator<Item = (Member, u32)> + '_ {
self.counts.iter().map(|(&o, &n)| (o, n))
}
pub(super) fn sorted(&self) -> Vec<Member> {
let mut ids: Vec<Member> = self
.counts()
.flat_map(|(o, n)| std::iter::repeat_n(o, n as usize))
.collect();
ids.sort_unstable();
ids
}
pub(super) fn insert(&mut self, o: Member) {
let n = self.counts.entry(o).or_insert(0);
*n = n.saturating_add(1);
self.len = self.len.saturating_add(1);
self.most = self.most.max(self.counts.len());
}
pub(super) fn remove(&mut self, o: Member) -> bool {
let Some(n) = self.counts.get_mut(&o) else {
return false;
};
*n -= 1;
if *n == 0 {
self.counts.remove(&o);
}
self.len -= 1;
true
}
pub(super) fn add(&mut self, mut other: Self) {
if other.counts.len() > self.counts.len() {
std::mem::swap(self, &mut other);
}
for (o, n) in other.counts {
let own = self.counts.entry(o).or_insert(0);
*own = own.saturating_add(n);
}
self.len = self.len.saturating_add(other.len);
self.most = self.most.max(self.counts.len());
}
pub(super) fn unite(&mut self, mut other: Self) {
if other.counts.len() > self.counts.len() {
std::mem::swap(self, &mut other);
}
for (o, n) in other.counts {
let own = self.counts.entry(o).or_insert(0);
if n > *own {
self.len = self.len.saturating_add((n - *own) as usize);
*own = n;
}
}
self.most = self.most.max(self.counts.len());
}
pub(super) fn bytes(&self) -> u64 {
table_bytes(self.most, size_of::<(Member, u32)>())
}
pub(super) fn is_subset(&self, other: &Self) -> bool {
self.len <= other.len
&& self
.counts()
.all(|(o, n)| other.counts.get(&o).is_some_and(|&m| n <= m))
}
}
#[derive(Clone, Debug)]
pub(crate) struct State {
pub(super) theta: Zone,
pub(super) gamma: Bag,
pub(super) outputs: usize,
pub(super) linear: u64,
pub(super) unrestricted: u64,
pub(super) goal: u64,
pub(super) any: bool,
}
#[derive(Clone, Debug, Default)]
pub(super) struct Eigens {
pub(super) set: HashSet<Eigen>,
pub(super) most: usize,
}
impl Eigens {
pub(super) fn insert(&mut self, a: Eigen) {
if self.set.insert(a) {
self.most = self.most.max(self.set.len());
}
}
pub(super) fn add(&mut self, mut other: Self) {
if other.set.len() > self.set.len() {
std::mem::swap(self, &mut other);
}
self.set.extend(other.set);
self.most = self.most.max(self.set.len());
}
pub(super) fn bytes(&self) -> u64 {
table_bytes(self.most, size_of::<Eigen>()) + size_of::<Self>() as u64 + 16
}
}
impl State {
pub(super) const OWN: u64 = size_of::<Self>() as u64 + 16;
pub(super) fn bytes(&self) -> u64 {
Self::OWN + self.theta.bytes() + self.gamma.bytes()
}
pub(super) fn entries(&self) -> u64 {
self.theta.len() as u64 + self.gamma.counts.len() as u64
}
pub(super) fn to_dyadic(&self) -> Dyadic {
let mut theta: Vec<Member> = self.theta.iter().collect();
theta.sort_unstable();
Dyadic {
theta,
gamma: self.gamma.sorted(),
any: self.any,
}
}
pub(crate) fn unrestricted(&self) -> impl Iterator<Item = Member> + '_ {
self.theta.iter()
}
pub(crate) fn linear(&self) -> impl Iterator<Item = (Member, u32)> + '_ {
self.gamma.counts()
}
pub(crate) fn needs(&self) -> usize {
self.theta.len()
}
pub(crate) fn absorbs(&self) -> bool {
self.any
}
pub(crate) fn weight(&self) -> u64 {
self.linear.saturating_add(self.unrestricted)
}
pub(crate) fn goal_weight(&self) -> u64 {
self.goal
}
}