Debian Codegraph @

pdata/xpdata/entity/generated_entityrefslice.go

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