-
Notifications
You must be signed in to change notification settings - Fork 18
/
util.go
44 lines (35 loc) · 868 Bytes
/
util.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
package posthog
type SizeLimitedMap struct {
ids map[string][]string
size int
}
func newSizeLimitedMap(size int) *SizeLimitedMap {
newMap := SizeLimitedMap{
ids: map[string][]string{},
size: size,
}
return &newMap
}
func (sizeLimitedMap *SizeLimitedMap) add(key string, element string) {
if len(sizeLimitedMap.ids) >= sizeLimitedMap.size {
sizeLimitedMap.ids = map[string][]string{}
}
if val, ok := sizeLimitedMap.ids[key]; ok {
sizeLimitedMap.ids[key] = append(val, element)
} else {
sizeLimitedMap.ids[key] = []string{element}
}
}
func (sizeLimitedMap *SizeLimitedMap) contains(key string, element string) bool {
if val, ok := sizeLimitedMap.ids[key]; ok {
for _, v := range val {
if v == element {
return true
}
}
}
return false
}
func (sizeLimitedMap *SizeLimitedMap) count() int {
return len(sizeLimitedMap.ids)
}