Initial commit, implements most of the static_data side.

This commit is contained in:
2021-01-30 22:29:59 +01:00
commit 2a08fb2645
33 changed files with 1237 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
use crate::utils::random::Random;
use std::collections::HashMap;
pub trait DataLibrary<'a, T: 'a> {
fn map(&self) -> &HashMap<String, T>;
fn list_values(&self) -> &Vec<String>;
fn get_modify(&mut self) -> (&mut HashMap<String, T>, &mut Vec<String>);
fn add(&mut self, key: &str, value: T) {
let modifies = self.get_modify();
modifies.0.insert(key.to_string(), value);
modifies.1.push(key.to_string());
}
fn remove(&mut self, key: &str) {
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: &str) -> Option<&T> {
self.map().get(key)
}
fn get_mut(&mut self, key: &str) -> 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];
}
}