use crate::location::state::LocationState;
use crate::LocId;
use ahash::AHashMap;
use parking_lot::{Mutex, RwLock};
use std::sync::Arc;
#[derive(Default)]
pub struct Shard<T> {
pub map: RwLock<AHashMap<LocId, Arc<Mutex<LocationState<T>>>>>,
}
impl<T> Shard<T> {
#[must_use]
pub fn get(&self, id: LocId) -> Option<Arc<Mutex<LocationState<T>>>> {
self.map.read().get(&id).cloned()
}
pub fn insert(&self, id: LocId, state: LocationState<T>) -> Arc<Mutex<LocationState<T>>> {
let arc = Arc::new(Mutex::new(state));
self.map.write().insert(id, arc.clone());
arc
}
pub fn remove(&self, id: LocId) -> bool {
self.map.write().remove(&id).is_some()
}
#[allow(clippy::len_without_is_empty)]
#[must_use]
pub fn len(&self) -> usize {
self.map.read().len()
}
}