sequences.unrolled: reimplement underlying (unrolled-each-integer) loop as a partial eval, sacrificing row polymorphism for flexibility

db4
Joe Groff 2010-05-24 00:33:41 -07:00
parent 8d66b14e59
commit 44ed11a084
3 changed files with 46 additions and 32 deletions

View File

@ -5,70 +5,70 @@ IN: sequences.unrolled
HELP: unrolled-collect
{ $values
{ "n" integer } { "quot" { $quotation "( ... n -- ... value )" } } { "into" sequence }
{ "n" integer } { "quot" { $quotation "( n -- value )" } } { "into" sequence }
}
{ $description "Unrolled version of " { $link collect } ". " { $snippet "n" } " must be a compile-time constant." } ;
HELP: unrolled-each
{ $values
{ "seq" sequence } { "len" integer } { "quot" { $quotation "( ... x -- ... )" } }
{ "seq" sequence } { "len" integer } { "quot" { $quotation "( x -- )" } }
}
{ $description "Unrolled version of " { $link each } " that iterates over the first " { $snippet "len" } " elements of " { $snippet "seq" } ". " { $snippet "len" } " must be a compile-time constant. If " { $snippet "seq" } " has fewer than " { $snippet "len" } " elements, raises an " { $link unrolled-bounds-error } "." } ;
HELP: unrolled-2each
{ $values
{ "xseq" sequence } { "yseq" sequence } { "len" integer } { "quot" { $quotation "( ... x y -- ... )" } }
{ "xseq" sequence } { "yseq" sequence } { "len" integer } { "quot" { $quotation "( x y -- )" } }
}
{ $description "Unrolled version of " { $link 2each } " that iterates over the first " { $snippet "len" } " elements of " { $snippet "xseq" } " and " { $snippet "yseq" } ". " { $snippet "len" } " must be a compile-time constant. If " { $snippet "seq" } " has fewer than " { $snippet "len" } " elements, raises an " { $link unrolled-bounds-error } "." } ;
HELP: unrolled-each-index
{ $values
{ "seq" sequence } { "len" integer } { "quot" { $quotation "( ... x i -- ... )" } }
{ "seq" sequence } { "len" integer } { "quot" { $quotation "( x i -- )" } }
}
{ $description "Unrolled version of " { $link each-index } " that iterates over the first " { $snippet "len" } " elements of " { $snippet "seq" } ". " { $snippet "len" } " must be a compile-time constant. If " { $snippet "seq" } " has fewer than " { $snippet "len" } " elements, raises an " { $link unrolled-bounds-error } "." } ;
HELP: unrolled-each-integer
{ $values
{ "n" integer } { "quot" { $quotation "( ... i -- ... )" } }
{ "n" integer } { "quot" { $quotation "( i -- )" } }
}
{ $description "Unrolled version of " { $link each-integer } ". " { $snippet "n" } " must be a compile-time constant." } ;
HELP: unrolled-map
{ $values
{ "seq" sequence } { "len" integer } { "quot" { $quotation "( ... x -- ... newx )" } }
{ "seq" sequence } { "len" integer } { "quot" { $quotation "( x -- newx )" } }
{ "newseq" sequence }
}
{ $description "Unrolled version of " { $link map } " that maps over the first " { $snippet "len" } " elements of " { $snippet "seq" } ". " { $snippet "len" } " must be a compile-time constant. If " { $snippet "seq" } " has fewer than " { $snippet "len" } " elements, raises an " { $link unrolled-bounds-error } "." } ;
HELP: unrolled-map-as
{ $values
{ "seq" sequence } { "len" integer } { "quot" { $quotation "( ... x -- ... newx )" } } { "exemplar" sequence }
{ "seq" sequence } { "len" integer } { "quot" { $quotation "( x -- newx )" } } { "exemplar" sequence }
{ "newseq" sequence }
}
{ $description "Unrolled version of " { $link map-as } " that maps over the first " { $snippet "len" } " elements of " { $snippet "seq" } ". " { $snippet "len" } " must be a compile-time constant. If " { $snippet "seq" } " has fewer than " { $snippet "len" } " elements, raises an " { $link unrolled-bounds-error } "." } ;
HELP: unrolled-2map
{ $values
{ "xseq" sequence } { "yseq" sequence } { "len" integer } { "quot" { $quotation "( ... x y -- ... newx )" } } { "newseq" sequence }
{ "xseq" sequence } { "yseq" sequence } { "len" integer } { "quot" { $quotation "( x y -- newx )" } } { "newseq" sequence }
}
{ $description "Unrolled version of " { $link 2map } " that iterates over the first " { $snippet "len" } " elements of " { $snippet "xseq" } " and " { $snippet "yseq" } ". " { $snippet "len" } " must be a compile-time constant. If " { $snippet "seq" } " has fewer than " { $snippet "len" } " elements, raises an " { $link unrolled-bounds-error } "." } ;
HELP: unrolled-2map-as
{ $values
{ "xseq" sequence } { "yseq" sequence } { "len" integer } { "quot" { $quotation "( ... x y -- ... newx )" } } { "exemplar" sequence } { "newseq" sequence }
{ "xseq" sequence } { "yseq" sequence } { "len" integer } { "quot" { $quotation "( x y -- newx )" } } { "exemplar" sequence } { "newseq" sequence }
}
{ $description "Unrolled version of " { $link 2map-as } " that iterates over the first " { $snippet "len" } " elements of " { $snippet "xseq" } " and " { $snippet "yseq" } ". " { $snippet "len" } " must be a compile-time constant. If " { $snippet "seq" } " has fewer than " { $snippet "len" } " elements, raises an " { $link unrolled-bounds-error } "." } ;
HELP: unrolled-map-index
{ $values
{ "seq" sequence } { "len" integer } { "quot" { $quotation "( ... x i -- ... newx )" } }
{ "seq" sequence } { "len" integer } { "quot" { $quotation "( x i -- newx )" } }
{ "newseq" sequence }
}
{ $description "Unrolled version of " { $link map-index } " that maps over the first " { $snippet "len" } " elements of " { $snippet "seq" } ". " { $snippet "len" } " must be a compile-time constant. If " { $snippet "seq" } " has fewer than " { $snippet "len" } " elements, raises an " { $link unrolled-bounds-error } "." } ;
HELP: unrolled-map-integers
{ $values
{ "n" integer } { "quot" { $quotation "( ... n -- ... value )" } } { "exemplar" sequence } { "newseq" sequence }
{ "n" integer } { "quot" { $quotation "( n -- value )" } } { "exemplar" sequence } { "newseq" sequence }
}
{ $description "Unrolled version of " { $link map-integers } ". " { $snippet "n" } " must be a compile-time constant." } ;

View File

@ -7,18 +7,28 @@ IN: sequences.unrolled.tests
[ { "0" "1" "2" } ] [ { 0 1 2 } [ 3 [ number>string ] unrolled-map ] compile-call ] unit-test
[ { "0" "1" "2" } ] [ [ { 0 1 2 } 3 [ number>string , ] unrolled-each ] { } make ] unit-test
[ { "0" "1" "2" } ] [ [ { 0 1 2 } [ 3 [ number>string , ] unrolled-each ] compile-call ] { } make ] unit-test
[ { "a0" "b1" "c2" } ]
[ [ { "a" "b" "c" } 3 [ number>string append , ] unrolled-each-index ] { } make ] unit-test
[ { "a0" "b1" "c2" } ]
[ [ { "a" "b" "c" } [ 3 [ number>string append , ] unrolled-each-index ] compile-call ] { } make ] unit-test
[ { "aI" "bII" "cIII" } ]
[ [ { "a" "b" "c" } { "I" "II" "III" } 3 [ append , ] unrolled-2each ] { } make ] unit-test
[ [ { "a" "b" "c" } { "I" "II" "III" } [ 3 [ append , ] unrolled-2each ] compile-call ] { } make ] unit-test
[ { "aI" "bII" "cIII" } ]
[ { "a" "b" "c" } { "I" "II" "III" } 3 [ append ] unrolled-2map ] unit-test
[ { "aI" "bII" "cIII" } ]
[ { "a" "b" "c" } { "I" "II" "III" } [ 3 [ append ] unrolled-2map ] compile-call ] unit-test
[ { "a0" "b1" "c2" } ]
[ { "a" "b" "c" } 3 [ number>string append ] unrolled-map-index ] unit-test
[ { "a0" "b1" "c2" } ]
[ { "a" "b" "c" } [ 3 [ number>string append ] unrolled-map-index ] compile-call ] unit-test
[ { 0 1 2 } 4 [ number>string ] unrolled-map ] [ unrolled-bounds-error? ] must-fail-with
[ { 0 1 2 3 } { 0 1 2 } 4 [ number>string append ] unrolled-2map ] [ unrolled-2bounds-error? ] must-fail-with

View File

@ -1,21 +1,25 @@
! (c)2010 Joe Groff bsd license
USING: combinators.short-circuit fry generalizations kernel
locals macros math quotations sequences ;
USING: combinators combinators.short-circuit fry generalizations kernel
locals macros math quotations sequences compiler.tree.propagation.transforms ;
FROM: sequences.private => (each) (each-index) (collect) (2each) ;
IN: sequences.unrolled
<PRIVATE
MACRO: (unrolled-each-integer) ( n -- )
[ iota >quotation ] keep '[ _ dip _ napply ] ;
: (unrolled-each-integer) ( quot n -- )
swap '[ _ call( i -- ) ] each-integer ;
<< \ (unrolled-each-integer) [
iota [ '[ _ swap call( i -- ) ] ] [ ] map-as '[ _ cleave ]
] 1 define-partial-eval >>
PRIVATE>
: unrolled-each-integer ( ... n quot: ( ... i -- ... ) -- ... )
: unrolled-each-integer ( n quot: ( i -- ) -- )
swap (unrolled-each-integer) ; inline
: unrolled-collect ( ... n quot: ( ... n -- ... value ) into -- ... )
: unrolled-collect ( n quot: ( n -- value ) into -- )
(collect) unrolled-each-integer ; inline
: unrolled-map-integers ( ... n quot: ( ... n -- ... value ) exemplar -- ... newseq )
: unrolled-map-integers ( n quot: ( n -- value ) exemplar -- newseq )
[ over ] dip [ [ unrolled-collect ] keep ] new-like ; inline
ERROR: unrolled-bounds-error
@ -42,44 +46,44 @@ ERROR: unrolled-2bounds-error
: (unrolled-2each) ( xseq yseq len quot -- len quot )
[ '[ _ ] 2dip ] dip (2each) nip ; inline
: unrolled-each-unsafe ( ... seq len quot: ( ... x -- ... ) -- ... )
: unrolled-each-unsafe ( seq len quot: ( x -- ) -- )
(unrolled-each) unrolled-each-integer ; inline
: unrolled-2each-unsafe ( ... xseq yseq len quot: ( ... x y -- ... ) -- ... )
: unrolled-2each-unsafe ( xseq yseq len quot: ( x y -- ) -- )
(unrolled-2each) unrolled-each-integer ; inline
: unrolled-each-index-unsafe ( ... seq len quot: ( ... x -- ... ) -- ... )
: unrolled-each-index-unsafe ( seq len quot: ( x -- ) -- )
(unrolled-each-index) unrolled-each-integer ; inline
: unrolled-map-as-unsafe ( ... seq len quot: ( ... x -- ... newx ) exemplar -- ... newseq )
: unrolled-map-as-unsafe ( seq len quot: ( x -- newx ) exemplar -- newseq )
[ (unrolled-each) ] dip unrolled-map-integers ; inline
: unrolled-2map-as-unsafe ( ... xseq yseq len quot: ( ... x y -- ... newx ) exemplar -- ... newseq )
: unrolled-2map-as-unsafe ( xseq yseq len quot: ( x y -- newx ) exemplar -- newseq )
[ (unrolled-2each) ] dip unrolled-map-integers ; inline
PRIVATE>
: unrolled-each ( ... seq len quot: ( ... x -- ... ) -- ... )
: unrolled-each ( seq len quot: ( x -- ) -- )
unrolled-bounds-check unrolled-each-unsafe ; inline
: unrolled-2each ( ... xseq yseq len quot: ( ... x y -- ... ) -- ... )
: unrolled-2each ( xseq yseq len quot: ( x y -- ) -- )
unrolled-2bounds-check unrolled-2each-unsafe ; inline
: unrolled-each-index ( ... seq len quot: ( ... x i -- ... ) -- ... )
: unrolled-each-index ( seq len quot: ( x i -- ) -- )
unrolled-bounds-check unrolled-each-index-unsafe ; inline
: unrolled-map-as ( ... seq len quot: ( ... x -- ... newx ) exemplar -- ... newseq )
: unrolled-map-as ( seq len quot: ( x -- newx ) exemplar -- newseq )
[ unrolled-bounds-check ] dip unrolled-map-as-unsafe ; inline
: unrolled-2map-as ( ... xseq yseq len quot: ( ... x y -- ... newx ) exemplar -- ... newseq )
: unrolled-2map-as ( xseq yseq len quot: ( x y -- newx ) exemplar -- newseq )
[ unrolled-2bounds-check ] dip unrolled-2map-as-unsafe ; inline
: unrolled-map ( ... seq len quot: ( ... x -- ... newx ) -- ... newseq )
: unrolled-map ( seq len quot: ( x -- newx ) -- newseq )
pick unrolled-map-as ; inline
: unrolled-2map ( ... xseq yseq len quot: ( ... x y -- ... newx ) -- ... newseq )
: unrolled-2map ( xseq yseq len quot: ( x y -- newx ) -- newseq )
4 npick unrolled-2map-as ; inline
: unrolled-map-index ( ... seq len quot: ( ... x i -- ... newx ) -- ... newseq )
: unrolled-map-index ( seq len quot: ( x i -- newx ) -- newseq )
[ dup length iota ] 2dip unrolled-2map ; inline