]> gitweb.factorcode.org Git - factor.git/blobdiff - basis/wrap/wrap.factor
basis: use lint.vocabs tool to trim using lists
[factor.git] / basis / wrap / wrap.factor
index 26868676d9d8852651ebc46211651c6179cfb2bb..4fe6247d991938c49d2ca43336ac465e808ab0e1 100644 (file)
@@ -1,73 +1,45 @@
 ! Copyright (C) 2009 Daniel Ehrenberg
+! Copyright (C) 2017 John Benediktsson
 ! See http://factorcode.org/license.txt for BSD license.
-USING: accessors arrays combinators combinators.short-circuit
-fry kernel kernel.private lists locals math sequences typed ;
+USING: accessors arrays kernel math sequences sequences.private ;
 IN: wrap
 
-! black is the text length, white is the whitespace length
 TUPLE: element contents black white ;
-C: <element> element
-
-<PRIVATE
-
-: element-length ( element -- n )
-    [ black>> ] [ white>> ] bi + ; inline
-
-TUPLE: paragraph line-max line-ideal lines head-width tail-cost ;
-C: <paragraph> paragraph
-
-: if-one-line ( paragraph true false -- )
-    [ dup lines>> 1list? ] 2dip if ; inline
-
-TYPED: top-fits? ( paragraph: paragraph -- ? )
-    [ head-width>> ]
-    [ [ line-ideal>> ] [ line-max>> ] if-one-line ] bi <= ; inline
-
-TYPED: fits? ( paragraph: paragraph -- ? )
-    ! Make this not count spaces at end
-    { [ lines>> car 1list? ] [ top-fits? ] } 1|| ; inline
-
-TYPED: paragraph-cost ( paragraph: paragraph -- cost )
-    [ drop 0 ] [
-        [ head-width>> ] [ line-ideal>> - sq ] [ tail-cost>> ] tri +
-    ] if-one-line ; inline
 
-: min-cost ( paragraphs -- paragraph )
-    [ paragraph-cost ] infimum-by ; inline
-
-TYPED: new-line ( paragraph: paragraph element: element -- paragraph )
-    {
-        [ drop [ line-max>> ] [ line-ideal>> ] bi ]
-        [ [ lines>> ] [ 1list ] bi* swons ]
-        [ nip black>> ]
-        [ drop paragraph-cost ]
-    } 2cleave <paragraph> ; inline
-
-TYPED: add-element ( paragraph: paragraph element: element -- )
-    [ '[ _ element-length + ] change-head-width ]
-    [ '[ unswons _ swons swons ] change-lines ] bi drop ; inline
-
-TYPED: wrap-step ( paragraphs: array element: element -- paragraphs )
-    [ [ min-cost ] dip new-line ]
-    [ dupd '[ _ add-element ] each ]
-    2bi swap prefix { array } declare
-    [ fits? ] filter ; inline
-
-: 1paragraph ( line-max line-ideal element -- paragraph )
-    [ 1list 1list ] [ black>> ] bi 0 <paragraph> ;
-
-: post-process ( paragraph -- array )
-    lines>> [ [ contents>> ] lmap>array ] lmap>array ;
-
-: initial-step ( line-max line-ideal elements -- elements paragraph )
-    reverse unclip [ -rot ] dip 1paragraph 1array ;
-
-PRIVATE>
+C: <element> element
 
-: wrap ( elements line-max line-ideal -- array )
-    rot [ 2drop { } ] [
-        initial-step
-        [ wrap-step ] reduce
-        min-cost
-        post-process
-    ] if-empty ;
+:: wrap ( elements width -- array )
+    elements length integer>fixnum-strict :> #elements
+    elements [ black>> ] { } map-as :> black
+    elements [ white>> ] { } map-as :> white
+
+    #elements 1 + f <array> :> minima
+    #elements 1 + 0 <array> :> breaks
+
+    0 0 minima set-nth-unsafe
+
+    minima [| base i |
+        0 i 1 + [ dup #elements <= ] [| j |
+            j 1 - black nth-unsafe + dup :> w
+            j 1 - white nth-unsafe +
+
+            w width > [
+                j 1 - i = [
+                    0 j minima set-nth-unsafe
+                    i j breaks set-nth-unsafe
+                ] when #elements
+            ] [
+                base
+                j #elements = [ width w - sq + ] unless :> cost
+                j minima nth-unsafe [ cost >= ] [ t ] if* [
+                    cost j minima set-nth-unsafe
+                    i j breaks set-nth-unsafe
+                ] when j
+            ] if 1 +
+        ] while 2drop
+    ] each-index
+
+    #elements [ dup 0 > ] [
+        [ breaks nth dup ] keep elements <slice>
+        [ contents>> ] map
+    ] produce nip reverse ;