~rbn/fit

ref: eab46d6d4412cad3bad4186d930993169b53d469 fit/strategy.go -rw-r--r-- 774 bytes View raw
eab46d6d — Ruben Schuller initial commit 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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package fit

import (
	"sort"
)

func First(bins []Bin, capacity int64, x Item) []Bin {
	for i, b := range bins {
		if !b.fits(x) {
			continue
		}
		bins[i].add(x)
		return bins
	}
	bins = append(bins, newBin(capacity, x))
	return bins
}

func Best(bins []Bin, capacity int64, x Item) []Bin {
	sort.Sort(sort.Reverse(BinsBySize(bins)))
	for i, b := range bins {
		if !b.fits(x) {
			continue
		}
		bins[i].add(x)
		return bins
	}
	bins = append(bins, newBin(capacity, x))
	return bins
}

func Worst(bins []Bin, capacity int64, x Item) []Bin {
	sort.Sort(BinsBySize(bins))
	for i, b := range bins {
		if !b.fits(x) {
			continue
		}
		bins[i].add(x)
		return bins
	}
	bins = append(bins, newBin(capacity, x))
	return bins
}

type StrategyFunc func([]Bin, int64, Item) []Bin