-
Notifications
You must be signed in to change notification settings - Fork 1
/
wrapper.go
115 lines (99 loc) · 1.87 KB
/
wrapper.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
package enu
type FilterEnumerator[T any] struct {
iter IEnumerator[T]
predicate func(item T, index int) bool
index int
}
func (e *FilterEnumerator[T]) GetEnumerator() IEnumerator[T] {
return e
}
func (e *FilterEnumerator[T]) Dispose() {
e.iter.Dispose()
e.index = 0
}
func (e *FilterEnumerator[T]) Next() (T, bool) {
for {
item, ok := e.iter.Next()
if !ok {
return empty[T](), false
}
if e.predicate(item, e.index) {
return item, true
}
e.index++
}
}
type RejectEnumerator[T any] struct {
iter IEnumerator[T]
predicate func(item T, index int) bool
index int
}
func (e *RejectEnumerator[T]) GetEnumerator() IEnumerator[T] {
return e
}
func (e *RejectEnumerator[T]) Dispose() {
e.iter.Dispose()
e.index = 0
}
func (e *RejectEnumerator[T]) Next() (T, bool) {
for {
item, ok := e.iter.Next()
if !ok {
return empty[T](), false
}
if !e.predicate(item, e.index) {
return item, true
}
e.index++
}
}
type TakeEnumerator[T any] struct {
iter IEnumerator[T]
size uint
index uint
}
func (e *TakeEnumerator[T]) GetEnumerator() IEnumerator[T] {
return e
}
func (e *TakeEnumerator[T]) Dispose() {
e.iter.Dispose()
e.index = 0
}
func (e *TakeEnumerator[T]) Next() (T, bool) {
if e.size == e.index {
return empty[T](), false
}
item, ok := e.iter.Next()
if !ok {
return empty[T](), false
}
e.index++
return item, true
}
type UniqEnumerator[T comparable] struct {
iter IEnumerator[T]
seen map[T]struct{}
}
func (e *UniqEnumerator[T]) GetEnumerator() IEnumerator[T] {
return e
}
func (e *UniqEnumerator[T]) Dispose() {
e.iter.Dispose()
e.seen = nil
}
func (e *UniqEnumerator[T]) Next() (T, bool) {
if e.seen == nil {
e.seen = map[T]struct{}{}
}
for {
item, ok := e.iter.Next()
if !ok {
return empty[T](), false
}
if _, ok := e.seen[item]; ok {
continue
}
e.seen[item] = struct{}{}
return item, true
}
}