···
-
fn get_at(world: &Vec<bool>, size: usize, x: isize, y: isize) -> u8 {
-
let isize = size as isize;
-
if x == isize || x == -1 || y == isize || y == -1 {
-
let (x, y) = (x as usize, y as usize);
-
if *world.get(y * size + x).expect("invalid position") {
fn get_at_bool(world: &Vec<bool>, size: usize, x: usize, y: usize) -> bool {
-
*world.get(y * size + x).expect("invalid position")
fn generations(times: u32, mut world: Vec<bool>, size: usize, stuck: bool) -> Vec<bool> {
···
-
let was = get_at_bool(&world, size, x, y);
let neighbours = get_at(&world, size, xm, ym)
+ get_at(&world, size, xo, ym)
+ get_at(&world, size, xp, ym)
···
+ get_at(&world, size, xm, yp)
+ get_at(&world, size, xo, yp)
+ get_at(&world, size, xp, yp);
-
new_world[y * size + x] = neighbours == 3 || (neighbours == 2 && was);
···
+
fn get_at(world: &Vec<bool>, size: usize, x: usize, y: usize) -> u8 {
+
// benefits from the integer overflow to simplify code
+
if x >= size || y >= size {
+
// this is in known bounds
+
unsafe { *world.get_unchecked(y * size + x) as u8 }
fn get_at_bool(world: &Vec<bool>, size: usize, x: usize, y: usize) -> bool {
+
// this is in known bounds
+
unsafe { *world.get_unchecked(y * size + x) }
fn generations(times: u32, mut world: Vec<bool>, size: usize, stuck: bool) -> Vec<bool> {
···
+
let ym = yo.wrapping_sub(1);
+
let xm = xo.wrapping_sub(1);
+
let was = get_at_bool(&world, size, xo, yo);
let neighbours = get_at(&world, size, xm, ym)
+ get_at(&world, size, xo, ym)
+ get_at(&world, size, xp, ym)
···
+ get_at(&world, size, xm, yp)
+ get_at(&world, size, xo, yp)
+ get_at(&world, size, xp, yp);
+
new_world[yo * size + xo] = neighbours == 3 || (neighbours == 2 && was);