~rbn/fit

ref: 62bd3c10691a1896258c3120c30a8f9ae412e752 fit/fit.go -rw-r--r-- 449 bytes
62bd3c10Ruben Schuller added .build.yml 1 year, 10 months ago
                                                                                
eab46d6d Ruben Schuller
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", x.Path, x.Size, capacity)
		}

		bins = strategy(bins, capacity, x)
	}

	return bins, nil
}