-
Notifications
You must be signed in to change notification settings - Fork 3
/
counter.go
69 lines (57 loc) · 1.91 KB
/
counter.go
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
60
61
62
63
64
65
66
67
68
69
package simpleflow
// Counter is an entity that keeps track of the number items it encounters
type Counter[T comparable] struct {
counts map[T]int
}
// NewCounter returns a new Counter which can be used to deduplicate slices values
func NewCounter[T comparable]() *Counter[T] {
return &Counter[T]{counts: make(map[T]int)}
}
// Add adds a item to the Counter and returns the current number of occurrences
func (c *Counter[T]) Add(v T) int {
c.counts[v]++
return c.counts[v]
}
// Count returns the current number of occurrences for the given value
func (c *Counter[T]) Count(v T) int {
return c.counts[v]
}
// Reset clears the values in the Counter{}
func (c *Counter[T]) Reset() {
c.counts = make(map[T]int)
}
// AddMany adds all the values in the provided slice to the counter
func (c *Counter[T]) AddMany(values []T) {
for _, v := range values {
c.Add(v)
}
}
// ObjectCounter is a counter that works on objects by creating an ID for each element. Objects
// with the same ID will be counted in the same bucket.
type ObjectCounter[T any] struct {
c *Counter[string]
toId func(T) string
}
// NewObjectCounter creates a ObjectCounter that uses the provided function in order to create IDs for
// needing to be counted.
func NewObjectCounter[T any](toId func(T) string) *ObjectCounter[T] {
return &ObjectCounter[T]{c: NewCounter[string](), toId: toId}
}
// Add adds an object to the Counter and returns the current number of occurrences
func (c *ObjectCounter[T]) Add(v T) int {
return c.c.Add(c.toId(v))
}
// Count returns the current number of occurrences for the given object
func (c *ObjectCounter[T]) Count(v T) int {
return c.c.Count(c.toId(v))
}
// Reset clears the values in the ObjectCounter{}
func (c *ObjectCounter[T]) Reset() {
c.c.Reset()
}
// AddMany adds all the values in the provided slice to the counter
func (c *ObjectCounter[T]) AddMany(values []T) {
for _, v := range values {
c.c.Add(c.toId(v))
}
}