-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharray.go
37 lines (32 loc) · 844 Bytes
/
array.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
package utils
// ArrContains check is array contains a string
func ArrContains[T Primitive](arr []T, tgt T) bool {
for _, a := range arr {
if a == tgt {
return true
}
}
return false
}
// ArrRM removes element from array (ordered)
func ArrRm[T Primitive](slice *[]T, t T) {
for i, a := range *slice {
if a == t {
*slice = append((*slice)[:i], (*slice)[i+1:]...)
return
}
}
return
}
// ArrRmF removes element from array (unordered,fast)
func ArrRmF[T any](slice *[]T, index uint32) {
(*slice)[index] = (*slice)[len(*slice)-1]
(*slice)[len(*slice)-1] = *new(T)
(*slice) = (*slice)[:len(*slice)-1]
}
// ArrRmS removes element from array (ordered,slow)
func ArrRmS[T any](slice *[]T, index uint32) {
copy((*slice)[index:], (*slice)[index+1:])
(*slice)[len(*slice)-1] = *new(T)
(*slice) = (*slice)[:len(*slice)-1]
}