-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsources.go
176 lines (164 loc) · 4.34 KB
/
sources.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
package iterutil
import (
"cmp"
"iter"
"slices"
"github.com/jub0bs/iterutil/internal"
"golang.org/x/exp/constraints"
)
// Empty returns an empty iterator.
func Empty[E any]() iter.Seq[E] {
return func(_ func(E) bool) {}
}
// SeqOf returns an iterator composed of elems.
func SeqOf[E any](elems ...E) iter.Seq[E] {
return func(yield func(E) bool) {
for _, e := range elems {
if !yield(e) {
return
}
}
}
}
// Between, if step is nonzero, returns an iterator
// ranging from n (inclusive) to m (exclusive) in increments of step;
// otherwise, it panics.
func Between[I constraints.Signed](n, m, step I) iter.Seq[I] {
switch cmp.Compare(step, 0) {
default:
panic("step cannot be zero")
case 1: // ascending
return func(yield func(I) bool) {
for ; n < m && yield(n); n += step {
// deliberately empty
}
}
case -1: // descending
return func(yield func(I) bool) {
for ; n > m && yield(n); n += step {
// deliberately empty
}
}
}
}
// Repeat returns an iterator whose values are invariably e.
// The resulting iterator, if count is non-negative, is of length count;
// otherwise, it's infinite.
func Repeat[I constraints.Integer, E any](e E, count I) iter.Seq[E] {
if 0 <= count {
return func(yield func(E) bool) {
for i := I(0); i < count; i++ {
if !yield(e) {
return
}
}
}
}
return func(yield func(E) bool) {
for yield(e) {
// deliberately empty
}
}
}
// Iterate returns an infinite iterator composed of repeated applications
// of f to e.
func Iterate[E any](e E, f func(E) E) iter.Seq[E] {
return func(yield func(E) bool) {
for yield(e) {
e = f(e)
}
}
}
// Cycle returns an iterator that infinitely repeats seq.
func Cycle[E any](seq iter.Seq[E]) iter.Seq[E] {
return func(yield func(E) bool) {
for {
for e := range seq {
if !yield(e) {
return
}
}
}
}
}
// SortedFromMap returns an iterator over the key-value pairs in m
// ordered by its keys.
func SortedFromMap[M ~map[K]V, K cmp.Ordered, V any](m M) iter.Seq2[K, V] {
return func(yield func(K, V) bool) {
// One possibility would be to simply iterate over
// slices.Sorted(maps.Keys(m)),
// but this approach incurs unnecessary allocations
// because it doesn't take advantage of how many keys the map contains.
//
// For small maps, sorting the keys upfront is faster.
if len(m) < mapSizeThreshold {
ks := make([]K, 0, len(m))
for k := range m {
ks = append(ks, k)
}
slices.Sort(ks)
for _, k := range ks {
if !yield(k, m[k]) {
return
}
}
return
}
//
// For larger maps, we can do better.
// As earthboundkid insightfully pointed out in
// https://github.com/golang/go/issues/61898#issuecomment-2479025873,
// instead of sorting all the keys upfront,
// we can optimize for cases where not all of the key-value pairs
// get pushed to the iterator's yield function: to do so, we build
// a binary heap of the keys (in linear time and linear space),
// and then pop (in logarithmic time) each requested key off the heap.
// The overall worst-case time complexity is O(n + k*log(n)), where
// - n is the number of keys in the map,
// - k is the number of pairs pushed to the iterator's yield function.
for k := range internal.NewHeap(keys(m)).Iterator {
if !yield(k, m[k]) {
return
}
}
}
}
// SortedFromMapFunc returns an iterator over the key-value pairs in m
// ordered by its keys, using cmp as comparison function.
//
// Note that, for a deterministic behavior,
// cmp must define a [total order] on K;
// for more details, see the testable example labeled "incorrect".
//
// [total order]: https://en.wikipedia.org/wiki/Total_order
func SortedFromMapFunc[M ~map[K]V, K comparable, V any](m M, cmp func(K, K) int) iter.Seq2[K, V] {
return func(yield func(K, V) bool) {
// see implementation comment in SortedFromMap
if len(m) < mapSizeThreshold {
ks := make([]K, 0, len(m))
for k := range m {
ks = append(ks, k)
}
slices.SortFunc(ks, cmp)
for _, k := range ks {
if !yield(k, m[k]) {
return
}
}
return
}
for k := range internal.NewHeapFunc(keys(m), cmp).Iterator {
if !yield(k, m[k]) {
return
}
}
}
}
const mapSizeThreshold = 256 // chosen on the basis of benchmark results
func keys[K comparable, V any](m map[K]V) []K {
ks := make([]K, 0, len(m))
for k := range m {
ks = append(ks, k)
}
return ks
}