github.com/grafana/pyroscope@v1.18.0/pkg/slices/slices.go (about)

     1  package slices
     2  
     3  import (
     4  	"slices"
     5  )
     6  
     7  // RemoveInPlace removes all elements from a slice that match the given predicate.
     8  // Does not allocate a new slice.
     9  func RemoveInPlace[T any](collection []T, predicate func(T, int) bool) []T {
    10  	i := 0
    11  	for j, x := range collection {
    12  		if !predicate(x, j) {
    13  			collection[j], collection[i] = collection[i], collection[j]
    14  			i++
    15  		}
    16  	}
    17  	return collection[:i]
    18  }
    19  
    20  func Reverse[S ~[]E, E any](s S) {
    21  	for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
    22  		s[i], s[j] = s[j], s[i]
    23  	}
    24  }
    25  
    26  func Clear[S ~[]E, E any](s S) {
    27  	var zero E
    28  	for i := range s {
    29  		s[i] = zero
    30  	}
    31  }
    32  
    33  func GrowLen[S ~[]E, E any](s S, n int) S {
    34  	return slices.Grow(s[:0], n)[:n]
    35  }