93 lines
1.5 KiB
Go
93 lines
1.5 KiB
Go
package tree
|
|
|
|
import (
|
|
"fmt"
|
|
"sync"
|
|
)
|
|
|
|
type ByteSize uint
|
|
|
|
const (
|
|
B ByteSize = 1 << (10 * iota)
|
|
KB
|
|
MB
|
|
GB
|
|
TB
|
|
PB
|
|
)
|
|
|
|
func isLastLevel(node Node, maxLevel int) bool {
|
|
return maxLevel-1 == node.Level()
|
|
}
|
|
|
|
func setUnit(unit string) ByteSize {
|
|
switch unit {
|
|
case "B":
|
|
return B
|
|
case "KB":
|
|
return KB
|
|
case "MB":
|
|
return MB
|
|
case "GB":
|
|
return GB
|
|
case "TB":
|
|
return TB
|
|
case "PB":
|
|
// I reckon this will never happen, but you never know
|
|
return PB
|
|
default:
|
|
panic(ErrUnknownUnit)
|
|
}
|
|
}
|
|
|
|
func fmtUnit(unit ByteSize) string {
|
|
switch unit {
|
|
case B:
|
|
return "B"
|
|
case KB:
|
|
return "KB"
|
|
case MB:
|
|
return "MB"
|
|
case GB:
|
|
return "GB"
|
|
case TB:
|
|
return "TB"
|
|
case PB:
|
|
// I reckon this will never happen, but you never know
|
|
return "PB"
|
|
default:
|
|
panic(ErrUnknownUnit)
|
|
}
|
|
}
|
|
|
|
func fmtSize(size int64, unit ByteSize) string {
|
|
var dimension float64
|
|
dimension = float64(size) / float64(unit)
|
|
return fmt.Sprintf("%.2f %s", dimension, fmtUnit(unit))
|
|
}
|
|
|
|
func merge(cs []<-chan int64) <-chan int64 {
|
|
var wg sync.WaitGroup
|
|
out := make(chan int64)
|
|
|
|
// Start an output goroutine for each input channel in cs. output
|
|
// copies values from c to out until c is closed, then calls wg.Done.
|
|
output := func(c <-chan int64) {
|
|
for n := range c {
|
|
out <- n
|
|
}
|
|
wg.Done()
|
|
}
|
|
wg.Add(len(cs))
|
|
for _, c := range cs {
|
|
go output(c)
|
|
}
|
|
|
|
// Start a goroutine to close out once all the output goroutines are
|
|
// done. This must start after the wg.Add call.
|
|
go func() {
|
|
wg.Wait()
|
|
close(out)
|
|
}()
|
|
return out
|
|
}
|