-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_rolling.go
More file actions
59 lines (51 loc) · 1.66 KB
/
string_rolling.go
File metadata and controls
59 lines (51 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Package rolling implements rolling sum, mean (avg), count and nunique calculations over a
// given number of float64 values.
package rolling
import (
"strings"
)
// RollingObject - the struct that holds the 'settings' and current values to be used in any
// calculations.
type RollingStringObject struct {
window int
values []string
ignoreZeroValues bool
}
// SetIgnoreInfValues - controls if we want to ignore zero values when producing the outputs of
// any calculations
func (ro *RollingStringObject) SetIgnoreZeroValues(ignoreZeroValues bool) {
ro.ignoreZeroValues = ignoreZeroValues
}
// Add - if given value meets the given conditions, append to the values used in the calculation,
// adjusting this so it it relevant for the supplied window
func (ro *RollingStringObject) Add(value string) {
if ro.ignoreZeroValues && (value == "") {
return
}
if len(ro.values) >= ro.window {
ro.values = ro.values[1:len(ro.values)]
}
ro.values = append(ro.values, value)
}
func (ro *RollingStringObject) Values() []string {
return ro.values
}
func (ro *RollingStringObject) Join(sep string, latestFirst bool) string {
tnRtn := make([]string, len(ro.values))
copy(tnRtn, ro.values)
if latestFirst {
last := len(tnRtn) - 1
for i := 0; i < len(tnRtn)/2; i++ {
tnRtn[i], tnRtn[last-i] = tnRtn[last-i], tnRtn[i]
}
}
return strings.Join(tnRtn, sep)
}
// NewRollingObject - set up a new rolling object with a supplied window with the default settings
func NewRollingStringObject(window int) *RollingStringObject {
return &RollingStringObject{
window: window,
values: []string{},
ignoreZeroValues: ignoreZeroValuesDefault,
}
}