-
Notifications
You must be signed in to change notification settings - Fork 1
/
adapters.go
196 lines (165 loc) · 3.79 KB
/
adapters.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
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
package glinq
import (
"bufio"
"io"
"reflect"
"sync/atomic"
)
type (
Slice[T any] []T
SliceEnumerator[T any] struct {
s Slice[T]
pos int
}
LineEnumerator struct {
reader *bufio.Reader
scanner *bufio.Scanner
taken int32
}
Pair[K comparable, V any] struct {
Key K
Value V
}
Map[K comparable, V any] map[K]V
MapEnumerator[K comparable, V any] struct {
m Map[K, V]
iter *reflect.MapIter
}
)
func (me Slice[T]) Count() int {
return len(me)
}
func (me Slice[T]) Any() bool {
return len(me) > 0
}
// func From[T any](v any) IEnumerable[T] {
// switch vv := v.(type) {
// case []T:
// return Slice[T](vv)
// default:
// return nil
// }
// t := reflect.TypeOf(v)
// if t.Kind() == reflect.Slice {
// reflect.SliceOf(t.Elem())
// }
// }
func FromSlice[T any](v []T) IEnumerable[T] {
return Slice[T](v)
}
// 先复制出一个 []Pair[K,V],再转成 IEnumerable,不适用于map很大的情况
func FromMapCopy[K comparable, V any](v map[K]V) IEnumerable[Pair[K, V]] {
values := make([]Pair[K, V], 0, len(v))
for kk, vv := range v {
values = append(values, Pair[K, V]{kk, vv})
}
return FromSlice(values)
}
// 通过 reflect.MapRange 遍历 map,线性读取快,随机访问慢
func FromMapReflect[K comparable, V any](v map[K]V) IEnumerable[Pair[K, V]] {
return Map[K, V](v)
}
func FromMap[K comparable, V any](v map[K]V) IEnumerable[Pair[K, V]] {
if len(v) < 1024 {
return FromMapCopy(v)
} else {
return FromMapReflect(v)
}
}
func (me Slice[T]) GetAt(i int) (v T, err error) {
if i < len(me) {
v = me[i]
} else {
err = io.ErrUnexpectedEOF
}
return
}
func (me Slice[T]) GetEnumerator() IEnumerator[T] {
return &SliceEnumerator[T]{s: me, pos: -1}
}
func (me *SliceEnumerator[T]) Current() T {
return me.s[me.pos]
}
func (me *SliceEnumerator[T]) GetAt(pos int) (v T, err error) {
return me.s.GetAt(pos)
}
func (me *SliceEnumerator[T]) SeekOnce(pos int) error {
if pos < len(me.s) {
me.pos = pos
return nil
}
return io.ErrUnexpectedEOF
}
func (me *SliceEnumerator[T]) MoveNext() error {
pos := me.pos + 1
if pos >= len(me.s) {
return io.EOF
}
me.pos = pos
return nil
}
func (me *SliceEnumerator[T]) Reset() error {
me.pos = -1
return nil
}
func ReadLines(reader io.Reader) *LineEnumerator {
var r *bufio.Reader
var ok bool
if r, ok = reader.(*bufio.Reader); !ok {
r = bufio.NewReader(reader)
}
return &LineEnumerator{
reader: r,
scanner: bufio.NewScanner(reader),
}
}
func (me *LineEnumerator) GetEnumerator() IEnumerator[string] {
if atomic.CompareAndSwapInt32(&me.taken, 0, 1) {
return me
}
panic(`read iterators cannot be read more than once`)
}
func (me *LineEnumerator) MoveNext() error {
if me.scanner.Scan() {
return nil
} else {
err := me.scanner.Err()
if err == nil {
err = io.EOF
}
return err
}
}
func (me *LineEnumerator) Current() string {
return me.scanner.Text()
}
func (me *LineEnumerator) Any() bool {
_, err := me.reader.Peek(1)
return err != nil
}
func (me *LineEnumerator) Count() int {
return -1
}
func (me Map[K, V]) Count() int {
return len(me)
}
func (me Map[K, V]) Any() bool {
return len(me) > 0
}
func (me Map[K, V]) GetEnumerator() IEnumerator[Pair[K, V]] {
v := reflect.ValueOf(me)
return &MapEnumerator[K, V]{me, v.MapRange()}
}
func (me *MapEnumerator[K, V]) Current() Pair[K, V] {
return Pair[K, V]{me.iter.Key().Interface().(K), me.iter.Value().Interface().(V)}
}
func (me *MapEnumerator[K, V]) MoveNext() error {
if me.iter.Next() {
return nil
}
return io.EOF
}
func (me *MapEnumerator[K, V]) Reset() error {
me.iter = reflect.ValueOf(me.m).MapRange()
return nil
}