use std::collections::HashSet;
#[derive(Debug, Clone)]
pub struct PiecePicker {
total: u32,
block_size: u32,
end_game_threshold: u32,
have: HashSet<u32>,
pending: HashSet<u32>,
skipped: HashSet<u32>,
availability: Vec<u32>,
}
impl PiecePicker {
pub fn new(total: u32, block_size: u32) -> Self {
Self::with_end_game(total, block_size, 4)
}
pub fn with_end_game(total: u32, block_size: u32, end_game_threshold: u32) -> Self {
Self {
total,
block_size,
end_game_threshold,
have: HashSet::new(),
pending: HashSet::new(),
skipped: HashSet::new(),
availability: vec![0; total as usize],
}
}
pub fn set_wanted_pieces(&mut self, wanted: &HashSet<u32>) {
self.skipped.clear();
for index in 0..self.total {
if !wanted.contains(&index) {
self.skipped.insert(index);
}
}
}
pub fn is_wanted(&self, index: u32) -> bool {
!self.skipped.contains(&index)
}
pub fn mark_have(&mut self, index: u32) {
self.have.insert(index);
self.pending.remove(&index);
}
pub fn has_piece(&self, index: u32) -> bool {
self.have.contains(&index)
}
pub fn release_piece(&mut self, index: u32) {
self.pending.remove(&index);
}
pub fn note_availability(&mut self, index: u32) {
if (index as usize) < self.availability.len() {
self.availability[index as usize] = self.availability[index as usize].saturating_add(1);
}
}
pub fn note_bitfield(&mut self, bitfield: &[u8]) {
crate::wire::apply_bitfield(&mut self.availability, bitfield);
}
pub fn remove_bitfield(&mut self, bitfield: &[u8]) {
crate::wire::remove_bitfield(&mut self.availability, bitfield);
}
pub fn clear_availability(&mut self, index: u32) {
if (index as usize) < self.availability.len() {
self.availability[index as usize] = self.availability[index as usize].saturating_sub(1);
}
}
pub fn remaining_count(&self) -> u32 {
let wanted = self.total.saturating_sub(self.skipped.len() as u32);
let have_wanted = self
.have
.iter()
.filter(|index| self.is_wanted(**index))
.count() as u32;
wanted.saturating_sub(have_wanted)
}
pub fn in_end_game(&self, threshold: u32) -> bool {
threshold > 0 && self.remaining_count() <= threshold
}
pub fn next_piece_endgame(&mut self) -> Option<u32> {
let mut best = None;
let mut best_score = u32::MAX;
for index in 0..self.total {
if self.have.contains(&index) || !self.is_wanted(index) {
continue;
}
let score = self.availability.get(index as usize).copied().unwrap_or(0);
if score < best_score {
best_score = score;
best = Some(index);
}
}
if let Some(index) = best {
self.pending.insert(index);
}
best
}
pub fn request_piece(&mut self, index: u32) -> bool {
if self.have.contains(&index) || !self.is_wanted(index) {
return false;
}
if self.pending.contains(&index) && !self.in_end_game(self.end_game_threshold) {
return false;
}
self.pending.insert(index);
true
}
pub fn next_piece(&mut self) -> Option<u32> {
if self.in_end_game(self.end_game_threshold) {
return self.next_piece_endgame();
}
let mut best = None;
let mut best_score = u32::MAX;
for index in 0..self.total {
if self.have.contains(&index) || self.pending.contains(&index) || !self.is_wanted(index) {
continue;
}
let score = self.availability.get(index as usize).copied().unwrap_or(0);
if score < best_score {
best_score = score;
best = Some(index);
}
}
if let Some(index) = best {
self.pending.insert(index);
}
best
}
pub fn block_size(&self) -> u32 {
self.block_size
}
pub fn complete_count(&self) -> usize {
self.have.len()
}
pub fn is_complete(&self) -> bool {
for index in 0..self.total {
if self.is_wanted(index) && !self.have.contains(&index) {
return false;
}
}
true
}
pub fn is_fully_complete(&self) -> bool {
self.total == 0 || self.have.len() as u32 == self.total
}
pub fn have_pieces(&self) -> HashSet<u32> {
self.have.clone()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn picks_rarer_piece_first() {
let mut picker = PiecePicker::new(3, 16384);
picker.note_availability(0);
picker.note_availability(0);
picker.note_availability(1);
assert_eq!(picker.next_piece(), Some(2));
}
#[test]
fn endgame_allows_duplicate_pending_picks() {
let mut picker = PiecePicker::with_end_game(2, 16384, 2);
picker.note_availability(0);
picker.note_availability(1);
let first = picker.next_piece_endgame().expect("first");
let second = picker.next_piece_endgame().expect("second");
assert_eq!(first, second);
}
}