-
Notifications
You must be signed in to change notification settings - Fork 273
/
join_primitives.go
373 lines (313 loc) · 9.64 KB
/
join_primitives.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package funk
type JoinIntFnc func(lx, rx []int) []int
// JoinInt combines two int collections using the given join method.
func JoinInt(larr, rarr []int, fnc JoinIntFnc) []int {
return fnc(larr, rarr)
}
// InnerJoinInt finds and returns matching data from two int collections.
func InnerJoinInt(lx, rx []int) []int {
result := make([]int, 0, len(lx)+len(rx))
rhash := hashSliceInt(rx)
lhash := make(map[int]struct{}, len(lx))
for _, v := range lx {
_, ok := rhash[v]
_, alreadyExists := lhash[v]
if ok && !alreadyExists {
lhash[v] = struct{}{}
result = append(result, v)
}
}
return result
}
// OuterJoinInt finds and returns dissimilar data from two int collections.
func OuterJoinInt(lx, rx []int) []int {
ljoin := LeftJoinInt(lx, rx)
rjoin := RightJoinInt(lx, rx)
result := make([]int, len(ljoin)+len(rjoin))
copy(result, ljoin)
for i, v := range rjoin {
result[len(ljoin)+i] = v
}
return result
}
// LeftJoinInt finds and returns dissimilar data from the first int collection (left).
func LeftJoinInt(lx, rx []int) []int {
result := make([]int, 0, len(lx))
rhash := hashSliceInt(rx)
for _, v := range lx {
_, ok := rhash[v]
if !ok {
result = append(result, v)
}
}
return result
}
// LeftJoinInt finds and returns dissimilar data from the second int collection (right).
func RightJoinInt(lx, rx []int) []int { return LeftJoinInt(rx, lx) }
func hashSliceInt(arr []int) map[int]struct{} {
hash := make(map[int]struct{}, len(arr))
for _, i := range arr {
hash[i] = struct{}{}
}
return hash
}
type JoinInt32Fnc func(lx, rx []int32) []int32
// JoinInt32 combines two int32 collections using the given join method.
func JoinInt32(larr, rarr []int32, fnc JoinInt32Fnc) []int32 {
return fnc(larr, rarr)
}
// InnerJoinInt32 finds and returns matching data from two int32 collections.
func InnerJoinInt32(lx, rx []int32) []int32 {
result := make([]int32, 0, len(lx)+len(rx))
rhash := hashSliceInt32(rx)
lhash := make(map[int32]struct{}, len(lx))
for _, v := range lx {
_, ok := rhash[v]
_, alreadyExists := lhash[v]
if ok && !alreadyExists {
lhash[v] = struct{}{}
result = append(result, v)
}
}
return result
}
// OuterJoinInt32 finds and returns dissimilar data from two int32 collections.
func OuterJoinInt32(lx, rx []int32) []int32 {
ljoin := LeftJoinInt32(lx, rx)
rjoin := RightJoinInt32(lx, rx)
result := make([]int32, len(ljoin)+len(rjoin))
copy(result, ljoin)
for i, v := range rjoin {
result[len(ljoin)+i] = v
}
return result
}
// LeftJoinInt32 finds and returns dissimilar data from the first int32 collection (left).
func LeftJoinInt32(lx, rx []int32) []int32 {
result := make([]int32, 0, len(lx))
rhash := hashSliceInt32(rx)
for _, v := range lx {
_, ok := rhash[v]
if !ok {
result = append(result, v)
}
}
return result
}
// LeftJoinInt32 finds and returns dissimilar data from the second int32 collection (right).
func RightJoinInt32(lx, rx []int32) []int32 { return LeftJoinInt32(rx, lx) }
func hashSliceInt32(arr []int32) map[int32]struct{} {
hash := make(map[int32]struct{}, len(arr))
for _, i := range arr {
hash[i] = struct{}{}
}
return hash
}
type JoinInt64Fnc func(lx, rx []int64) []int64
// JoinInt64 combines two int64 collections using the given join method.
func JoinInt64(larr, rarr []int64, fnc JoinInt64Fnc) []int64 {
return fnc(larr, rarr)
}
// InnerJoinInt64 finds and returns matching data from two int64 collections.
func InnerJoinInt64(lx, rx []int64) []int64 {
result := make([]int64, 0, len(lx)+len(rx))
rhash := hashSliceInt64(rx)
lhash := make(map[int64]struct{}, len(lx))
for _, v := range lx {
_, ok := rhash[v]
_, alreadyExists := lhash[v]
if ok && !alreadyExists {
lhash[v] = struct{}{}
result = append(result, v)
}
}
return result
}
// OuterJoinInt64 finds and returns dissimilar data from two int64 collections.
func OuterJoinInt64(lx, rx []int64) []int64 {
ljoin := LeftJoinInt64(lx, rx)
rjoin := RightJoinInt64(lx, rx)
result := make([]int64, len(ljoin)+len(rjoin))
copy(result, ljoin)
for i, v := range rjoin {
result[len(ljoin)+i] = v
}
return result
}
// LeftJoinInt64 finds and returns dissimilar data from the first int64 collection (left).
func LeftJoinInt64(lx, rx []int64) []int64 {
result := make([]int64, 0, len(lx))
rhash := hashSliceInt64(rx)
for _, v := range lx {
_, ok := rhash[v]
if !ok {
result = append(result, v)
}
}
return result
}
// LeftJoinInt64 finds and returns dissimilar data from the second int64 collection (right).
func RightJoinInt64(lx, rx []int64) []int64 { return LeftJoinInt64(rx, lx) }
func hashSliceInt64(arr []int64) map[int64]struct{} {
hash := make(map[int64]struct{}, len(arr))
for _, i := range arr {
hash[i] = struct{}{}
}
return hash
}
type JoinStringFnc func(lx, rx []string) []string
// JoinString combines two string collections using the given join method.
func JoinString(larr, rarr []string, fnc JoinStringFnc) []string {
return fnc(larr, rarr)
}
// InnerJoinString finds and returns matching data from two string collections.
func InnerJoinString(lx, rx []string) []string {
result := make([]string, 0, len(lx)+len(rx))
rhash := hashSliceString(rx)
lhash := make(map[string]struct{}, len(lx))
for _, v := range lx {
_, ok := rhash[v]
_, alreadyExists := lhash[v]
if ok && !alreadyExists {
lhash[v] = struct{}{}
result = append(result, v)
}
}
return result
}
// OuterJoinString finds and returns dissimilar data from two string collections.
func OuterJoinString(lx, rx []string) []string {
ljoin := LeftJoinString(lx, rx)
rjoin := RightJoinString(lx, rx)
result := make([]string, len(ljoin)+len(rjoin))
copy(result, ljoin)
for i, v := range rjoin {
result[len(ljoin)+i] = v
}
return result
}
// LeftJoinString finds and returns dissimilar data from the first string collection (left).
func LeftJoinString(lx, rx []string) []string {
result := make([]string, 0, len(lx))
rhash := hashSliceString(rx)
for _, v := range lx {
_, ok := rhash[v]
if !ok {
result = append(result, v)
}
}
return result
}
// LeftJoinString finds and returns dissimilar data from the second string collection (right).
func RightJoinString(lx, rx []string) []string { return LeftJoinString(rx, lx) }
func hashSliceString(arr []string) map[string]struct{} {
hash := make(map[string]struct{}, len(arr))
for _, i := range arr {
hash[i] = struct{}{}
}
return hash
}
type JoinFloat32Fnc func(lx, rx []float32) []float32
// JoinFloat32 combines two float32 collections using the given join method.
func JoinFloat32(larr, rarr []float32, fnc JoinFloat32Fnc) []float32 {
return fnc(larr, rarr)
}
// InnerJoinFloat32 finds and returns matching data from two float32 collections.
func InnerJoinFloat32(lx, rx []float32) []float32 {
result := make([]float32, 0, len(lx)+len(rx))
rhash := hashSliceFloat32(rx)
lhash := make(map[float32]struct{}, len(lx))
for _, v := range lx {
_, ok := rhash[v]
_, alreadyExists := lhash[v]
if ok && !alreadyExists {
lhash[v] = struct{}{}
result = append(result, v)
}
}
return result
}
// OuterJoinFloat32 finds and returns dissimilar data from two float32 collections.
func OuterJoinFloat32(lx, rx []float32) []float32 {
ljoin := LeftJoinFloat32(lx, rx)
rjoin := RightJoinFloat32(lx, rx)
result := make([]float32, len(ljoin)+len(rjoin))
copy(result, ljoin)
for i, v := range rjoin {
result[len(ljoin)+i] = v
}
return result
}
// LeftJoinFloat32 finds and returns dissimilar data from the first float32 collection (left).
func LeftJoinFloat32(lx, rx []float32) []float32 {
result := make([]float32, 0, len(lx))
rhash := hashSliceFloat32(rx)
for _, v := range lx {
_, ok := rhash[v]
if !ok {
result = append(result, v)
}
}
return result
}
// LeftJoinFloat32 finds and returns dissimilar data from the second float32 collection (right).
func RightJoinFloat32(lx, rx []float32) []float32 { return LeftJoinFloat32(rx, lx) }
func hashSliceFloat32(arr []float32) map[float32]struct{} {
hash := make(map[float32]struct{}, len(arr))
for _, i := range arr {
hash[i] = struct{}{}
}
return hash
}
type JoinFloat64Fnc func(lx, rx []float64) []float64
// JoinFloat64 combines two float64 collections using the given join method.
func JoinFloat64(larr, rarr []float64, fnc JoinFloat64Fnc) []float64 {
return fnc(larr, rarr)
}
// InnerJoinFloat64 finds and returns matching data from two float64 collections.
func InnerJoinFloat64(lx, rx []float64) []float64 {
result := make([]float64, 0, len(lx)+len(rx))
rhash := hashSliceFloat64(rx)
lhash := make(map[float64]struct{}, len(lx))
for _, v := range lx {
_, ok := rhash[v]
_, alreadyExists := lhash[v]
if ok && !alreadyExists {
lhash[v] = struct{}{}
result = append(result, v)
}
}
return result
}
// OuterJoinFloat64 finds and returns dissimilar data from two float64 collections.
func OuterJoinFloat64(lx, rx []float64) []float64 {
ljoin := LeftJoinFloat64(lx, rx)
rjoin := RightJoinFloat64(lx, rx)
result := make([]float64, len(ljoin)+len(rjoin))
copy(result, ljoin)
for i, v := range rjoin {
result[len(ljoin)+i] = v
}
return result
}
// LeftJoinFloat64 finds and returns dissimilar data from the first float64 collection (left).
func LeftJoinFloat64(lx, rx []float64) []float64 {
result := make([]float64, 0, len(lx))
rhash := hashSliceFloat64(rx)
for _, v := range lx {
_, ok := rhash[v]
if !ok {
result = append(result, v)
}
}
return result
}
// LeftJoinFloat64 finds and returns dissimilar data from the second float64 collection (right).
func RightJoinFloat64(lx, rx []float64) []float64 { return LeftJoinFloat64(rx, lx) }
func hashSliceFloat64(arr []float64) map[float64]struct{} {
hash := make(map[float64]struct{}, len(arr))
for _, i := range arr {
hash[i] = struct{}{}
}
return hash
}