~rbn/fit

ref: 277c36d91196048f9a8f73a0a74b014e1e25907d fit/cmd/fit/main.go -rw-r--r-- 1.2 KiB View raw
277c36d9Ruben Schuller moved fit.go to pack.go 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main

import (
	"flag"
	"fmt"
	"io"
	"log"
	"os"
	"strings"

	"sr.ht/~rbn/fit"
)

func printBins(bins []fit.Bin, w io.Writer) error {
	for _, bin := range bins {
		paths := []string{}
		for _, item := range bin.Items() {
			paths = append(paths, item.Path)
		}
		_, err := fmt.Fprintln(w, strings.Join(paths, "\t"))
		if err != nil {
			return err
		}
	}
	return nil
}

func main() {
	capacity := flag.Int64("capacity", 25*1024*1024*1024, "bin capacity in bytes")
	strategyBest := flag.Bool("best", false, "best fit decreasing")
	strategyFirst := flag.Bool("first", false, "first fit decreasing")
	strategyWorst := flag.Bool("worst", false, "worst fit decreasing")
	flag.Parse()

	if *strategyBest && *strategyFirst && *strategyWorst {
		log.Fatal("multiple strategies selected")
	}

	if !(*strategyBest || *strategyFirst || *strategyWorst) {
		log.Fatal("no strategy selected")
	}

	var strategy fit.StrategyFunc
	switch {
	case *strategyBest:
		strategy = fit.Best
	case *strategyFirst:
		strategy = fit.First
	case *strategyWorst:
		strategy = fit.Worst
	}

	items, err := parseDu(os.Stdin)
	if err != nil {
		log.Fatal(err)
	}

	bins, err := fit.Pack(strategy, *capacity, items)
	if err != nil {
		log.Fatal(err)
	}

	printBins(bins, os.Stdout)
}