forked from Maxluli/RustyPropagation
less frequest cast with size
This commit is contained in:
+16
-12
@@ -15,6 +15,7 @@ pub struct Population {
|
|||||||
pub height: i32,
|
pub height: i32,
|
||||||
pub age: i32,
|
pub age: i32,
|
||||||
pub plague: Disease,
|
pub plague: Disease,
|
||||||
|
pub size: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn human_idx(x: i32, y: i32, width: i32) -> usize {
|
pub fn human_idx(x: i32, y: i32, width: i32) -> usize {
|
||||||
@@ -32,28 +33,30 @@ impl Population {
|
|||||||
) -> Self {
|
) -> Self {
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::thread_rng();
|
||||||
|
|
||||||
|
let size: usize = (width * height) as usize;
|
||||||
|
|
||||||
let mut the_humans: Vec<Human> = vec![
|
let mut the_humans: Vec<Human> = vec![
|
||||||
Human {
|
Human {
|
||||||
x: 0,
|
x: 0,
|
||||||
y: 0,
|
y: 0,
|
||||||
present_state: State::Normal
|
present_state: State::Normal
|
||||||
};
|
};
|
||||||
(width * height) as usize
|
size
|
||||||
];
|
];
|
||||||
for x in 0..width {
|
for x in 0..width {
|
||||||
for y in 0..height {
|
for y in 0..height {
|
||||||
let idx = human_idx(x, y, width);
|
let idx = human_idx(x, y, width);
|
||||||
let mut present_state = State::Normal;
|
let mut present_state = State::Normal;
|
||||||
if (start_infected_ratio) > 0
|
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;
|
present_state = State::Infected;
|
||||||
} else if start_immune_ratio > 0
|
} else if (start_immune_ratio > 0)
|
||||||
&& rng.gen_range(0..CORRECTED_PERCENTAGE) <= start_immune_ratio as i32
|
&& (rng.gen_range(0..CORRECTED_PERCENTAGE) <= start_immune_ratio)
|
||||||
{
|
{
|
||||||
present_state = State::Immune;
|
present_state = State::Immune;
|
||||||
} else if start_dead_ratio > 0
|
} else if (start_dead_ratio > 0)
|
||||||
&& rng.gen_range(0..CORRECTED_PERCENTAGE) <= start_dead_ratio as i32
|
&& (rng.gen_range(0..CORRECTED_PERCENTAGE) <= start_dead_ratio)
|
||||||
{
|
{
|
||||||
present_state = State::Dead;
|
present_state = State::Dead;
|
||||||
}
|
}
|
||||||
@@ -69,6 +72,7 @@ impl Population {
|
|||||||
plague: plague,
|
plague: plague,
|
||||||
age: 0,
|
age: 0,
|
||||||
humans: the_humans,
|
humans: the_humans,
|
||||||
|
size: size,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// pub fn change_disease(&mut self, plague:Disease){
|
// pub fn change_disease(&mut self, plague:Disease){
|
||||||
@@ -98,15 +102,15 @@ impl Population {
|
|||||||
|
|
||||||
pub fn propagate(&mut self) -> [i32; 4] {
|
pub fn propagate(&mut self) -> [i32; 4] {
|
||||||
let mut people_to_check: Vec<Point> =
|
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> =
|
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> =
|
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> =
|
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> =
|
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];
|
let mut stats: [i32; 4] = [0, 0, 0, 0];
|
||||||
// stats[0] Normal stats[1] Infected stats[2] Immune stats[3] Dead
|
// stats[0] Normal stats[1] Infected stats[2] Immune stats[3] Dead
|
||||||
|
|
||||||
@@ -233,7 +237,7 @@ impl Population {
|
|||||||
}
|
}
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
stats[0] + stats[1] + stats[2] + stats[3],
|
stats[0] + stats[1] + stats[2] + stats[3],
|
||||||
self.humans.len() as i32
|
self.size as i32
|
||||||
);
|
);
|
||||||
stats
|
stats
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user