next up previous
Next: Some simple benchmarks Up: Accompanying material for: A Previous: Dijkstra's shortest path algorithm

One-dimensional bin packing


>  module BinPacking(ItemBin, packFirstFit', packFirstFit)

>  where
>  import Prelude hiding (lookup)
>  import Balanced


>  type Item                     =  Double

>  type Bin                      =  Double

List-based solution.

>  packFirstFit'                 :: [Item] -> [Bin]

>  packFirstFit'                 =  foldl firstFit' []

>  firstFit'                     :: [Bin] -> Item -> [Bin]
>  firstFit' [] i                =  [ i ]
>  firstFit' (b : bs) i
>    | b + i <= 1                =  b + i : bs
>    | otherwise                 =  b     : firstFit' bs i


>  type No                       =  Int


>  packFirstFit                  :: [Item] -> [Bin]
>  packFirstFit is               =  [ prio b | b <- toOrdList q ]
>      where (q, _)              =  foldl firstFit (empty, 0) is

>  firstFit                      :: (PSQ No BinNo) -> Item -> (PSQ No BinNo)
>  firstFit (q, n) i             =  case atMost (1 - i) q of
>                                     []            -> (insert (n :-> i) q, n + 1)
>                                     (k :-> _) : _ -> (adjust (+ i) k q, n)


next up previous
Next: Some simple benchmarks Up: Accompanying material for: A Previous: Dijkstra's shortest path algorithm
Ralf Hinze 2001-03-20