/
threadsafe-set.go
164 lines (145 loc) · 2.58 KB
/
threadsafe-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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package utils
import (
"sync"
)
type Set[T comparable] struct {
m map[T]struct{}
sync.RWMutex
}
func NewSet[T comparable](list ...[]T) *Set[T] {
s := &Set[T]{
m: make(map[T]struct{}),
}
if len(list) > 0 {
s.AddList(list[0])
}
return s
}
//func main() {
// // Initialize our Set
// s := New()
//
// // Add example items
// s.Add("item1")
// s.Add("item1") // duplicate item
// s.Add("item2")
// fmt.Printf("%d items\n", s.Len())
//
// // Clear all items
// s.Clear()
// if s.IsEmpty() {
// fmt.Printf("0 items\n")
// }
//
// s.Add("item2")
// s.Add("item3")
// s.Add("item4")
//
// // Check for existence
// if s.Has("item2") {
// fmt.Println("item2 does exist")
// }
//
// // Remove some of our items
// s.Remove("item2")
// s.Remove("item4")
// fmt.Println("list of all items:", s.List())
//}
// Add add
func (s *Set[T]) Add(item T) {
s.Lock()
defer s.Unlock()
s.m[item] = struct{}{}
}
func (s *Set[T]) AddList(items []T) {
for _, item := range items {
s.Add(item)
}
}
// Remove deletes the specified item from the map
func (s *Set[T]) Remove(item T) {
s.Lock()
defer s.Unlock()
delete(s.m, item)
}
// Has looks for the existence of an item
func (s *Set[T]) Has(item T) bool {
s.RLock()
defer s.RUnlock()
_, ok := s.m[item]
return ok
}
// Len returns the number of items in a set.
func (s *Set[T]) Len() int {
return len(s.List())
}
// Clear removes all items from the set
func (s *Set[T]) Clear() {
s.Lock()
defer s.Unlock()
s.m = make(map[T]struct{})
}
// IsEmpty checks for emptiness
func (s *Set[T]) IsEmpty() bool {
if s.Len() == 0 {
return true
}
return false
}
// Set returns a slice of all items
func (s *Set[T]) List() []T {
s.RLock()
defer s.RUnlock()
list := make([]T, 0)
for item := range s.m {
list = append(list, item)
}
return list
}
func (s *Set[T]) ForEach(h func(T)) {
s.RLock()
defer s.RUnlock()
for item := range s.m {
h(item)
}
}
func (s *Set[T]) Diff(other *Set[T]) *Set[T] {
s.RLock()
other.RLock()
defer s.RUnlock()
defer other.RUnlock()
diff := NewSet[T]()
for item := range s.m {
if !other.Has(item) {
diff.Add(item)
}
}
return diff
}
func (s *Set[T]) And(other *Set[T]) *Set[T] {
s.RLock()
other.RLock()
defer s.RUnlock()
defer other.RUnlock()
intersection := NewSet[T]()
for item := range s.m {
if other.Has(item) {
intersection.Add(item)
}
}
return intersection
}
func (s *Set[T]) Or(other *Set[T]) *Set[T] {
s.RLock()
other.RLock()
defer s.RUnlock()
defer other.RUnlock()
union := NewSet[T]()
for item := range s.m {
union.Add(item)
}
for item := range other.m {
union.Add(item)
}
return union
}