Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package histogram

import (
"fmt"
"math"
"sort"
"sync"
Expand Down Expand Up @@ -36,6 +37,48 @@ func NewFast() *Fast {
return f
}

// GetState dumps fast histogram state to a map
func (f *Fast) GetState() map[string]interface{} {
return map[string]interface{}{
"max": f.max,
"min": f.min,
"count": f.count,
"a": f.a,
"tmp": f.tmp,
}
}

// FromState restores fast histogram state from a map
func FromState(raw map[string]interface{}) (*Fast, error) {
f := &Fast{}
if min, ok := raw["min"].(float64); ok {
f.min = min
} else {
return nil, fmt.Errorf("state map value min is of invalid type")
}
if max, ok := raw["max"].(float64); ok {
f.max = max
} else {
return nil, fmt.Errorf("state map value max is of invalid type")
}
if count, ok := raw["count"].(uint64); ok {
f.count = count
} else {
return nil, fmt.Errorf("state map value count is of invalid type")
}
if a, ok := raw["a"].([]float64); ok {
f.a = a
} else {
return nil, fmt.Errorf("state map value a is of invalid type")
}
if tmp, ok := raw["tmp"].([]float64); ok {
f.tmp = tmp
} else {
return nil, fmt.Errorf("state map value tmp is of invalid type")
}
return f, nil
}

// Reset resets the histogram.
func (f *Fast) Reset() {
f.max = infNeg
Expand Down