use super::Run;
use super::counts::{Split, Tally};
use super::split::Frame;
use super::zone::{Cursors, Linear as _, Unrestricted as _, Zone};
use crate::linear::occurrences::OccId;
use crate::linear::proofs::{Branch, NodeId};
use crate::linear::search::memory::{Charged, bytes_of};
impl<Z: Zone> Run<'_, Z> {
pub(super) fn take_set(&mut self) -> Z::Unrestricted {
match self.pools.sets.pop() {
Some(mut set) => {
set.clear();
set
}
None => {
self.scratch.charge(self.set_bytes());
Z::Unrestricted::empty(self.problem.forest)
}
}
}
pub(super) fn give_set(&mut self, set: Z::Unrestricted) {
self.pools.sets.push(set);
}
pub(super) fn take_context(&mut self) -> Z::Linear {
match self.pools.contexts.pop() {
Some(mut context) => {
context.clear();
context
}
None => {
self.scratch.charge(self.set_bytes());
Z::Linear::empty(self.problem.forest)
}
}
}
pub(super) fn take_context_any(&mut self) -> Z::Linear {
self.pools.contexts.pop().unwrap_or_else(|| {
self.scratch.charge(self.set_bytes());
Z::Linear::empty(self.problem.forest)
})
}
pub(super) fn take_context_from(&mut self, gamma: &Z::Linear) -> Z::Linear {
let mut context = self.take_context_any();
context.copy_from(gamma);
context
}
pub(super) fn give_context(&mut self, context: Z::Linear) {
self.pools.contexts.push(context);
}
pub(super) fn take_list(&mut self) -> Pooled<Z::Member> {
let mut list = self.pools.lists.pop().unwrap_or_default();
list.clear();
list
}
pub(super) fn give_list(&mut self, mut list: Pooled<Z::Member>) {
list.settle(&mut self.scratch);
self.pools.lists.push(list);
}
pub(super) fn take_words(&mut self) -> Pooled<OccId> {
let mut words = Z::word_lists(&mut self.pools).pop().unwrap_or_default();
words.clear();
words
}
pub(super) fn give_words(&mut self, mut words: Pooled<OccId>) {
words.settle(&mut self.scratch);
Z::word_lists(&mut self.pools).push(words);
}
pub(super) fn take_tally(&mut self) -> Tally {
match self.pools.tallies.pop() {
Some(mut tally) => {
tally.clear();
tally
}
None => {
self.scratch.charge(self.problem.counts.tally_bytes());
self.problem.counts.tally()
}
}
}
pub(super) fn give_tally(&mut self, tally: Tally) {
self.pools.tallies.push(tally);
}
#[expect(
clippy::unnecessary_box_returns,
reason = "a split search's frame holds its counts, and the box keeps that frame a pointer wide on every level of the recursion"
)]
pub(super) fn take_split(&mut self) -> Box<Split> {
match self.pools.splits.pop() {
Some(mut split) => {
split.clear();
split
}
None => {
self.scratch.charge(self.problem.counts.split_bytes());
Box::new(self.problem.counts.split())
}
}
}
pub(super) fn give_split(&mut self, split: Box<Split>) {
self.pools.splits.push(split);
}
pub(super) fn take_trail(&mut self) -> Pooled<Branch> {
let mut trail = self.pools.trails.pop().unwrap_or_default();
trail.clear();
trail
}
pub(super) fn give_trail(&mut self, mut trail: Pooled<Branch>) {
trail.settle(&mut self.scratch);
self.pools.trails.push(trail);
}
pub(super) fn take_links(&mut self) -> Pooled<(Z::Member, NodeId, Branch)> {
let mut links = self.pools.links.pop().unwrap_or_default();
links.clear();
links
}
pub(super) fn give_links(&mut self, mut links: Pooled<(Z::Member, NodeId, Branch)>) {
links.settle(&mut self.scratch);
self.pools.links.push(links);
}
pub(super) fn take_frames(&mut self) -> Vec<Frame<Z>> {
self.pools.frames.pop().unwrap_or_default()
}
pub(super) fn give_frames(&mut self, frames: Vec<Frame<Z>>) {
debug_assert!(frames.is_empty(), "the frames are closed");
self.pools.frames.push(frames);
}
pub(super) fn take_cursors(&mut self) -> Cursors {
self.pools.cursors.pop().unwrap_or_else(|| {
let lists = self.problem.forest.lists();
self.scratch.charge(lists * size_of::<u32>());
Cursors {
passed: vec![0; lists],
moved: Vec::new(),
}
})
}
pub(super) fn give_cursors(&mut self, mut cursors: Cursors) {
for list in cursors.moved.drain(..) {
cursors.passed[list as usize] = 0;
}
self.pools.cursors.push(cursors);
}
}
pub(super) struct Pools<Z: Zone> {
sets: Vec<Z::Unrestricted>,
contexts: Vec<Z::Linear>,
pub(super) lists: Vec<Pooled<Z::Member>>,
tallies: Vec<Tally>,
#[allow(clippy::vec_box)]
splits: Vec<Box<Split>>,
trails: Vec<Pooled<Branch>>,
links: Vec<Pooled<(Z::Member, NodeId, Branch)>>,
cursors: Vec<Cursors>,
frames: Vec<Vec<Frame<Z>>>,
pub(super) words: Vec<Pooled<OccId>>,
}
impl<Z: Zone> Default for Pools<Z> {
fn default() -> Self {
Self {
sets: Vec::new(),
contexts: Vec::new(),
lists: Vec::new(),
tallies: Vec::new(),
splits: Vec::new(),
trails: Vec::new(),
links: Vec::new(),
cursors: Vec::new(),
frames: Vec::new(),
words: Vec::new(),
}
}
}
#[derive(Debug)]
pub(super) struct Pooled<T> {
items: Vec<T>,
charged: usize,
}
impl<T> Default for Pooled<T> {
fn default() -> Self {
Self {
items: Vec::new(),
charged: 0,
}
}
}
impl<T> Pooled<T> {
pub(super) fn settle(&mut self, account: &mut Charged<'_>) {
let bytes = bytes_of(&self.items);
if bytes != self.charged {
account.resize(self.charged, bytes);
self.charged = bytes;
}
}
pub(super) fn charged(&self) -> usize {
self.charged
}
}
impl<T> std::ops::Deref for Pooled<T> {
type Target = Vec<T>;
fn deref(&self) -> &Vec<T> {
&self.items
}
}
impl<T> std::ops::DerefMut for Pooled<T> {
fn deref_mut(&mut self) -> &mut Vec<T> {
&mut self.items
}
}