-
Notifications
You must be signed in to change notification settings - Fork 675
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Deregister
to metrics.MultiGatherer
interface
#3498
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand this is O(1) and not O(n) like the regular There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This definitely isn't performance critical... But I also feel like I've re-implemented this same code like 5 times... So I feel like it is a reasonable helper to add |
||
// 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] | ||
} |
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) | ||
}) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(No action required) Maybe use a name other than
require
to avoid shadowing the package?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do this shadowing pretty much everywhere... I feel like the shadowing here is almost beneficial, as there is no reason to ever use any of the functions in the package after this point.