-
Notifications
You must be signed in to change notification settings - Fork 0
/
slices.go
75 lines (54 loc) · 1.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
package common
func SliceClone[S ~[]E, E any](s S) S {
n := make([]E, len(s))
copy(n, s)
return n
}
func SliceContains[S ~[]E, E comparable](s S, e E) bool {
return SliceIndex(s, e) != -1
}
func SliceIndex[S ~[]E, E comparable](s S, e E) int {
for i, t := range s {
if t == e {
return i
}
}
return -1
}
func SliceIndexFunc[S ~[]E, E any](s S, fn func(E) bool) int {
for i, t := range s {
if fn(t) {
return i
}
}
return -1
}
func SliceAppend[S ~[]E, E any](s S, e ...E) S {
return append(s, e...)
}
func SliceRemove[S ~[]E, E comparable](s S, e E) S {
p := SliceIndex(s, e)
if p == -1 {
return s
}
n := make([]E, len(s)-1)
copy(n, s[:p])
copy(n[p:], s[p+1:])
return n
}
func SliceInsert[S ~[]E, E any](s S, index int, e ...E) S {
n := make([]E, len(s)+len(e))
copy(n, s[:index])
copy(n[index:], e)
copy(n[index+len(e):], s[index:])
return n
}
func SliceDeleteRange[S ~[]E, E any](s S, index0 int, index1 int) S {
n := make([]E, len(s)-(index1-index0))
copy(n, s[:index0])
copy(n[index0:], s[index1:])
return n
}
func SliceDelete[S ~[]E, E any](s S, index int) S {
return SliceDeleteRange(s, index, index+1)
}