-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiter.go
173 lines (168 loc) · 4.24 KB
/
iter.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
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package reflect
import (
"iter"
)
func rangeNum[T int8 | int16 | int32 | int64 | int |
uint8 | uint16 | uint32 | uint64 | uint |
uintptr, N int64 | uint64](num N, t Type) iter.Seq[Value] {
return func(yield func(v Value) bool) {
convert := t.PkgPath() != ""
// cannot use range T(v) because no core type.
for i := T(0); i < T(num); i++ {
tmp := ValueOf(i)
// if the iteration value type is define by
// type T built-in type.
if convert {
tmp = tmp.Convert(t)
}
if !yield(tmp) {
return
}
}
}
}
// Seq returns an iter.Seq[Value] that loops over the elements of v.
// If v's kind is Func, it must be a function that has no results and
// that takes a single argument of type func(T) bool for some type T.
// If v's kind is Pointer, the pointer element type must have kind Array.
// Otherwise v's kind must be Int, Int8, Int16, Int32, Int64,
// Uint, Uint8, Uint16, Uint32, Uint64, Uintptr,
// Array, Chan, Map, Slice, or String.
func (v Value) Seq() iter.Seq[Value] {
if canRangeFunc(v.abiType()) {
return func(yield func(Value) bool) {
rf := MakeFunc(v.Type().In(0), func(in []Value) []Value {
return []Value{ValueOf(yield(in[0]))}
})
v.Call([]Value{rf})
}
}
switch v.kind() {
case Int:
return rangeNum[int](v.Int(), v.Type())
case Int8:
return rangeNum[int8](v.Int(), v.Type())
case Int16:
return rangeNum[int16](v.Int(), v.Type())
case Int32:
return rangeNum[int32](v.Int(), v.Type())
case Int64:
return rangeNum[int64](v.Int(), v.Type())
case Uint:
return rangeNum[uint](v.Uint(), v.Type())
case Uint8:
return rangeNum[uint8](v.Uint(), v.Type())
case Uint16:
return rangeNum[uint16](v.Uint(), v.Type())
case Uint32:
return rangeNum[uint32](v.Uint(), v.Type())
case Uint64:
return rangeNum[uint64](v.Uint(), v.Type())
case Uintptr:
return rangeNum[uintptr](v.Uint(), v.Type())
case Pointer:
if v.Elem().kind() != Array {
break
}
return func(yield func(Value) bool) {
v = v.Elem()
for i := range v.Len() {
if !yield(ValueOf(i)) {
return
}
}
}
case Array, Slice:
return func(yield func(Value) bool) {
for i := range v.Len() {
if !yield(ValueOf(i)) {
return
}
}
}
case String:
return func(yield func(Value) bool) {
for i := range v.String() {
if !yield(ValueOf(i)) {
return
}
}
}
case Map:
return func(yield func(Value) bool) {
i := v.MapRange()
for i.Next() {
if !yield(i.Key()) {
return
}
}
}
case Chan:
return func(yield func(Value) bool) {
for value, ok := v.Recv(); ok; value, ok = v.Recv() {
if !yield(value) {
return
}
}
}
}
panic("reflect: " + v.Type().String() + " cannot produce iter.Seq[Value]")
}
// Seq2 returns an iter.Seq2[Value, Value] that loops over the elements of v.
// If v's kind is Func, it must be a function that has no results and
// that takes a single argument of type func(K, V) bool for some type K, V.
// If v's kind is Pointer, the pointer element type must have kind Array.
// Otherwise v's kind must be Array, Map, Slice, or String.
func (v Value) Seq2() iter.Seq2[Value, Value] {
if canRangeFunc2(v.abiType()) {
return func(yield func(Value, Value) bool) {
rf := MakeFunc(v.Type().In(0), func(in []Value) []Value {
return []Value{ValueOf(yield(in[0], in[1]))}
})
v.Call([]Value{rf})
}
}
switch v.Kind() {
case Pointer:
if v.Elem().kind() != Array {
break
}
return func(yield func(Value, Value) bool) {
v = v.Elem()
for i := range v.Len() {
if !yield(ValueOf(i), v.Index(i)) {
return
}
}
}
case Array, Slice:
return func(yield func(Value, Value) bool) {
for i := range v.Len() {
if !yield(ValueOf(i), v.Index(i)) {
return
}
}
}
case String:
return func(yield func(Value, Value) bool) {
for i, v := range v.String() {
if !yield(ValueOf(i), ValueOf(v)) {
return
}
}
}
case Map:
return func(yield func(Value, Value) bool) {
i := v.MapRange()
for i.Next() {
if !yield(i.Key(), i.Value()) {
return
}
}
}
}
panic("reflect: " + v.Type().String() + " cannot produce iter.Seq2[Value, Value]")
}