math.binpack: faster by storing only items, not weights.

db4
John Benediktsson 2014-11-29 17:34:27 -08:00
parent c675694619
commit 921e995ed9
2 changed files with 10 additions and 11 deletions

View File

@ -5,18 +5,18 @@ USING: kernel tools.test sequences ;
IN: math.binpack
{ { { } } } [ { } 1 binpack ] unit-test
{ { V{ } } } [ { } 1 binpack ] unit-test
{ { { 3 } { 2 1 } } } [ { 1 2 3 } 2 binpack ] unit-test
{ { V{ 3 } V{ 2 1 } } } [ { 1 2 3 } 2 binpack ] unit-test
{ { { 1000 } { 100 60 30 7 } { 70 60 40 23 3 } } }
{ { V{ 1000 } V{ 100 60 30 7 } V{ 70 60 40 23 3 } } }
[ { 100 23 40 60 1000 30 60 07 70 03 } 3 binpack ] unit-test
{
{
{ "violet" "orange" }
{ "indigo" "green" }
{ "yellow" "blue" "red" }
V{ "violet" "orange" }
V{ "indigo" "green" }
V{ "yellow" "blue" "red" }
}
} [
{ "red" "orange" "yellow" "green" "blue" "indigo" "violet" }

View File

@ -15,15 +15,14 @@ TUPLE: bin items total ;
: smallest-bin ( bins -- bin )
[ total>> ] infimum-by ; inline
: add-to-bin ( item bin -- )
[ items>> push ]
[ [ second ] dip [ + ] change-total drop ] 2bi ;
: add-to-bin ( item weight bin -- )
[ + ] change-total items>> push ;
:: (binpack) ( alist #bins -- bins )
alist sort-values <reversed> :> items
#bins [ <bin> ] replicate :> bins
items [ bins smallest-bin add-to-bin ] each
bins [ items>> keys ] map ;
items [ bins smallest-bin add-to-bin ] assoc-each
bins [ items>> ] map ;
PRIVATE>