-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset.go
220 lines (187 loc) · 4.29 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package go_
import (
"strings"
"sync"
)
type Set interface {
Add(element interface{})
Contains(element interface{}) bool
Remove(element interface{})
SetIfNotExist(element interface{}) bool
Values() []interface{}
Size() int
Replace(element1, element2 interface{})
ForEach(f func(element interface{}))
Map(f func(element interface{}) interface{}) Set
Intersect(another Set) Set
Union(another Set) Set
Diff(another Set) Set
}
type HashSet map[interface{}]bool
func NewHashSet(size int) Set {
return HashSet(make(map[interface{}]bool, size))
}
func NewHashSetFromInt64List(data []int64) Set {
set := NewHashSet(len(data))
for _, v := range data {
set.Add(v)
}
return set
}
func NewHashSetFromStringList(data []string) Set {
set := NewHashSet(len(data))
for _, v := range data {
set.Add(v)
}
return set
}
func NewHashSetFromCommaString(data string) Set {
return NewHashSetFromStringList(strings.Split(data, ","))
}
func (set HashSet) Add(ele interface{}) {
set[ele] = true
}
func (set HashSet) Contains(ele interface{}) bool {
return set[ele]
}
func (set HashSet) SetIfNotExist(ele interface{}) bool {
pre := set.Contains(ele)
set.Add(ele)
return pre
}
func (set HashSet) Remove(ele interface{}) {
delete(set, ele)
}
func (set HashSet) Values() []interface{} {
res := make([]interface{}, 0, len(set))
for e := range set {
res = append(res, e)
}
return res
}
func (set HashSet) Size() int {
return len(set)
}
func (set HashSet) Replace(ele1, ele2 interface{}) {
set.Remove(ele1)
set.Add(ele2)
}
func (set HashSet) ForEach(f func(interface{})) {
for k := range set {
f(k)
}
}
func (set HashSet) Map(f func(interface{}) interface{}) Set {
res := NewHashSet(set.Size())
for e := range set {
res.Add(f(e))
}
return res
}
func (set HashSet) Intersect(another Set) Set {
res := NewHashSet(len(set))
set.ForEach(func(ele interface{}) {
if another.Contains(ele) {
res.Add(ele)
}
})
return res
}
func (set HashSet) Union(another Set) Set {
res := NewHashSet(set.Size() + another.Size())
set.ForEach(res.Add)
another.ForEach(res.Add)
return res
}
func (set HashSet) Diff(another Set) Set {
res := NewHashSet(len(set))
res.ForEach(func(ele interface{}) {
if !another.Contains(ele) {
res.Add(ele)
}
})
return res
}
type ConcurrentHashSet struct {
data sync.Map
}
func NewConcurrentHashSet() *ConcurrentHashSet {
return &ConcurrentHashSet{}
}
func (cSet ConcurrentHashSet) Add(element interface{}) {
cSet.SetIfNotExist(element)
}
func (cSet ConcurrentHashSet) Contains(element interface{}) bool {
val, found := cSet.data.Load(element)
return found && val.(bool)
}
func (cSet ConcurrentHashSet) Remove(element interface{}) {
cSet.data.Delete(element)
}
func (cSet ConcurrentHashSet) SetIfNotExist(element interface{}) bool {
actual, loaded := cSet.data.LoadOrStore(element, true)
return loaded && actual.(bool)
}
func (cSet ConcurrentHashSet) Values() []interface{} {
res := make([]interface{}, 0)
cSet.data.Range(func(key, value interface{}) bool {
res = append(res, key)
return true
})
return res
}
func (cSet ConcurrentHashSet) Size() int {
l := 0
cSet.data.Range(func(key, value interface{}) bool {
if value.(bool) {
l++
}
return true
})
return l
}
func (cSet ConcurrentHashSet) Replace(element1, element2 interface{}) {
cSet.data.Delete(element1)
cSet.data.Store(element2, true)
}
func (cSet ConcurrentHashSet) ForEach(f func(element interface{})) {
cSet.data.Range(func(key, value interface{}) bool {
f(key)
return true
})
}
func (cSet ConcurrentHashSet) Map(f func(element interface{}) interface{}) Set {
set := NewConcurrentHashSet()
cSet.ForEach(func(element interface{}) {
set.Add(f(element))
})
return set
}
func (cSet ConcurrentHashSet) Intersect(another Set) Set {
res := NewConcurrentHashSet()
another.ForEach(func(element interface{}) {
if cSet.Contains(element) {
res.Add(element)
}
})
return res
}
func (cSet ConcurrentHashSet) Union(another Set) Set {
set := NewConcurrentHashSet()
cSet.ForEach(func(element interface{}) {
set.Add(element)
})
another.ForEach(func(element interface{}) {
set.Add(element)
})
return set
}
func (cSet ConcurrentHashSet) Diff(another Set) Set {
set := NewConcurrentHashSet()
cSet.ForEach(func(element interface{}) {
if !another.Contains(element) {
set.Add(element)
}
})
return set
}