Debian Codegraph @

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