pub(super) struct Swept {
pub held: u64,
pub evicted: u64,
}
pub(super) fn to_cap<R: Ord, E>(
mut entries: Vec<(R, u64, E)>,
cap: u64,
mut drop_one: impl FnMut(&E) -> bool,
) -> Swept {
let found: u64 = entries.iter().map(|(_, bytes, _)| bytes).sum();
let mut total = found;
if total > cap {
entries.sort_by(|a, b| a.0.cmp(&b.0));
let target = cap / 10 * 9;
for (_, bytes, entry) in &entries {
if total <= target {
break;
}
if drop_one(entry) {
total -= bytes;
}
}
}
Swept {
held: total,
evicted: found - total,
}
}