forked from Maxluli/RustyPropagation
unfold neighbor functions to gain some speed
This commit is contained in:
+23
-38
@@ -116,8 +116,11 @@ impl Population {
|
|||||||
|
|
||||||
for idx in lower_bound..upper_bound {
|
for idx in lower_bound..upper_bound {
|
||||||
let human = &humans[idx];
|
let human = &humans[idx];
|
||||||
let mut neighbors: Vec<&Human> = Vec::with_capacity(8);
|
// let mut neighbors: Vec<&Human> = Vec::with_capacity(8);
|
||||||
if human.present_state == State::Normal {
|
let mut new_human = human.clone();
|
||||||
|
match human.present_state {
|
||||||
|
State::Normal => {
|
||||||
|
stats[0] += 1;
|
||||||
let possible = [
|
let possible = [
|
||||||
(human.x - 1, human.y - 1), (human.x, human.y - 1), (human.x + 1, human.y - 1),
|
(human.x - 1, human.y - 1), (human.x, human.y - 1), (human.x + 1, human.y - 1),
|
||||||
(human.x - 1, human.y) , (human.x + 1, human.y),
|
(human.x - 1, human.y) , (human.x + 1, human.y),
|
||||||
@@ -126,15 +129,23 @@ impl Population {
|
|||||||
for neigh_coords in possible.iter() {
|
for neigh_coords in possible.iter() {
|
||||||
let neigh_idx = point_to_index(neigh_coords.0, neigh_coords.1, width, height);
|
let neigh_idx = point_to_index(neigh_coords.0, neigh_coords.1, width, height);
|
||||||
match neigh_idx {
|
match neigh_idx {
|
||||||
Some(x) => neighbors.push(&humans[x]),
|
Some(x) => if humans[x].present_state == State::Infected {
|
||||||
None => {},
|
if roll(infection_rate) {
|
||||||
|
new_human.present_state = State::Infected;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let new_human = evolve(human, neighbors, infection_rate, curing_rate, death_rate);
|
State::Infected => {
|
||||||
match human.present_state {
|
stats[1] += 1;
|
||||||
State::Normal => { stats[0] += 1; }
|
match die_or_cure(curing_rate, death_rate) {
|
||||||
State::Infected => { stats[1] += 1; }
|
Some(x) => new_human.present_state = x,
|
||||||
|
None => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
State::Immune => { stats[2] += 1; }
|
State::Immune => { stats[2] += 1; }
|
||||||
State::Dead => { stats[3] += 1; }
|
State::Dead => { stats[3] += 1; }
|
||||||
}
|
}
|
||||||
@@ -157,39 +168,13 @@ impl Population {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn evolve(human: &Human, neighbors: Vec<&Human>, infection_rate: i32, curing_rate: i32, death_rate: i32) -> Human {
|
fn die_or_cure(curing_rate: i32, death_rate: i32) -> Option<State> {
|
||||||
let mut new_human = human.clone();
|
|
||||||
match human.present_state {
|
|
||||||
State::Normal => {
|
|
||||||
new_human.present_state = infect_by_neighbors(neighbors, infection_rate);
|
|
||||||
}
|
|
||||||
State::Infected => {
|
|
||||||
new_human.present_state = die_or_cure(curing_rate, death_rate);
|
|
||||||
}
|
|
||||||
State::Immune => {}
|
|
||||||
State::Dead => {}
|
|
||||||
}
|
|
||||||
new_human
|
|
||||||
}
|
|
||||||
|
|
||||||
fn infect_by_neighbors(neighbors: Vec<&Human>, infection_rate: i32) -> State {
|
|
||||||
for neighbor in neighbors.iter() {
|
|
||||||
if neighbor.present_state == State::Infected {
|
|
||||||
if roll(infection_rate) {
|
|
||||||
return State::Infected;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
State::Normal
|
|
||||||
}
|
|
||||||
|
|
||||||
fn die_or_cure(curing_rate: i32, death_rate: i32) -> State {
|
|
||||||
if roll(curing_rate) {
|
if roll(curing_rate) {
|
||||||
State::Immune
|
Some(State::Immune)
|
||||||
} else if roll(death_rate) {
|
} else if roll(death_rate) {
|
||||||
State::Dead
|
Some(State::Dead)
|
||||||
} else {
|
} else {
|
||||||
State::Infected
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user