-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
358 lines (297 loc) · 7.62 KB
/
list.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package handy
import (
"github.com/hsldymq/goiter"
"iter"
"math/rand"
"slices"
"time"
)
func NewList[T any](elems ...T) *List[T] {
l := &List[T]{
elems: make([]T, 0, len(elems)),
comparable: isTypeComparable[T](),
}
if len(elems) > 0 {
l.Add(elems...)
}
return l
}
func NewListFromIter[T any](it iter.Seq[T]) *List[T] {
l := NewList[T]()
for each := range it {
l.Add(each)
}
return l
}
type List[T any] struct {
elems []T
comparable bool
}
func (l *List[T]) Add(items ...T) {
l.elems = slices.Concat(l.elems, items)
}
func (l *List[T]) Merge(tl ...*List[T]) {
if len(tl) == 0 {
return
}
s := make([][]T, 0, len(tl)+1)
s = append(s, l.elems)
for _, each := range tl {
s = append(s, each.elems)
}
l.elems = slices.Concat(s...)
}
func (l *List[T]) MergeSlices(sl ...[]T) {
if len(sl) == 0 {
return
}
s := make([][]T, 0, len(sl)+1)
s = append(s, l.elems)
s = append(s, sl...)
l.elems = slices.Concat(s...)
}
func (l *List[T]) Remove(item T) {
idx := l.IndexOf(item)
if idx >= 0 {
l.RemoveAt(idx)
}
}
func (l *List[T]) RemoveAt(i int) (T, bool) {
idx, valid := l.actualIndex(i)
if !valid {
return zVal[T](), false
}
lastIdx := len(l.elems) - 1
elem := l.elems[idx]
if idx == 0 {
l.elems = l.elems[1:]
} else if idx == lastIdx {
l.elems = l.elems[:lastIdx]
} else {
copy(l.elems[idx:], l.elems[idx+1:])
l.elems = l.elems[:lastIdx]
}
return elem, true
}
func (l *List[T]) Pop() (T, bool) {
return l.RemoveAt(-1)
}
func (l *List[T]) Shift() (T, bool) {
return l.RemoveAt(0)
}
func (l *List[T]) Get(idx int) (T, bool) {
if idx >= len(l.elems) || idx < 0 {
return zVal[T](), false
}
return l.elems[idx], true
}
func (l *List[T]) Find(predicate func(T) bool) (T, bool) {
for _, each := range l.elems {
if predicate(each) {
return each, true
}
}
return zVal[T](), false
}
func (l *List[T]) FindOrDefault(predicate func(T) bool, def T) T {
v, found := l.Find(predicate)
if !found {
return def
}
return v
}
func (l *List[T]) FindLast(predicate func(T) bool) (T, bool) {
for i := len(l.elems) - 1; i >= 0; i-- {
each := l.elems[i]
if predicate(each) {
return each, true
}
}
return zVal[T](), false
}
func (l *List[T]) FindLastOrDefault(predicate func(T) bool, def T) T {
v, found := l.FindLast(predicate)
if !found {
return def
}
return v
}
func (l *List[T]) IndexOf(item T) int {
return l.indexOf(item, 0)
}
func (l *List[T]) Contains(item T) bool {
return l.IndexOf(item) >= 0
}
func (l *List[T]) Sort(cmp func(T, T) int) {
slices.SortFunc(l.elems, cmp)
}
func (l *List[T]) StableSort(cmp func(T, T) int) {
slices.SortStableFunc(l.elems, cmp)
}
func (l *List[T]) FilterSelf(predicate func(T) bool) {
newElems := make([]T, 0, len(l.elems))
for _, each := range l.elems {
if predicate(each) {
newElems = append(newElems, each)
}
}
l.elems = newElems
}
func (l *List[T]) FilterTo(predicate func(T) bool) *List[T] {
newElems := make([]T, 0, len(l.elems))
for _, each := range l.elems {
if predicate(each) {
newElems = append(newElems, each)
}
}
return NewList(newElems...)
}
func (l *List[T]) ShuffleSelf() {
count := len(l.elems)
if count < 2 {
return
}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
// Fisher–Yates shuffle
for i := count - 1; i > 0; i-- {
idx := r.Intn(i + 1)
l.elems[idx], l.elems[i] = l.elems[i], l.elems[idx]
}
}
func (l *List[T]) ShuffleTo() *List[T] {
newList := l.Clone()
newList.ShuffleSelf()
return newList
}
func (l *List[T]) Clone() *List[T] {
clonedList := NewList[T]()
clonedList.elems = make([]T, l.Count())
copy(clonedList.elems, l.elems)
return clonedList
}
func (l *List[T]) Clear() {
l.elems = l.elems[:0]
}
func (l *List[T]) IterBackward() iter.Seq[T] {
return goiter.SliceSourceElems(func() []T { return l.elems }, true).Seq()
}
func (l *List[T]) Iter() iter.Seq[T] {
return goiter.SliceSourceElems(func() []T { return l.elems }).Seq()
}
func (l *List[T]) Count() int {
return len(l.elems)
}
func (l *List[T]) Any(predicate func(T) bool) bool {
for _, each := range l.elems {
if predicate(each) {
return true
}
}
return false
}
func (l *List[T]) All(predicate func(T) bool) bool {
for _, each := range l.elems {
if !predicate(each) {
return false
}
}
return true
}
func (l *List[T]) Filter(predicate func(T) bool) Enumerable[T] {
return filter(l, predicate)
}
func (l *List[T]) Take(n int) Enumerable[T] {
return NewEnumerator(goiter.Take(l.Iter(), n))
}
func (l *List[T]) TakeLast(n int) Enumerable[T] {
iterator := func(yield func(T) bool) {
if n <= 0 {
return
}
elems := l.elems
startIdx := max(len(elems)-n, 0)
for i := 0; i < n && startIdx+i < len(elems); i++ {
if !yield(elems[startIdx+i]) {
return
}
}
}
return NewEnumerator(iterator)
}
func (l *List[T]) Skip(n int) Enumerable[T] {
return NewEnumerator(goiter.Skip(l.Iter(), n))
}
func (l *List[T]) SkipLast(n int) Enumerable[T] {
iterator := func(yield func(T) bool) {
elems := l.elems
endIdx := len(elems) - 1
if n > 0 {
endIdx = len(elems) - n - 1
}
for i := 0; i <= endIdx; i++ {
if !yield(elems[i]) {
return
}
}
}
return NewEnumerator(iterator)
}
func (l *List[T]) Distinct() Enumerable[T] {
return distinct[T](l)
}
func (l *List[T]) DistinctBy(keySelector func(T) any) Enumerable[T] {
return distinctBy(l, keySelector)
}
func (l *List[T]) Union(target Iterable[T]) Enumerable[T] {
return union(l, target)
}
func (l *List[T]) UnionBy(target Iterable[T], keySelector func(T) any) Enumerable[T] {
return unionBy(l, target, keySelector)
}
func (l *List[T]) Intersect(target Iterable[T]) Enumerable[T] {
return intersect(l, target)
}
func (l *List[T]) IntersectBy(target Iterable[T], keySelector func(T) any) Enumerable[T] {
return intersectBy(l, target, keySelector)
}
func (l *List[T]) Except(target Iterable[T]) Enumerable[T] {
return except(l, target)
}
func (l *List[T]) ExceptBy(target Iterable[T], keySelector func(T) any) Enumerable[T] {
return exceptBy(l, target, keySelector)
}
func (l *List[T]) SequenceEqual(target Iterable[T]) bool {
return sequenceEqual[T](l, target)
}
func (l *List[T]) SequenceEqualBy(target Iterable[T], keySelector func(T) any) bool {
return sequenceEqualBy(l, target, keySelector)
}
func (l *List[T]) Concat(iterables ...Iterable[T]) Enumerable[T] {
return concat(l, iterables...)
}
func (l *List[T]) OrderBy(cmp func(T, T) int) Enumerable[T] {
return orderBy(l, cmp)
}
func (l *List[T]) actualIndex(idx int) (int, bool) {
if idx < 0 {
idx = len(l.elems) + idx
if idx < 0 {
return -1, false
}
} else if idx >= len(l.elems) {
return -1, false
}
return idx, true
}
func (l *List[T]) indexOf(item T, startsFrom int) int {
if !l.comparable {
return -1
}
for idx := startsFrom; idx < len(l.elems); idx++ {
elem := l.elems[idx]
if any(elem) == any(item) {
return idx
}
}
return -1
}