use std::cmp::Ordering; use hashbrown::{HashMap, HashSet}; use crate::Vec2; #[derive(Debug, Default)] pub struct ChunkInterests { pub map: HashMap, HashSet>, pub weights: HashMap, f32>, } impl ChunkInterests { pub fn new() -> Self { Self { ..Default::default() } } pub fn is_interested(&self, client_id: &str, coords: &Vec2) -> bool { self.map .get(coords) .map(|clients| clients.contains(client_id)) .unwrap_or(false) } pub fn get_interests(&self, coords: &Vec2) -> Option<&HashSet> { self.map.get(coords) } pub fn set_weight(&mut self, coords: &Vec2, weight: f32) { self.weights.insert(coords.to_owned(), weight); } pub fn get_weight(&self, coords: &Vec2) -> Option<&f32> { self.weights.get(coords) } pub fn compare(&self, coords_a: &Vec2, coords_b: &Vec2) -> Ordering { let weight_a = self.get_weight(coords_a).unwrap_or(&f32::MAX); let weight_b = self.get_weight(coords_b).unwrap_or(&f32::MAX); weight_a.partial_cmp(weight_b).unwrap_or(Ordering::Equal) } pub fn has_interests(&self, coords: &Vec2) -> bool { self.map.contains_key(coords) } pub fn add(&mut self, client_id: &str, coords: &Vec2) { let mut clients = self.map.remove(coords).unwrap_or_default(); clients.insert(client_id.to_owned()); self.map.insert(coords.to_owned(), clients); } pub fn remove(&mut self, client_id: &str, coords: &Vec2) { if let Some(clients) = self.map.get_mut(coords) { clients.remove(client_id); if clients.is_empty() { self.map.remove(coords); } } } pub fn add_many(&mut self, client_id: &str, coords: &[Vec2]) { for coord in coords { self.add(client_id, coord); } } pub fn remove_many(&mut self, client_id: &str, coords: &[Vec2]) { for coord in coords { self.remove(client_id, coord); } } }