Skip to content

Commit

Permalink
sliceop: generalize Take and Find signatures
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastien Binet <binet@cern.ch>
  • Loading branch information
sbinet committed Oct 3, 2022
1 parent bfaadd2 commit aad018e
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions sliceop/sliceop.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func Map[T, U any](dst []U, src []T, f func(v T) U) []U {
// Find creates a slice with all indices corresponding to elements for which f(x) is true.
// Find uses dst as work buffer, storing indices at the start of the slice.
// Find clears dst if a slice is passed, and allocates a new slice if dst is nil.
func Find[T any](dst []int, src []T, f func(v T) bool) []int {
func Find[S ~[]E, E any](dst []int, src S, f func(v E) bool) []int {

if dst == nil {
dst = make([]int, 0, len(src))
Expand All @@ -80,14 +80,14 @@ func Find[T any](dst []int, src []T, f func(v T) bool) []int {
// Take will panic if indices is not sorted or has duplicates.
// Take will panic if length of indices is larger than length of src.
// Take will panic if length of indices is different from length of dst.
func Take[T any](dst, src []T, indices []int) []T {
func Take[S ~[]E, E any](dst, src S, indices []int) S {

if len(indices) > len(src) {
panic(errLength)
}

if dst == nil {
dst = make([]T, len(indices))
dst = make(S, len(indices))
}

if len(dst) != len(indices) {
Expand Down

0 comments on commit aad018e

Please sign in to comment.