From 0f2702a5c0faabe740aa1450a92e572dbdc0bb9e Mon Sep 17 00:00:00 2001 From: Rene Luria Date: Wed, 4 May 2022 00:21:10 +0200 Subject: [PATCH] less frequest cast with size --- src/population.rs | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/population.rs b/src/population.rs index c7acbb4..ee52375 100644 --- a/src/population.rs +++ b/src/population.rs @@ -15,6 +15,7 @@ pub struct Population { pub height: i32, pub age: i32, pub plague: Disease, + pub size: usize, } pub fn human_idx(x: i32, y: i32, width: i32) -> usize { @@ -32,28 +33,30 @@ impl Population { ) -> Self { let mut rng = rand::thread_rng(); + let size: usize = (width * height) as usize; + let mut the_humans: Vec = vec![ Human { x: 0, y: 0, present_state: State::Normal }; - (width * height) as usize + size ]; for x in 0..width { for y in 0..height { let idx = human_idx(x, y, width); let mut present_state = State::Normal; if (start_infected_ratio) > 0 - && (rng.gen_range(0..CORRECTED_PERCENTAGE) <= start_infected_ratio as i32) + && (rng.gen_range(0..CORRECTED_PERCENTAGE) <= start_infected_ratio) { present_state = State::Infected; - } else if start_immune_ratio > 0 - && rng.gen_range(0..CORRECTED_PERCENTAGE) <= start_immune_ratio as i32 + } else if (start_immune_ratio > 0) + && (rng.gen_range(0..CORRECTED_PERCENTAGE) <= start_immune_ratio) { present_state = State::Immune; - } else if start_dead_ratio > 0 - && rng.gen_range(0..CORRECTED_PERCENTAGE) <= start_dead_ratio as i32 + } else if (start_dead_ratio > 0) + && (rng.gen_range(0..CORRECTED_PERCENTAGE) <= start_dead_ratio) { present_state = State::Dead; } @@ -69,6 +72,7 @@ impl Population { plague: plague, age: 0, humans: the_humans, + size: size, } } // pub fn change_disease(&mut self, plague:Disease){ @@ -98,15 +102,15 @@ impl Population { pub fn propagate(&mut self) -> [i32; 4] { let mut people_to_check: Vec = - Vec::with_capacity((self.width * self.height) as usize); + Vec::with_capacity(self.size); let mut possible_infected: Vec = - Vec::with_capacity((self.width * self.height) as usize); + Vec::with_capacity(self.size); let mut people_to_infect: Vec = - Vec::with_capacity((self.width * self.height) as usize); + Vec::with_capacity(self.size); let mut people_to_cure: Vec = - Vec::with_capacity((self.width * self.height) as usize); + Vec::with_capacity(self.size); let mut people_to_kill: Vec = - Vec::with_capacity((self.width * self.height) as usize); + Vec::with_capacity(self.size); let mut stats: [i32; 4] = [0, 0, 0, 0]; // stats[0] Normal stats[1] Infected stats[2] Immune stats[3] Dead @@ -233,7 +237,7 @@ impl Population { } assert_eq!( stats[0] + stats[1] + stats[2] + stats[3], - self.humans.len() as i32 + self.size as i32 ); stats }