]> gitweb.factorcode.org Git - factor.git/commitdiff
game-of-life: even faster by factoring out the neighbor rows.
authorJohn Benediktsson <mrjbq7@gmail.com>
Sun, 11 Mar 2018 18:48:35 +0000 (11:48 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Sun, 11 Mar 2018 18:48:35 +0000 (11:48 -0700)
extra/game-of-life/game-of-life.factor

index ce7c297216136dc6b14d11463770f42db3c2da95..1425606f4bb1763e822f39aecb408a79abf7cf68 100644 (file)
@@ -29,18 +29,22 @@ IN: game-of-life
     grid grid-dim { fixnum fixnum } declare :> ( rows cols )
     rows [ cols <byte-array> ] replicate :> neighbors
     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 |
             cell [
-                { -1 0 1 } [| y |
-                    y j fixnum+fast 0 rows 1 fixnum-fast wraparound
-                    neighbors nth-unsafe { byte-array } declare
-                    { -1 0 1 } [| x |
-                        x y [ 0 eq? ] both? [ drop ] [
-                            x i fixnum+fast 0 cols 1 fixnum-fast wraparound
-                            swap [ 1 fixnum+fast ] change-nth-unsafe
-                        ] if
-                    ] with each
-                ] each
+                i 1 fixnum-fast 0 cols 1 fixnum-fast wraparound
+                i
+                i 1 fixnum+fast 0 cols 1 fixnum-fast wraparound
+
+                [ [ above [ 1 fixnum+fast ] change-nth-unsafe ] tri@ ]
+                [ nip [ same [ 1 fixnum+fast ] change-nth-unsafe ] bi@ ]
+                [ [ below [ 1 fixnum+fast ] change-nth-unsafe ] tri@ ]
+                3tri
             ] when
         ] each-index
     ] each-index neighbors ;