-
Notifications
You must be signed in to change notification settings - Fork 1
/
skipWhile.go
81 lines (73 loc) · 1.65 KB
/
skipWhile.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
package glinq
import "sync/atomic"
type (
SkipWhileIterator[T any] struct {
iterator[T]
source IEnumerable[T]
pred func(T) bool
enumerator IEnumerator[T]
}
)
func NewSkipWhileIterator[T any](source IEnumerable[T], pred func(T) bool) *SkipWhileIterator[T] {
result := &SkipWhileIterator[T]{
source: source,
pred: pred,
}
result.child = result
return result
}
func SkipWhile[T any](source IEnumerable[T], pred func(T) bool) IEnumerable[T] {
return NewSkipWhileIterator(source, pred)
}
func (me *SkipWhileIterator[T]) Clone() IEnumerator[T] {
result := NewSkipWhileIterator(me.source, me.pred)
result.state = 1
return result
}
func (me *SkipWhileIterator[T]) MoveNext() error {
switch me.state {
case 1:
enumerator := me.source.GetEnumerator()
me.enumerator = enumerator
for {
if err := enumerator.MoveNext(); err != nil {
return err
}
current := enumerator.Current()
if !me.pred(current) {
me.current = current
break
}
}
me.state = 2
return nil
case 2:
err := me.enumerator.MoveNext()
me.current = me.enumerator.Current()
return err
}
return ErrInvalidState
}
func (me *SkipWhileIterator[T]) Any() bool {
return me.GetEnumerator().MoveNext() == nil
}
func (me *SkipWhileIterator[T]) Count() int {
if atomic.CompareAndSwapInt32(&me.state, 0, 1) {
count := me.source.Count()
if count < 0 {
return count
}
enum := me.source.GetEnumerator()
for err := enum.MoveNext(); err == nil; err = enum.MoveNext() {
if me.pred(enum.Current()) {
count--
} else {
break
}
}
return count
} else {
result := NewSkipWhileIterator(me.source, me.pred)
return result.Count()
}
}