-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
49 lines (44 loc) · 811 Bytes
/
utils.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
package fspider
import "sync"
type PathSet interface {
Add(string)
Remove(string)
Contains(string) bool
Paths() []string
}
// 并发安全的字符串集合
type pathSetImpl struct {
m map[string]struct{}
l *sync.RWMutex
}
func NewPathSet() *pathSetImpl {
return &pathSetImpl{
m: make(map[string]struct{}),
l: &sync.RWMutex{},
}
}
func (s *pathSetImpl) Add(str string) {
s.l.Lock()
s.m[str] = struct{}{}
s.l.Unlock()
}
func (s *pathSetImpl) Remove(str string) {
s.l.Lock()
delete(s.m, str)
s.l.Unlock()
}
func (s *pathSetImpl) Contains(str string) bool {
s.l.RLock()
_, ok := s.m[str]
s.l.RUnlock()
return ok
}
func (s *pathSetImpl) Paths() []string {
s.l.RLock()
defer s.l.RUnlock()
ret := make([]string, 0, len(s.m))
for k := range s.m {
ret = append(ret, k)
}
return ret
}