count-neighbors ( grid -- counts )


Vocabulary
game-of-life

Inputs
gridan object


Outputs
countsan object


Definition


:: count-neighbors ( grid -- counts )
grid grid-dim { fixnum fixnum } declare
:> ( rows cols ) rows 1 - { fixnum } declare
:> max-rows cols 1 - { fixnum } declare :> max-cols rows
[ cols <byte-array> ] replicate :> neighbors grid
{ array } declare [| row j |
j 0 eq? [ max-rows ] [ j 1 - ] if j j max-rows eq?
[ 0 ] [ j 1 + ] if
[ neighbors nth-unsafe { byte-array } declare ] tri@
:> ( above same below ) row { bit-array } declare [|
cell i |
cell [
i 0 eq? [ max-cols ] [ i 1 - ] if
i i max-cols eq? [ 0 ] [ i 1 + ] if
[ [ above ~quotation~ change-nth-unsafe ] tri@ ]
[
nip
[ same ~quotation~ change-nth-unsafe ] bi@
]
[ [ below ~quotation~ change-nth-unsafe ] tri@ ]
3tri
] when
] each-index
] each-index neighbors ;