util.go 922 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package stats
  2. import (
  3. "sort"
  4. "time"
  5. )
  6. // float64ToInt rounds a float64 to an int
  7. func float64ToInt(input float64) (output int) {
  8. r, _ := Round(input, 0)
  9. return int(r)
  10. }
  11. // unixnano returns nanoseconds from UTC epoch
  12. func unixnano() int64 {
  13. return time.Now().UTC().UnixNano()
  14. }
  15. // copyslice copies a slice of float64s
  16. func copyslice(input Float64Data) Float64Data {
  17. s := make(Float64Data, input.Len())
  18. copy(s, input)
  19. return s
  20. }
  21. // sortedCopy returns a sorted copy of float64s
  22. func sortedCopy(input Float64Data) (copy Float64Data) {
  23. copy = copyslice(input)
  24. sort.Float64s(copy)
  25. return
  26. }
  27. // sortedCopyDif returns a sorted copy of float64s
  28. // only if the original data isn't sorted.
  29. // Only use this if returned slice won't be manipulated!
  30. func sortedCopyDif(input Float64Data) (copy Float64Data) {
  31. if sort.Float64sAreSorted(input) {
  32. return input
  33. }
  34. copy = copyslice(input)
  35. sort.Float64s(copy)
  36. return
  37. }