1
0
Fork 0
mirror of https://codeberg.org/demostf/parser.git synced 2026-06-03 10:14:06 +02:00

store classlist as u16s

This commit is contained in:
Robin Appelman 2024-05-04 16:44:13 +02:00
commit b35daf5c12

View file

@ -148,12 +148,12 @@ impl Class {
} }
#[derive(Default, Debug, Eq, PartialEq, Deserialize, Clone)] #[derive(Default, Debug, Eq, PartialEq, Deserialize, Clone)]
#[serde(from = "HashMap<Class, u8>")] #[serde(from = "HashMap<Class, u16>")]
pub struct ClassList([u8; 10]); pub struct ClassList([u16; 10]);
impl ClassList { impl ClassList {
/// Get an iterator for all classes played and the number of spawn on the class /// Get an iterator for all classes played and the number of spawn on the class
pub fn iter(&self) -> impl Iterator<Item = (Class, u8)> + '_ { pub fn iter(&self) -> impl Iterator<Item = (Class, u16)> + '_ {
self.0 self.0
.iter() .iter()
.copied() .copied()
@ -163,19 +163,19 @@ impl ClassList {
} }
/// Get an iterator for all classes played and the number of spawn on the class, sorted by the number of spawns /// Get an iterator for all classes played and the number of spawn on the class, sorted by the number of spawns
pub fn sorted(&self) -> impl Iterator<Item = (Class, u8)> { pub fn sorted(&self) -> impl Iterator<Item = (Class, u16)> {
let mut classes = self.iter().collect::<Vec<(Class, u8)>>(); let mut classes = self.iter().collect::<Vec<(Class, u16)>>();
classes.sort_by(|a, b| a.1.cmp(&b.1).reverse()); classes.sort_by(|a, b| a.1.cmp(&b.1).reverse());
classes.into_iter() classes.into_iter()
} }
pub fn get(&self, class: Class) -> u8 { pub fn get(&self, class: Class) -> u16 {
// class number is always in bounds // class number is always in bounds
#[allow(clippy::indexing_slicing)] #[allow(clippy::indexing_slicing)]
self.0[class as u8 as usize] self.0[class as u8 as usize]
} }
pub fn get_mut(&mut self, class: Class) -> &mut u8 { pub fn get_mut(&mut self, class: Class) -> &mut u16 {
// class number is always in bounds // class number is always in bounds
#[allow(clippy::indexing_slicing)] #[allow(clippy::indexing_slicing)]
&mut self.0[class as u8 as usize] &mut self.0[class as u8 as usize]
@ -208,8 +208,8 @@ impl Serialize for ClassList {
} }
} }
impl From<HashMap<Class, u8>> for ClassList { impl From<HashMap<Class, u16>> for ClassList {
fn from(map: HashMap<Class, u8>) -> Self { fn from(map: HashMap<Class, u16>) -> Self {
let mut classes = ClassList::default(); let mut classes = ClassList::default();
for (class, count) in map.into_iter() { for (class, count) in map.into_iter() {