Export ReverseSliceInPlace and ReverseSlice
... and add tests. Test: TestReverseSliceInPlace, TestReverseSlice Change-Id: I70f811207fdb2af891f7ef3fe19cbe5c7276135a
This commit is contained in:
@@ -338,15 +338,19 @@ func firstUniqueMap[T comparable](in []T) []T {
|
||||
return in[0:writeIndex]
|
||||
}
|
||||
|
||||
// reverseSliceInPlace reverses the elements of a slice in place.
|
||||
func reverseSliceInPlace[T any](in []T) {
|
||||
// ReverseSliceInPlace reverses the elements of a slice in place and returns it.
|
||||
func ReverseSliceInPlace[T any](in []T) []T {
|
||||
for i, j := 0, len(in)-1; i < j; i, j = i+1, j-1 {
|
||||
in[i], in[j] = in[j], in[i]
|
||||
}
|
||||
return in
|
||||
}
|
||||
|
||||
// reverseSlice returns a copy of a slice in reverse order.
|
||||
func reverseSlice[T any](in []T) []T {
|
||||
// ReverseSlice returns a copy of a slice in reverse order.
|
||||
func ReverseSlice[T any](in []T) []T {
|
||||
if in == nil {
|
||||
return in
|
||||
}
|
||||
out := make([]T, len(in))
|
||||
for i := 0; i < len(in); i++ {
|
||||
out[i] = in[len(in)-1-i]
|
||||
|
Reference in New Issue
Block a user