-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathset.go
74 lines (63 loc) · 998 Bytes
/
set.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
package goutils
import (
"strconv"
"strings"
"sync"
)
type Set struct {
m map[int64]bool
sync.RWMutex
}
func NewSet() *Set {
return &Set{
m: map[int64]bool{},
}
}
func (s *Set) Add(item int64) {
s.Lock()
defer s.Unlock()
s.m[item] = true
}
func (s *Set) Remove(item int64) {
s.Lock()
s.Unlock()
delete(s.m, item)
}
func (s *Set) Has(item int64) bool {
s.RLock()
defer s.RUnlock()
_, ok := s.m[item]
return ok
}
func (s *Set) Len() int {
return len(s.List())
}
func (s *Set) Clear() {
s.Lock()
defer s.Unlock()
s.m = map[int64]bool{}
}
func (s *Set) IsEmpty() bool {
if s.Len() == 0 {
return true
}
return false
}
func (s *Set) String() string {
s.RLock()
defer s.RUnlock()
list := []string{}
for item := range s.m {
list = append(list, strconv.FormatInt(item, 10))
}
return strings.Join(list, ",")
}
func (s *Set) List() []int64 {
s.RLock()
defer s.RUnlock()
list := []int64{}
for item := range s.m {
list = append(list, item)
}
return list
}