-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslices.go
80 lines (73 loc) · 2.15 KB
/
slices.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
package pandati
import (
"fmt"
"reflect"
)
// ExistsInSlice returns true if the given value exists in the given slice.
func ExistsInSlice(slice interface{}, value interface{}) bool {
s := reflect.ValueOf(slice)
if s.Kind() != reflect.Slice {
panic(fmt.Sprintf("%v is not a slice", slice))
}
for i := 0; i < s.Len(); i++ {
if s.Index(i).Interface() == value {
return true
}
}
return false
}
// RemoveFromSlice removes the given value from the given slice.
// The value is replaced with Zero value of the same type.
// For string it is an empty string.
// For integers it is 0.
func RemoveFromSlice(slice interface{}, value interface{}) {
s := reflect.ValueOf(slice)
if s.Kind() != reflect.Slice {
panic(fmt.Sprintf("%v is not a slice", slice))
}
for i := 0; i < s.Len(); i++ {
if s.Index(i).Interface() == value {
s.Index(i).Set(reflect.Zero(s.Index(i).Type()))
return
}
}
}
// RemoveFromSlice removes the given value from the given slice using provided index.
// The value is replaced with Zero value of the same type.
// For string it is an empty string.
// For integers it is 0.
func RemoveFromSliceByIndex(slice interface{}, index int) {
s := reflect.ValueOf(slice)
if s.Kind() != reflect.Slice {
panic(fmt.Sprintf("%v is not a slice", slice))
}
if index < 0 || index >= s.Len() {
panic(fmt.Sprintf("index %d is out of bounds", index))
}
s.Index(index).Set(reflect.Zero(s.Index(index).Type()))
}
// UniqueSlice returns slice with reordered and unique values.
// Please note that function does not modify the original slice but returns a new one.
func UniqueSlice(slice interface{}) interface{} {
s := reflect.ValueOf(slice)
if s.Kind() != reflect.Slice {
panic(fmt.Sprintf("%v is not a slice", slice))
}
for i := 0; i < s.Len(); i++ {
for j := 0; j < s.Len(); j++ {
if i == j {
continue
}
if s.Index(i).Interface() == s.Index(j).Interface() {
RemoveFromSliceByIndex(slice, j)
}
}
}
sliceCleaned := reflect.MakeSlice(reflect.TypeOf(slice), 0, 0)
for i := 0; i < s.Len(); i++ {
if !IsZero(s.Index(i).Interface()) {
sliceCleaned = reflect.Append(sliceCleaned, s.Index(i))
}
}
return sliceCleaned.Interface()
}