-
Notifications
You must be signed in to change notification settings - Fork 49
/
transform.go
235 lines (212 loc) · 5 KB
/
transform.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
package stl4go
import "math/rand"
// Copy make a copy of slice a.
//
// Complexity: O(len(a)).
func Copy[T any](a []T) []T {
return append([]T{}, a...)
}
// CopyTo copies all elements in slice a to slice to, return the copied slice.
// if slice to is large enough, no memory allocation occurs.
//
// Complexity: O(len(a)).
func CopyTo[T any](a []T, to []T) []T {
b := append(to[0:0], a...)
return b
}
// Fill fills each element in slice a with new value v.
//
// Complexity: O(len(a)).
func Fill[T any](a []T, v T) {
if len(a) == 0 {
return
}
// Preload the first value into the array/slice.
a[0] = v
// Incrementally duplicate the value into the rest of the container.
// About 2~3 times faster than naive fill.
// https://gist.github.com/taylorza/df2f89d5f9ab3ffd06865062a4cf015d
for j := 1; j < len(a); j *= 2 {
copy(a[j:], a[:j])
}
}
// FillPattern fills elements in slice a with specified pattern.
//
// Complexity: O(len(a)).
func FillPattern[T any](a []T, pattern []T) {
if len(pattern) == 0 {
panic("pattern can't be empty")
}
// Copy the pattern into the start of the container
copy(a, pattern)
// Incrementally duplicate the pattern throughout the container
for j := len(pattern); j < len(a); j *= 2 {
copy(a[j:], a[:j])
}
}
// TransformTo applies the function op to each element in slice a and fill it to slice b,
// return the transformed slice.
// If cap(b) >= len(a), no memory allocation.
//
// Complexity: O(len(a)).
func TransformTo[R any, T any](a []T, op func(T) R, b []R) []R {
b = b[0:0]
for i := range a {
b = append(b, op(a[i]))
}
return b
}
// Transform applies the function op to each element in slice a and set it back to the same place in a.
//
// Complexity: O(len(a)).
func Transform[T any](a []T, op func(T) T) {
for i, v := range a {
a[i] = op(v)
}
}
// TransformCopy applies the function op to each element in slice a and return all the result as a slice.
//
// Complexity: O(len(a)).
func TransformCopy[R any, T any](a []T, op func(T) R) []R {
r := make([]R, len(a))
for i, v := range a {
r[i] = op(v)
}
return r
}
// Replace replaces every element that equals to old with new.
//
// Complexity: O(len(a)).
func Replace[T comparable](a []T, old, new T) {
for i := range a {
if a[i] == old {
a[i] = new
}
}
}
// ReplaceIf replaces every element that make preq returns true with new.
//
// Complexity: O(len(a)).
func ReplaceIf[T any](a []T, pred func(v T) bool, new T) {
for i := range a {
if pred(a[i]) {
a[i] = new
}
}
}
// Unique remove adjacent repeated elements from the input slice.
// return the processed slice with new length.
//
// Complexity: O(len(a)).
func Unique[T comparable](a []T) []T {
if len(a) == 0 {
return a
}
i := 1
prev := a[0]
for _, v := range a[1:] {
if v != prev {
a[i] = v
i++
prev = v
}
}
return a[:i]
}
// UniqueCopy remove adjacent repeated elements from the input slice.
// return the result slice, and the input slice is kept unchanged.
//
// Complexity: O(len(a)).
func UniqueCopy[T comparable](a []T) []T {
var r []T
if len(a) == 0 {
return r
}
for _, v := range a {
if len(r) > 0 && r[len(r)-1] == v {
continue
}
r = append(r, v)
}
return r
}
// Remove remove the elements which equals to x from the input slice.
// return the processed slice with new length.
//
// Complexity: O(len(a)).
func Remove[T comparable](a []T, x T) []T {
j := 0
for _, v := range a {
if v != x {
a[j] = v
j++
}
}
return a[:j]
}
// RemoveCopy remove all elements which equals to x from the input slice.
// return a new slice with processed results. The input slice is kept unchanged.
//
// Complexity: O(len(a)).
func RemoveCopy[T comparable](a []T, x T) []T {
r := make([]T, 0, len(a))
for _, v := range a {
if v != x {
r = append(r, v)
}
}
return r
}
// RemoveIf remove each element which make cond(x) returns true from the input slice,
// copy other elements to a new slice and return it.
//
// Complexity: O(len(a)).
func RemoveIf[T any](a []T, cond func(T) bool) []T {
j := 0
for _, v := range a {
if !cond(v) {
a[j] = v
j++
}
}
return a[:j]
}
// RemoveIfCopy drops each element which make cond(x) returns true from the input slice,
// copy other elements to a new slice and return it. The input slice is kept unchanged.
//
// Complexity: O(len(a)).
func RemoveIfCopy[T any](a []T, cond func(T) bool) []T {
r := make([]T, 0, len(a))
for _, v := range a {
if !cond(v) {
r = append(r, v)
}
}
return r
}
// Shuffle pseudo-randomizes the order of elements.
//
// Complexity: O(len(a)).
func Shuffle[T any](a []T) {
rand.Shuffle(len(a), func(i, j int) {
a[i], a[j] = a[j], a[i]
})
}
// Reverse reverses the order of the elements in the slice a.
//
// Complexity: O(len(a)).
func Reverse[T any](a []T) {
for i, j := 0, len(a)-1; i < j; i, j = i+1, j-1 {
a[i], a[j] = a[j], a[i]
}
}
// ReverseCopy returns a reversed copy of slice a.
//
// Complexity: O(len(a)).
func ReverseCopy[T any](a []T) []T {
b := make([]T, 0, len(a))
for i := len(a) - 1; i >= 0; i-- {
b = append(b, a[i])
}
return b
}