orx_selfref_col/memory/
on_threshold.rsuse super::{policy::MemoryPolicy, reclaimer::MemoryReclaimer};
use crate::{CoreCol, Node, NodePtr, Variant};
use core::marker::PhantomData;
use orx_pinned_vec::PinnedVec;
pub struct MemoryReclaimOnThreshold<const D: usize, V: Variant, R: MemoryReclaimer<V>> {
phantom: PhantomData<(V, R)>,
}
impl<const D: usize, V: Variant, R: MemoryReclaimer<V>> Default
for MemoryReclaimOnThreshold<D, V, R>
{
fn default() -> Self {
Self {
phantom: Default::default(),
}
}
}
impl<const D: usize, V: Variant, R: MemoryReclaimer<V>> Clone
for MemoryReclaimOnThreshold<D, V, R>
{
fn clone(&self) -> Self {
Self::default()
}
}
impl<const D: usize, V, R> MemoryPolicy<V> for MemoryReclaimOnThreshold<D, V, R>
where
V: Variant,
R: MemoryReclaimer<V>,
{
fn reclaim_closed_nodes<P>(col: &mut CoreCol<V, P>, _closed_node_ptr: &NodePtr<V>) -> bool
where
P: PinnedVec<Node<V>>,
{
let num_active_nodes = col.len();
let used = col.nodes().len();
let allowed_vacant = used >> D;
let num_vacant = used - num_active_nodes;
match num_vacant <= allowed_vacant {
true => false,
false => {
let nodes_moved = R::reclaim_nodes(col);
col.nodes_mut().truncate(num_active_nodes);
nodes_moved
}
}
}
}