Debian Codegraph @

pdata/pprofile/generated_stackslice.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 pprofile
8
9 import (
10 "iter"
11 "sort"
12
13 "go.opentelemetry.io/collector/pdata/internal"
14 )
15
16 // StackSlice logically represents a slice of Stack.
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 NewStackSlice function to create new instances.
22 // Important: zero-initialized instance is not valid for use.
23 type StackSlice struct {
24 orig *[]*internal.Stack
25 state *internal.State
26 }
27
28 func newStackSlice(orig *[]*internal.Stack, state *internal.State) StackSlice {
29 return StackSlice{orig: orig, state: state}
30 }
31
32 // NewStackSlice creates a StackSliceWrapper with 0 elements.
33 // Can use "EnsureCapacity" to initialize with a given capacity.
34 func NewStackSlice() StackSlice {
35 orig := []*internal.Stack(nil)
36 return newStackSlice(&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 "NewStackSlice()".
42 func (es StackSlice) 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 StackSlice) At(i int) Stack {
55 return newStack((*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 StackSlice) All() iter.Seq2[int, Stack] {
64 return func(yield func(int, Stack) 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 StackSlice can be initialized:
78 //
79 // es := NewStackSlice()
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 StackSlice) EnsureCapacity(newCap int) {
86 es.state.AssertMutable()
87 oldCap := cap(*es.orig)
88 if newCap <= oldCap {
89 return
90 }
91
92 newOrig := make([]*internal.Stack, 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 Stack.
98 // It returns the newly added Stack.
99 func (es StackSlice) AppendEmpty() Stack {
100 es.state.AssertMutable()
101 *es.orig = append(*es.orig, internal.NewStack())
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 StackSlice) MoveAndAppendTo(dest StackSlice) {
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 StackSlice) RemoveIf(f func(Stack) 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.DeleteStack((*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 StackSlice) CopyTo(dest StackSlice) {
150 dest.state.AssertMutable()
151 if es.orig == dest.orig {
152 return
153 }
154 *dest.orig = internal.CopyStackPtrSlice(*dest.orig, *es.orig)
155 }
156
157 // Sort sorts the Stack elements within StackSlice given the
158 // provided less function so that two instances of StackSlice
159 // can be compared.
160 func (es StackSlice) Sort(less func(a, b Stack) bool) {
161 es.state.AssertMutable()
162 sort.SliceStable(*es.orig, func(i, j int) bool { return less(es.At(i), es.At(j)) })
163 }