game-of-life: even faster by factoring out the neighbor rows.
parent
ac1029e8eb
commit
6a215adbbb
|
@ -29,18 +29,22 @@ IN: game-of-life
|
||||||
grid grid-dim { fixnum fixnum } declare :> ( rows cols )
|
grid grid-dim { fixnum fixnum } declare :> ( rows cols )
|
||||||
rows [ cols <byte-array> ] replicate :> neighbors
|
rows [ cols <byte-array> ] replicate :> neighbors
|
||||||
grid { array } declare [| row j |
|
grid { array } declare [| row j |
|
||||||
|
j 1 fixnum-fast 0 rows 1 fixnum-fast wraparound
|
||||||
|
j
|
||||||
|
j 1 fixnum+fast 0 rows 1 fixnum-fast wraparound
|
||||||
|
[ neighbors nth-unsafe { byte-array } declare ] tri@ :>
|
||||||
|
( above same below )
|
||||||
|
|
||||||
row { bit-array } declare [| cell i |
|
row { bit-array } declare [| cell i |
|
||||||
cell [
|
cell [
|
||||||
{ -1 0 1 } [| y |
|
i 1 fixnum-fast 0 cols 1 fixnum-fast wraparound
|
||||||
y j fixnum+fast 0 rows 1 fixnum-fast wraparound
|
i
|
||||||
neighbors nth-unsafe { byte-array } declare
|
i 1 fixnum+fast 0 cols 1 fixnum-fast wraparound
|
||||||
{ -1 0 1 } [| x |
|
|
||||||
x y [ 0 eq? ] both? [ drop ] [
|
[ [ above [ 1 fixnum+fast ] change-nth-unsafe ] tri@ ]
|
||||||
x i fixnum+fast 0 cols 1 fixnum-fast wraparound
|
[ nip [ same [ 1 fixnum+fast ] change-nth-unsafe ] bi@ ]
|
||||||
swap [ 1 fixnum+fast ] change-nth-unsafe
|
[ [ below [ 1 fixnum+fast ] change-nth-unsafe ] tri@ ]
|
||||||
] if
|
3tri
|
||||||
] with each
|
|
||||||
] each
|
|
||||||
] when
|
] when
|
||||||
] each-index
|
] each-index
|
||||||
] each-index neighbors ;
|
] each-index neighbors ;
|
||||||
|
|
Loading…
Reference in New Issue