-
Notifications
You must be signed in to change notification settings - Fork 682
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f212c3e
commit acb3d7d
Showing
6 changed files
with
197 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package utils | ||
|
||
// DeleteIndex moves the last element in the slice to index [i] and shrinks the | ||
// size of the slice by 1. | ||
// | ||
// This is an O(1) operation that allows the removal of an element from a slice | ||
// when the order of the slice is not important. | ||
// | ||
// If [i] is out of bounds, this function will panic. | ||
func DeleteIndex[S ~[]E, E any](s S, i int) S { | ||
newSize := len(s) - 1 | ||
s[i] = s[newSize] | ||
s[newSize] = Zero[E]() | ||
return s[:newSize] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package utils | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestDeleteIndex(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
s []int | ||
i int | ||
expected []int | ||
}{ | ||
{ | ||
name: "delete only element", | ||
s: []int{0}, | ||
i: 0, | ||
expected: []int{}, | ||
}, | ||
{ | ||
name: "delete first element", | ||
s: []int{0, 1}, | ||
i: 0, | ||
expected: []int{1}, | ||
}, | ||
{ | ||
name: "delete middle element", | ||
s: []int{0, 1, 2, 3}, | ||
i: 1, | ||
expected: []int{0, 3, 2}, | ||
}, | ||
{ | ||
name: "delete last element", | ||
s: []int{0, 1, 2, 3}, | ||
i: 3, | ||
expected: []int{0, 1, 2}, | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
s := DeleteIndex(test.s, test.i) | ||
require.Equal(t, test.expected, s) | ||
}) | ||
} | ||
} |