kos/util/arrays/array.go

44 lines
697 B
Go
Raw Normal View History

2023-08-22 16:11:34 +08:00
package arrays
func IndexOf[T comparable](a T, vs []T) int {
for i, v := range vs {
if v == a {
return i
}
}
return -1
}
func Exists[T comparable](a T, vs []T) bool {
return IndexOf(a, vs) > -1
}
func Fill[T any](startIndex int, num uint, value T) map[int]T {
m := make(map[int]T)
var i uint
for i = 0; i < num; i++ {
m[startIndex] = value
startIndex++
}
return m
}
func Reverse[T comparable](s []T) []T {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return s
}
func Merge[T comparable](ss ...[]T) []T {
n := 0
for _, v := range ss {
n += len(v)
}
s := make([]T, 0, n)
for _, v := range ss {
s = append(s, v...)
}
return s
}