~rbn/fit

ref: eab46d6d4412cad3bad4186d930993169b53d469 fit/bin.go -rw-r--r-- 616 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
package fit

type Bin struct {
	items    []Item
	size     int64
	capacity int64
}

func (b *Bin) add(i Item) {
	b.items = append(b.items, i)
	b.size += i.Size
}

func (b *Bin) fits(i Item) bool {
	return b.size+i.Size <= b.capacity
}

func newBin(capacity int64, i Item) Bin {
	return Bin{items: []Item{i}, size: i.Size, capacity: capacity}
}

func (b *Bin) Items() []Item {
	return b.items
}

type BinsBySize []Bin

func (x BinsBySize) Len() int           { return len(x) }
func (x BinsBySize) Less(i, j int) bool { return x[i].size < x[j].size }
func (x BinsBySize) Swap(i, j int)      { x[i], x[j] = x[j], x[i] }