<commit_before>! Copyright (C) 2018 <NAME>
! See http://factorcode.org/license.txt for BSD license

USING: accessors arrays assocs bit-arrays byte-arrays calendar
colors.constants combinators combinators.short-circuit fry
kernel kernel.private locals math math.order math.private
math.ranges namespaces opengl random sequences sequences.private
timers ui ui.commands ui.gadgets ui.gadgets.toolbar
ui.gadgets.tracks ui.gestures ui.render words ;

IN: game-of-life

: make-grid ( rows cols -- grid )
    '[ _ <bit-array> ] replicate ;

: grid-dim ( grid -- rows cols )
    [ length ] [ first length ] bi ;

: random-grid! ( grid -- )
    [
        [ length>> ] [ underlying>> length random-bytes ] bi
        bit-array boa
    ] map! drop ;

:: wraparound ( x min max -- y )
    x min fixnum< [ max ] [ x max fixnum> min x ? ] if ; inline

:: count-neighbors ( grid -- counts )
    grid grid-dim { fixnum fixnum } declare :> ( rows cols )
    rows [ cols <byte-array> ] replicate :> neighbors
    grid { array } declare [| row j |
        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
            ] when
        ] each-index
    ] each-index neighbors ;
<commit_msg>game-of-life: even faster by factoring out the neighbor rows.<commit_after>! Copyright (C) 2018 <NAME>
! See http://factorcode.org/license.txt for BSD license

USING: accessors arrays assocs bit-arrays byte-arrays calendar
colors.constants combinators combinators.short-circuit fry
kernel kernel.private locals math math.order math.private
math.ranges namespaces opengl random sequences sequences.private
timers ui ui.commands ui.gadgets ui.gadgets.toolbar
ui.gadgets.tracks ui.gestures ui.render words ;

IN: game-of-life

: make-grid ( rows cols -- grid )
    '[ _ <bit-array> ] replicate ;

: grid-dim ( grid -- rows cols )
    [ length ] [ first length ] bi ;

: random-grid! ( grid -- )
    [
        [ length>> ] [ underlying>> length random-bytes ] bi
        bit-array boa
    ] map! drop ;

:: wraparound ( x min max -- y )
    x min fixnum< [ max ] [ x max fixnum> min x ? ] if ; inline

:: count-neighbors ( grid -- counts )
    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 [
                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 ;

{'fraction_non_alphanumeric': 0.10329985652797705, 'fraction_numerical': 0.010329985652797705, 'mean_word_length': 1.9001663893510816, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4611133', 'n_tokens_mistral': 1106, 'n_tokens_neox': 1046, 'n_words': 339}