-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueryT.go
More file actions
1297 lines (1236 loc) · 25 KB
/
queryT.go
File metadata and controls
1297 lines (1236 loc) · 25 KB
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package linq
import (
"context"
"iter"
"slices"
"sync"
)
type Signed interface {
~int | ~int8 | ~int16 | ~int32 | ~int64
}
type Unsigned interface {
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}
type Integer interface {
Signed | Unsigned
}
type Float interface {
~float32 | ~float64
}
type Complex interface {
~complex64 | ~complex128
}
// KV 键值对结构体
type KV[K comparable, V any] struct {
Key K
Value V
}
// CompareFunc 比较函数类型
type CompareFunc[T comparable] func(a, b T) int
// Query 查询结构体,是 LINQ 操作的核心类型
type Query[T comparable] struct {
compare CompareFunc[T]
iterate iter.Seq[T]
fastSlice []T
fastWhere func(T) bool
capacity int
materialize func() []T
sortSource *Query[T]
sortCompares []CompareFunc[T]
sortStable bool
}
// Seq 返回供 for-range 从头到尾遍历的迭代器
func (q Query[T]) Seq() iter.Seq[T] {
if q.fastSlice != nil {
source := q.fastSlice
predicate := q.fastWhere
if predicate == nil {
return slices.Values(source)
}
return func(yield func(T) bool) {
for _, item := range source {
if !predicate(item) {
continue
}
if !yield(item) {
return
}
}
}
}
return q.iterate
}
// ToSlice 将查询结果收集为切片
func (q Query[T]) ToSlice() []T {
if q.fastSlice != nil {
source := q.fastSlice
predicate := q.fastWhere
if predicate == nil {
return slices.Clone(source)
}
result := make([]T, 0, q.capacity/2+1) // 估算
for _, v := range source {
if predicate(v) {
result = append(result, v)
}
}
return result
}
if q.materialize != nil {
return q.materialize()
}
var result []T
if q.capacity > 0 {
result = make([]T, 0, q.capacity/2+1)
}
for item := range q.iterate {
result = append(result, item)
}
return result
}
// ToChannel 将查询结果收集为通道,支持上下文取消
func (q Query[T]) ToChannel(ctx context.Context) <-chan T {
if ctx == nil {
ctx = context.Background()
}
ch := make(chan T)
go func() {
defer close(ch)
if q.fastSlice != nil {
for _, item := range q.fastSlice {
if q.fastWhere != nil && !q.fastWhere(item) {
continue
}
select {
case <-ctx.Done():
return
case ch <- item:
}
}
return
}
for item := range q.iterate {
select {
case <-ctx.Done():
return
case ch <- item:
}
}
}()
return ch
}
// Reverse 返回反转后的序列的查询对象
func (q Query[T]) Reverse() Query[T] {
return Query[T]{
iterate: func(yield func(T) bool) {
var items []T
if q.fastSlice != nil {
if q.fastWhere == nil {
items = q.fastSlice
} else {
predicate := q.fastWhere
items = make([]T, 0, q.capacity/2+1)
for _, item := range q.fastSlice {
if predicate(item) {
items = append(items, item)
}
}
}
} else {
if q.capacity > 0 {
items = make([]T, 0, q.capacity)
}
for item := range q.iterate {
items = append(items, item)
}
}
for i := len(items) - 1; i >= 0; i-- {
if !yield(items[i]) {
return
}
}
},
capacity: q.capacity,
materialize: func() []T {
if q.fastSlice != nil {
if q.fastWhere == nil {
n := len(q.fastSlice)
result := make([]T, n)
for i := 0; i < n; i++ {
result[i] = q.fastSlice[n-1-i]
}
return result
}
predicate := q.fastWhere
filtered := make([]T, 0, q.capacity/2+1)
for _, item := range q.fastSlice {
if predicate(item) {
filtered = append(filtered, item)
}
}
for i, j := 0, len(filtered)-1; i < j; i, j = i+1, j-1 {
filtered[i], filtered[j] = filtered[j], filtered[i]
}
return filtered
}
var result []T
if q.capacity > 0 {
result = make([]T, 0, q.capacity)
}
for item := range q.iterate {
result = append(result, item)
}
for i, j := 0, len(result)-1; i < j; i, j = i+1, j-1 {
result[i], result[j] = result[j], result[i]
}
return result
},
}
}
// Distinct 代理
func (q Query[T]) Distinct() Query[T] {
return Distinct(q)
}
// Intersect 代理
func (q Query[T]) Intersect(q2 Query[T]) Query[T] {
return Intersect(q, q2)
}
// Union 代理
func (q Query[T]) Union(q2 Query[T]) Query[T] {
return Union(q, q2)
}
// Except 代理
func (q Query[T]) Except(q2 Query[T]) Query[T] {
return Except(q, q2)
}
// AppendTo 追加到目标切片中
func (q Query[T]) AppendTo(dest []T) []T {
if q.capacity > 0 {
dest = slices.Grow(dest, q.capacity)
}
if q.fastSlice != nil {
for _, item := range q.fastSlice {
if q.fastWhere != nil && !q.fastWhere(item) {
continue
}
dest = append(dest, item)
}
return dest
}
for item := range q.iterate {
dest = append(dest, item)
}
return dest
}
// ToMapSlice 将序列转换为 []map[string]T,通常用于 JSON 序列化
func (q Query[T]) ToMapSlice(selector func(T) map[string]T) (r []map[string]T) {
if q.capacity > 0 {
r = make([]map[string]T, 0, q.capacity)
}
if q.fastSlice != nil {
for _, item := range q.fastSlice {
if q.fastWhere != nil && !q.fastWhere(item) {
continue
}
r = append(r, selector(item))
}
return r
}
for item := range q.iterate {
r = append(r, selector(item))
}
return r
}
// ForEach 遍历序列并对每个元素执行指定操作
func (q Query[T]) ForEach(action func(T) bool) {
if q.fastSlice != nil {
source := q.fastSlice
preFilter := q.fastWhere
for _, item := range source {
if preFilter != nil && !preFilter(item) {
continue
}
if !action(item) {
return
}
}
return
}
for item := range q.iterate {
if !action(item) {
break
}
}
}
// ForEachIndexed 带索引遍历序列
func (q Query[T]) ForEachIndexed(action func(int, T) bool) {
index := 0
if q.fastSlice != nil {
source := q.fastSlice
preFilter := q.fastWhere
for _, item := range source {
if preFilter != nil && !preFilter(item) {
continue
}
if !action(index, item) {
return
}
index++
}
return
}
for item := range q.iterate {
if !action(index, item) {
break
}
index++
}
}
// ForEachParallelCtx 支持 Context 取消的并发遍历执行器(不保证顺序)
func (q Query[T]) ForEachParallelCtx(ctx context.Context, action func(T), workers ...int) {
if ctx == nil {
ctx = context.Background()
}
iworkers := 1
if len(workers) > 0 && workers[0] > 0 {
iworkers = workers[0]
}
workerCtx, cancel := context.WithCancel(ctx)
defer cancel()
jobs := make(chan T, iworkers)
errCh := make(chan any, 1)
var wg sync.WaitGroup
for i := 0; i < iworkers; i++ {
wg.Add(1)
go func() {
defer wg.Done()
defer func() {
if r := recover(); r != nil {
select {
case errCh <- r:
default:
}
cancel()
}
}()
for {
select {
case <-workerCtx.Done():
return
case item, ok := <-jobs:
if !ok {
return
}
action(item)
}
}
}()
}
emit := func(item T) bool {
select {
case <-workerCtx.Done():
return false
case jobs <- item:
return true
}
}
if q.fastSlice != nil {
for _, item := range q.fastSlice {
if q.fastWhere != nil && !q.fastWhere(item) {
continue
}
if !emit(item) {
break
}
}
} else {
for item := range q.iterate {
if !emit(item) {
break
}
}
}
close(jobs)
wg.Wait()
select {
case panicErr := <-errCh:
panic(panicErr)
default:
}
}
// ForEachParallel 并发遍历无 context (底层封装 Ctx 版本)
func (q Query[T]) ForEachParallel(action func(T), workers ...int) {
q.ForEachParallelCtx(context.Background(), action, workers...)
}
// Count 返回序列中的元素个数
func (q Query[T]) Count() int {
if q.fastSlice != nil {
if q.fastWhere == nil {
return len(q.fastSlice)
}
count := 0
for _, item := range q.fastSlice {
if q.fastWhere(item) {
count++
}
}
return count
}
count := 0
for range q.iterate {
count++
}
return count
}
// CountWith 统计满足条件的元素个数
func (q Query[T]) CountWith(predicate func(T) bool) int {
if q.fastSlice != nil {
source := q.fastSlice
preFilter := q.fastWhere
count := 0
for _, v := range source {
if preFilter != nil && !preFilter(v) {
continue
}
if predicate(v) {
count++
}
}
return count
}
count := 0
for item := range q.iterate {
if predicate(item) {
count++
}
}
return count
}
// Any 判断序列是否包含任何元素
func (q Query[T]) Any() bool {
if q.fastSlice != nil {
if q.fastWhere == nil {
return len(q.fastSlice) > 0
}
for _, v := range q.fastSlice {
if q.fastWhere(v) {
return true
}
}
return false
}
for range q.iterate {
return true
}
return false
}
// AnyWith 判断序列是否包含满足指定条件的元素
func (q Query[T]) AnyWith(predicate func(T) bool) bool {
if q.fastSlice != nil {
for _, v := range q.fastSlice {
if q.fastWhere != nil && !q.fastWhere(v) {
continue
}
if predicate(v) {
return true
}
}
return false
}
for item := range q.iterate {
if predicate(item) {
return true
}
}
return false
}
// All 判断序列中的所有元素是否都满足指定条件
func (q Query[T]) All(predicate func(T) bool) bool {
if q.fastSlice != nil {
for _, v := range q.fastSlice {
if q.fastWhere != nil && !q.fastWhere(v) {
continue
}
if !predicate(v) {
return false
}
}
return true
}
for item := range q.iterate {
if !predicate(item) {
return false
}
}
return true
}
func (q Query[T]) SumIntBy(selector func(T) int) int { return SumBy(q, selector) }
func (q Query[T]) SumInt8By(selector func(T) int8) int8 { return SumBy(q, selector) }
func (q Query[T]) SumInt16By(selector func(T) int16) int16 { return SumBy(q, selector) }
func (q Query[T]) SumInt32By(selector func(T) int32) int32 { return SumBy(q, selector) }
func (q Query[T]) SumInt64By(selector func(T) int64) int64 { return SumBy(q, selector) }
func (q Query[T]) SumUIntBy(selector func(T) uint) uint { return SumBy(q, selector) }
func (q Query[T]) SumUInt8By(selector func(T) uint8) uint8 { return SumBy(q, selector) }
func (q Query[T]) SumUInt16By(selector func(T) uint16) uint16 { return SumBy(q, selector) }
func (q Query[T]) SumUInt32By(selector func(T) uint32) uint32 { return SumBy(q, selector) }
func (q Query[T]) SumUInt64By(selector func(T) uint64) uint64 { return SumBy(q, selector) }
func (q Query[T]) SumFloat32By(selector func(T) float32) float32 { return SumBy(q, selector) }
func (q Query[T]) SumFloat64By(selector func(T) float64) float64 { return SumBy(q, selector) }
func (q Query[T]) AvgBy(selector func(T) float64) float64 { return AverageBy(q, selector) }
func (q Query[T]) AvgIntBy(selector func(T) int) float64 { return AverageBy(q, selector) }
func (q Query[T]) AvgInt64By(selector func(T) int64) float64 { return AverageBy(q, selector) }
// First 返回第一元素,如果没有则返回零值
func (q Query[T]) First() T {
if q.fastSlice != nil {
for _, v := range q.fastSlice {
if q.fastWhere != nil && !q.fastWhere(v) {
continue
}
return v
}
var zero T
return zero
}
for item := range q.iterate {
return item
}
var zero T
return zero
}
// FirstWith 返回满足条件的第一个元素
func (q Query[T]) FirstWith(predicate func(T) bool) T {
if q.fastSlice != nil {
for _, v := range q.fastSlice {
if q.fastWhere != nil && !q.fastWhere(v) {
continue
}
if predicate(v) {
return v
}
}
var zero T
return zero
}
for item := range q.iterate {
if predicate(item) {
return item
}
}
var zero T
return zero
}
// FirstOK 返回第一个元素以及是否存在
func (q Query[T]) FirstOK() (T, bool) {
if q.fastSlice != nil {
for _, v := range q.fastSlice {
if q.fastWhere != nil && !q.fastWhere(v) {
continue
}
return v, true
}
var zero T
return zero, false
}
for item := range q.iterate {
return item, true
}
var zero T
return zero, false
}
// FirstWithOK 返回满足条件的第一个元素以及是否存在
func (q Query[T]) FirstWithOK(predicate func(T) bool) (T, bool) {
if q.fastSlice != nil {
for _, v := range q.fastSlice {
if q.fastWhere != nil && !q.fastWhere(v) {
continue
}
if predicate(v) {
return v, true
}
}
var zero T
return zero, false
}
for item := range q.iterate {
if predicate(item) {
return item, true
}
}
var zero T
return zero, false
}
// Last 返回最后一个元素,如果没有则返回零值
func (q Query[T]) Last() T {
if q.fastSlice != nil {
source := q.fastSlice
pre := q.fastWhere
if pre == nil {
if len(source) > 0 {
return source[len(source)-1]
}
var zero T
return zero
} else {
for i := len(source) - 1; i >= 0; i-- {
if pre(source[i]) {
return source[i]
}
}
var zero T
return zero
}
}
var last T
for item := range q.iterate {
last = item
}
return last
}
// LastOK 返回最后一个元素以及是否存在
func (q Query[T]) LastOK() (T, bool) {
if q.fastSlice != nil {
source := q.fastSlice
pre := q.fastWhere
if pre == nil {
if len(source) > 0 {
return source[len(source)-1], true
}
} else {
for i := len(source) - 1; i >= 0; i-- {
if pre(source[i]) {
return source[i], true
}
}
}
var zero T
return zero, false
}
var last T
found := false
for item := range q.iterate {
last = item
found = true
}
return last, found
}
// LastWith 返回满足条件的最后一个元素
func (q Query[T]) LastWith(predicate func(T) bool) T {
if q.fastSlice != nil {
source := q.fastSlice
pre := q.fastWhere
for i := len(source) - 1; i >= 0; i-- {
v := source[i]
if pre != nil && !pre(v) {
continue
}
if predicate(v) {
return v
}
}
var zero T
return zero
}
var last T
for item := range q.iterate {
if predicate(item) {
last = item
}
}
return last
}
// LastWithOK 返回满足条件的最后一个元素以及是否存在
func (q Query[T]) LastWithOK(predicate func(T) bool) (T, bool) {
if q.fastSlice != nil {
source := q.fastSlice
pre := q.fastWhere
for i := len(source) - 1; i >= 0; i-- {
v := source[i]
if pre != nil && !pre(v) {
continue
}
if predicate(v) {
return v, true
}
}
var zero T
return zero, false
}
var last T
found := false
for item := range q.iterate {
if predicate(item) {
last = item
found = true
}
}
return last, found
}
// FirstDefault 返回第一个元素,若空返回 defaultValue
func (q Query[T]) FirstDefault(defaultValue ...T) T {
if q.fastSlice != nil {
for _, v := range q.fastSlice {
if q.fastWhere != nil && !q.fastWhere(v) {
continue
}
return v
}
} else {
for item := range q.iterate {
return item
}
}
if len(defaultValue) > 0 {
return defaultValue[0]
}
var zero T
return zero
}
// LastDefault 返回最后一个元素,若空返回 defaultValue
func (q Query[T]) LastDefault(defaultValue ...T) T {
if q.fastSlice != nil {
source := q.fastSlice
pre := q.fastWhere
for i := len(source) - 1; i >= 0; i-- {
v := source[i]
if pre != nil && !pre(v) {
continue
}
return v
}
} else {
var last T
found := false
for item := range q.iterate {
last = item
found = true
}
if found {
return last
}
}
if len(defaultValue) > 0 {
return defaultValue[0]
}
var zero T
return zero
}
// Single 返回包含且仅包含一个元素的序列的那个元素,如果不等于1个返回零值
func (q Query[T]) Single() T {
var val T
count := 0
if q.fastSlice != nil {
for _, v := range q.fastSlice {
if q.fastWhere != nil && !q.fastWhere(v) {
continue
}
val = v
count++
if count > 1 {
var zero T
return zero
}
}
} else {
for item := range q.iterate {
val = item
count++
if count > 1 {
var zero T
return zero
}
}
}
if count == 0 {
var zero T
return zero
}
return val
}
// SingleWith 返回满足条件的那个元素,如果不等于1个返回零值
func (q Query[T]) SingleWith(predicate func(T) bool) T {
return q.Where(predicate).Single()
}
// SingleDefault 返回包含且仅包含一个元素的序列的那个元素,如果不等于1个返回默认值或者零值
func (q Query[T]) SingleDefault(defaultValue ...T) T {
var val T
count := 0
if q.fastSlice != nil {
for _, v := range q.fastSlice {
if q.fastWhere != nil && !q.fastWhere(v) {
continue
}
val = v
count++
if count > 1 {
if len(defaultValue) > 0 {
return defaultValue[0]
}
var zero T
return zero
}
}
} else {
for item := range q.iterate {
val = item
count++
if count > 1 {
if len(defaultValue) > 0 {
return defaultValue[0]
}
var zero T
return zero
}
}
}
if count == 0 {
if len(defaultValue) > 0 {
return defaultValue[0]
}
var zero T
return zero
}
return val
}
// SingleOK 返回唯一元素以及是否存在且唯一
func (q Query[T]) SingleOK() (T, bool) {
var val T
count := 0
if q.fastSlice != nil {
for _, v := range q.fastSlice {
if q.fastWhere != nil && !q.fastWhere(v) {
continue
}
val = v
count++
if count > 1 {
var zero T
return zero, false
}
}
} else {
for item := range q.iterate {
val = item
count++
if count > 1 {
var zero T
return zero, false
}
}
}
if count == 1 {
return val, true
}
var zero T
return zero, false
}
// SingleWithOK 返回满足条件的唯一元素以及是否存在且唯一
func (q Query[T]) SingleWithOK(predicate func(T) bool) (T, bool) {
return q.Where(predicate).SingleOK()
}
// IndexOfWith 返回满足条件的元素的索引
func (q Query[T]) IndexOfWith(predicate func(T) bool) int {
index := 0
if q.fastSlice != nil {
for _, item := range q.fastSlice {
if q.fastWhere != nil && !q.fastWhere(item) {
continue
}
if predicate(item) {
return index
}
index++
}
} else {
for item := range q.iterate {
if predicate(item) {
return index
}
index++
}
}
return -1
}
// LastIndexOfWith 返回满足条件的元素最后出现的索引
func (q Query[T]) LastIndexOfWith(predicate func(T) bool) int {
index := 0
last := -1
if q.fastSlice != nil {
for _, item := range q.fastSlice {
if q.fastWhere != nil && !q.fastWhere(item) {
continue
}
if predicate(item) {
last = index
}
index++
}
} else {
for item := range q.iterate {
if predicate(item) {
last = index
}
index++
}
}
return last
}
// Where 过滤元素
func (q Query[T]) Where(predicate func(T) bool) Query[T] {
if q.fastSlice != nil {
source := q.fastSlice
var combinedPred func(T) bool
if q.fastWhere == nil {
combinedPred = predicate
} else {
oldPred := q.fastWhere
combinedPred = func(t T) bool { return oldPred(t) && predicate(t) }
}
return Query[T]{
iterate: q.iterate,
fastSlice: source,
fastWhere: combinedPred,
capacity: q.capacity,
}
}
return Query[T]{
iterate: func(yield func(T) bool) {
for item := range q.iterate {
if predicate(item) {
if !yield(item) {
break
}
}
}
},
capacity: q.capacity,
}
}
// Skip 跳过前 N 个元素
func (q Query[T]) Skip(count int) Query[T] {
if q.fastSlice != nil && q.fastWhere == nil {
if count >= len(q.fastSlice) {
return QueryEmpty[T]()
}
if count <= 0 {
return q
}
return From(q.fastSlice[count:])
}
return Query[T]{
iterate: func(yield func(T) bool) {
n := count
if q.fastSlice != nil {
for _, item := range q.fastSlice {
if q.fastWhere != nil && !q.fastWhere(item) {
continue
}
if n > 0 {
n--
continue
}
if !yield(item) {
break
}
}
return
}
for item := range q.iterate {
if n > 0 {
n--
continue
}
if !yield(item) {
break
}
}
},
}
}