Debian Codegraph @

pdata/pcommon/generated_stringslice.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 "slices"
12
13 "go.opentelemetry.io/collector/pdata/internal"
14 )
15
16 // StringSlice represents a []string slice.
17 // The instance of StringSlice can be assigned to multiple objects since it's immutable.
18 //
19 // Must use NewStringSlice function to create new instances.
20 // Important: zero-initialized instance is not valid for use.
21 type StringSlice internal.StringSliceWrapper
22
23 func (ms StringSlice) getOrig() *[]string {
24 return internal.GetStringSliceOrig(internal.StringSliceWrapper(ms))
25 }
26
27 func (ms StringSlice) getState() *internal.State {
28 return internal.GetStringSliceState(internal.StringSliceWrapper(ms))
29 }
30
31 // NewStringSlice creates a new empty StringSlice.
32 func NewStringSlice() StringSlice {
33 orig := []string(nil)
34 return StringSlice(internal.NewStringSliceWrapper(&orig, internal.NewState()))
35 }
36
37 // AsRaw returns a copy of the []string slice.
38 func (ms StringSlice) AsRaw() []string {
39 return copyStringSlice(nil, *ms.getOrig())
40 }
41
42 // FromRaw copies raw []string into the slice StringSlice.
43 func (ms StringSlice) FromRaw(val []string) {
44 ms.getState().AssertMutable()
45 *ms.getOrig() = copyStringSlice(*ms.getOrig(), val)
46 }
47
48 // Len returns length of the []string slice value.
49 // Equivalent of len(stringSlice).
50 func (ms StringSlice) Len() int {
51 return len(*ms.getOrig())
52 }
53
54 // At returns an item from particular index.
55 // Equivalent of stringSlice[i].
56 func (ms StringSlice) At(i int) string {
57 return (*ms.getOrig())[i]
58 }
59
60 // All returns an iterator over index-value pairs in the slice.
61 func (ms StringSlice) All() iter.Seq2[int, string] {
62 return func(yield func(int, string) bool) {
63 for i := 0; i < ms.Len(); i++ {
64 if !yield(i, ms.At(i)) {
65 return
66 }
67 }
68 }
69 }
70
71 // SetAt sets string item at particular index.
72 // Equivalent of stringSlice[i] = val
73 func (ms StringSlice) SetAt(i int, val string) {
74 ms.getState().AssertMutable()
75 (*ms.getOrig())[i] = val
76 }
77
78 // EnsureCapacity ensures StringSlice has at least the specified capacity.
79 // 1. If the newCap <= cap, then is no change in capacity.
80 // 2. If the newCap > cap, then the slice capacity will be expanded to the provided value which will be equivalent of:
81 // buf := make([]string, len(stringSlice), newCap)
82 // copy(buf, stringSlice)
83 // stringSlice = buf
84 func (ms StringSlice) EnsureCapacity(newCap int) {
85 ms.getState().AssertMutable()
86 oldCap := cap(*ms.getOrig())
87 if newCap <= oldCap {
88 return
89 }
90
91 newOrig := make([]string, len(*ms.getOrig()), newCap)
92 copy(newOrig, *ms.getOrig())
93 *ms.getOrig() = newOrig
94 }
95
96 // Append appends extra elements to StringSlice.
97 // Equivalent of stringSlice = append(stringSlice, elms...)
98 func (ms StringSlice) Append(elms ...string) {
99 ms.getState().AssertMutable()
100 *ms.getOrig() = append(*ms.getOrig(), elms...)
101 }
102
103 // MoveTo moves all elements from the current slice overriding the destination and
104 // resetting the current instance to its zero value.
105 func (ms StringSlice) MoveTo(dest StringSlice) {
106 ms.getState().AssertMutable()
107 dest.getState().AssertMutable()
108 // If they point to the same data, they are the same, nothing to do.
109 if ms.getOrig() == dest.getOrig() {
110 return
111 }
112 *dest.getOrig() = *ms.getOrig()
113 *ms.getOrig() = nil
114 }
115
116 // MoveAndAppendTo moves all elements from the current slice and appends them to the dest.
117 // The current slice will be cleared.
118 func (ms StringSlice) MoveAndAppendTo(dest StringSlice) {
119 ms.getState().AssertMutable()
120 dest.getState().AssertMutable()
121 if *dest.getOrig() == nil {
122 // We can simply move the entire vector and avoid any allocations.
123 *dest.getOrig() = *ms.getOrig()
124 } else {
125 *dest.getOrig() = append(*dest.getOrig(), *ms.getOrig()...)
126 }
127 *ms.getOrig() = nil
128 }
129
130 // RemoveIf calls f sequentially for each element present in the slice.
131 // If f returns true, the element is removed from the slice.
132 func (ms StringSlice) RemoveIf(f func(string) bool) {
133 ms.getState().AssertMutable()
134 newLen := 0
135 for i := 0; i < len(*ms.getOrig()); i++ {
136 if f((*ms.getOrig())[i]) {
137 continue
138 }
139 if newLen == i {
140 // Nothing to move, element is at the right place.
141 newLen++
142 continue
143 }
144 (*ms.getOrig())[newLen] = (*ms.getOrig())[i]
145 var zero string
146 (*ms.getOrig())[i] = zero
147 newLen++
148 }
149 *ms.getOrig() = (*ms.getOrig())[:newLen]
150 }
151
152 // CopyTo copies all elements from the current slice overriding the destination.
153 func (ms StringSlice) CopyTo(dest StringSlice) {
154 dest.getState().AssertMutable()
155 if ms.getOrig() == dest.getOrig() {
156 return
157 }
158 *dest.getOrig() = copyStringSlice(*dest.getOrig(), *ms.getOrig())
159 }
160
161 // Equal checks equality with another StringSlice
162 func (ms StringSlice) Equal(val StringSlice) bool {
163 return slices.Equal(*ms.getOrig(), *val.getOrig())
164 }
165
166 func copyStringSlice(dst, src []string) []string {
167 return append(dst[:0], src...)
168 }