cranpose-core 0.26.0

Composition runtime and reactive state for Cranpose
Documentation
//! Giving back the room a collection's growth left past its items: a
//! collection that lives as long as a screen and doubled to take one more
//! item would keep room for as many again.

use std::hash::{BuildHasher, Hash};

const MIN_TRIMMED_SLACK: usize = 16;

fn slack_target(len: usize, capacity: usize) -> Option<usize> {
    let slack = capacity.saturating_sub(len);
    (slack > (len / 4).max(MIN_TRIMMED_SLACK)).then_some(len + len / 8)
}

/// A collection that can give back the room its growth left past its items.
pub trait GrowthSlack {
    /// Gives back the room past the items once it is more than a quarter of
    /// them, and 16 at least, down to an eighth of them.
    fn trim_growth_slack(&mut self);
}

// The trims run out of line: one copy for each collection type serves every
// collection of it.
impl<T> GrowthSlack for Vec<T> {
    #[inline(never)]
    fn trim_growth_slack(&mut self) {
        if let Some(target) = slack_target(self.len(), self.capacity()) {
            self.shrink_to(target);
        }
    }
}

#[expect(
    clippy::disallowed_types,
    reason = "every hash map trims alike, whatever it hashes with"
)]
impl<K: Eq + Hash, V, S: BuildHasher> GrowthSlack for std::collections::HashMap<K, V, S> {
    #[inline(never)]
    fn trim_growth_slack(&mut self) {
        // A map rounds its buckets up to a power of two, which leaves room
        // past the items: asked for room past them, it may keep twice that.
        if slack_target(self.len(), self.capacity()).is_some() {
            self.shrink_to(self.len());
        }
    }
}

#[expect(
    clippy::disallowed_types,
    reason = "every hash set trims alike, whatever it hashes with"
)]
impl<T: Eq + Hash, S: BuildHasher> GrowthSlack for std::collections::HashSet<T, S> {
    #[inline(never)]
    fn trim_growth_slack(&mut self) {
        if slack_target(self.len(), self.capacity()).is_some() {
            self.shrink_to(self.len());
        }
    }
}