forked from Maxluli/RustyPropagation
Compare commits
1 Commits
new_propa_
...
threads
| Author | SHA1 | Date | |
|---|---|---|---|
| ea74e1029b |
@@ -1,4 +1,8 @@
|
|||||||
|
use std::sync::Arc;
|
||||||
|
use std::sync::Mutex;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
use std::thread;
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Point {
|
pub struct Point {
|
||||||
@@ -10,7 +14,7 @@ pub struct Population {
|
|||||||
pub start_infected_ratio: i32,
|
pub start_infected_ratio: i32,
|
||||||
pub start_immune_ratio: i32,
|
pub start_immune_ratio: i32,
|
||||||
pub start_dead_ratio: i32,
|
pub start_dead_ratio: i32,
|
||||||
pub humans: Vec<Human>,
|
pub humans: Arc<Mutex<Vec<Human>>>,
|
||||||
pub width: i32,
|
pub width: i32,
|
||||||
pub height: i32,
|
pub height: i32,
|
||||||
pub age: i32,
|
pub age: i32,
|
||||||
@@ -35,14 +39,15 @@ impl Population {
|
|||||||
|
|
||||||
let size: usize = (width * height) as usize;
|
let size: usize = (width * height) as usize;
|
||||||
|
|
||||||
let mut the_humans: Vec<Human> = vec![
|
let the_humans_arc = Arc::new(Mutex::new(vec![
|
||||||
Human {
|
Human {
|
||||||
x: 0,
|
x: 0,
|
||||||
y: 0,
|
y: 0,
|
||||||
present_state: State::Normal
|
present_state: State::Normal
|
||||||
};
|
};
|
||||||
size
|
size
|
||||||
];
|
]));
|
||||||
|
let the_humans = Arc::clone(&the_humans_arc);
|
||||||
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);
|
||||||
@@ -60,7 +65,7 @@ impl Population {
|
|||||||
{
|
{
|
||||||
present_state = State::Dead;
|
present_state = State::Dead;
|
||||||
}
|
}
|
||||||
the_humans[idx] = Human{x: x, y: y, present_state: present_state};
|
the_humans.lock().unwrap()[idx] = Human{x: x, y: y, present_state: present_state};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Self {
|
Self {
|
||||||
@@ -71,7 +76,7 @@ impl Population {
|
|||||||
height: height,
|
height: height,
|
||||||
plague: plague,
|
plague: plague,
|
||||||
age: 0,
|
age: 0,
|
||||||
humans: the_humans,
|
humans: the_humans_arc,
|
||||||
size: size,
|
size: size,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -88,9 +93,11 @@ impl Population {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn is_inside_and_infected(&self, point: Point) -> bool {
|
fn is_inside_and_infected(&self, point: Point) -> bool {
|
||||||
|
let the_humans_arc = Arc::clone(&self.humans);
|
||||||
if self.is_inside(&point) {
|
if self.is_inside(&point) {
|
||||||
let idx = human_idx(point.x, point.y, self.width);
|
let idx = human_idx(point.x, point.y, self.width);
|
||||||
if self.humans[idx].present_state == State::Infected {
|
let humans = the_humans_arc.lock().unwrap();
|
||||||
|
if humans[idx].present_state == State::Infected {
|
||||||
roll(self.plague.infection_rate)
|
roll(self.plague.infection_rate)
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
@@ -114,7 +121,8 @@ impl Population {
|
|||||||
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
|
||||||
|
|
||||||
for h in self.humans.iter() {
|
let humans = Arc::clone(&self.humans);
|
||||||
|
for h in humans.lock().unwrap().iter() {
|
||||||
match h.present_state {
|
match h.present_state {
|
||||||
State::Normal => {
|
State::Normal => {
|
||||||
possible_infected.push(Point{ x: h.x, y: h.y});
|
possible_infected.push(Point{ x: h.x, y: h.y});
|
||||||
@@ -202,36 +210,60 @@ impl Population {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for infected_position in people_to_infect.iter() {
|
// for infected_position in people_to_infect.iter() {
|
||||||
// println!("To infect: {:?}", infected_position);
|
// // println!("To infect: {:?}", infected_position);
|
||||||
//people_to_infect.iter().map(|infected_position|{
|
// //people_to_infect.iter().map(|infected_position|{
|
||||||
let infected_index = human_idx(infected_position.x, infected_position.y, self.width);
|
// let infected_index = human_idx(infected_position.x, infected_position.y, self.width);
|
||||||
// let _ = infected_position.x;
|
// // let _ = infected_position.x;
|
||||||
//DEBUG
|
// //DEBUG
|
||||||
//println!("x: {} y: {} index: {}",infected_position.x,infected_position.y,infected_index);
|
// //println!("x: {} y: {} index: {}",infected_position.x,infected_position.y,infected_index);
|
||||||
self.humans[infected_index].present_state = State::Infected;
|
// self.humans[infected_index].present_state = State::Infected;
|
||||||
//DEBUG
|
// //DEBUG
|
||||||
//println!("Infected someone");
|
// //println!("Infected someone");
|
||||||
|
// }
|
||||||
|
|
||||||
|
let mut threads = vec![];
|
||||||
|
{
|
||||||
|
let humans = Arc::clone(&self.humans);
|
||||||
|
let width = self.width;
|
||||||
|
threads.push(thread::spawn(move || {
|
||||||
|
for infected_position in people_to_infect.iter() {
|
||||||
|
let infected_index = human_idx(infected_position.x, infected_position.y, width);
|
||||||
|
humans.lock().unwrap()[infected_index].present_state = State::Infected;
|
||||||
|
}
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
for cured_position in people_to_cure.iter() {
|
{
|
||||||
//people_to_cure.iter().map(|cured_position|{
|
let humans = Arc::clone(&self.humans);
|
||||||
let cured_index = human_idx(cured_position.x, cured_position.y, self.width);
|
let width = self.width;
|
||||||
if self.humans[cured_index].present_state != State::Infected {
|
threads.push(thread::spawn(move || {
|
||||||
println!("not infected");
|
for cured_position in people_to_cure.iter() {
|
||||||
}
|
//people_to_cure.iter().map(|cured_position|{
|
||||||
self.humans[cured_index].present_state = State::Immune;
|
let cured_index = human_idx(cured_position.x, cured_position.y, width);
|
||||||
//DEBUG
|
if humans.lock().unwrap()[cured_index].present_state != State::Infected {
|
||||||
//println!("Cured someone");
|
println!("not infected");
|
||||||
|
} else {
|
||||||
|
humans.lock().unwrap()[cured_index].present_state = State::Immune;
|
||||||
|
}
|
||||||
|
//DEBUG
|
||||||
|
//println!("Cured someone");
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
for t in threads {
|
||||||
|
t.join().unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
for dead_position in people_to_kill.iter() {
|
for dead_position in people_to_kill.iter() {
|
||||||
|
let humans = Arc::clone(&self.humans);
|
||||||
//people_to_kill.iter().map(|dead_position|{
|
//people_to_kill.iter().map(|dead_position|{
|
||||||
let dead_index = human_idx(dead_position.x, dead_position.y, self.width);
|
let dead_index = human_idx(dead_position.x, dead_position.y, self.width);
|
||||||
if self.humans[dead_index].present_state == State::Dead {
|
if humans.lock().unwrap()[dead_index].present_state == State::Dead {
|
||||||
// println!("Already dead");
|
// println!("Already dead");
|
||||||
} else {
|
} else {
|
||||||
self.humans[dead_index].present_state = State::Dead;
|
humans.lock().unwrap()[dead_index].present_state = State::Dead;
|
||||||
}
|
}
|
||||||
//DEBUG
|
//DEBUG
|
||||||
}
|
}
|
||||||
@@ -364,13 +396,14 @@ mod tests {
|
|||||||
let disease = Disease::new(20, 10, 5, String::from("Covid 44"));
|
let disease = Disease::new(20, 10, 5, String::from("Covid 44"));
|
||||||
let (width, height) = (5, 7);
|
let (width, height) = (5, 7);
|
||||||
let population = Population::new(20, 10, 5, 5, 7, disease);
|
let population = Population::new(20, 10, 5, 5, 7, disease);
|
||||||
assert_eq!(population.humans.len(), 5 * 7);
|
let humans = Arc::clone(&population.humans);
|
||||||
for h in population.humans.iter() {
|
assert_eq!(humans.lock().unwrap().len(), 5 * 7);
|
||||||
|
for h in humans.lock().unwrap().iter() {
|
||||||
let idx = human_idx(h.x, h.y, width);
|
let idx = human_idx(h.x, h.y, width);
|
||||||
assert_eq!(population.humans[idx].x, h.x, "coordinates should match");
|
assert_eq!(humans.lock().unwrap()[idx].x, h.x, "coordinates should match");
|
||||||
assert_eq!(population.humans[idx].y, h.y, "coordinates should match");
|
assert_eq!(humans.lock().unwrap()[idx].y, h.y, "coordinates should match");
|
||||||
}
|
}
|
||||||
assert_eq!(population.humans.len(), (width * height) as usize);
|
assert_eq!(humans.lock().unwrap().len(), (width * height) as usize);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
Reference in New Issue
Block a user