-
Notifications
You must be signed in to change notification settings - Fork 1
/
skip.go
79 lines (71 loc) · 1.64 KB
/
skip.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
package glinq
type (
SkipIterator[T any] struct {
iterator[T]
source IEnumerable[T]
skip int
enumerator IEnumerator[T]
}
)
func NewSkipIterator[T any](source IEnumerable[T], n int) *SkipIterator[T] {
result := &SkipIterator[T]{
source: source,
skip: n,
}
result.child = result
return result
}
func Skip[T any](source IEnumerable[T], n int) IEnumerable[T] {
return NewSkipIterator(source, n)
}
func (me *SkipIterator[T]) Clone() IEnumerator[T] {
result := NewSkipIterator(me.source, me.skip)
result.state = 1
return result
}
func (me *SkipIterator[T]) MoveNext() error {
switch me.state {
case 1:
enumerator := me.source.GetEnumerator()
me.enumerator = enumerator
if randomAcceesor, ok := enumerator.(IRangeEnumerator[T]); ok {
err := randomAcceesor.SeekOnce(me.skip)
me.current = enumerator.Current()
me.state = 2
return err
}
for i := 0; i < me.skip; i++ {
if err := enumerator.MoveNext(); err != nil {
return err
}
}
me.current = enumerator.Current()
me.state = 2
fallthrough
case 2:
err := me.enumerator.MoveNext()
me.current = me.enumerator.Current()
return err
}
return ErrInvalidState
}
func (me *SkipIterator[T]) Any() bool {
return me.GetEnumerator().MoveNext() == nil
}
func (me *SkipIterator[T]) Count() int {
iter := me.source.GetEnumerator()
if counter, ok := iter.(ICount[T]); ok {
result := counter.Count() - me.skip
if result >= 0 {
return result
}
return 0
}
result := 0
Do(me.source, func(T) { result++ })
result -= me.skip
if result < 0 {
result = 0
}
return result
}