-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathmisc.go
More file actions
155 lines (128 loc) · 3.23 KB
/
Copy pathmisc.go
File metadata and controls
155 lines (128 loc) · 3.23 KB
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
package iter
import (
"fmt"
"io"
"math/rand"
)
// IotaIterator is an input iterator that yields x, x+1, x+2, and so on.
type IotaIterator[T Numeric] struct {
x T
}
func (r IotaIterator[T]) Read() T {
return r.x
}
func (r IotaIterator[T]) Next() IotaIterator[T] {
return IotaIterator[T]{x: r.x + 1}
}
func (r IotaIterator[T]) Eq(IotaIterator[T]) bool {
return false
}
// IotaReader creates an InputIter that returns [x, x+1, x+2...).
func IotaReader[T Numeric](x T) IotaIterator[T] {
return IotaIterator[T]{x: x}
}
// IotaGenerator creates a Generator that returns [x, x+1, x+2...).
func IotaGenerator[T Numeric](x T) func() T {
r := IotaReader(x)
return func() T {
v := r.Read()
r = r.Next()
return v
}
}
// RepeatIterator is an input iterator that yields the same value indefinitely.
type RepeatIterator[T any] struct {
x T
}
func (r RepeatIterator[T]) Read() T { return r.x }
func (r RepeatIterator[T]) Next() RepeatIterator[T] { return r }
func (r RepeatIterator[T]) Eq(RepeatIterator[T]) bool { return false }
// RepeatReader creates an InputIter that returns [x, x, x...).
func RepeatReader[T any](x T) RepeatIterator[T] {
return RepeatIterator[T]{x: x}
}
// RepeatGenerator creates a Generator that returns [x, x, x...).
func RepeatGenerator[T any](x T) func() T {
return func() T { return x }
}
// RandomGenerator creates a generator that returns random item of a slice.
func RandomGenerator[T any](s []T, r *rand.Rand) func() T {
return func() T { return s[r.Intn(len(s))] }
}
// ChannelReader is a single-pass input iterator over a channel.
// A nil *ChannelReader is its end sentinel.
type ChannelReader[T any] struct {
ch chan T
cur T
read1 bool
eof bool
}
func (cr *ChannelReader[T]) recv() {
v, ok := <-cr.ch
cr.cur, cr.read1, cr.eof = v, true, !ok
}
func (cr *ChannelReader[T]) Read() T {
if !cr.read1 {
cr.recv()
}
return cr.cur
}
func (cr *ChannelReader[T]) Next() *ChannelReader[T] {
if !cr.read1 {
cr.recv()
}
if !cr.eof {
cr.recv()
}
return cr
}
func (cr *ChannelReader[T]) Eq(x *ChannelReader[T]) bool {
if !cr.read1 {
cr.recv()
}
return cr.eof && x == nil
}
// ChannelWriter is an output iterator that sends values to a channel.
type ChannelWriter[T any] struct {
ch chan T
}
func (cr *ChannelWriter[T]) Write(x T) {
cr.ch <- x
}
// ChanReader returns an InputIter that reads from a channel.
func ChanReader[T any](c chan T) *ChannelReader[T] {
return &ChannelReader[T]{
ch: c,
}
}
// ChanWriter returns an OutputIter that writes to a channel.
func ChanWriter[T any](c chan T) *ChannelWriter[T] {
return &ChannelWriter[T]{
ch: c,
}
}
// OutputWriter is an output iterator that formats values to an io.Writer.
type OutputWriter[T any] struct {
w io.Writer
written bool
delimiter []byte
}
func (w *OutputWriter[T]) Write(x T) {
if w.written && len(w.delimiter) > 0 {
_, err := w.w.Write(w.delimiter)
if err != nil {
panic(err)
}
} else {
w.written = true
}
_, err := fmt.Fprint(w.w, x)
if err != nil {
panic(err)
}
}
// IOWriter returns an OutputIter that writes values to an io.Writer.
// It panics if the writer returns an error.
func IOWriter[T any](w io.Writer, delimiter string) *OutputWriter[T] {
return &OutputWriter[T]{w: w, delimiter: []byte(delimiter)}
}