-
Notifications
You must be signed in to change notification settings - Fork 0
/
set.go
73 lines (62 loc) · 1.08 KB
/
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
package standard
import (
"sync"
)
// 不重复切片
type Set[T comparable] struct {
mutex_ sync.RWMutex
m_ map[T]struct{}
}
func NewSet[T comparable]() *Set[T] {
return &Set[T]{
mutex_: sync.RWMutex{},
m_: make(map[T]struct{}),
}
}
func (s *Set[T]) Insert(key T) {
s.mutex_.Lock()
s.m_[key] = struct{}{}
s.mutex_.Unlock()
}
func (s *Set[T]) Erase(key T) {
s.mutex_.Lock()
delete(s.m_, key)
s.mutex_.Unlock()
}
func (s *Set[T]) Has(key T) bool {
s.mutex_.RLock()
defer s.mutex_.RUnlock()
_, ok := s.m_[key]
return ok
}
func (s *Set[T]) Size() int {
s.mutex_.RLock()
defer s.mutex_.RUnlock()
return len(s.m_)
}
func (s *Set[T]) Empty() bool {
s.mutex_.RLock()
defer s.mutex_.RUnlock()
return s.Size() == 0
}
func (s *Set[T]) Clear() {
s.mutex_.Lock()
s.m_ = make(map[T]struct{})
s.mutex_.Unlock()
}
func (s *Set[T]) List() []T {
s.mutex_.RLock()
defer s.mutex_.RUnlock()
list := make([]T, 0, s.Size())
for i := range s.m_ {
list = append(list, i)
}
return list
}
/*
func (s *Set[T]) SortList() []T {
list := s.List()
sort.Strings(list)
return list
}
*/