wrap: remove use of namespace, speed up a little.

John Benediktsson 2012-08-03 08:26:58 -07:00
parent 722ef4d693
commit c654a823b6
3 changed files with 39 additions and 44 deletions

View File

@ -8,11 +8,11 @@ IN: wrap.strings
: split-lines ( string -- elements-lines ) : split-lines ( string -- elements-lines )
string-lines [ string-lines [
" \t" split harvest " \t" split harvest
[ dup length 1 <element> ] map [ dup length 1 <element> ] map!
] map ; ] map! ;
: join-elements ( wrapped-lines -- lines ) : join-elements ( wrapped-lines -- lines )
[ " " join ] map ; [ " " join ] map! ;
: join-lines ( strings -- string ) : join-lines ( strings -- string )
"\n" join ; "\n" join ;
@ -20,10 +20,10 @@ IN: wrap.strings
PRIVATE> PRIVATE>
: wrap-lines ( lines width -- newlines ) : wrap-lines ( lines width -- newlines )
[ split-lines ] dip '[ _ dup wrap join-elements ] map concat ; [ split-lines ] dip '[ _ dup wrap join-elements ] map! concat ;
: wrap-string ( string width -- newstring ) : wrap-string ( string width -- newstring )
wrap-lines join-lines ; wrap-lines join-lines ;
: wrap-indented-string ( string width indent -- newstring ) : wrap-indented-string ( string width indent -- newstring )
[ length - wrap-lines ] keep '[ _ prepend ] map join-lines ; [ length - wrap-lines ] keep '[ _ prepend ] map! join-lines ;

View File

@ -1,6 +1,7 @@
! Copyright (C) 2009 Daniel Ehrenberg ! Copyright (C) 2009 Daniel Ehrenberg
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: sequences kernel splitting.monotonic accessors grouping wrap ; USING: accessors grouping kernel math sequences
sequences.private splitting.monotonic wrap ;
IN: wrap.words IN: wrap.words
TUPLE: word key width break? ; TUPLE: word key width break? ;
@ -13,9 +14,11 @@ C: <word> word
: make-element ( whites blacks -- element ) : make-element ( whites blacks -- element )
[ append ] [ [ words-length ] bi@ ] 2bi <element> ; [ append ] [ [ words-length ] bi@ ] 2bi <element> ;
: ?first2 ( seq -- first/f second/f ) : ?first2 ( seq -- first/f second/f )
[ ?first ] [ ?second ] bi ; dup length dup 1 > [ first2-unsafe ] [
0 > [ first-unsafe f ] [ drop f f ] if
] if ;
: split-words ( seq -- half-elements ) : split-words ( seq -- half-elements )
[ [ break?>> ] same? ] monotonic-split ; [ [ break?>> ] same? ] monotonic-split ;
@ -26,7 +29,7 @@ C: <word> word
[ f ] if ; [ f ] if ;
: make-elements ( seq f/element -- elements ) : make-elements ( seq f/element -- elements )
[ 2 <groups> [ ?first2 make-element ] map ] dip [ 2 group [ ?first2 make-element ] map! ] dip
[ prefix ] when* ; [ prefix ] when* ;
: words>elements ( seq -- newseq ) : words>elements ( seq -- newseq )
@ -35,5 +38,5 @@ C: <word> word
PRIVATE> PRIVATE>
: wrap-words ( words line-max line-ideal -- lines ) : wrap-words ( words line-max line-ideal -- lines )
[ words>elements ] 2dip wrap [ concat ] map ; [ words>elements ] 2dip wrap [ concat ] map! ;

View File

@ -1,6 +1,6 @@
! Copyright (C) 2009 Daniel Ehrenberg ! Copyright (C) 2009 Daniel Ehrenberg
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: kernel sequences math arrays locals fry accessors USING: combinators kernel sequences math arrays locals fry accessors
lists splitting make combinators.short-circuit namespaces lists splitting make combinators.short-circuit namespaces
grouping splitting.monotonic ; grouping splitting.monotonic ;
IN: wrap IN: wrap
@ -12,18 +12,12 @@ C: <element> element
: element-length ( element -- n ) : element-length ( element -- n )
[ black>> ] [ white>> ] bi + ; [ black>> ] [ white>> ] bi + ;
TUPLE: paragraph lines head-width tail-cost ; TUPLE: paragraph line-max line-ideal lines head-width tail-cost ;
C: <paragraph> paragraph C: <paragraph> paragraph
SYMBOL: line-max
SYMBOL: line-ideal
: deviation ( length -- n )
line-ideal get - sq ;
: top-fits? ( paragraph -- ? ) : top-fits? ( paragraph -- ? )
[ head-width>> ] [ head-width>> ]
[ lines>> 1list? line-ideal line-max ? get ] bi <= ; [ dup lines>> 1list? [ line-ideal>> ] [ line-max>> ] if ] bi <= ;
: fits? ( paragraph -- ? ) : fits? ( paragraph -- ? )
! Make this not count spaces at end ! Make this not count spaces at end
@ -37,7 +31,7 @@ SYMBOL: line-ideal
: paragraph-cost ( paragraph -- cost ) : paragraph-cost ( paragraph -- cost )
dup lines>> 1list? [ drop 0 ] [ dup lines>> 1list? [ drop 0 ] [
[ head-width>> deviation ] [ [ head-width>> ] [ line-ideal>> ] bi - sq ]
[ tail-cost>> ] bi + [ tail-cost>> ] bi +
] if ; ] if ;
@ -45,16 +39,20 @@ SYMBOL: line-ideal
[ paragraph-cost ] min-by ; [ paragraph-cost ] min-by ;
: new-line ( paragraph element -- paragraph ) : new-line ( paragraph element -- paragraph )
[ [ lines>> ] [ 1list ] bi* swons ] {
[ nip black>> ] [ drop [ line-max>> ] [ line-ideal>> ] bi ]
[ drop paragraph-cost ] 2tri [ [ lines>> ] [ 1list ] bi* swons ]
<paragraph> ; [ nip black>> ]
[ drop paragraph-cost ]
} 2cleave <paragraph> ;
: glue ( paragraph element -- paragraph ) : glue ( paragraph element -- paragraph )
[ [ lines>> unswons ] dip swons swons ] {
[ [ head-width>> ] [ element-length ] bi* + ] [ drop [ line-max>> ] [ line-ideal>> ] bi ]
[ drop tail-cost>> ] 2tri [ [ lines>> unswons ] dip swons swons ]
<paragraph> ; [ [ head-width>> ] [ element-length ] bi* + ]
[ drop tail-cost>> ]
} 2cleave <paragraph> ;
: wrap-step ( paragraphs element -- paragraphs ) : wrap-step ( paragraphs element -- paragraphs )
[ '[ _ glue ] map ] [ '[ _ glue ] map ]
@ -62,25 +60,19 @@ SYMBOL: line-ideal
2bi prefix 2bi prefix
[ fits? ] filter ; [ fits? ] filter ;
: 1paragraph ( element -- paragraph ) : 1paragraph ( line-max line-ideal element -- paragraph )
[ 1list 1list ] [ 1list 1list ] [ black>> ] bi 0 <paragraph> ;
[ black>> ] bi
0 <paragraph> ;
: post-process ( paragraph -- array ) : post-process ( paragraph -- array )
lines>> [ [ contents>> ] lmap>array ] lmap>array ; lines>> [ [ contents>> ] lmap>array ] lmap>array ;
: initialize ( elements -- elements paragraph ) : initialize ( line-max line-ideal elements -- elements paragraph )
<reversed> unclip-slice 1paragraph 1array ; <reversed> unclip-slice [ -rot ] dip 1paragraph 1array ;
: wrap ( elements line-max line-ideal -- paragraph ) : wrap ( elements line-max line-ideal -- paragraph )
[ rot [ 2drop { } ] [
line-ideal set initialize
line-max set [ wrap-step ] reduce
[ { } ] [ min-cost
initialize post-process
[ wrap-step ] reduce ] if-empty ;
min-cost
post-process
] if-empty
] with-scope ;