Skip to content

Commit

Permalink
Un-exported utility function.
Browse files Browse the repository at this point in the history
  • Loading branch information
bsipos committed Aug 29, 2019
1 parent 07ccd4c commit 984ba80
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 25 deletions.
8 changes: 4 additions & 4 deletions bar.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ package thist
// BarSimple plots a bar plot on the terminal. Does not use unicode charcters.
func BarSimple(x, y []float64, xlab, ylab []string, title string, info []string) string {
if len(xlab) == 0 {
xlab = AutoLabel(x, Mean(AbsFloats(x)))
xlab = AutoLabel(x, mean(absFloats(x)))
}
if len(ylab) == 0 {
ylab = AutoLabel(y, Mean(AbsFloats(y)))
ylab = AutoLabel(y, mean(absFloats(y)))
}
return Plot(x, y, xlab, ylab, title, info, "#", "@", " ", "_", "|", "-", "|")
}

// Bar plots a bar plot on the terminal. It makes use of unicode characters.
func Bar(x, y []float64, xlab, ylab []string, title string, info []string) string {
if len(xlab) == 0 {
xlab = AutoLabel(x, Mean(AbsFloats(x)))
xlab = AutoLabel(x, mean(absFloats(x)))
}
if len(ylab) == 0 {
ylab = AutoLabel(y, Mean(AbsFloats(y)))
ylab = AutoLabel(y, mean(absFloats(y)))
}
return Plot(x, y, xlab, ylab, title, info, "\u2588", "\u2591", " ", "_", "\u2502", "\u2500", "\u2524")
}
4 changes: 2 additions & 2 deletions hist.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func NewHist(data []float64, title, binMode string, maxBins int, normalize bool)
h.updatePrecision()
h.Counts = make([]float64, len(h.BinStart))
for _, v := range data {
c := RoundFloat64(v, h.Precision)
c := roundFloat64(v, h.Precision)
h.DataMap[c]++
i := sort.SearchFloat64s(h.BinStart, c) - 1
if i < 0 {
Expand Down Expand Up @@ -193,7 +193,7 @@ func (h *Hist) Update(p float64) {
h.updateInfo()
}

h.DataMap[RoundFloat64(p, h.Precision)]++
h.DataMap[roundFloat64(p, h.Precision)]++

if !math.IsNaN(oldMin) && p >= oldMin && !math.IsNaN(oldMax) && p <= oldMax {
var i int
Expand Down
16 changes: 8 additions & 8 deletions plot.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ func Plot(x, y []float64, xlab, ylab []string, title string, info []string, symb
height = 24
}

xll := StringsMaxLen(xlab)
yll := StringsMaxLen(ylab)
xll := stringsMaxLen(xlab)
yll := stringsMaxLen(ylab)
width -= yll + 1

res := strings.Repeat(space, yll+1) + CenterPad2Len(title, space, int(width)) + "\n"
res := strings.Repeat(space, yll+1) + centerPad2Len(title, space, int(width)) + "\n"
height -= 4
height -= len(info)

Expand Down Expand Up @@ -86,9 +86,9 @@ func Plot(x, y []float64, xlab, ylab []string, title string, info []string, symb
}
}
for _, c := range ny {
if l == Abs(c) {
if l == abs(c) {
res += topBar
} else if l < Abs(c) {
} else if l < abs(c) {
if c < 0 {
res += nblock
} else {
Expand All @@ -106,7 +106,7 @@ func Plot(x, y []float64, xlab, ylab []string, title string, info []string, symb
if xll < xf-2 {
res += strings.Repeat(space, yll) + vbar
for _, xl := range xlab {
res += vbar + RightPad2Len(xl, space, xf-1)
res += vbar + rightPad2Len(xl, space, xf-1)
}
} else {
for i := 0; i < xll; i++ {
Expand All @@ -129,14 +129,14 @@ func Plot(x, y []float64, xlab, ylab []string, title string, info []string, symb
}
}
for _, il := range info {
res += strings.Repeat(space, yll) + vbar + CenterPad2Len(il, space, int(width)) + "\n"
res += strings.Repeat(space, yll) + vbar + centerPad2Len(il, space, int(width)) + "\n"
}
return res
}

// normalizeY normalizes y values to a maximum height.
func normalizeY(y []float64, height int) []int {
max := Max(y)
max := max(y)
res := make([]int, len(y))

for i, x := range y {
Expand Down
22 changes: 11 additions & 11 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
)

// Max calculates the maximum of a float64 slice.
func Max(s []float64) float64 {
func max(s []float64) float64 {
if len(s) == 0 {
return math.NaN()
}
Expand All @@ -43,7 +43,7 @@ func Max(s []float64) float64 {
}

// Min calculates the minimum of a float64 slice.
func Min(s []float64) float64 {
func min(s []float64) float64 {
if len(s) == 0 {
return math.NaN()
}
Expand All @@ -58,7 +58,7 @@ func Min(s []float64) float64 {
}

// Mean calculates the mean of a float64 slice.
func Mean(s []float64) float64 {
func mean(s []float64) float64 {
if len(s) == 0 {
return math.NaN()
}
Expand All @@ -71,7 +71,7 @@ func Mean(s []float64) float64 {
}

// AbsFloats calculates the absolute value of a float64 slice.
func AbsFloats(s []float64) []float64 {
func absFloats(s []float64) []float64 {
res := make([]float64, len(s))
for i, x := range s {
res[i] = math.Abs(x)
Expand All @@ -80,7 +80,7 @@ func AbsFloats(s []float64) []float64 {
}

// Abs calculates the absolute value of an integer.
func Abs(n int) int {
func abs(n int) int {
if n < 0 {
return -n
}
Expand All @@ -98,7 +98,7 @@ func ClearScreenString() string {
}

// StringsMaxLen returns the length of a longest string in a slice.
func StringsMaxLen(s []string) int {
func stringsMaxLen(s []string) int {
if len(s) == 0 {
return 0
}
Expand All @@ -118,7 +118,7 @@ func AutoLabel(s []float64, m float64) []string {
res := make([]string, len(s))
nf := false
var digits int
if Min(s) < 0 {
if min(s) < 0 {
nf = true
}
if math.Abs(m) == 0 {
Expand Down Expand Up @@ -161,7 +161,7 @@ func AutoLabel(s []float64, m float64) []string {
}

// RoundFloat64 rounds a float value to the given precision.
func RoundFloat64(f float64, n float64) float64 {
func roundFloat64(f float64, n float64) float64 {
if n == 0.0 {
return math.Round(f)
}
Expand All @@ -171,23 +171,23 @@ func RoundFloat64(f float64, n float64) float64 {

// LeftPad2Len left pads a string to a given length.
// https://github.com/DaddyOh/golang-samples/blob/master/pad.go
func LeftPad2Len(s string, padStr string, overallLen int) string {
func leftPad2Len(s string, padStr string, overallLen int) string {
var padCountInt = 1 + ((overallLen - len(padStr)) / len(padStr))
var retStr = strings.Repeat(padStr, padCountInt) + s
return retStr[(len(retStr) - overallLen):]
}

// RightPad2Len right pads a string to a given length.
// https://github.com/DaddyOh/golang-samples/blob/master/pad.go
func RightPad2Len(s string, padStr string, overallLen int) string {
func rightPad2Len(s string, padStr string, overallLen int) string {
var padCountInt = 1 + ((overallLen - len(padStr)) / len(padStr))
var retStr = s + strings.Repeat(padStr, padCountInt)
return retStr[:overallLen]
}

// CenterPad2Len center pads a string to a given length.
// https://www.socketloop.com/tutorials/golang-aligning-strings-to-right-left-and-center-with-fill-example
func CenterPad2Len(s string, fill string, n int) string {
func centerPad2Len(s string, fill string, n int) string {
if len(s) >= n {
return s
}
Expand Down

0 comments on commit 984ba80

Please sign in to comment.