-
Notifications
You must be signed in to change notification settings - Fork 3
/
set_util.go
79 lines (67 loc) · 1.08 KB
/
set_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
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
70
71
72
73
74
75
76
77
78
79
package utils
import (
"sync"
)
type Set struct {
mapInst map[interface{}]bool
sync.RWMutex
}
func New() *Set {
return &Set{
mapInst: map[interface{}]bool{},
}
}
func (p *Set) Add(item interface{}) {
p.Lock()
defer p.Unlock()
p.mapInst[item] = true
}
func (p *Set) Remove(item interface{}) {
p.Lock()
defer p.Unlock()
delete(p.mapInst, item)
}
func (p *Set) PopOne() (item interface{}, ok bool) {
p.Lock()
defer p.Unlock()
if len(p.mapInst) > 0 {
for k := range p.mapInst {
item = k
delete(p.mapInst, k)
break
}
ok = true
} else {
ok = false
}
return
}
func (p *Set) Has(item interface{}) bool {
p.RLock()
defer p.RUnlock()
_, ok := p.mapInst[item]
return ok
}
func (p *Set) Len() int {
return int(len(p.mapInst))
}
func (p *Set) Clear() {
p.Lock()
defer p.Unlock()
p.mapInst = map[interface{}]bool{}
}
func (p *Set) IsEmpty() bool {
if p.Len() == 0 {
return true
}
return false
}
func (p *Set) List() []interface{} {
p.RLock()
defer p.RUnlock()
list := []interface{}{}
for item := range p.mapInst {
list = append(list, item)
}
return list
}