~rbn/fit

ref: 257085b3f4eff0bdc980dac54db6d92a71c24621 fit/pack_test.go -rw-r--r-- 659 bytes
257085b3Ruben Schuller go.mod whoops 7 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
package fit

import (
	"fmt"
	"sort"
)

func ExamplePack() {
	items := []Item{
		&ExampleItem{data: "first item", size: 4},
		&ExampleItem{data: "second item", size: 2},
		&ExampleItem{data: "third item", size: 6},
		&ExampleItem{data: "fourth item", size: 5},
	}

	// sort biggest items first
	sort.Sort(sort.Reverse(ItemsBySize(items)))

	// pack into bins selecting the best fitting bin
	bins, err := Pack(Best, 10, items)
	if err != nil {
		fmt.Println(err)
		return
	}

	for _, bin := range bins {
		for _, item := range bin.Items() {
			fmt.Println(item.(*ExampleItem).data)
		}
	}
	//Output:
	//third item
	//first item
	//fourth item
	//second item
}