Debian Codegraph @

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