~rbn/fit

ref: 257085b3f4eff0bdc980dac54db6d92a71c24621 fit/pack.go -rw-r--r-- 717 bytes
257085b3Ruben Schuller go.mod whoops 7 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
}