Skip to content

Commit a4a8dfd

Browse files
authored
[pdata/pcommon] Add RemoveIf method to primitive slice types (#14028)
Add `RemoveIf` method to primitive slice types (StringSlice, Int64Slice, UInt64Slice, Float64Slice, Int32Slice, ByteSlice) implemented consistently with other slices
1 parent c854264 commit a4a8dfd

15 files changed

+356
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
7+
component: pkg/pdata
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: "Add `RemoveIf` method to primitive slice types (StringSlice, Int64Slice, UInt64Slice, Float64Slice, Int32Slice, ByteSlice)"
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [14027]
14+
15+
# Optional: The change log or logs in which this entry should be included.
16+
# e.g. '[user]' or '[user, api]'
17+
# Include 'user' if the change is relevant to end users.
18+
# Include 'api' if there is a change to a library API.
19+
# Default: '[user]'
20+
change_logs: [api]

internal/cmd/pdatagen/internal/pdata/templates/primitive_slice.go.tmpl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,28 @@ func (ms {{ .structName }}) MoveAndAppendTo(dest {{ .structName }}) {
126126
*ms.getOrig() = nil
127127
}
128128

129+
// RemoveIf calls f sequentially for each element present in the slice.
130+
// If f returns true, the element is removed from the slice.
131+
func (ms {{ .structName }}) RemoveIf(f func({{ .itemType }}) bool) {
132+
ms.getState().AssertMutable()
133+
newLen := 0
134+
for i := 0; i < len(*ms.getOrig()); i++ {
135+
if f((*ms.getOrig())[i]) {
136+
continue
137+
}
138+
if newLen == i {
139+
// Nothing to move, element is at the right place.
140+
newLen++
141+
continue
142+
}
143+
(*ms.getOrig())[newLen] = (*ms.getOrig())[i]
144+
var zero {{ .itemType }}
145+
(*ms.getOrig())[i] = zero
146+
newLen++
147+
}
148+
*ms.getOrig() = (*ms.getOrig())[:newLen]
149+
}
150+
129151
// CopyTo copies all elements from the current slice overriding the destination.
130152
func (ms {{ .structName }}) CopyTo(dest {{ .structName }}) {
131153
dest.getState().AssertMutable()

internal/cmd/pdatagen/internal/pdata/templates/primitive_slice_test.go.tmpl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,32 @@ func Test{{ .structName }}MoveAndAppendTo(t *testing.T) {
153153
assert.Equal(t, ms2.Len(), 6)
154154
}
155155

156+
func Test{{ .structName }}RemoveIf(t *testing.T) {
157+
emptySlice := New{{ .structName }}()
158+
emptySlice.RemoveIf(func(el {{ .itemType }}) bool {
159+
t.Fail()
160+
return false
161+
})
162+
163+
ms := New{{ .structName }}()
164+
ms.FromRaw([]{{ .itemType }}{ {{ .testOrigVal }} })
165+
pos := 0
166+
ms.RemoveIf(func(el {{ .itemType }}) bool {
167+
pos++
168+
return pos%2 == 1
169+
})
170+
assert.Equal(t, pos/2, ms.Len())
171+
}
172+
173+
func Test{{ .structName }}RemoveIfAll(t *testing.T) {
174+
ms := New{{ .structName }}()
175+
ms.FromRaw([]{{ .itemType }}{ {{ .testOrigVal }} })
176+
ms.RemoveIf(func(el {{ .itemType }}) bool {
177+
return true
178+
})
179+
assert.Equal(t, 0, ms.Len())
180+
}
181+
156182
func Test{{ .structName }}Equal(t *testing.T) {
157183
ms := New{{ .structName }}()
158184
ms2 := New{{ .structName }}()

pdata/pcommon/generated_byteslice.go

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/pcommon/generated_byteslice_test.go

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/pcommon/generated_float64slice.go

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/pcommon/generated_float64slice_test.go

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/pcommon/generated_int32slice.go

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/pcommon/generated_int32slice_test.go

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/pcommon/generated_int64slice.go

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)