This repository has been archived by the owner on Apr 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathintersect.go
68 lines (63 loc) · 1.53 KB
/
intersect.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
package slices
import (
"unsafe"
)
type intersectFn func(sptr, optr unsafe.Pointer) interface{}
// Intersect returns a slice with the intersection of slice and other.
// When the slices are the same, slice is returned.
func Intersect(slice, other interface{}) interface{} {
fn, ok := intersectOf(slice, other)
if fn == nil {
panic("slice is not supported slice type")
}
if !ok {
panic("other is not the same type as slice")
}
sptr := noescape(ptrOf(slice))
optr := noescape(ptrOf(other))
return fn(sptr, optr)
}
func intersectOf(slice, other interface{}) (intersectFn, bool) {
switch slice.(type) {
case []string:
_, ok := other.([]string)
return stringIntersect, ok
case []int:
_, ok := other.([]int)
return intIntersect, ok
case []int8:
_, ok := other.([]int8)
return int8Intersect, ok
case []int16:
_, ok := other.([]int16)
return int16Intersect, ok
case []int32:
_, ok := other.([]int32)
return int32Intersect, ok
case []int64:
_, ok := other.([]int64)
return int64Intersect, ok
case []uint:
_, ok := other.([]uint)
return uintIntersect, ok
case []uint8:
_, ok := other.([]uint8)
return uint8Intersect, ok
case []uint16:
_, ok := other.([]uint16)
return uint16Intersect, ok
case []uint32:
_, ok := other.([]uint32)
return uint32Intersect, ok
case []uint64:
_, ok := other.([]uint64)
return uint64Intersect, ok
case []float32:
_, ok := other.([]float32)
return float32Intersect, ok
case []float64:
_, ok := other.([]float64)
return float64Intersect, ok
}
return nil, false
}