| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
// Code generated by "internal/cmd/pdatagen/main.go". DO NOT EDIT. |
| 5 |
// To regenerate this file run "make genpdata". |
| 6 |
|
| 7 |
package ptrace |
| 8 |
|
| 9 |
import ( |
| 10 |
"iter" |
| 11 |
"sort" |
| 12 |
|
| 13 |
"go.opentelemetry.io/collector/pdata/internal" |
| 14 |
) |
| 15 |
|
| 16 |
// ResourceSpansSlice logically represents a slice of ResourceSpans. |
| 17 |
// |
| 18 |
// This is a reference type. If passed by value and callee modifies it, the |
| 19 |
// caller will see the modification. |
| 20 |
// |
| 21 |
// Must use NewResourceSpansSlice function to create new instances. |
| 22 |
// Important: zero-initialized instance is not valid for use. |
| 23 |
type ResourceSpansSlice struct { |
| 24 |
orig *[]*internal.ResourceSpans |
| 25 |
state *internal.State |
| 26 |
} |
| 27 |
|
| 28 |
func newResourceSpansSlice(orig *[]*internal.ResourceSpans, state *internal.State) ResourceSpansSlice { |
| 29 |
return ResourceSpansSlice{orig: orig, state: state} |
| 30 |
} |
| 31 |
|
| 32 |
// NewResourceSpansSlice creates a ResourceSpansSliceWrapper with 0 elements. |
| 33 |
// Can use "EnsureCapacity" to initialize with a given capacity. |
| 34 |
func NewResourceSpansSlice() ResourceSpansSlice { |
| 35 |
orig := []*internal.ResourceSpans(nil) |
| 36 |
return newResourceSpansSlice(&orig, internal.NewState()) |
| 37 |
} |
| 38 |
|
| 39 |
// Len returns the number of elements in the slice. |
| 40 |
// |
| 41 |
// Returns "0" for a newly instance created with "NewResourceSpansSlice()". |
| 42 |
func (es ResourceSpansSlice) Len() int { |
| 43 |
return len(*es.orig) |
| 44 |
} |
| 45 |
|
| 46 |
// At returns the element at the given index. |
| 47 |
// |
| 48 |
// This function is used mostly for iterating over all the values in the slice: |
| 49 |
// |
| 50 |
// for i := 0; i < es.Len(); i++ { |
| 51 |
// e := es.At(i) |
| 52 |
// ... // Do something with the element |
| 53 |
// } |
| 54 |
func (es ResourceSpansSlice) At(i int) ResourceSpans { |
| 55 |
return newResourceSpans((*es.orig)[i], es.state) |
| 56 |
} |
| 57 |
|
| 58 |
// All returns an iterator over index-value pairs in the slice. |
| 59 |
// |
| 60 |
// for i, v := range es.All() { |
| 61 |
// ... // Do something with index-value pair |
| 62 |
// } |
| 63 |
func (es ResourceSpansSlice) All() iter.Seq2[int, ResourceSpans] { |
| 64 |
return func(yield func(int, ResourceSpans) bool) { |
| 65 |
for i := 0; i < es.Len(); i++ { |
| 66 |
if !yield(i, es.At(i)) { |
| 67 |
return |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
// EnsureCapacity is an operation that ensures the slice has at least the specified capacity. |
| 74 |
// 1. If the newCap <= cap then no change in capacity. |
| 75 |
// 2. If the newCap > cap then the slice capacity will be expanded to equal newCap. |
| 76 |
// |
| 77 |
// Here is how a new ResourceSpansSlice can be initialized: |
| 78 |
// |
| 79 |
// es := NewResourceSpansSlice() |
| 80 |
// es.EnsureCapacity(4) |
| 81 |
// for i := 0; i < 4; i++ { |
| 82 |
// e := es.AppendEmpty() |
| 83 |
// // Here should set all the values for e. |
| 84 |
// } |
| 85 |
func (es ResourceSpansSlice) EnsureCapacity(newCap int) { |
| 86 |
es.state.AssertMutable() |
| 87 |
oldCap := cap(*es.orig) |
| 88 |
if newCap <= oldCap { |
| 89 |
return |
| 90 |
} |
| 91 |
|
| 92 |
newOrig := make([]*internal.ResourceSpans, len(*es.orig), newCap) |
| 93 |
copy(newOrig, *es.orig) |
| 94 |
*es.orig = newOrig |
| 95 |
} |
| 96 |
|
| 97 |
// AppendEmpty will append to the end of the slice an empty ResourceSpans. |
| 98 |
// It returns the newly added ResourceSpans. |
| 99 |
func (es ResourceSpansSlice) AppendEmpty() ResourceSpans { |
| 100 |
es.state.AssertMutable() |
| 101 |
*es.orig = append(*es.orig, internal.NewResourceSpans()) |
| 102 |
return es.At(es.Len() - 1) |
| 103 |
} |
| 104 |
|
| 105 |
// MoveAndAppendTo moves all elements from the current slice and appends them to the dest. |
| 106 |
// The current slice will be cleared. |
| 107 |
func (es ResourceSpansSlice) MoveAndAppendTo(dest ResourceSpansSlice) { |
| 108 |
es.state.AssertMutable() |
| 109 |
dest.state.AssertMutable() |
| 110 |
// If they point to the same data, they are the same, nothing to do. |
| 111 |
if es.orig == dest.orig { |
| 112 |
return |
| 113 |
} |
| 114 |
if *dest.orig == nil { |
| 115 |
// We can simply move the entire vector and avoid any allocations. |
| 116 |
*dest.orig = *es.orig |
| 117 |
} else { |
| 118 |
*dest.orig = append(*dest.orig, *es.orig...) |
| 119 |
} |
| 120 |
*es.orig = nil |
| 121 |
} |
| 122 |
|
| 123 |
// RemoveIf calls f sequentially for each element present in the slice. |
| 124 |
// If f returns true, the element is removed from the slice. |
| 125 |
func (es ResourceSpansSlice) RemoveIf(f func(ResourceSpans) bool) { |
| 126 |
es.state.AssertMutable() |
| 127 |
newLen := 0 |
| 128 |
for i := 0; i < len(*es.orig); i++ { |
| 129 |
if f(es.At(i)) { |
| 130 |
internal.DeleteResourceSpans((*es.orig)[i], true) |
| 131 |
(*es.orig)[i] = nil |
| 132 |
|
| 133 |
continue |
| 134 |
} |
| 135 |
if newLen == i { |
| 136 |
// Nothing to move, element is at the right place. |
| 137 |
newLen++ |
| 138 |
continue |
| 139 |
} |
| 140 |
(*es.orig)[newLen] = (*es.orig)[i] |
| 141 |
// Cannot delete here since we just move the data(or pointer to data) to a different position in the slice. |
| 142 |
(*es.orig)[i] = nil |
| 143 |
newLen++ |
| 144 |
} |
| 145 |
*es.orig = (*es.orig)[:newLen] |
| 146 |
} |
| 147 |
|
| 148 |
// CopyTo copies all elements from the current slice overriding the destination. |
| 149 |
func (es ResourceSpansSlice) CopyTo(dest ResourceSpansSlice) { |
| 150 |
dest.state.AssertMutable() |
| 151 |
if es.orig == dest.orig { |
| 152 |
return |
| 153 |
} |
| 154 |
*dest.orig = internal.CopyResourceSpansPtrSlice(*dest.orig, *es.orig) |
| 155 |
} |
| 156 |
|
| 157 |
// Sort sorts the ResourceSpans elements within ResourceSpansSlice given the |
| 158 |
// provided less function so that two instances of ResourceSpansSlice |
| 159 |
// can be compared. |
| 160 |
func (es ResourceSpansSlice) Sort(less func(a, b ResourceSpans) bool) { |
| 161 |
es.state.AssertMutable() |
| 162 |
sort.SliceStable(*es.orig, func(i, j int) bool { return less(es.At(i), es.At(j)) }) |
| 163 |
} |