less frequest cast with size

This commit is contained in:
2022-05-04 00:21:10 +02:00
parent b47c1fb2b6
commit 0f2702a5c0
+16 -12
View File
@@ -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<Human> = 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<Point> =
Vec::with_capacity((self.width * self.height) as usize);
Vec::with_capacity(self.size);
let mut possible_infected: Vec<Point> =
Vec::with_capacity((self.width * self.height) as usize);
Vec::with_capacity(self.size);
let mut people_to_infect: Vec<Point> =
Vec::with_capacity((self.width * self.height) as usize);
Vec::with_capacity(self.size);
let mut people_to_cure: Vec<Point> =
Vec::with_capacity((self.width * self.height) as usize);
Vec::with_capacity(self.size);
let mut people_to_kill: Vec<Point> =
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
}