mirror of
https://github.com/coder/coder.git
synced 2025-07-06 15:41:45 +00:00
* feat: Support generating generics in interfaces * Switch struct to a template * Support generics in apitypings
50 lines
1.1 KiB
Go
50 lines
1.1 KiB
Go
package slice
|
|
|
|
func ContainsCompare[T any](haystack []T, needle T, equal func(a, b T) bool) bool {
|
|
for _, hay := range haystack {
|
|
if equal(needle, hay) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func Contains[T comparable](haystack []T, needle T) bool {
|
|
return ContainsCompare(haystack, needle, func(a, b T) bool {
|
|
return a == b
|
|
})
|
|
}
|
|
|
|
// Overlap returns if the 2 sets have any overlap (element(s) in common)
|
|
func Overlap[T comparable](a []T, b []T) bool {
|
|
return OverlapCompare(a, b, func(a, b T) bool {
|
|
return a == b
|
|
})
|
|
}
|
|
|
|
// Unique returns a new slice with all duplicate elements removed.
|
|
// This is a slow function on large lists.
|
|
// TODO: Sort elements and implement a faster search algorithm if we
|
|
// really start to use this.
|
|
func Unique[T comparable](a []T) []T {
|
|
cpy := make([]T, 0, len(a))
|
|
for _, v := range a {
|
|
v := v
|
|
if !Contains(cpy, v) {
|
|
cpy = append(cpy, v)
|
|
}
|
|
}
|
|
return cpy
|
|
}
|
|
|
|
func OverlapCompare[T any](a []T, b []T, equal func(a, b T) bool) bool {
|
|
// For each element in b, if at least 1 is contained in 'a',
|
|
// return true.
|
|
for _, element := range b {
|
|
if ContainsCompare(a, element, equal) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|