-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcollision.go
63 lines (46 loc) · 986 Bytes
/
collision.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
package xload
import (
"sync"
)
type collisionSyncMap sync.Map
func (cm *collisionSyncMap) add(key string) {
m := (*sync.Map)(cm)
v, loaded := m.LoadOrStore(key, 1)
if loaded {
m.Store(key, v.(int)+1)
}
}
func (cm *collisionSyncMap) err() error {
var collidedKeys []string
m := (*sync.Map)(cm)
m.Range(func(key, v any) bool {
if key == "" {
return true
}
if count, _ := v.(int); count > 1 {
collidedKeys = append(collidedKeys, key.(string))
}
return true
})
return keysToErr(collidedKeys)
}
type collisionMap map[string]int
func (cm collisionMap) add(key string) { cm[key]++ }
func (cm collisionMap) err() error {
var collidedKeys []string
for key, count := range cm {
if key == "" {
continue
}
if count > 1 {
collidedKeys = append(collidedKeys, key)
}
}
return keysToErr(collidedKeys)
}
func keysToErr(collidedKeys []string) error {
if len(collidedKeys) == 0 {
return nil
}
return &ErrCollision{keys: collidedKeys}
}