···
4
-
fn get_at(world: &Vec<bool>, size: usize, x: isize, y: isize) -> u8 {
5
-
let isize = size as isize;
6
-
if x == isize || x == -1 || y == isize || y == -1 {
4
+
fn get_at(world: &Vec<bool>, size: usize, x: usize, y: usize) -> u8 {
5
+
// benefits from the integer overflow to simplify code
6
+
if x >= size || y >= size {
9
-
let (x, y) = (x as usize, y as usize);
10
-
if *world.get(y * size + x).expect("invalid position") {
9
+
// this is in known bounds
10
+
unsafe { *world.get_unchecked(y * size + x) as u8 }
fn get_at_bool(world: &Vec<bool>, size: usize, x: usize, y: usize) -> bool {
19
-
*world.get(y * size + x).expect("invalid position")
15
+
// this is in known bounds
16
+
unsafe { *world.get_unchecked(y * size + x) }
fn generations(times: u32, mut world: Vec<bool>, size: usize, stuck: bool) -> Vec<bool> {
···
35
-
let xo = x as isize;
36
-
let yo = y as isize;
31
+
let ym = yo.wrapping_sub(1);
34
+
let xm = xo.wrapping_sub(1);
42
-
let was = get_at_bool(&world, size, x, y);
37
+
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);
51
-
new_world[y * size + x] = neighbours == 3 || (neighbours == 2 && was);
46
+
new_world[yo * size + xo] = neighbours == 3 || (neighbours == 2 && was);