1 ! Copyright (C) 2009 Daniel Ehrenberg
2 ! Copyright (C) 2017 John Benediktsson
3 ! See http://factorcode.org/license.txt for BSD license.
4 USING: accessors arrays kernel locals math sequences
8 TUPLE: element contents black white ;
12 :: wrap ( elements width -- array )
13 elements length integer>fixnum-strict :> #elements
14 elements [ black>> ] { } map-as :> black
15 elements [ white>> ] { } map-as :> white
17 #elements 1 + f <array> :> minima
18 #elements 1 + 0 <array> :> breaks
20 0 0 minima set-nth-unsafe
23 0 i 1 + [ dup #elements <= ] [| j |
24 j 1 - black nth-unsafe + dup :> w
25 j 1 - white nth-unsafe +
29 0 j minima set-nth-unsafe
30 i j breaks set-nth-unsafe
34 j #elements = [ width w - sq + ] unless :> cost
35 j minima nth-unsafe [ cost >= ] [ t ] if* [
36 cost j minima set-nth-unsafe
37 i j breaks set-nth-unsafe
43 #elements [ dup 0 > ] [
44 [ breaks nth dup ] keep elements <slice>
46 ] produce nip reverse ;