~rbn/fit

47b2e2bf0c559a771b9924f267ce7a734bb26fb3 — Ruben Schuller 1 year, 3 months ago be89c47
add blockSize parameter and some help text
2 files changed, 47 insertions(+), 4 deletions(-)

M cmd/fit/input.go
M cmd/fit/main.go
M cmd/fit/input.go => cmd/fit/input.go +2 -2
@@ 10,7 10,7 @@ import (
	"sr.ht/~rbn/fit"
)

func parseDu(r io.Reader) ([]fit.Item, error) {
func parseDu(r io.Reader, blockSize int64) ([]fit.Item, error) {
	items := []fit.Item{}

	s := bufio.NewScanner(r)


@@ 25,7 25,7 @@ func parseDu(r io.Reader) ([]fit.Item, error) {
			return nil, fmt.Errorf("invalid size in input: %v", s.Text())
		}

		items = append(items, fit.Item{Size: size, Data: []byte(fs[1])})
		items = append(items, fit.Item{Size: size * blockSize, Data: []byte(fs[1])})
	}

	if err := s.Err(); err != nil {

M cmd/fit/main.go => cmd/fit/main.go +45 -2
@@ 11,6 11,13 @@ import (
	"sr.ht/~rbn/fit"
)

const (
	BRRSL = 25025314816
	DVDRSL = 4707319808
	CDR74 = 681984000
	CDR80 = 737280000
)

func printBins(bins []fit.Bin, w io.Writer) error {
	for _, bin := range bins {
		paths := []string{}


@@ 26,10 33,46 @@ func printBins(bins []fit.Bin, w io.Writer) error {
}

func main() {
	capacity := flag.Int64("capacity", 25*1024*1024*1024, "bin capacity in bytes")
	capacity := flag.Int64("capacity", BRRSL, "bin capacity in bytes")
	blockSize := flag.Int64("blockSize", 1, "block size to multiply the input columns with")
	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.Usage = func() {
		fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0])
		flag.PrintDefaults()
		fmt.Fprintf(flag.CommandLine.Output(), `
fit uses (first|best|worst) fit decreasing heuristic algorithms [1] to put 
items into containers of a given capacity. the input is read from stdin and
consists of lines of size and data columns seperated by a tabulator character,
like the output of du.

common capacity values:
Blu-ray R single layer	%v
DVD-R single layer	%v
CD-ROM R 74m		%v
CD-ROM R 80m		%v

note that there will be overhead from file systems and aligning to block size.
rounding up to the nearst block size can be done using the -B parameter to
(GNU) du. when using this flag for du, use the -blockSize parameter with the
same block size to re-scale the input sizes:

du -B 2048 . | fit -blockSize 2048 -best

when reading du input which is in nested directories, you must use * to let
the shell expand the single directories

du /path/to/parent/*/* | fit -best -capacity 681984000

otherwise the parent directories will also be printed in the output, and
considered by fit to be an item with the combined size of it and its
subdirectories.

[1] https://en.wikipedia.org/wiki/Bin_packing_problem
`, BRRSL, DVDRSL, CDR74, CDR80)
	}

	flag.Parse()

	if *strategyBest && *strategyFirst && *strategyWorst {


@@ 50,7 93,7 @@ func main() {
		strategy = fit.Worst
	}

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