/
slice.go
112 lines (96 loc) · 2.65 KB
/
slice.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
package util
// SliceSearch search in a slice has the length of n,
// return the first position where f(i) is true,
// -1 is returned if nothing found.
func SliceSearch(n int, f func(int) bool) int {
for i := 0; i < n; i++ {
if f(i) {
return i
}
}
return -1
}
// SliceSearchInt search x in an int slice, return the first position of x,
// -1 is returned if nothing found.
func SliceSearchInt(a []int, x int) int {
return SliceSearch(len(a), func(i int) bool { return a[i] == x })
}
// SliceSearchString search x in a string slice, return the first position of x,
// -1 is returned if nothing found.
func SliceSearchString(a []string, x string) int {
return SliceSearch(len(a), func(i int) bool { return a[i] == x })
}
// SliceUniqueInt retrieve unique items item from int slice.
func SliceUniqueInt(a []int) []int {
l := int(len(a)/2) + 1
result, exists := make([]int, 0, l), make(map[int]bool)
for _, v := range a {
if !exists[v] {
result = append(result, v)
exists[v] = true
}
}
return result
}
// SliceUniqueString retrieve unique string items from string slice.
func SliceUniqueString(a []string) []string {
l := int(len(a)/2) + 1
result, exists := make([]string, 0, l), make(map[string]bool)
for _, v := range a {
if !exists[v] {
result = append(result, v)
exists[v] = true
}
}
return result
}
type SliceFilterIntFunc func(s int) bool
// SliceFilterInt 依次将 slice 中的每个值传递到 callback 函数。
// 如果 callback 函数返回 true,则 slice 的当前值会被包含在返回的结果slice中。
// 如果callback 未传 就会过滤零值
func SliceFilterInt(a []int, callbacks ...SliceFilterIntFunc) []int {
l := int(len(a)/2) + 1
result := make([]int, 0, l)
var callback SliceFilterIntFunc
if len(callbacks) > 0 {
callback = callbacks[0]
}
for _, v := range a {
if callback == nil {
if v == 0 {
continue
}
} else {
if callback(v) == false {
continue
}
}
result = append(result, v)
}
return result
}
type SliceFilterStringFunc func(s string) bool
// SliceFilterString 依次将 slice 中的每个值传递到 callback 函数。
// 如果 callback 函数返回 true,则 slice 的当前值会被包含在返回的结果slice中。
// 如果callback 未传 就会过滤零值
func SliceFilterString(a []string, callbacks ...SliceFilterStringFunc) []string {
l := int(len(a)/2) + 1
result := make([]string, 0, l)
var callback SliceFilterStringFunc
if len(callbacks) > 0 {
callback = callbacks[0]
}
for _, v := range a {
if callback == nil {
if v == "" {
continue
}
} else {
if callback(v) == false {
continue
}
}
result = append(result, v)
}
return result
}