use std::collections::HashMap;
#[derive(Debug, Default)]
pub struct AnchorSupply {
anchor_map: HashMap<usize, usize>,
}
impl AnchorSupply {
pub fn new() -> Self {
Self {
anchor_map: HashMap::new(),
}
}
pub fn with_capacity(capacity: usize) -> Self {
Self {
anchor_map: HashMap::with_capacity(capacity),
}
}
#[inline]
pub fn get(&self, entity_idx: usize) -> Option<usize> {
self.anchor_map.get(&entity_idx).copied()
}
#[inline]
pub fn set(&mut self, entity_idx: usize, anchor_idx: usize) -> Option<usize> {
self.anchor_map.insert(entity_idx, anchor_idx)
}
#[inline]
pub fn remove(&mut self, entity_idx: usize) -> Option<usize> {
self.anchor_map.remove(&entity_idx)
}
#[inline]
pub fn cascade(&mut self, entity_indices: impl IntoIterator<Item = usize>, anchor_idx: usize) {
for entity_idx in entity_indices {
self.anchor_map.insert(entity_idx, anchor_idx);
}
}
#[inline]
pub fn clear(&mut self) {
self.anchor_map.clear();
}
#[inline]
pub fn len(&self) -> usize {
self.anchor_map.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.anchor_map.is_empty()
}
#[inline]
pub fn iter(&self) -> impl Iterator<Item = (&usize, &usize)> {
self.anchor_map.iter()
}
pub fn entities_for_anchor(&self, anchor_idx: usize) -> Vec<usize> {
self.anchor_map
.iter()
.filter(|(_, &a)| a == anchor_idx)
.map(|(&e, _)| e)
.collect()
}
}