use crate::Random; use crate::StringKey; use hashbrown::HashMap; pub trait DataLibrary<'a, T: 'a> { fn map(&self) -> &HashMap; fn list_values(&self) -> &Vec; fn get_modify(&mut self) -> (&mut HashMap, &mut Vec); fn add(&mut self, key: &StringKey, value: T) { let modifies = self.get_modify(); modifies.0.insert(key.clone(), value); modifies.1.push(key.clone()); } fn remove(&mut self, key: &StringKey) { let modifies = self.get_modify(); let index = modifies.1.iter().position(|r| r == key).unwrap(); modifies.0.remove(key); modifies.1.remove(index); } fn get(&self, key: &StringKey) -> Option<&T> { self.map().get(key) } fn get_mut(&mut self, key: &StringKey) -> Option<&mut T> { self.get_modify().0.get_mut(key) } fn len(&self) -> usize { self.map().len() } fn is_empty(&self) -> bool { self.map().is_empty() } fn random_value(&self, rand: &mut Random) -> &T { let i = rand.get_between(0, self.list_values().len() as i32); let key = &self.list_values()[i as usize]; return &self.map()[key]; } }