/
set.go
74 lines (63 loc) · 1.27 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
74
// Package set Set Data Structure based on map and sync.RWMutex
package set
import "sync"
// Set safe map
type Set struct {
m map[interface{}]struct{}
sync.RWMutex
}
// New new set
func New() *Set {
return &Set{
m: make(map[interface{}]struct{}),
}
}
// Add add
func (s *Set) Add(item interface{}) {
s.Lock()
defer s.Unlock()
s.m[item] = struct{}{}
}
// Remove deletes the specified item from the map
func (s *Set) Remove(item interface{}) {
s.Lock()
defer s.Unlock()
delete(s.m, item)
}
// Has looks for the existence of an item
func (s *Set) Has(item interface{}) bool {
s.RLock()
defer s.RUnlock()
_, ok := s.m[item]
return ok
}
// Len returns the number of items in a set.
func (s *Set) Len() int {
return len(s.List())
}
// Clear removes all items from the set
func (s *Set) Clear() {
s.Lock()
defer s.Unlock()
s.m = make(map[interface{}]struct{})
}
// IsEmpty checks for emptiness
func (s *Set) IsEmpty() bool {
return s.Len() == 0
}
// Each call fn on each element in the Set
func (s *Set) Each(fn func(interface{})) {
for e := range s.m {
fn(e)
}
}
// List Set returns a slice of all items
func (s *Set) List() []interface{} {
s.RLock()
defer s.RUnlock()
var list []interface{}
for item := range s.m {
list = append(list, item)
}
return list
}