-
Notifications
You must be signed in to change notification settings - Fork 1
/
iterator.go
50 lines (43 loc) · 950 Bytes
/
iterator.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
package glinq
import "sync/atomic"
type (
iterator[T any] struct {
child iteratorChild[T]
// -1 = disposed, not usable anymore
// 0 = unused
// 1 = at first item
// 2 = at other item
// 3 = in use by Count()
state int32
current T
}
IEnumerableAndEnumerator[T any] interface {
IEnumerable[T]
IEnumerator[T]
}
iteratorChild[T any] interface {
IEnumerator[T]
Clone() IEnumerator[T]
}
)
func Any[T any](q IEnumerable[T]) bool {
return q.Any()
}
func (me *iterator[T]) GetEnumerator() IEnumerator[T] {
var result IEnumerator[T]
if atomic.CompareAndSwapInt32(&me.state, 0, 1) {
result = me.child
} else {
result = me.child.Clone()
}
return result
}
func (me *iterator[T]) Current() T {
return me.current
}
func (me *iterator[T]) MoveNext() error {
panic(`not implemented`)
}
// func (me *iterator[T]) Reset() error {
// panic(`not implemented`)
// }