-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistribution.go
45 lines (36 loc) · 867 Bytes
/
distribution.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
package main
// Distribution is used to make sure the values are distributed randomly
type Distribution[T comparable] struct {
store map[T]int
}
func NewDistribution[T comparable]() *Distribution[T] {
return &Distribution[T]{
store: make(map[T]int),
}
}
func NewDistributionWithKnownKeys[T comparable](knownKeys []T) *Distribution[T] {
distribution := NewDistribution[T]()
for _, key := range knownKeys {
distribution.Add(key)
}
return distribution
}
func (d *Distribution[T]) Add(value T) {
d.store[value] = d.store[value] + 1
}
func (d *Distribution[T]) IsTooFrequent(value T) bool {
if len(d.store) == 0 {
return false
}
valueCount, found := d.store[value]
valueCount++
if found && len(d.store) == 1 && valueCount > 1 {
return true
}
for _, count := range d.store {
if valueCount-count > 1 {
return true
}
}
return false
}