-
Notifications
You must be signed in to change notification settings - Fork 273
/
intersection.go
282 lines (230 loc) · 6.06 KB
/
intersection.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package funk
import (
"reflect"
)
// Intersect returns the intersection between two collections.
//
// Deprecated: use Join(x, y, InnerJoin) instead of Intersect, InnerJoin
// implements deduplication mechanism, so verify your code behaviour
// before using it
func Intersect(x interface{}, y interface{}) interface{} {
if !IsCollection(x) {
panic("First parameter must be a collection")
}
if !IsCollection(y) {
panic("Second parameter must be a collection")
}
hash := map[interface{}]struct{}{}
xValue := reflect.ValueOf(x)
xType := xValue.Type()
yValue := reflect.ValueOf(y)
yType := yValue.Type()
if NotEqual(xType, yType) {
panic("Parameters must have the same type")
}
zType := reflect.SliceOf(xType.Elem())
zSlice := reflect.MakeSlice(zType, 0, 0)
for i := 0; i < xValue.Len(); i++ {
v := xValue.Index(i).Interface()
hash[v] = struct{}{}
}
for i := 0; i < yValue.Len(); i++ {
v := yValue.Index(i).Interface()
_, ok := hash[v]
if ok {
zSlice = reflect.Append(zSlice, yValue.Index(i))
}
}
return zSlice.Interface()
}
// IntersectString returns the intersection between two collections of string.
func IntersectString(x []string, y []string) []string {
if len(x) == 0 || len(y) == 0 {
return []string{}
}
set := []string{}
hash := map[string]struct{}{}
for _, v := range x {
hash[v] = struct{}{}
}
for _, v := range y {
_, ok := hash[v]
if ok {
set = append(set, v)
}
}
return set
}
// Difference returns the difference between two collections.
func Difference(x interface{}, y interface{}) (interface{}, interface{}) {
if !IsIteratee(x) {
panic("First parameter must be an iteratee")
}
if !IsIteratee(y) {
panic("Second parameter must be an iteratee")
}
xValue := reflect.ValueOf(x)
xType := xValue.Type()
yValue := reflect.ValueOf(y)
yType := yValue.Type()
if NotEqual(xType, yType) {
panic("Parameters must have the same type")
}
if xType.Kind() == reflect.Map {
leftType := reflect.MapOf(xType.Key(), xType.Elem())
rightType := reflect.MapOf(xType.Key(), xType.Elem())
leftMap := reflect.MakeMap(leftType)
rightMap := reflect.MakeMap(rightType)
xIter := xValue.MapRange()
for xIter.Next() {
k := xIter.Key()
xv := xIter.Value()
yv := yValue.MapIndex(k)
equalTo := equal(xv.Interface(), true)
if !yv.IsValid() || !equalTo(yv, yv) {
leftMap.SetMapIndex(k, xv)
}
}
yIter := yValue.MapRange()
for yIter.Next() {
k := yIter.Key()
yv := yIter.Value()
xv := xValue.MapIndex(k)
equalTo := equal(yv.Interface(), true)
if !xv.IsValid() || !equalTo(xv, xv) {
rightMap.SetMapIndex(k, yv)
}
}
return leftMap.Interface(), rightMap.Interface()
} else {
leftType := reflect.SliceOf(xType.Elem())
rightType := reflect.SliceOf(yType.Elem())
leftSlice := reflect.MakeSlice(leftType, 0, 0)
rightSlice := reflect.MakeSlice(rightType, 0, 0)
for i := 0; i < xValue.Len(); i++ {
v := xValue.Index(i).Interface()
if !Contains(y, v) {
leftSlice = reflect.Append(leftSlice, xValue.Index(i))
}
}
for i := 0; i < yValue.Len(); i++ {
v := yValue.Index(i).Interface()
if !Contains(x, v) {
rightSlice = reflect.Append(rightSlice, yValue.Index(i))
}
}
return leftSlice.Interface(), rightSlice.Interface()
}
}
// DifferenceString returns the difference between two collections of strings.
func DifferenceString(x []string, y []string) ([]string, []string) {
leftSlice := []string{}
rightSlice := []string{}
for _, v := range x {
if !ContainsString(y, v) {
leftSlice = append(leftSlice, v)
}
}
for _, v := range y {
if !ContainsString(x, v) {
rightSlice = append(rightSlice, v)
}
}
return leftSlice, rightSlice
}
// DifferenceInt64 returns the difference between two collections of int64s.
func DifferenceInt64(x []int64, y []int64) ([]int64, []int64) {
leftSlice := []int64{}
rightSlice := []int64{}
for _, v := range x {
if !ContainsInt64(y, v) {
leftSlice = append(leftSlice, v)
}
}
for _, v := range y {
if !ContainsInt64(x, v) {
rightSlice = append(rightSlice, v)
}
}
return leftSlice, rightSlice
}
// DifferenceInt32 returns the difference between two collections of ints32.
func DifferenceInt32(x []int32, y []int32) ([]int32, []int32) {
leftSlice := []int32{}
rightSlice := []int32{}
for _, v := range x {
if !ContainsInt32(y, v) {
leftSlice = append(leftSlice, v)
}
}
for _, v := range y {
if !ContainsInt32(x, v) {
rightSlice = append(rightSlice, v)
}
}
return leftSlice, rightSlice
}
// DifferenceInt returns the difference between two collections of ints.
func DifferenceInt(x []int, y []int) ([]int, []int) {
leftSlice := []int{}
rightSlice := []int{}
for _, v := range x {
if !ContainsInt(y, v) {
leftSlice = append(leftSlice, v)
}
}
for _, v := range y {
if !ContainsInt(x, v) {
rightSlice = append(rightSlice, v)
}
}
return leftSlice, rightSlice
}
// DifferenceUInt returns the difference between two collections of uints.
func DifferenceUInt(x []uint, y []uint) ([]uint, []uint) {
leftSlice := []uint{}
rightSlice := []uint{}
for _, v := range x {
if !ContainsUInt(y, v) {
leftSlice = append(leftSlice, v)
}
}
for _, v := range y {
if !ContainsUInt(x, v) {
rightSlice = append(rightSlice, v)
}
}
return leftSlice, rightSlice
}
// DifferenceUInt32 returns the difference between two collections of uints32.
func DifferenceUInt32(x []uint32, y []uint32) ([]uint32, []uint32) {
leftSlice := []uint32{}
rightSlice := []uint32{}
for _, v := range x {
if !ContainsUInt32(y, v) {
leftSlice = append(leftSlice, v)
}
}
for _, v := range y {
if !ContainsUInt32(x, v) {
rightSlice = append(rightSlice, v)
}
}
return leftSlice, rightSlice
}
// DifferenceUInt64 returns the difference between two collections of uints64.
func DifferenceUInt64(x []uint64, y []uint64) ([]uint64, []uint64) {
leftSlice := []uint64{}
rightSlice := []uint64{}
for _, v := range x {
if !ContainsUInt64(y, v) {
leftSlice = append(leftSlice, v)
}
}
for _, v := range y {
if !ContainsUInt64(x, v) {
rightSlice = append(rightSlice, v)
}
}
return leftSlice, rightSlice
}