~rbn/fit

ref: 277c36d91196048f9a8f73a0a74b014e1e25907d fit/bin.go -rw-r--r-- 616 bytes
277c36d9Ruben Schuller moved fit.go to pack.go 1 year, 10 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] }