package fit
import (
"fmt"
)
//Pack items into bins.
//
//Each item from items is placed into a bin, according to the bin selection strategy.
//After every item is processed, the resulting bins are returned. If an error occures
//during the process, nil bins and the error are returned.
func Pack(binSelect BinSelectFunc, capacity int64, items []Item) ([]Bin, error) {
bins := []Bin{}
for len(items) > 0 {
var x Item
x, items = items[0], items[1:]
if x.Size() > capacity {
return nil, fmt.Errorf("item size exceeds bin capacity: size: %v capacity: %v", x.Size(), capacity)
}
var err error
bins, err = binSelect(bins, capacity, x)
if err != nil {
return nil, err
}
}
return bins, nil
}