-
Notifications
You must be signed in to change notification settings - Fork 1
/
slice_uint8.go
273 lines (233 loc) · 5.83 KB
/
slice_uint8.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// Code generated by go generate; DO NOT EDIT.
// This file was generated by robots at 2020-04-08 01:37:45.854513 +0000 UTC.
package golist
import (
"fmt"
"math/rand"
"sort"
"time"
)
// SliceUint8 is a slice of type uint8.
type SliceUint8 struct {
data []uint8
}
// NewSliceUint8 returns a pointer to a new SliceUint8 initialized with the specified elements.
func NewSliceUint8(elems ...uint8) *SliceUint8 {
s := new(SliceUint8)
s.data = make([]uint8, len(elems))
for i := 0; i < len(elems); i++ {
s.data[i] = elems[i]
}
return s
}
// Append adds the elements to the end of SliceUint8.
func (s *SliceUint8) Append(elems ...uint8) *SliceUint8 {
if s == nil {
return nil
}
s.data = append(s.data, elems...)
return s
}
// Prepend adds the elements to the beginning of SliceUint8.
func (s *SliceUint8) Prepend(elems ...uint8) *SliceUint8 {
if s == nil {
return nil
}
s.data = append(elems, s.data...)
return s
}
// At returns the element in SliceUint8 at the specified index.
func (s *SliceUint8) At(index int) uint8 {
if s.data == nil || len(s.data) == 0 {
panic("SliceUint8 does not contain any elements")
}
if index >= len(s.data) || index < 0 {
panic(fmt.Sprintf("index %d outside the range of SliceUint8", index))
}
return s.data[index]
}
// Set sets the element of SliceUint8 at the specified index.
func (s *SliceUint8) Set(index int, elem uint8) *SliceUint8 {
if s == nil {
return nil
}
s.data[index] = elem
return s
}
// Insert inserts the elements into SliceUint8 at the specified index.
func (s *SliceUint8) Insert(index int, elems ...uint8) *SliceUint8 {
if s == nil {
return nil
}
// Grow the slice by the number of elements (using the zero value)
var zero uint8
for i := 0; i < len(elems); i++ {
s.data = append(s.data, zero)
}
// Use copy to move the upper part of the slice out of the way and open a hole.
copy(s.data[index+len(elems):], s.data[index:])
// Store the new values
for i := 0; i < len(elems); i++ {
s.data[index+i] = elems[i]
}
// Return the result.
return s
}
// Remove removes the element from SliceUint8 at the specified index.
func (s *SliceUint8) Remove(index int) *SliceUint8 {
if s == nil {
return nil
}
s.data = append(s.data[:index], s.data[index+1:]...)
return s
}
// Filter removes elements from SliceUint8 that do not satisfy the filter function.
func (s *SliceUint8) Filter(fn func(elem uint8) bool) *SliceUint8 {
if s == nil {
return nil
}
data := s.data[:0]
for _, elem := range s.data {
if fn(elem) {
data = append(data, elem)
}
}
s.data = data
return s
}
// Transform modifies each element of SliceUint8 according to the specified function.
func (s *SliceUint8) Transform(fn func(elem uint8) uint8) *SliceUint8 {
if s == nil {
return nil
}
for i, elem := range s.data {
s.data[i] = fn(elem)
}
return s
}
// Unique modifies SliceUint8 to keep only the first occurrence of each element (removing any duplicates).
func (s *SliceUint8) Unique() *SliceUint8 {
if s == nil {
return nil
}
seen := make(map[uint8]struct{})
data := s.data[:0]
for _, elem := range s.data {
if _, ok := seen[elem]; !ok {
data = append(data, elem)
seen[elem] = struct{}{}
}
}
s.data = data
return s
}
// Reverse reverses the order of the elements of SliceUint8.
func (s *SliceUint8) Reverse() *SliceUint8 {
if s == nil {
return nil
}
for i := len(s.data)/2 - 1; i >= 0; i-- {
opp := len(s.data) - 1 - i
s.Swap(i, opp)
}
return s
}
// Shuffle randomly shuffles the order of the elements in SliceUint8.
func (s *SliceUint8) Shuffle(seed int64) *SliceUint8 {
if s == nil {
return nil
}
if seed == 0 {
seed = time.Now().UnixNano()
}
r := rand.New(rand.NewSource(seed))
r.Shuffle(s.Count(), s.Swap)
return s
}
// Data returns the raw elements of SliceUint8.
func (s *SliceUint8) Data() []uint8 {
if s == nil {
return nil
}
return s.data
}
// Count returns the number of elements in SliceUint8.
func (s *SliceUint8) Count() int {
return len(s.data)
}
// Len returns the number of elements in SliceUint8 (alias for Count).
func (s *SliceUint8) Len() int {
return s.Count()
}
// Swap swaps the elements in SliceUint8 specified by the indices i and j.
func (s *SliceUint8) Swap(i, j int) {
s.data[i], s.data[j] = s.data[j], s.data[i]
}
// Less returns true if the SliceUint8 element at index i is less than the element at index j.
func (s *SliceUint8) Less(i, j int) bool {
return s.data[i] < s.data[j]
}
// Sort sorts the elements of SliceUint8 in increasing order.
func (s *SliceUint8) Sort() *SliceUint8 {
if s == nil {
return nil
}
sort.Sort(s)
return s
}
// Min returns the smallest (least ordered) element in SliceUint8.
func (s *SliceUint8) Min() uint8 {
if s.data == nil || len(s.data) == 0 {
panic("SliceUint8 does not contain any elements")
}
// start with the first value
min := s.data[0]
for _, elem := range s.data[1:] {
if elem < min {
min = elem
}
}
return min
}
// Max returns the largest (greatest ordered) element in SliceUint8.
func (s *SliceUint8) Max() uint8 {
if s.data == nil || len(s.data) == 0 {
panic("SliceUint8 does not contain any elements")
}
// start with the first value
max := s.data[0]
for _, elem := range s.data[1:] {
if elem > max {
max = elem
}
}
return max
}
// Clone performs a deep copy of SliceUint8 and returns it
func (s *SliceUint8) Clone() *SliceUint8 {
if s == nil {
return nil
}
s2 := new(SliceUint8)
s2.data = make([]uint8, len(s.data))
copy(s2.data, s.data)
return s2
}
// Equal returns true if the SliceUint8 is logically equivalent to the specified SliceUint8.
func (s *SliceUint8) Equal(s2 *SliceUint8) bool {
if s == s2 {
return true
}
if s == nil || s2 == nil {
return false // has to be false because s == s2 tested earlier
}
if len(s.data) != len(s2.data) {
return false
}
for i, elem := range s.data {
if elem != s2.data[i] {
return false
}
}
return true
}