-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgset.go
180 lines (163 loc) · 2.92 KB
/
gset.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
package gset
type (
SafeType int
)
const (
ThreadSafe = iota
ThreadUnSafe
)
type Gset interface {
Add(elems ...interface{}) bool
AddString(elems ...string) bool
Remove(elems ...interface{}) bool
Len() int
IsEmpty() bool
IsEqual(s Gset) bool
IsSubset(s Gset) bool
IsSuperset(s Gset) bool
Has(elems ...interface{}) bool
String() string
List() []interface{}
Each(func(interface{}) bool)
Merge(s Gset)
Clear()
Copy() Gset
Separate(s Gset)
}
type gset struct {
m map[interface{}]bool
}
// New new gset
func New(safeType SafeType, size ...int) Gset {
if safeType == ThreadUnSafe {
return newGsetUnsafe(size...)
}
return newGsetSafe(size...)
}
// Union 合并
func Union(set1, set2 Gset, sets ...Gset) Gset {
u := set1.Copy()
set2.Each(func(elem interface{}) bool {
u.Add(elem)
return true
})
for _, set := range sets {
set.Each(func(elem interface{}) bool {
u.Add(elem)
return true
})
}
return u
}
// Difference
func Difference(set1, set2 Gset, sets ...Gset) Gset {
// tran copy
s := set1.Copy()
// remove set2 by set 1
s.Separate(set2)
for _, set := range sets {
s.Separate(set)
}
return s
}
// Intersection 交集
func Intersection(set1, set2 Gset, sets ...Gset) Gset {
all := Union(set1, set2, sets...)
result := Union(set1, set2, sets...)
all.Each(func(item interface{}) bool {
if !set1.Has(item) || !set2.Has(item) {
result.Remove(item)
}
for _, set := range sets {
if !set.Has(item) {
result.Remove(item)
}
}
return true
})
return result
}
// StringSlice
func StringSlice(s Gset) []string {
slice := make([]string, 0)
for _, item := range s.List() {
v, ok := item.(string)
if !ok {
continue
}
slice = append(slice, v)
}
return slice
}
// IntSlice
func IntSlice(s Gset) []int {
slice := make([]int, 0)
for _, item := range s.List() {
v, ok := item.(int)
if !ok {
continue
}
slice = append(slice, v)
}
return slice
}
// Int32Slice
func Int32Slice(s Gset) []int32 {
slice := make([]int32, 0)
for _, item := range s.List() {
v, ok := item.(int32)
if !ok {
continue
}
slice = append(slice, v)
}
return slice
}
// Int64Slice
func Int64Slice(s Gset) []int64 {
slice := make([]int64, 0)
for _, item := range s.List() {
v, ok := item.(int64)
if !ok {
continue
}
slice = append(slice, v)
}
return slice
}
// Float32Slice
func Float32Slice(s Gset) []float32 {
slice := make([]float32, 0)
for _, item := range s.List() {
v, ok := item.(float32)
if !ok {
continue
}
slice = append(slice, v)
}
return slice
}
// Float64Slice
func Float64Slice(s Gset) []float64 {
slice := make([]float64, 0)
for _, item := range s.List() {
v, ok := item.(float64)
if !ok {
continue
}
slice = append(slice, v)
}
return slice
}
// BoolSlice
func BoolSlice(s Gset) []bool {
slice := make([]bool, 0)
for _, item := range s.List() {
v, ok := item.(bool)
if !ok {
continue
}
slice = append(slice, v)
}
return slice
}