~rbn/fit

ref: be89c47f35a510556bf10a989ba06db073c166f8 fit/pack.go -rw-r--r-- 457 bytes View raw
be89c47fRuben Schuller rename item field Path to Data and change type to []byte 1 year, 5 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
package fit

import (
	"fmt"
	"sort"
)

func Pack(strategy StrategyFunc, capacity int64, items []Item) ([]Bin, error) {
	sort.Sort(itemsBySize(items))
	bins := []Bin{}

	for len(items) > 0 {
		var x Item
		x, items = items[len(items)-1], items[:len(items)-1]

		if x.Size > capacity {
			return nil, fmt.Errorf("%v exceeds capacity: size: %v capacity: %v", string(x.Data), x.Size, capacity)
		}

		bins = strategy(bins, capacity, x)
	}

	return bins, nil
}