1 ! Copyright (C) 2005, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: sequences ui.gadgets kernel math math.functions
4 math.vectors namespaces math.order accessors math.geometry.rect ;
10 { gap initial: { 0 0 } } ;
12 : packed-dim-2 ( gadget sizes -- list )
13 [ over rect-dim over v- rot fill>> v*n v+ ] with map ;
15 : packed-dims ( gadget sizes -- seq )
16 2dup packed-dim-2 swap orient ;
18 : gap-locs ( gap sizes -- seq )
19 { 0 0 } [ v+ over v+ ] accumulate 2nip ;
21 : aligned-locs ( gadget sizes -- seq )
22 [ >r dup align>> swap rect-dim r> v- n*v ] with map ;
24 : packed-locs ( gadget sizes -- seq )
25 over gap>> over gap-locs >r dupd aligned-locs r> orient ;
27 : round-dims ( seq -- newseq )
29 [ swap v- dup [ ceiling >fixnum ] map [ swap v- ] keep ] map
32 : pack-layout ( pack sizes -- )
33 round-dims over children>>
34 >r dupd packed-dims r> 2dup [ (>>dim) ] 2each
35 >r packed-locs r> [ (>>loc) ] 2each ;
37 : <pack> ( orientation -- pack )
41 : <pile> ( -- pack ) { 0 1 } <pack> ;
43 : <filled-pile> ( -- pack ) <pile> 1 >>fill ;
45 : <shelf> ( -- pack ) { 1 0 } <pack> ;
47 : gap-dims ( gap sizes -- seeq )
48 [ dim-sum ] keep length 1 [-] rot n*v v+ ;
50 : pack-pref-dim ( gadget sizes -- dim )
51 over gap>> over gap-dims >r max-dim r>
52 rot orientation>> set-axis ;
55 dup children>> pref-dims pack-pref-dim ;
58 dup children>> pref-dims pack-layout ;
60 M: pack children-on ( rect gadget -- seq )
61 dup orientation>> swap children>>
62 [ fast-children-on ] keep <slice> ;