Paste: binpack

Author: mrjbq7
Mode: factor
Date: Thu, 2 Oct 2008 03:12:40
Plain Text |
! Copyright (C) 2008 John Benediktsson
! See http://factorcode.org/license.txt for BSD license

USING: sequences kernel arrays vectors accessors assocs sorting math math.functions ;

IN: math.binpack 

: (binpack) ( bins item -- )
    swap dup [ [ second ] map sum ] map swap zip sort-keys values first push ;

: binpack ( assoc n -- bins )
    [ sort-values reverse [ length ] keep swap ] dip 
    [ / ceiling ] keep swap <array> [ <vector> ] map 
    swap [ dupd (binpack) ] each ;

: binpack* ( items n -- bins )
    [ dup zip ] dip binpack [ keys ] map ;

: binpack! ( items quot n -- bins ) 
    [ dup ] 2dip [ map zip ] dip binpack [ keys ] map ;

Annotation: cleaned up some

Author: erg
Mode: factor
Date: Thu, 2 Oct 2008 03:41:16
Plain Text |
: (binpack) ( item bins -- )
    [ [ values sum ] map ] keep
    zip sort-keys values first push ;

: binpack ( assoc n -- bins )
    [ sort-values <reversed> dup length ] dip
    tuck / ceiling <array> [ <vector> ] map
    tuck [ (binpack) ] curry each ;

: binpack* ( items n -- bins )
    [ dup zip ] dip binpack [ keys ] map ;

: binpack! ( items quot n -- bins )
    [ dupd map zip ] dip binpack [ keys ] map ;

New Annotation

Summary:
Author:
Mode:
Body: